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: 24609 $
00027  * $Author: rjongbloed $
00028  * $Date: 2010-07-29 02:18:34 -0500 (Thu, 29 Jul 2010) $
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 <opal/pres_ent.h>
00047 #include <opal/connection.h>
00048 #include <sip/sippdu.h>
00049 
00050 
00051 /* Class to handle SIP REGISTER, SUBSCRIBE, MESSAGE, and renew
00052  * the 'bindings' before they expire.
00053  */
00054 class SIPHandler : public PSafeObject 
00055 {
00056   PCLASSINFO(SIPHandler, PSafeObject);
00057 
00058 protected:
00059   SIPHandler(
00060     SIPEndPoint & ep,
00061     const SIPParameters & params
00062   );
00063 
00064 public:
00065   ~SIPHandler();
00066 
00067   virtual Comparison Compare(const PObject & other) const;
00068 
00069   virtual bool ShutDown();
00070 
00071   enum State {
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     NumStates
00080   };
00081 
00082   void SetState (SIPHandler::State s);
00083 
00084   inline SIPHandler::State GetState () 
00085   { return m_state; }
00086 
00087   virtual OpalTransport * GetTransport();
00088 
00089   virtual SIPAuthentication * GetAuthentication()
00090   { return authentication; }
00091 
00092   virtual const SIPURL & GetAddressOfRecord()
00093     { return m_addressOfRecord; }
00094 
00095   virtual PBoolean OnReceivedNOTIFY(SIP_PDU & response);
00096 
00097   virtual void SetExpire(int e);
00098 
00099   virtual int GetExpire()
00100     { return expire; }
00101 
00102   virtual PString GetCallID()
00103     { return callID; }
00104 
00105   virtual void SetBody(const PString & b)
00106     { body = b;}
00107 
00108   virtual bool IsDuplicateCSeq(unsigned ) { return false; }
00109 
00110   virtual SIPTransaction * CreateTransaction(OpalTransport & t) = 0;
00111 
00112   virtual SIP_PDU::Methods GetMethod() = 0;
00113   virtual SIPSubscribe::EventPackage GetEventPackage() const
00114   { return SIPEventPackage(); }
00115 
00116   virtual void OnReceivedResponse(SIPTransaction & transaction, SIP_PDU & response);
00117   virtual void OnReceivedIntervalTooBrief(SIPTransaction & transaction, SIP_PDU & response);
00118   virtual void OnReceivedTemporarilyUnavailable(SIPTransaction & transaction, SIP_PDU & response);
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 
00123   virtual void OnFailed(const SIP_PDU & response);
00124   virtual void OnFailed(SIP_PDU::StatusCodes);
00125 
00126   bool ActivateState(SIPHandler::State state);
00127   virtual bool SendNotify(const PObject * /*body*/) { return false; }
00128 
00129   SIPEndPoint & GetEndPoint() const { return endpoint; }
00130 
00131   const OpalProductInfo & GetProductInfo() const { return m_productInfo; }
00132 
00133   const PString & GetUsername() const     { return m_username; }
00134   const PString & GetPassword() const     { return m_password; }
00135   const PString & GetRealm() const        { return m_realm; }
00136   const SIPURL & GetRemoteAddress() const { return m_remoteAddress; }
00137   const SIPURL & GetProxy() const         { return m_proxy; }
00138 
00139   SIPMIMEInfo m_mime;
00140 
00141 protected:
00142   virtual PBoolean SendRequest(SIPHandler::State state);
00143   void RetryLater(unsigned after);
00144   PDECLARE_NOTIFIER(PTimer, SIPHandler, OnExpireTimeout);
00145   static PBoolean WriteSIPHandler(OpalTransport & transport, void * info);
00146   virtual bool WriteSIPHandler(OpalTransport & transport, bool forked);
00147 
00148   SIPEndPoint               & endpoint;
00149 
00150   SIPAuthentication         * authentication;
00151   PString                     m_username;
00152   PString                     m_password;
00153   PString                     m_realm;
00154 
00155   PSafeList<SIPTransaction>   transactions;
00156   OpalTransport             * m_transport;
00157   SIPURL                      m_addressOfRecord;
00158   SIPURL                      m_remoteAddress;
00159   PString                     callID;
00160   int                         expire;
00161   int                         originalExpire;
00162   int                         offlineExpire;
00163   PString                     body;
00164   unsigned                    authenticationAttempts;
00165   State                       m_state;
00166   queue<State>                m_stateQueue;
00167   bool                        m_receivedResponse;
00168   PTimer                      expireTimer; 
00169   PTimeInterval               retryTimeoutMin; 
00170   PTimeInterval               retryTimeoutMax; 
00171   SIPURL                      m_proxy;
00172   OpalProductInfo             m_productInfo;
00173 
00174   // Keep a copy of the keys used for easy removal on destruction
00175   typedef std::map<PString, PSafePtr<SIPHandler> > IndexMap;
00176   std::pair<IndexMap::iterator, bool> m_byCallID;
00177   std::pair<IndexMap::iterator, bool> m_byAorAndPackage;
00178   std::pair<IndexMap::iterator, bool> m_byAuthIdAndRealm;
00179   std::pair<IndexMap::iterator, bool> m_byAorUserAndRealm;
00180 
00181   friend class SIPHandlersList;
00182 };
00183 
00184 #if PTRACING
00185 ostream & operator<<(ostream & strm, SIPHandler::State state);
00186 #endif
00187 
00188 
00189 class SIPRegisterHandler : public SIPHandler
00190 {
00191   PCLASSINFO(SIPRegisterHandler, SIPHandler);
00192 
00193 public:
00194   SIPRegisterHandler(
00195     SIPEndPoint & ep,
00196     const SIPRegister::Params & params
00197   );
00198 
00199   ~SIPRegisterHandler();
00200 
00201   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00202   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00203   virtual SIP_PDU::Methods GetMethod()
00204     { return SIP_PDU::Method_REGISTER; }
00205 
00206   virtual void OnFailed(SIP_PDU::StatusCodes r);
00207 
00208   void UpdateParameters(const SIPRegister::Params & params);
00209 
00210   const SIPRegister::Params & GetParams() const { return m_parameters; }
00211 
00212 protected:
00213   virtual PBoolean SendRequest(SIPHandler::State state);
00214   void SendStatus(SIP_PDU::StatusCodes code, State state);
00215 
00216   SIPRegister::Params m_parameters;
00217   unsigned            m_sequenceNumber;
00218 };
00219 
00220 
00221 class SIPSubscribeHandler : public SIPHandler
00222 {
00223   PCLASSINFO(SIPSubscribeHandler, SIPHandler);
00224 public:
00225   SIPSubscribeHandler(SIPEndPoint & ep, const SIPSubscribe::Params & params);
00226   ~SIPSubscribeHandler();
00227 
00228   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00229   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00230   virtual PBoolean OnReceivedNOTIFY(SIP_PDU & response);
00231   virtual void OnFailed(const SIP_PDU & response);
00232   virtual SIP_PDU::Methods GetMethod ()
00233     { return SIP_PDU::Method_SUBSCRIBE; }
00234   virtual SIPEventPackage GetEventPackage() const
00235     { return m_parameters.m_eventPackage; }
00236 
00237   void UpdateParameters(const SIPSubscribe::Params & params);
00238 
00239   virtual bool IsDuplicateCSeq(unsigned sequenceNumber) { return m_dialog.IsDuplicateCSeq(sequenceNumber); }
00240 
00241   const SIPSubscribe::Params & GetParams() const { return m_parameters; }
00242 
00243 protected:
00244   virtual PBoolean SendRequest(SIPHandler::State state);
00245   virtual bool WriteSIPHandler(OpalTransport & transport, bool forked);
00246   void SendStatus(SIP_PDU::StatusCodes code, State state);
00247   bool DispatchNOTIFY(SIP_PDU & request, SIP_PDU & response);
00248 
00249   SIPSubscribe::Params     m_parameters;
00250   SIPDialogContext         m_dialog;
00251   bool                     m_unconfirmed;
00252   SIPEventPackageHandler * m_packageHandler;
00253 };
00254 
00255 
00256 class SIPNotifyHandler : public SIPHandler
00257 {
00258   PCLASSINFO(SIPNotifyHandler, SIPHandler);
00259 public:
00260   SIPNotifyHandler(
00261     SIPEndPoint & ep,
00262     const PString & targetAddress,
00263     const SIPEventPackage & eventPackage,
00264     const SIPDialogContext & dialog
00265   );
00266   ~SIPNotifyHandler();
00267 
00268   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00269   virtual SIP_PDU::Methods GetMethod ()
00270     { return SIP_PDU::Method_NOTIFY; }
00271   virtual SIPEventPackage GetEventPackage() const
00272     { return m_eventPackage; }
00273 
00274   virtual bool IsDuplicateCSeq(unsigned sequenceNumber) { return m_dialog.IsDuplicateCSeq(sequenceNumber); }
00275   virtual bool SendNotify(const PObject * body);
00276 
00277   enum Reasons {
00278     Deactivated,
00279     Probation,
00280     Rejected,
00281     Timeout,
00282     GiveUp,
00283     NoResource
00284   };
00285 
00286 protected:
00287   virtual PBoolean SendRequest(SIPHandler::State state);
00288   virtual bool WriteSIPHandler(OpalTransport & transport, bool forked);
00289 
00290   SIPEventPackage          m_eventPackage;
00291   SIPDialogContext         m_dialog;
00292   Reasons                  m_reason;
00293   SIPEventPackageHandler * m_packageHandler;
00294 };
00295 
00296 
00297 class SIPPublishHandler : public SIPHandler
00298 {
00299   PCLASSINFO(SIPPublishHandler, SIPHandler);
00300 
00301 public:
00302   SIPPublishHandler(SIPEndPoint & ep, 
00303                     const SIPSubscribe::Params & params,
00304                     const PString & body);
00305   ~SIPPublishHandler();
00306 
00307   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00308   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00309   virtual SIP_PDU::Methods GetMethod()
00310     { return SIP_PDU::Method_PUBLISH; }
00311   virtual SIPEventPackage GetEventPackage() const
00312     { return m_parameters.m_eventPackage; }
00313 
00314 private:
00315   SIPSubscribe::Params m_parameters;
00316   PString              m_sipETag;
00317 };
00318 
00319 
00320 class SIPMessageHandler : public SIPHandler
00321 {
00322   PCLASSINFO(SIPMessageHandler, SIPHandler);
00323 public:
00324   SIPMessageHandler(SIPEndPoint & ep, 
00325                     const SIPMessage::Params & params,
00326                     const PString & body);
00327   ~SIPMessageHandler();
00328 
00329   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00330   virtual SIP_PDU::Methods GetMethod ()
00331     { return SIP_PDU::Method_MESSAGE; }
00332   virtual void OnFailed (SIP_PDU::StatusCodes);
00333   virtual void SetBody(const PString & b);
00334 
00335   PURL m_localAddress;
00336   PString m_id;
00337 
00338 private:
00339   SIPMessage::Params m_parameters;
00340   virtual void OnExpireTimeout(PTimer &, INT);
00341   PString m_body;
00342 };
00343 
00344 
00345 class SIPPingHandler : public SIPHandler
00346 {
00347   PCLASSINFO(SIPPingHandler, SIPHandler);
00348 public:
00349   SIPPingHandler(SIPEndPoint & ep, const PURL & to);
00350   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00351   virtual SIP_PDU::Methods GetMethod ()
00352     { return SIP_PDU::Method_MESSAGE; }
00353 };
00354 
00355 
00359 class SIPHandlersList
00360 {
00361   public:
00364     void Append(SIPHandler * handler);
00365 
00370     void Remove(SIPHandler * handler);
00371 
00374     void Update(SIPHandler * handler);
00375 
00378     bool DeleteObjectsToBeRemoved()
00379       { return m_handlersList.DeleteObjectsToBeRemoved(); }
00380 
00384     PSafePtr<SIPHandler> GetFirstHandler(PSafetyMode mode = PSafeReference) const
00385       { return PSafePtr<SIPHandler>(m_handlersList, mode); }
00386 
00390     unsigned GetCount(SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
00391 
00395     PStringList GetAddresses(bool includeOffline, SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
00396 
00400     PSafePtr<SIPHandler> FindSIPHandlerByCallID(const PString & callID, PSafetyMode m);
00401 
00405     PSafePtr<SIPHandler> FindSIPHandlerByAuthRealm(const PString & authRealm, const PString & userName, PSafetyMode m);
00406 
00414     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PURL & url, SIP_PDU::Methods meth, PSafetyMode m);
00415     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PURL & url, SIP_PDU::Methods meth, const PString & eventPackage, PSafetyMode m);
00416 
00422     PSafePtr <SIPHandler> FindSIPHandlerByDomain(const PString & name, SIP_PDU::Methods meth, PSafetyMode m);
00423 
00424   protected:
00425     void RemoveIndexes(SIPHandler * handler);
00426 
00427     PMutex m_extraMutex;
00428     PSafeList<SIPHandler> m_handlersList;
00429 
00430     typedef SIPHandler::IndexMap IndexMap;
00431     PSafePtr<SIPHandler> FindBy(IndexMap & by, const PString & key, PSafetyMode m);
00432 
00433     IndexMap m_byCallID;
00434     IndexMap m_byAorAndPackage;
00435     IndexMap m_byAuthIdAndRealm;
00436     IndexMap m_byAorUserAndRealm;
00437 };
00438 
00439 
00442 class SIPPresenceInfo : public OpalPresenceInfo
00443 {
00444 public:
00445   SIPPresenceInfo(
00446     const PString & personId = PString::Empty(),
00447     State state = Unchanged
00448   );
00449 
00450   // basic presence defined by RFC 3863
00451   PString m_tupleId;
00452   PString m_contact;
00453 
00454   // presence extensions defined by RFC 4480
00455   PStringArray m_activities;  // list of activities, seperated by newline
00456 
00457   // presence agent
00458   PString m_presenceAgent;
00459 
00460   PString AsXML() const;
00461 
00462   void PrintOn(ostream & strm) const;
00463   friend ostream & operator<<(ostream & strm, const SIPPresenceInfo & info) { info.PrintOn(strm); return strm; }
00464 
00465   static State FromSIPActivityString(const PString & str);
00466 
00467   static bool AsSIPActivityString(State state, PString & str);
00468   bool AsSIPActivityString(PString & str) const;
00469 
00470   PString m_personId;
00471 };
00472 
00473 
00476 struct SIPDialogNotification : public PObject
00477 {
00478   PCLASSINFO(SIPDialogNotification, PObject);
00479 
00480   enum States {
00481     Terminated,
00482     Trying,
00483     Proceeding,
00484     Early,
00485     Confirmed,
00486 
00487     FirstState = Terminated,
00488     LastState = Confirmed
00489   };
00490   friend States operator++(States & state) { return (state = (States)(state+1)); }
00491   friend States operator--(States & state) { return (state = (States)(state-1)); }
00492   static PString GetStateName(States state);
00493   PString GetStateName() const { return GetStateName(m_state); }
00494 
00495   enum Events {
00496     NoEvent = -1,
00497     Cancelled,
00498     Rejected,
00499     Replaced,
00500     LocalBye,
00501     RemoteBye,
00502     Error,
00503     Timeout,
00504 
00505     FirstEvent = Cancelled,
00506     LastEvent = Timeout
00507   };
00508   friend Events operator++(Events & evt) { return (evt = (Events)(evt+1)); }
00509   friend Events operator--(Events & evt) { return (evt = (Events)(evt-1)); }
00510   static PString GetEventName(Events state);
00511   PString GetEventName() const { return GetEventName(m_eventType); }
00512 
00513   enum Rendering {
00514     RenderingUnknown = -1,
00515     NotRenderingMedia,
00516     RenderingMedia
00517   };
00518 
00519   PString  m_entity;
00520   PString  m_dialogId;
00521   PString  m_callId;
00522   bool     m_initiator;
00523   States   m_state;
00524   Events   m_eventType;
00525   unsigned m_eventCode;
00526   struct Participant {
00527     Participant() : m_appearance(-1), m_byeless(false), m_rendering(RenderingUnknown) { }
00528     PString   m_URI;
00529     PString   m_dialogTag;
00530     PString   m_identity;
00531     PString   m_display;
00532     int       m_appearance;
00533     bool      m_byeless;
00534     Rendering m_rendering;
00535   } m_local, m_remote;
00536 
00537   SIPDialogNotification(const PString & entity = PString::Empty());
00538 
00539   void PrintOn(ostream & strm) const;
00540 };
00541 
00542 
00543 #endif // OPAL_SIP
00544 
00545 #endif // OPAL_SIP_HANDLERS_H

Generated on Mon Feb 21 20:19:21 2011 for OPAL by  doxygen 1.4.7