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: 24239 $
00027  * $Author: rjongbloed $
00028  * $Date: 2010-04-13 00:04:36 -0500 (Tue, 13 Apr 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   bool WriteSIPHandler(OpalTransport & transport);
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   void SendStatus(SIP_PDU::StatusCodes code, State state);
00246   bool DispatchNOTIFY(SIP_PDU & request, SIP_PDU & response);
00247 
00248   SIPSubscribe::Params     m_parameters;
00249   SIPDialogContext         m_dialog;
00250   bool                     m_unconfirmed;
00251   SIPEventPackageHandler * m_packageHandler;
00252 };
00253 
00254 
00255 class SIPNotifyHandler : public SIPHandler
00256 {
00257   PCLASSINFO(SIPNotifyHandler, SIPHandler);
00258 public:
00259   SIPNotifyHandler(
00260     SIPEndPoint & ep,
00261     const PString & targetAddress,
00262     const SIPEventPackage & eventPackage,
00263     const SIPDialogContext & dialog
00264   );
00265   ~SIPNotifyHandler();
00266 
00267   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00268   virtual SIP_PDU::Methods GetMethod ()
00269     { return SIP_PDU::Method_NOTIFY; }
00270   virtual SIPEventPackage GetEventPackage() const
00271     { return m_eventPackage; }
00272 
00273   virtual bool IsDuplicateCSeq(unsigned sequenceNumber) { return m_dialog.IsDuplicateCSeq(sequenceNumber); }
00274   virtual bool SendNotify(const PObject * body);
00275 
00276   enum Reasons {
00277     Deactivated,
00278     Probation,
00279     Rejected,
00280     Timeout,
00281     GiveUp,
00282     NoResource
00283   };
00284 
00285 protected:
00286   virtual PBoolean SendRequest(SIPHandler::State state);
00287 
00288   SIPEventPackage          m_eventPackage;
00289   SIPDialogContext         m_dialog;
00290   Reasons                  m_reason;
00291   SIPEventPackageHandler * m_packageHandler;
00292 };
00293 
00294 
00295 class SIPPublishHandler : public SIPHandler
00296 {
00297   PCLASSINFO(SIPPublishHandler, SIPHandler);
00298 
00299 public:
00300   SIPPublishHandler(SIPEndPoint & ep, 
00301                     const SIPSubscribe::Params & params,
00302                     const PString & body);
00303   ~SIPPublishHandler();
00304 
00305   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00306   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00307   virtual SIP_PDU::Methods GetMethod()
00308     { return SIP_PDU::Method_PUBLISH; }
00309   virtual SIPEventPackage GetEventPackage() const
00310     { return m_parameters.m_eventPackage; }
00311 
00312 private:
00313   SIPSubscribe::Params m_parameters;
00314   PString              m_sipETag;
00315 };
00316 
00317 
00318 class SIPMessageHandler : public SIPHandler
00319 {
00320   PCLASSINFO(SIPMessageHandler, SIPHandler);
00321 public:
00322   SIPMessageHandler(SIPEndPoint & ep, 
00323                     const SIPMessage::Params & params,
00324                     const PString & body);
00325   ~SIPMessageHandler();
00326 
00327   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00328   virtual SIP_PDU::Methods GetMethod ()
00329     { return SIP_PDU::Method_MESSAGE; }
00330   virtual void OnFailed (SIP_PDU::StatusCodes);
00331   virtual void SetBody(const PString & b);
00332 
00333   PURL m_localAddress;
00334   PString m_id;
00335 
00336 private:
00337   SIPMessage::Params m_parameters;
00338   virtual void OnExpireTimeout(PTimer &, INT);
00339   PString m_body;
00340 };
00341 
00342 
00343 class SIPPingHandler : public SIPHandler
00344 {
00345   PCLASSINFO(SIPPingHandler, SIPHandler);
00346 public:
00347   SIPPingHandler(SIPEndPoint & ep, const PURL & to);
00348   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00349   virtual SIP_PDU::Methods GetMethod ()
00350     { return SIP_PDU::Method_MESSAGE; }
00351 };
00352 
00353 
00357 class SIPHandlersList
00358 {
00359   public:
00362     void Append(SIPHandler * handler);
00363 
00368     void Remove(SIPHandler * handler);
00369 
00372     void Update(SIPHandler * handler);
00373 
00376     bool DeleteObjectsToBeRemoved()
00377       { return m_handlersList.DeleteObjectsToBeRemoved(); }
00378 
00382     PSafePtr<SIPHandler> GetFirstHandler(PSafetyMode mode = PSafeReference) const
00383       { return PSafePtr<SIPHandler>(m_handlersList, mode); }
00384 
00388     unsigned GetCount(SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
00389 
00393     PStringList GetAddresses(bool includeOffline, SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
00394 
00398     PSafePtr<SIPHandler> FindSIPHandlerByCallID(const PString & callID, PSafetyMode m);
00399 
00403     PSafePtr<SIPHandler> FindSIPHandlerByAuthRealm(const PString & authRealm, const PString & userName, PSafetyMode m);
00404 
00412     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PURL & url, SIP_PDU::Methods meth, PSafetyMode m);
00413     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PURL & url, SIP_PDU::Methods meth, const PString & eventPackage, PSafetyMode m);
00414 
00420     PSafePtr <SIPHandler> FindSIPHandlerByDomain(const PString & name, SIP_PDU::Methods meth, PSafetyMode m);
00421 
00422   protected:
00423     void RemoveIndexes(SIPHandler * handler);
00424 
00425     PMutex m_extraMutex;
00426     PSafeList<SIPHandler> m_handlersList;
00427 
00428     typedef SIPHandler::IndexMap IndexMap;
00429     PSafePtr<SIPHandler> FindBy(IndexMap & by, const PString & key, PSafetyMode m);
00430 
00431     IndexMap m_byCallID;
00432     IndexMap m_byAorAndPackage;
00433     IndexMap m_byAuthIdAndRealm;
00434     IndexMap m_byAorUserAndRealm;
00435 };
00436 
00437 
00440 class SIPPresenceInfo : public OpalPresenceInfo
00441 {
00442 public:
00443   SIPPresenceInfo(
00444     const PString & personId = PString::Empty(),
00445     State state = Unchanged
00446   );
00447 
00448   // basic presence defined by RFC 3863
00449   PString m_tupleId;
00450   PString m_contact;
00451 
00452   // presence extensions defined by RFC 4480
00453   PStringArray m_activities;  // list of activities, seperated by newline
00454 
00455   // presence agent
00456   PString m_presenceAgent;
00457 
00458   PString AsXML() const;
00459 
00460   void PrintOn(ostream & strm) const;
00461   friend ostream & operator<<(ostream & strm, const SIPPresenceInfo & info) { info.PrintOn(strm); return strm; }
00462 
00463   static State FromSIPActivityString(const PString & str);
00464 
00465   static bool AsSIPActivityString(State state, PString & str);
00466   bool AsSIPActivityString(PString & str) const;
00467 
00468   PString m_personId;
00469 };
00470 
00471 
00474 struct SIPDialogNotification : public PObject
00475 {
00476   PCLASSINFO(SIPDialogNotification, PObject);
00477 
00478   enum States {
00479     Terminated,
00480     Trying,
00481     Proceeding,
00482     Early,
00483     Confirmed,
00484 
00485     FirstState = Terminated,
00486     LastState = Confirmed
00487   };
00488   friend States operator++(States & state) { return (state = (States)(state+1)); }
00489   friend States operator--(States & state) { return (state = (States)(state-1)); }
00490   static PString GetStateName(States state);
00491   PString GetStateName() const { return GetStateName(m_state); }
00492 
00493   enum Events {
00494     NoEvent = -1,
00495     Cancelled,
00496     Rejected,
00497     Replaced,
00498     LocalBye,
00499     RemoteBye,
00500     Error,
00501     Timeout,
00502 
00503     FirstEvent = Cancelled,
00504     LastEvent = Timeout
00505   };
00506   friend Events operator++(Events & evt) { return (evt = (Events)(evt+1)); }
00507   friend Events operator--(Events & evt) { return (evt = (Events)(evt-1)); }
00508   static PString GetEventName(Events state);
00509   PString GetEventName() const { return GetEventName(m_eventType); }
00510 
00511   enum Rendering {
00512     RenderingUnknown = -1,
00513     NotRenderingMedia,
00514     RenderingMedia
00515   };
00516 
00517   PString  m_entity;
00518   PString  m_dialogId;
00519   PString  m_callId;
00520   bool     m_initiator;
00521   States   m_state;
00522   Events   m_eventType;
00523   unsigned m_eventCode;
00524   struct Participant {
00525     Participant() : m_appearance(-1), m_byeless(false), m_rendering(RenderingUnknown) { }
00526     PString   m_URI;
00527     PString   m_dialogTag;
00528     PString   m_identity;
00529     PString   m_display;
00530     int       m_appearance;
00531     bool      m_byeless;
00532     Rendering m_rendering;
00533   } m_local, m_remote;
00534 
00535   SIPDialogNotification(const PString & entity = PString::Empty());
00536 
00537   void PrintOn(ostream & strm) const;
00538 };
00539 
00540 
00541 #endif // OPAL_SIP
00542 
00543 #endif // OPAL_SIP_HANDLERS_H

Generated on Wed Dec 15 07:55:08 2010 for OPAL by  doxygen 1.4.7