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: 23287 $
00027  * $Author: rjongbloed $
00028  * $Date: 2009-08-28 01:42:00 +0000 (Fri, 28 Aug 2009) $
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 & target,
00063     const PString & remote,
00064     int expireTime = 0,
00065     int offlineExpire = 30,
00066     const PTimeInterval & retryMin = PMaxTimeInterval,
00067     const PTimeInterval & retryMax = PMaxTimeInterval
00068   );
00069 
00070 public:
00071   ~SIPHandler();
00072 
00073   virtual bool ShutDown();
00074 
00075   enum State {
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 & GetAddressOfRecord()
00096     { return m_addressOfRecord; }
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 bool IsDuplicateCSeq(unsigned ) { return false; }
00112 
00113   virtual SIPTransaction * CreateTransaction(OpalTransport & t) = 0;
00114 
00115   virtual SIP_PDU::Methods GetMethod() = 0;
00116   virtual SIPSubscribe::EventPackage GetEventPackage() const
00117   { return PString::Empty(); }
00118 
00119   virtual void OnReceivedResponse(SIPTransaction & transaction, SIP_PDU & response);
00120   virtual void OnReceivedIntervalTooBrief(SIPTransaction & transaction, SIP_PDU & response);
00121   virtual void OnReceivedTemporarilyUnavailable(SIPTransaction & transaction, SIP_PDU & response);
00122   virtual void OnReceivedAuthenticationRequired(SIPTransaction & transaction, SIP_PDU & response);
00123   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00124   virtual void OnTransactionFailed(SIPTransaction & transaction);
00125   virtual void OnFailed(SIP_PDU::StatusCodes);
00126 
00127   bool ActivateState(SIPHandler::State state, unsigned msecs = 1000);
00128   virtual bool SendNotify(const PObject * /*body*/) { return false; }
00129 
00130   SIPEndPoint & GetEndPoint() const { return endpoint; }
00131 
00132   const OpalProductInfo & GetProductInfo() const { return m_productInfo; }
00133 
00134   const PString & GetUsername() const     { return m_username; }
00135   const PString & GetPassword() const     { return m_password; }
00136   const PString & GetRealm() const        { return m_realm; }
00137   const SIPURL & GetRemoteAddress() const { return m_remoteAddress; }
00138 
00139 protected:
00140   virtual PBoolean SendRequest(SIPHandler::State state);
00141   PDECLARE_NOTIFIER(PTimer, SIPHandler, OnExpireTimeout);
00142   static PBoolean WriteSIPHandler(OpalTransport & transport, void * info);
00143   bool WriteSIPHandler(OpalTransport & transport);
00144 
00145   SIPEndPoint               & endpoint;
00146 
00147   SIPAuthentication         * authentication;
00148   PString                     m_username;
00149   PString                     m_password;
00150   PString                     m_realm;
00151 
00152   PSafeList<SIPTransaction>   transactions;
00153   OpalTransport             * m_transport;
00154   SIPURL                      m_addressOfRecord;
00155   SIPURL                      m_remoteAddress;
00156   PString                     callID;
00157   int                         expire;
00158   int                         originalExpire;
00159   int                         offlineExpire;
00160   PString                     body;
00161   unsigned                    authenticationAttempts;
00162   State                       state;
00163   PTimer                      expireTimer; 
00164   PTimeInterval               retryTimeoutMin; 
00165   PTimeInterval               retryTimeoutMax; 
00166   SIPURL                      m_proxy;
00167   OpalProductInfo             m_productInfo;
00168 };
00169 
00170 #if PTRACING
00171 ostream & operator<<(ostream & strm, SIPHandler::State state);
00172 #endif
00173 
00174 
00175 class SIPRegisterHandler : public SIPHandler
00176 {
00177   PCLASSINFO(SIPRegisterHandler, SIPHandler);
00178 
00179 public:
00180   SIPRegisterHandler(
00181     SIPEndPoint & ep,
00182     const SIPRegister::Params & params
00183   );
00184 
00185   ~SIPRegisterHandler();
00186 
00187   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00188   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00189   virtual SIP_PDU::Methods GetMethod()
00190     { return SIP_PDU::Method_REGISTER; }
00191 
00192   virtual void OnFailed(SIP_PDU::StatusCodes r);
00193 
00194   void UpdateParameters(const SIPRegister::Params & params);
00195 
00196 protected:
00197   virtual PBoolean SendRequest(SIPHandler::State state);
00198   void SendStatus(SIP_PDU::StatusCodes code, State state);
00199 
00200   SIPRegister::Params m_parameters;
00201   unsigned            m_sequenceNumber;
00202 };
00203 
00204 
00205 class SIPSubscribeHandler : public SIPHandler
00206 {
00207   PCLASSINFO(SIPSubscribeHandler, SIPHandler);
00208 public:
00209   SIPSubscribeHandler(SIPEndPoint & ep, const SIPSubscribe::Params & params);
00210   ~SIPSubscribeHandler();
00211 
00212   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00213   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00214   virtual PBoolean OnReceivedNOTIFY(SIP_PDU & response);
00215   virtual void OnFailed(SIP_PDU::StatusCodes r);
00216   virtual SIP_PDU::Methods GetMethod ()
00217     { return SIP_PDU::Method_SUBSCRIBE; }
00218   virtual SIPEventPackage GetEventPackage() const
00219     { return m_parameters.m_eventPackage; }
00220 
00221   void UpdateParameters(const SIPSubscribe::Params & params);
00222 
00223   virtual bool IsDuplicateCSeq(unsigned sequenceNumber) { return m_dialog.IsDuplicateCSeq(sequenceNumber); }
00224 
00225 protected:
00226   virtual PBoolean SendRequest(SIPHandler::State state);
00227   void SendStatus(SIP_PDU::StatusCodes code, State state);
00228 
00229   SIPSubscribe::Params     m_parameters;
00230   SIPDialogContext         m_dialog;
00231   bool                     m_unconfirmed;
00232   SIPEventPackageHandler * m_packageHandler;
00233 };
00234 
00235 
00236 class SIPNotifyHandler : public SIPHandler
00237 {
00238   PCLASSINFO(SIPNotifyHandler, SIPHandler);
00239 public:
00240   SIPNotifyHandler(
00241     SIPEndPoint & ep,
00242     const PString & targetAddress,
00243     const SIPEventPackage & eventPackage,
00244     const SIPDialogContext & dialog
00245   );
00246   ~SIPNotifyHandler();
00247 
00248   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00249   virtual SIP_PDU::Methods GetMethod ()
00250     { return SIP_PDU::Method_NOTIFY; }
00251   virtual SIPEventPackage GetEventPackage() const
00252     { return m_eventPackage; }
00253 
00254   virtual bool IsDuplicateCSeq(unsigned sequenceNumber) { return m_dialog.IsDuplicateCSeq(sequenceNumber); }
00255   virtual bool SendNotify(const PObject * body);
00256 
00257   enum Reasons {
00258     Deactivated,
00259     Probation,
00260     Rejected,
00261     Timeout,
00262     GiveUp,
00263     NoResource
00264   };
00265 
00266 protected:
00267   virtual PBoolean SendRequest(SIPHandler::State state);
00268 
00269   SIPEventPackage          m_eventPackage;
00270   SIPDialogContext         m_dialog;
00271   Reasons                  m_reason;
00272   SIPEventPackageHandler * m_packageHandler;
00273 };
00274 
00275 
00276 class SIPPublishHandler : public SIPHandler
00277 {
00278   PCLASSINFO(SIPPublishHandler, SIPHandler);
00279 
00280 public:
00281   SIPPublishHandler(SIPEndPoint & ep, 
00282                     const SIPSubscribe::Params & params,
00283                     const PString & body);
00284   ~SIPPublishHandler();
00285 
00286   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00287   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00288   virtual SIP_PDU::Methods GetMethod()
00289     { return SIP_PDU::Method_PUBLISH; }
00290   virtual SIPEventPackage GetEventPackage() const
00291     { return m_parameters.m_eventPackage; }
00292   virtual void SetBody(const PString & body);
00293 
00294 private:
00295   SIPSubscribe::Params m_parameters;
00296   PString              m_sipETag;
00297   bool                 m_stateChanged;
00298 };
00299 
00300 
00301 class SIPMessageHandler : public SIPHandler
00302 {
00303   PCLASSINFO(SIPMessageHandler, SIPHandler);
00304 public:
00305   SIPMessageHandler(SIPEndPoint & ep, 
00306                     const PString & to,
00307                     const PString & body,
00308                     const PString & remoteContact,
00309                     const PString & callId);
00310   ~SIPMessageHandler();
00311 
00312   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00313   virtual SIP_PDU::Methods GetMethod ()
00314     { return SIP_PDU::Method_MESSAGE; }
00315   virtual void OnFailed (SIP_PDU::StatusCodes);
00316   virtual void SetBody(const PString & b);
00317 
00318 private:
00319   virtual void OnExpireTimeout(PTimer &, INT);
00320 };
00321 
00322 
00323 class SIPPingHandler : public SIPHandler
00324 {
00325   PCLASSINFO(SIPPingHandler, SIPHandler);
00326 public:
00327   SIPPingHandler(SIPEndPoint & ep, 
00328                  const PString & to);
00329   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00330   virtual SIP_PDU::Methods GetMethod ()
00331     { return SIP_PDU::Method_MESSAGE; }
00332 };
00333 
00334 
00338 class SIPHandlersList
00339 {
00340   public:
00343     void Append(SIPHandler * handler)
00344       { m_handlersList.Append(handler); }
00345 
00350     void Remove(SIPHandler * handler)
00351       { m_handlersList.Remove(handler); }
00352 
00355     bool DeleteObjectsToBeRemoved()
00356       { return m_handlersList.DeleteObjectsToBeRemoved(); }
00357 
00361     PSafePtr<SIPHandler> GetFirstHandler(PSafetyMode mode = PSafeReference) const
00362       { return PSafePtr<SIPHandler>(m_handlersList, mode); }
00363 
00367     unsigned GetCount(SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
00368 
00372     PStringList GetAddresses(bool includeOffline, SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
00373 
00377     PSafePtr<SIPHandler> FindSIPHandlerByCallID(const PString & callID, PSafetyMode m);
00378 
00382     PSafePtr<SIPHandler> FindSIPHandlerByAuthRealm(const PString & authRealm, const PString & userName, PSafetyMode m);
00383 
00391     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, PSafetyMode m);
00392     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, const PString & eventPackage, PSafetyMode m);
00393 
00399     PSafePtr <SIPHandler> FindSIPHandlerByDomain(const PString & name, SIP_PDU::Methods meth, PSafetyMode m);
00400 
00401   protected:
00402     PSafeList<SIPHandler> m_handlersList;
00403 };
00404 
00405 
00408 struct SIPPresenceInfo
00409 {
00410   enum BasicStates {
00411     Unknown,
00412     Open,
00413     Closed
00414   };
00415 
00416   SIPPresenceInfo() : m_basic(Unknown) { }
00417 
00418   PString     m_address;
00419   PString     m_entity;
00420   BasicStates m_basic;
00421   PString     m_note;
00422   PString     m_contact;
00423   PString     m_to;
00424 
00425   PString AsString() const;
00426 };
00427 
00428 
00431 struct SIPDialogNotification : public PObject
00432 {
00433   PCLASSINFO(SIPDialogNotification, PObject);
00434 
00435   enum States {
00436     Terminated,
00437     Trying,
00438     Proceeding,
00439     Early,
00440     Confirmed,
00441 
00442     FirstState = Terminated,
00443     LastState = Confirmed
00444   };
00445   friend States operator++(States & state) { return (state = (States)(state+1)); }
00446   friend States operator--(States & state) { return (state = (States)(state-1)); }
00447   static PString GetStateName(States state);
00448   PString GetStateName() const { return GetStateName(m_state); }
00449 
00450   enum Events {
00451     NoEvent = -1,
00452     Cancelled,
00453     Rejected,
00454     Replaced,
00455     LocalBye,
00456     RemoteBye,
00457     Error,
00458     Timeout,
00459 
00460     FirstEvent = Cancelled,
00461     LastEvent = Timeout
00462   };
00463   friend Events operator++(Events & evt) { return (evt = (Events)(evt+1)); }
00464   friend Events operator--(Events & evt) { return (evt = (Events)(evt-1)); }
00465   static PString GetEventName(Events state);
00466   PString GetEventName() const { return GetEventName(m_eventType); }
00467 
00468   enum Rendering {
00469     RenderingUnknown = -1,
00470     NotRenderingMedia,
00471     RenderingMedia
00472   };
00473 
00474   PString  m_entity;
00475   PString  m_dialogId;
00476   PString  m_callId;
00477   bool     m_initiator;
00478   States   m_state;
00479   Events   m_eventType;
00480   unsigned m_eventCode;
00481   struct Participant {
00482     Participant() : m_appearance(-1), m_byeless(false), m_rendering(RenderingUnknown) { }
00483     PString   m_URI;
00484     PString   m_dialogTag;
00485     PString   m_identity;
00486     PString   m_display;
00487     int       m_appearance;
00488     bool      m_byeless;
00489     Rendering m_rendering;
00490   } m_local, m_remote;
00491 
00492   SIPDialogNotification(const PString & entity = PString::Empty());
00493 
00494   void PrintOn(ostream & strm) const;
00495 };
00496 
00497 
00498 #endif // OPAL_SIP
00499 
00500 #endif // OPAL_SIP_HANDLERS_H

Generated on Mon Feb 1 00:25:49 2010 for OPAL by  doxygen 1.5.1