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: 21283 $
00027  * $Author: rjongbloed $
00028  * $Date: 2008-10-11 07:10:58 +0000 (Sat, 11 Oct 2008) $
00029  */
00030 
00031 #ifndef OPAL_SIP_HANDLERS_H
00032 #define OPAL_SIP_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 #if OPAL_SIP
00045 
00046 #include <ptlib/safecoll.h>
00047 
00048 #include <opal/connection.h>
00049 #include <sip/sippdu.h>
00050 
00051 
00052 /* Class to handle SIP REGISTER, SUBSCRIBE, MESSAGE, and renew
00053  * the 'bindings' before they expire.
00054  */
00055 class SIPHandler : public PSafeObject 
00056 {
00057   PCLASSINFO(SIPHandler, PSafeObject);
00058 
00059 protected:
00060   SIPHandler(
00061     SIPEndPoint & ep, 
00062     const PString & to,
00063     int expireTime,
00064     int offlineExpire = 30,
00065     const PTimeInterval & retryMin = PMaxTimeInterval,
00066     const PTimeInterval & retryMax = PMaxTimeInterval
00067   );
00068 
00069 public:
00070   ~SIPHandler();
00071 
00072   virtual bool ShutDown();
00073 
00074   enum State {
00075 
00076     Subscribed,       // The registration is active
00077     Subscribing,      // The registration is in process
00078     Unavailable,      // The registration is offline and still being attempted
00079     Refreshing,       // The registration is being refreshed
00080     Restoring,        // The registration is trying to be restored after being offline
00081     Unsubscribing,    // The unregistration is in process
00082     Unsubscribed      // The registrating is inactive
00083   };
00084 
00085   void SetState (SIPHandler::State s);
00086 
00087   inline SIPHandler::State GetState () 
00088   { return state; }
00089 
00090   virtual OpalTransport * GetTransport();
00091 
00092   virtual SIPAuthentication * GetAuthentication()
00093   { return authentication; }
00094 
00095   virtual const SIPURL & GetTargetAddress()
00096     { return targetAddress; }
00097 
00098   virtual const PString GetRemotePartyAddress();
00099 
00100   virtual PBoolean OnReceivedNOTIFY(SIP_PDU & response);
00101 
00102   virtual void SetExpire(int e);
00103 
00104   virtual int GetExpire()
00105     { return expire; }
00106 
00107   virtual PString GetCallID()
00108     { return callID; }
00109 
00110   virtual void SetBody(const PString & b)
00111     { body = b;}
00112 
00113   virtual SIPTransaction * CreateTransaction(OpalTransport & t) = 0;
00114 
00115   virtual SIP_PDU::Methods GetMethod() = 0;
00116   virtual PCaselessString GetEventPackage() const
00117   { return PString::Empty(); }
00118 
00119   virtual void OnReceivedAuthenticationRequired(SIPTransaction & transaction, SIP_PDU & response);
00120   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00121   virtual void OnTransactionFailed(SIPTransaction & transaction);
00122   virtual void OnFailed(SIP_PDU::StatusCodes);
00123 
00124   virtual PBoolean SendRequest(SIPHandler::State state);
00125 
00126   const PStringList & GetRouteSet() const { return routeSet; }
00127 
00128   const OpalProductInfo & GetProductInfo() const { return m_productInfo; }
00129 
00130   PString                     authenticationUsername;
00131   PString                     authenticationPassword;
00132   PString                     authenticationAuthRealm;
00133 
00134 protected:
00135   void CollapseFork(SIPTransaction & transaction);
00136   PDECLARE_NOTIFIER(PTimer, SIPHandler, OnExpireTimeout);
00137   static PBoolean WriteSIPHandler(OpalTransport & transport, void * info);
00138   bool WriteSIPHandler(OpalTransport & transport);
00139 
00140   SIPEndPoint               & endpoint;
00141 
00142   SIPAuthentication           * authentication;
00143 
00144   PSafeList<SIPTransaction>   transactions;
00145   OpalTransport             * transport;
00146   SIPURL                      targetAddress;
00147   PString                     callID;
00148   int                           expire;
00149   int                         originalExpire;
00150   int                         offlineExpire;
00151   PStringList                 routeSet;
00152   PString                                 body;
00153   unsigned                    authenticationAttempts;
00154   State                       state;
00155   PTimer                      expireTimer; 
00156   PTimeInterval               retryTimeoutMin; 
00157   PTimeInterval               retryTimeoutMax; 
00158   PString remotePartyAddress;
00159   SIPURL proxy;
00160   OpalProductInfo             m_productInfo;
00161 };
00162 
00163 #if PTRACING
00164 ostream & operator<<(ostream & strm, SIPHandler::State state);
00165 #endif
00166 
00167 
00168 class SIPRegisterHandler : public SIPHandler
00169 {
00170   PCLASSINFO(SIPRegisterHandler, SIPHandler);
00171 
00172 public:
00173   SIPRegisterHandler(
00174     SIPEndPoint & ep,
00175     const SIPRegister::Params & params
00176   );
00177 
00178   ~SIPRegisterHandler();
00179 
00180   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00181   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00182   virtual SIP_PDU::Methods GetMethod()
00183     { return SIP_PDU::Method_REGISTER; }
00184 
00185   virtual void OnFailed(SIP_PDU::StatusCodes r);
00186   virtual PBoolean SendRequest(SIPHandler::State state);
00187 
00188   void UpdateParameters(const SIPRegister::Params & params);
00189 
00190 private:
00191   void SendStatus(SIP_PDU::StatusCodes code);
00192 
00193   SIPRegister::Params m_parameters;
00194   unsigned            m_sequenceNumber;
00195 };
00196 
00197 
00198 class SIPSubscribeHandler : public SIPHandler
00199 {
00200   PCLASSINFO(SIPSubscribeHandler, SIPHandler);
00201 public:
00202   SIPSubscribeHandler(SIPEndPoint & ep, const SIPSubscribe::Params & params);
00203   ~SIPSubscribeHandler();
00204 
00205   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00206   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00207   virtual PBoolean OnReceivedNOTIFY(SIP_PDU & response);
00208   virtual void OnFailed(SIP_PDU::StatusCodes r);
00209   virtual PBoolean SendRequest(SIPHandler::State state);
00210   virtual SIP_PDU::Methods GetMethod ()
00211     { return SIP_PDU::Method_SUBSCRIBE; }
00212   virtual PCaselessString GetEventPackage() const
00213     { return m_parameters.m_eventPackage; }
00214 
00215   void UpdateParameters(const SIPSubscribe::Params & params);
00216 
00217 private:
00218   void SendStatus(SIP_PDU::StatusCodes code);
00219   PBoolean OnReceivedMWINOTIFY(SIP_PDU & response);
00220   PBoolean OnReceivedPresenceNOTIFY(SIP_PDU & response);
00221 
00222   SIPSubscribe::Params m_parameters;
00223 
00224   PString  localPartyAddress;
00225   PBoolean dialogCreated;
00226   unsigned lastSentCSeq;
00227   unsigned lastReceivedCSeq;
00228 };
00229 
00230 
00231 class SIPPublishHandler : public SIPHandler
00232 {
00233   PCLASSINFO(SIPPublishHandler, SIPHandler);
00234 
00235 public:
00236   SIPPublishHandler(SIPEndPoint & ep, 
00237                     const PString & to,
00238                     const PString & body,
00239                     int expire);
00240   ~SIPPublishHandler();
00241 
00242   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00243   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00244   virtual SIP_PDU::Methods GetMethod()
00245     { return SIP_PDU::Method_PUBLISH; }
00246   virtual void SetBody(const PString & body);
00247   static PString BuildBody(const PString & to,
00248                            const PString & basic,
00249                            const PString & note);
00250 
00251 private:
00252   PDECLARE_NOTIFIER(PTimer, SIPPublishHandler, OnPublishTimeout);
00253   PTimer publishTimer;
00254   PString sipETag;
00255   PBoolean stateChanged;
00256 };
00257 
00258 
00259 class SIPMessageHandler : public SIPHandler
00260 {
00261   PCLASSINFO(SIPMessageHandler, SIPHandler);
00262 public:
00263   SIPMessageHandler(SIPEndPoint & ep, 
00264                     const PString & to,
00265                     const PString & body);
00266   ~SIPMessageHandler();
00267 
00268   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00269   virtual SIP_PDU::Methods GetMethod ()
00270     { return SIP_PDU::Method_MESSAGE; }
00271   virtual void OnFailed (SIP_PDU::StatusCodes);
00272   virtual void SetBody(const PString & b);
00273 
00274 private:
00275   virtual void OnExpireTimeout(PTimer &, INT);
00276 };
00277 
00278 
00279 class SIPPingHandler : public SIPHandler
00280 {
00281   PCLASSINFO(SIPPingHandler, SIPHandler);
00282 public:
00283   SIPPingHandler(SIPEndPoint & ep, 
00284                  const PString & to);
00285   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00286   virtual SIP_PDU::Methods GetMethod ()
00287     { return SIP_PDU::Method_MESSAGE; }
00288 };
00289 
00290 
00295 class SIPHandlersList : public PSafeList<SIPHandler>
00296 {
00297   public:
00301     unsigned GetCount(SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
00302 
00306     PSafePtr<SIPHandler> FindSIPHandlerByCallID(const PString & callID, PSafetyMode m);
00307 
00311     PSafePtr<SIPHandler> FindSIPHandlerByAuthRealm(const PString & authRealm, const PString & userName, PSafetyMode m);
00312 
00320     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, PSafetyMode m);
00321     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, const PString & eventPackage, PSafetyMode m);
00322 
00328     PSafePtr <SIPHandler> FindSIPHandlerByDomain(const PString & name, SIP_PDU::Methods meth, PSafetyMode m);
00329 };
00330 
00331 
00332 #endif // OPAL_SIP
00333 
00334 #endif // OPAL_SIP_HANDLERS_H

Generated on Mon Feb 23 02:01:36 2009 for OPAL by  doxygen 1.5.1