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

Generated on Mon Sep 15 11:49:11 2008 for OPAL by  doxygen 1.5.1