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: 22006 $
00027  * $Author: rjongbloed $
00028  * $Date: 2009-02-06 04:23:05 +0000 (Fri, 06 Feb 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 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   virtual bool SendNotify(const PObject * /*body*/) { return false; }
00126 
00127   SIPEndPoint & GetEndPoint() const { return endpoint; }
00128 
00129   const OpalProductInfo & GetProductInfo() const { return m_productInfo; }
00130 
00131   const PString & GetUsername() const     { return m_username; }
00132   const PString & GetPassword() const     { return m_password; }
00133   const PString & GetRealm() const        { return m_realm; }
00134   const SIPURL & GetRemoteAddress() const { return m_remoteAddress; }
00135 
00136 protected:
00137   void CollapseFork(SIPTransaction & transaction);
00138   PDECLARE_NOTIFIER(PTimer, SIPHandler, OnExpireTimeout);
00139   static PBoolean WriteSIPHandler(OpalTransport & transport, void * info);
00140   bool WriteSIPHandler(OpalTransport & transport);
00141 
00142   SIPEndPoint               & endpoint;
00143 
00144   SIPAuthentication         * authentication;
00145   PString                     m_username;
00146   PString                     m_password;
00147   PString                     m_realm;
00148 
00149   PSafeList<SIPTransaction>   transactions;
00150   OpalTransport             * m_transport;
00151   SIPURL                      m_addressOfRecord;
00152   SIPURL                      m_remoteAddress;
00153   PString                     callID;
00154   int                         expire;
00155   int                         originalExpire;
00156   int                         offlineExpire;
00157   PString                     body;
00158   unsigned                    authenticationAttempts;
00159   State                       state;
00160   PTimer                      expireTimer; 
00161   PTimeInterval               retryTimeoutMin; 
00162   PTimeInterval               retryTimeoutMax; 
00163   SIPURL                      m_proxy;
00164   OpalProductInfo             m_productInfo;
00165 };
00166 
00167 #if PTRACING
00168 ostream & operator<<(ostream & strm, SIPHandler::State state);
00169 #endif
00170 
00171 
00172 class SIPRegisterHandler : public SIPHandler
00173 {
00174   PCLASSINFO(SIPRegisterHandler, SIPHandler);
00175 
00176 public:
00177   SIPRegisterHandler(
00178     SIPEndPoint & ep,
00179     const SIPRegister::Params & params
00180   );
00181 
00182   ~SIPRegisterHandler();
00183 
00184   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00185   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00186   virtual SIP_PDU::Methods GetMethod()
00187     { return SIP_PDU::Method_REGISTER; }
00188 
00189   virtual void OnFailed(SIP_PDU::StatusCodes r);
00190   virtual PBoolean SendRequest(SIPHandler::State state);
00191 
00192   void UpdateParameters(const SIPRegister::Params & params);
00193 
00194 private:
00195   void SendStatus(SIP_PDU::StatusCodes code, State state);
00196 
00197   SIPRegister::Params m_parameters;
00198   unsigned            m_sequenceNumber;
00199 };
00200 
00201 
00202 class SIPSubscribeHandler : public SIPHandler
00203 {
00204   PCLASSINFO(SIPSubscribeHandler, SIPHandler);
00205 public:
00206   SIPSubscribeHandler(SIPEndPoint & ep, const SIPSubscribe::Params & params);
00207   ~SIPSubscribeHandler();
00208 
00209   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00210   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00211   virtual PBoolean OnReceivedNOTIFY(SIP_PDU & response);
00212   virtual void OnFailed(SIP_PDU::StatusCodes r);
00213   virtual PBoolean SendRequest(SIPHandler::State state);
00214   virtual SIP_PDU::Methods GetMethod ()
00215     { return SIP_PDU::Method_SUBSCRIBE; }
00216   virtual SIPEventPackage GetEventPackage() const
00217     { return m_parameters.m_eventPackage; }
00218 
00219   void UpdateParameters(const SIPSubscribe::Params & params);
00220 
00221   virtual bool IsDuplicateCSeq(unsigned sequenceNumber) { return m_dialog.IsDuplicateCSeq(sequenceNumber); }
00222 
00223 private:
00224   void SendStatus(SIP_PDU::StatusCodes code, State state);
00225 
00226   SIPSubscribe::Params     m_parameters;
00227   SIPDialogContext         m_dialog;
00228   bool                     m_unconfirmed;
00229   SIPEventPackageHandler * m_packageHandler;
00230 };
00231 
00232 
00233 class SIPNotifyHandler : public SIPHandler
00234 {
00235   PCLASSINFO(SIPNotifyHandler, SIPHandler);
00236 public:
00237   SIPNotifyHandler(
00238     SIPEndPoint & ep,
00239     const PString & targetAddress,
00240     const SIPEventPackage & eventPackage,
00241     const SIPDialogContext & dialog
00242   );
00243   ~SIPNotifyHandler();
00244 
00245   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00246   virtual PBoolean SendRequest(SIPHandler::State state);
00247   virtual SIP_PDU::Methods GetMethod ()
00248     { return SIP_PDU::Method_NOTIFY; }
00249   virtual SIPEventPackage GetEventPackage() const
00250     { return m_eventPackage; }
00251 
00252   virtual bool IsDuplicateCSeq(unsigned sequenceNumber) { return m_dialog.IsDuplicateCSeq(sequenceNumber); }
00253   virtual bool SendNotify(const PObject * body);
00254 
00255   enum Reasons {
00256     Deactivated,
00257     Probation,
00258     Rejected,
00259     Timeout,
00260     GiveUp,
00261     NoResource
00262   };
00263 
00264 private:
00265   SIPEventPackage          m_eventPackage;
00266   SIPDialogContext         m_dialog;
00267   Reasons                  m_reason;
00268   SIPEventPackageHandler * m_packageHandler;
00269 };
00270 
00271 
00272 class SIPPublishHandler : public SIPHandler
00273 {
00274   PCLASSINFO(SIPPublishHandler, SIPHandler);
00275 
00276 public:
00277   SIPPublishHandler(SIPEndPoint & ep, 
00278                     const SIPSubscribe::Params & params,
00279                     const PString & body);
00280   ~SIPPublishHandler();
00281 
00282   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00283   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00284   virtual SIP_PDU::Methods GetMethod()
00285     { return SIP_PDU::Method_PUBLISH; }
00286   virtual SIPEventPackage GetEventPackage() const
00287     { return m_parameters.m_eventPackage; }
00288   virtual void SetBody(const PString & body);
00289 
00290 private:
00291   SIPSubscribe::Params m_parameters;
00292   PString              m_sipETag;
00293   bool                 m_stateChanged;
00294 };
00295 
00296 
00297 class SIPMessageHandler : public SIPHandler
00298 {
00299   PCLASSINFO(SIPMessageHandler, SIPHandler);
00300 public:
00301   SIPMessageHandler(SIPEndPoint & ep, 
00302                     const PString & to,
00303                     const PString & body,
00304                     const PString & remoteContact,
00305                     const PString & callId);
00306   ~SIPMessageHandler();
00307 
00308   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00309   virtual SIP_PDU::Methods GetMethod ()
00310     { return SIP_PDU::Method_MESSAGE; }
00311   virtual void OnFailed (SIP_PDU::StatusCodes);
00312   virtual void SetBody(const PString & b);
00313 
00314 private:
00315   virtual void OnExpireTimeout(PTimer &, INT);
00316 };
00317 
00318 
00319 class SIPPingHandler : public SIPHandler
00320 {
00321   PCLASSINFO(SIPPingHandler, SIPHandler);
00322 public:
00323   SIPPingHandler(SIPEndPoint & ep, 
00324                  const PString & to);
00325   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00326   virtual SIP_PDU::Methods GetMethod ()
00327     { return SIP_PDU::Method_MESSAGE; }
00328 };
00329 
00330 
00334 class SIPHandlersList : public PSafeList<SIPHandler>
00335 {
00336   public:
00340     unsigned GetCount(SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
00341 
00345     PStringList GetAddresses(bool includeOffline, SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
00346 
00350     PSafePtr<SIPHandler> FindSIPHandlerByCallID(const PString & callID, PSafetyMode m);
00351 
00355     PSafePtr<SIPHandler> FindSIPHandlerByAuthRealm(const PString & authRealm, const PString & userName, PSafetyMode m);
00356 
00364     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, PSafetyMode m);
00365     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, const PString & eventPackage, PSafetyMode m);
00366 
00372     PSafePtr <SIPHandler> FindSIPHandlerByDomain(const PString & name, SIP_PDU::Methods meth, PSafetyMode m);
00373 };
00374 
00375 
00378 struct SIPPresenceInfo
00379 {
00380   enum BasicStates {
00381     Unknown,
00382     Open,
00383     Closed
00384   };
00385 
00386   SIPPresenceInfo() : m_basic(Unknown) { }
00387 
00388   PString     m_address;
00389   PString     m_entity;
00390   BasicStates m_basic;
00391   PString     m_note;
00392   PString     m_contact;
00393 
00394   PString AsString() const;
00395 };
00396 
00397 
00400 struct SIPDialogNotification : public PObject
00401 {
00402   PCLASSINFO(SIPDialogNotification, PObject);
00403 
00404   enum States {
00405     Terminated,
00406     Trying,
00407     Proceeding,
00408     Early,
00409     Confirmed,
00410 
00411     FirstState = Terminated,
00412     LastState = Confirmed
00413   };
00414   friend States operator++(States & state) { return (state = (States)(state+1)); }
00415   friend States operator--(States & state) { return (state = (States)(state-1)); }
00416   static PString GetStateName(States state);
00417   PString GetStateName() const { return GetStateName(m_state); }
00418 
00419   enum Events {
00420     NoEvent = -1,
00421     Cancelled,
00422     Rejected,
00423     Replaced,
00424     LocalBye,
00425     RemoteBye,
00426     Error,
00427     Timeout,
00428 
00429     FirstEvent = Cancelled,
00430     LastEvent = Timeout
00431   };
00432   friend Events operator++(Events & evt) { return (evt = (Events)(evt+1)); }
00433   friend Events operator--(Events & evt) { return (evt = (Events)(evt-1)); }
00434   static PString GetEventName(Events state);
00435   PString GetEventName() const { return GetEventName(m_eventType); }
00436 
00437   enum Rendering {
00438     RenderingUnknown = -1,
00439     NotRenderingMedia,
00440     RenderingMedia
00441   };
00442 
00443   PString  m_entity;
00444   PString  m_dialogId;
00445   PString  m_callId;
00446   bool     m_initiator;
00447   States   m_state;
00448   Events   m_eventType;
00449   unsigned m_eventCode;
00450   struct Participant {
00451     Participant() : m_appearance(-1), m_byeless(false), m_rendering(RenderingUnknown) { }
00452     PString   m_URI;
00453     PString   m_dialogTag;
00454     PString   m_identity;
00455     PString   m_display;
00456     int       m_appearance;
00457     bool      m_byeless;
00458     Rendering m_rendering;
00459   } m_local, m_remote;
00460 
00461   SIPDialogNotification(const PString & entity = PString::Empty());
00462 
00463   void PrintOn(ostream & strm) const;
00464 };
00465 
00466 
00467 #endif // OPAL_SIP
00468 
00469 #endif // OPAL_SIP_HANDLERS_H

Generated on Tue May 19 09:56:23 2009 for OPAL by  doxygen 1.5.1