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

Generated on Fri Mar 7 07:36:35 2008 for OPAL by  doxygen 1.5.1