handlers.h

Go to the documentation of this file.
00001 /*
00002  * handlers.h
00003  *
00004  * Session Initiation Protocol endpoint.
00005  *
00006  * Open Phone Abstraction Library (OPAL)
00007  *
00008  * Copyright (c) 2000 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Open Phone Abstraction Library.
00021  *
00022  * The Initial Developer of the Original Code is Damien Sandras. 
00023  *
00024  * Contributor(s): ______________________________________.
00025  *
00026  * $Revision: 21004 $
00027  * $Author: rjongbloed $
00028  * $Date: 2008-09-16 07:08:56 +0000 (Tue, 16 Sep 2008) $
00029  */
00030 
00031 #ifndef __OPAL_HANDLERS_H
00032 #define __OPAL_HANDLERS_H
00033 
00034 #ifdef P_USE_PRAGMA
00035 #pragma interface
00036 #endif
00037 
00038 #ifndef _PTLIB_H
00039 #include <ptlib.h>
00040 #endif
00041 
00042 #include <opal/buildopts.h>
00043 
00044 #include <ptlib/safecoll.h>
00045 
00046 #include <opal/connection.h>
00047 #include <sip/sippdu.h>
00048 
00049 
00050 /* Class to handle SIP REGISTER, SUBSCRIBE, MESSAGE, and renew
00051  * the 'bindings' before they expire.
00052  */
00053 class SIPHandler : public PSafeObject 
00054 {
00055   PCLASSINFO(SIPHandler, PSafeObject);
00056 
00057 protected:
00058   SIPHandler(
00059     SIPEndPoint & ep, 
00060     const PString & to,
00061     int expireTime,
00062     int offlineExpire = 30,
00063     const PTimeInterval & retryMin = PMaxTimeInterval,
00064     const PTimeInterval & retryMax = PMaxTimeInterval
00065   );
00066 
00067 public:
00068   ~SIPHandler();
00069 
00070   virtual bool ShutDown();
00071 
00072   enum State {
00073 
00074     Subscribed,       // The registration is active
00075     Subscribing,      // The registration is in process
00076     Unavailable,      // The registration is offline and still being attempted
00077     Refreshing,       // The registration is being refreshed
00078     Restoring,        // The registration is trying to be restored after being offline
00079     Unsubscribing,    // The unregistration is in process
00080     Unsubscribed      // The registrating is inactive
00081   };
00082 
00083   void SetState (SIPHandler::State s);
00084 
00085   inline SIPHandler::State GetState () 
00086   { return state; }
00087 
00088   virtual OpalTransport * GetTransport();
00089 
00090   virtual SIPAuthentication * GetAuthentication()
00091   { return authentication; }
00092 
00093   virtual const SIPURL & GetTargetAddress()
00094     { return targetAddress; }
00095 
00096   virtual const PString GetRemotePartyAddress();
00097 
00098   virtual PBoolean OnReceivedNOTIFY(SIP_PDU & response);
00099 
00100   virtual void SetExpire(int e);
00101 
00102   virtual int GetExpire()
00103     { return expire; }
00104 
00105   virtual PString GetCallID()
00106     { return callID; }
00107 
00108   virtual void SetBody(const PString & b)
00109     { body = b;}
00110 
00111   virtual SIPTransaction * CreateTransaction(OpalTransport & t) = 0;
00112 
00113   virtual SIP_PDU::Methods GetMethod() = 0;
00114   virtual PCaselessString GetEventPackage() const
00115   { return PString::Empty(); }
00116 
00117   virtual void OnReceivedAuthenticationRequired(SIPTransaction & transaction, SIP_PDU & response);
00118   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00119   virtual void OnTransactionFailed(SIPTransaction & transaction);
00120   virtual void OnFailed(SIP_PDU::StatusCodes);
00121 
00122   virtual PBoolean SendRequest(SIPHandler::State state);
00123 
00124   const PStringList & GetRouteSet() const { return routeSet; }
00125 
00126   const OpalProductInfo & GetProductInfo() const { return m_productInfo; }
00127 
00128   PString                     authenticationUsername;
00129   PString                     authenticationPassword;
00130   PString                     authenticationAuthRealm;
00131 
00132 protected:
00133   void CollapseFork(SIPTransaction & transaction);
00134   PDECLARE_NOTIFIER(PTimer, SIPHandler, OnExpireTimeout);
00135   static PBoolean WriteSIPHandler(OpalTransport & transport, void * info);
00136   bool WriteSIPHandler(OpalTransport & transport);
00137 
00138   SIPEndPoint               & endpoint;
00139 
00140   SIPAuthentication           * authentication;
00141 
00142   PSafeList<SIPTransaction>   transactions;
00143   OpalTransport             * transport;
00144   SIPURL                      targetAddress;
00145   PString                     callID;
00146   int                           expire;
00147   int                         originalExpire;
00148   int                         offlineExpire;
00149   PStringList                 routeSet;
00150   PString                                 body;
00151   unsigned                    authenticationAttempts;
00152   State                       state;
00153   PTimer                      expireTimer; 
00154   PTimeInterval               retryTimeoutMin; 
00155   PTimeInterval               retryTimeoutMax; 
00156   PString remotePartyAddress;
00157   SIPURL proxy;
00158   OpalProductInfo             m_productInfo;
00159 };
00160 
00161 #if PTRACING
00162 ostream & operator<<(ostream & strm, SIPHandler::State state);
00163 #endif
00164 
00165 
00166 class SIPRegisterHandler : public SIPHandler
00167 {
00168   PCLASSINFO(SIPRegisterHandler, SIPHandler);
00169 
00170 public:
00171   SIPRegisterHandler(
00172     SIPEndPoint & ep,
00173     const SIPRegister::Params & params
00174   );
00175 
00176   ~SIPRegisterHandler();
00177 
00178   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00179   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00180   virtual SIP_PDU::Methods GetMethod()
00181     { return SIP_PDU::Method_REGISTER; }
00182 
00183   virtual void OnFailed(SIP_PDU::StatusCodes r);
00184   virtual PBoolean SendRequest(SIPHandler::State state);
00185 
00186   void UpdateParameters(const SIPRegister::Params & params);
00187 
00188 private:
00189   void SendStatus(SIP_PDU::StatusCodes code);
00190 
00191   SIPRegister::Params m_parameters;
00192 };
00193 
00194 
00195 class SIPSubscribeHandler : public SIPHandler
00196 {
00197   PCLASSINFO(SIPSubscribeHandler, SIPHandler);
00198 public:
00199   SIPSubscribeHandler(SIPEndPoint & ep, const SIPSubscribe::Params & params);
00200   ~SIPSubscribeHandler();
00201 
00202   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00203   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00204   virtual PBoolean OnReceivedNOTIFY(SIP_PDU & response);
00205   virtual void OnFailed(SIP_PDU::StatusCodes r);
00206   virtual PBoolean SendRequest(SIPHandler::State state);
00207   virtual SIP_PDU::Methods GetMethod ()
00208     { return SIP_PDU::Method_SUBSCRIBE; }
00209   virtual PCaselessString GetEventPackage() const
00210     { return m_parameters.m_eventPackage; }
00211 
00212   void UpdateParameters(const SIPSubscribe::Params & params);
00213 
00214 private:
00215   void SendStatus(SIP_PDU::StatusCodes code);
00216   PBoolean OnReceivedMWINOTIFY(SIP_PDU & response);
00217   PBoolean OnReceivedPresenceNOTIFY(SIP_PDU & response);
00218 
00219   SIPSubscribe::Params m_parameters;
00220 
00221   PString  localPartyAddress;
00222   PBoolean dialogCreated;
00223   unsigned lastSentCSeq;
00224   unsigned lastReceivedCSeq;
00225 };
00226 
00227 
00228 class SIPPublishHandler : public SIPHandler
00229 {
00230   PCLASSINFO(SIPPublishHandler, SIPHandler);
00231 
00232 public:
00233   SIPPublishHandler(SIPEndPoint & ep, 
00234                     const PString & to,
00235                     const PString & body,
00236                     int expire);
00237   ~SIPPublishHandler();
00238 
00239   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00240   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00241   virtual SIP_PDU::Methods GetMethod()
00242     { return SIP_PDU::Method_PUBLISH; }
00243   virtual void SetBody(const PString & body);
00244   static PString BuildBody(const PString & to,
00245                            const PString & basic,
00246                            const PString & note);
00247 
00248 private:
00249   PDECLARE_NOTIFIER(PTimer, SIPPublishHandler, OnPublishTimeout);
00250   PTimer publishTimer;
00251   PString sipETag;
00252   PBoolean stateChanged;
00253 };
00254 
00255 
00256 class SIPMessageHandler : public SIPHandler
00257 {
00258   PCLASSINFO(SIPMessageHandler, SIPHandler);
00259 public:
00260   SIPMessageHandler(SIPEndPoint & ep, 
00261                     const PString & to,
00262                     const PString & body);
00263   ~SIPMessageHandler();
00264 
00265   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00266   virtual SIP_PDU::Methods GetMethod ()
00267     { return SIP_PDU::Method_MESSAGE; }
00268   virtual void OnFailed (SIP_PDU::StatusCodes);
00269   virtual void SetBody(const PString & b);
00270 
00271 private:
00272   virtual void OnExpireTimeout(PTimer &, INT);
00273 };
00274 
00275 
00276 class SIPPingHandler : public SIPHandler
00277 {
00278   PCLASSINFO(SIPPingHandler, SIPHandler);
00279 public:
00280   SIPPingHandler(SIPEndPoint & ep, 
00281                  const PString & to);
00282   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00283   virtual SIP_PDU::Methods GetMethod ()
00284     { return SIP_PDU::Method_MESSAGE; }
00285 };
00286 
00287 
00292 class SIPHandlersList : public PSafeList<SIPHandler>
00293 {
00294   public:
00298     unsigned GetCount(SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
00299 
00303     PSafePtr<SIPHandler> FindSIPHandlerByCallID(const PString & callID, PSafetyMode m);
00304 
00308     PSafePtr<SIPHandler> FindSIPHandlerByAuthRealm(const PString & authRealm, const PString & userName, PSafetyMode m);
00309 
00317     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, PSafetyMode m);
00318     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, const PString & eventPackage, PSafetyMode m);
00319 
00325     PSafePtr <SIPHandler> FindSIPHandlerByDomain(const PString & name, SIP_PDU::Methods meth, PSafetyMode m);
00326 };
00327 
00328 
00329 #endif

Generated on Mon Sep 22 12:24:50 2008 for OPAL by  doxygen 1.5.1