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: 23083 $
00027  * $Author: csoutheren $
00028  * $Date: 2009-07-06 08:31:42 +0000 (Mon, 06 Jul 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 
00123   virtual void OnFailed(const SIP_PDU & response);
00124   virtual void OnFailed(SIP_PDU::StatusCodes);
00125 
00126   bool ActivateState(SIPHandler::State state, unsigned msecs = 1000);
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 
00138 protected:
00139   virtual PBoolean SendRequest(SIPHandler::State state);
00140   void CollapseFork(SIPTransaction & transaction);
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 public:
00170   std::string                 m_userNameAndRealmKey;
00171   std::string                 m_urlKey;
00172   std::string                 m_urlAndPackageKey;
00173   SIPMIMEInfo                 m_mime;
00174 };
00175 
00176 #if PTRACING
00177 ostream & operator<<(ostream & strm, SIPHandler::State state);
00178 #endif
00179 
00180 
00181 class SIPRegisterHandler : public SIPHandler
00182 {
00183   PCLASSINFO(SIPRegisterHandler, SIPHandler);
00184 
00185 public:
00186   SIPRegisterHandler(
00187     SIPEndPoint & ep,
00188     const SIPRegister::Params & params
00189   );
00190 
00191   ~SIPRegisterHandler();
00192 
00193   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00194   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00195   virtual SIP_PDU::Methods GetMethod()
00196     { return SIP_PDU::Method_REGISTER; }
00197 
00198   virtual void OnFailed(SIP_PDU::StatusCodes r);
00199 
00200   void UpdateParameters(const SIPRegister::Params & params);
00201 
00202 protected:
00203   virtual PBoolean SendRequest(SIPHandler::State state);
00204   void SendStatus(SIP_PDU::StatusCodes code, State state);
00205 
00206   SIPRegister::Params m_parameters;
00207   unsigned            m_sequenceNumber;
00208 };
00209 
00210 
00211 class SIPSubscribeHandler : public SIPHandler
00212 {
00213   PCLASSINFO(SIPSubscribeHandler, SIPHandler);
00214 public:
00215   SIPSubscribeHandler(SIPEndPoint & ep, const SIPSubscribe::Params & params);
00216   ~SIPSubscribeHandler();
00217 
00218   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00219   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00220   virtual PBoolean OnReceivedNOTIFY(SIP_PDU & response);
00221   virtual void OnFailed(const SIP_PDU & response);
00222   virtual SIP_PDU::Methods GetMethod ()
00223     { return SIP_PDU::Method_SUBSCRIBE; }
00224   virtual SIPEventPackage GetEventPackage() const
00225     { return m_parameters.m_eventPackage; }
00226 
00227   void UpdateParameters(const SIPSubscribe::Params & params);
00228 
00229   virtual bool IsDuplicateCSeq(unsigned sequenceNumber) { return m_dialog.IsDuplicateCSeq(sequenceNumber); }
00230 
00231   SIPSubscribe::Params & GetParams() { return m_parameters; }
00232 
00233 protected:
00234   virtual PBoolean SendRequest(SIPHandler::State state);
00235   void SendStatus(SIP_PDU::StatusCodes code, State state);
00236 
00237   SIPSubscribe::Params     m_parameters;
00238   SIPDialogContext         m_dialog;
00239   bool                     m_unconfirmed;
00240   SIPEventPackageHandler * m_packageHandler;
00241 };
00242 
00243 
00244 class SIPNotifyHandler : public SIPHandler
00245 {
00246   PCLASSINFO(SIPNotifyHandler, SIPHandler);
00247 public:
00248   SIPNotifyHandler(
00249     SIPEndPoint & ep,
00250     const PString & targetAddress,
00251     const SIPEventPackage & eventPackage,
00252     const SIPDialogContext & dialog
00253   );
00254   ~SIPNotifyHandler();
00255 
00256   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00257   virtual SIP_PDU::Methods GetMethod ()
00258     { return SIP_PDU::Method_NOTIFY; }
00259   virtual SIPEventPackage GetEventPackage() const
00260     { return m_eventPackage; }
00261 
00262   virtual bool IsDuplicateCSeq(unsigned sequenceNumber) { return m_dialog.IsDuplicateCSeq(sequenceNumber); }
00263   virtual bool SendNotify(const PObject * body);
00264 
00265   enum Reasons {
00266     Deactivated,
00267     Probation,
00268     Rejected,
00269     Timeout,
00270     GiveUp,
00271     NoResource
00272   };
00273 
00274 protected:
00275   virtual PBoolean SendRequest(SIPHandler::State state);
00276 
00277   SIPEventPackage          m_eventPackage;
00278   SIPDialogContext         m_dialog;
00279   Reasons                  m_reason;
00280   SIPEventPackageHandler * m_packageHandler;
00281 };
00282 
00283 
00284 class SIPPublishHandler : public SIPHandler
00285 {
00286   PCLASSINFO(SIPPublishHandler, SIPHandler);
00287 
00288 public:
00289   SIPPublishHandler(SIPEndPoint & ep, 
00290                     const SIPSubscribe::Params & params,
00291                     const PString & body);
00292   ~SIPPublishHandler();
00293 
00294   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00295   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00296   virtual SIP_PDU::Methods GetMethod()
00297     { return SIP_PDU::Method_PUBLISH; }
00298   virtual SIPEventPackage GetEventPackage() const
00299     { return m_parameters.m_eventPackage; }
00300   virtual void SetBody(const PString & body);
00301 
00302 private:
00303   SIPSubscribe::Params m_parameters;
00304   PString              m_sipETag;
00305   bool                 m_stateChanged;
00306 };
00307 
00308 
00309 class SIPMessageHandler : public SIPHandler
00310 {
00311   PCLASSINFO(SIPMessageHandler, SIPHandler);
00312 public:
00313   SIPMessageHandler(SIPEndPoint & ep, 
00314                     const PString & to,
00315                     const PString & body,
00316                     const PString & remoteContact,
00317                     const PString & callId);
00318   ~SIPMessageHandler();
00319 
00320   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00321   virtual SIP_PDU::Methods GetMethod ()
00322     { return SIP_PDU::Method_MESSAGE; }
00323   virtual void OnFailed (SIP_PDU::StatusCodes);
00324   virtual void SetBody(const PString & b);
00325 
00326   SIPURL m_localAddress;
00327 
00328 private:
00329   virtual void OnExpireTimeout(PTimer &, INT);
00330 };
00331 
00332 
00333 class SIPPingHandler : public SIPHandler
00334 {
00335   PCLASSINFO(SIPPingHandler, SIPHandler);
00336 public:
00337   SIPPingHandler(SIPEndPoint & ep, 
00338                  const PString & to);
00339   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00340   virtual SIP_PDU::Methods GetMethod ()
00341     { return SIP_PDU::Method_MESSAGE; }
00342 };
00343 
00344 
00348 class SIPHandlersList
00349 {
00350   public:
00353     void Append(SIPHandler * handler);
00354 
00359     void Remove(SIPHandler * handler);
00360 
00363     bool DeleteObjectsToBeRemoved()
00364       { return m_handlersList.DeleteObjectsToBeRemoved(); }
00365 
00369     PSafePtr<SIPHandler> GetFirstHandler(PSafetyMode mode = PSafeReference) const
00370       { return PSafePtr<SIPHandler>(m_handlersList, mode); }
00371 
00375     unsigned GetCount(SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
00376 
00380     PStringList GetAddresses(bool includeOffline, SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
00381 
00385     PSafePtr<SIPHandler> FindSIPHandlerByCallID(const PString & callID, PSafetyMode m);
00386 
00390     PSafePtr<SIPHandler> FindSIPHandlerByAuthRealm(const PString & authRealm, const PString & userName, PSafetyMode m);
00391 
00399     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, PSafetyMode m);
00400     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, const PString & eventPackage, PSafetyMode m);
00401 
00407     PSafePtr <SIPHandler> FindSIPHandlerByDomain(const PString & name, SIP_PDU::Methods meth, PSafetyMode m);
00408 
00409   protected:
00410     PMutex m_extraMutex;
00411     PSafeList<SIPHandler> m_handlersList;
00412     typedef std::map<std::string, PSafePtr<SIPHandler> > StringToHandlerMap;
00413     StringToHandlerMap m_handlersByCallId;
00414     StringToHandlerMap m_handlersByUserNameAndRealm;
00415     StringToHandlerMap m_handlersByUrl;
00416     StringToHandlerMap m_handlersByUrlAndPackage;
00417 };
00418 
00419 
00422 struct SIPPresenceInfo
00423 {
00424   enum BasicStates {
00425     Unknown,
00426     Open,
00427     Closed,
00428     Unchanged
00429   };
00430 
00431   enum ExtendedStates {
00432     UnknownExtended,
00433     Appointment,
00434     Away,
00435     Breakfast,
00436     Busy,
00437     Dinner,
00438     Holiday,
00439     InTransit,
00440     LookingForWork,
00441     Lunch,
00442     Meal,
00443     Meeting,
00444     OnThePhone,
00445     Other,
00446     Performance,
00447     PermanentAbsence,
00448     Playing,
00449     Presentation,
00450     Shopping,
00451     Sleeping,
00452     Spectator,
00453     Steering,
00454     Travel,
00455     TV,
00456     Vacation,
00457     Working,
00458     Worship
00459   };
00460 
00461   SIPPresenceInfo() : m_basic(Unknown) { }
00462 
00463   // basic presence defined by RFC 3863
00464   PString     m_address;
00465   PString     m_entity;
00466   BasicStates m_basic;
00467   PString     m_note;
00468   PString     m_contact;
00469   PString     m_to;
00470 
00471   // presence extensions defined by RFC 4480
00472   PStringArray m_activities;  // list of activities, seperated by newline
00473   ExtendedStates m_activity;  // primary activity
00474 
00475   // presence agent
00476   PString m_presenceAgent;
00477 
00478   PString AsXML() const;
00479 
00480   void PrintOn(ostream & strm) const;
00481   friend ostream & operator<<(ostream & strm, const SIPPresenceInfo & info) { info.PrintOn(strm); return strm; }
00482 };
00483 
00484 
00487 struct SIPDialogNotification : public PObject
00488 {
00489   PCLASSINFO(SIPDialogNotification, PObject);
00490 
00491   enum States {
00492     Terminated,
00493     Trying,
00494     Proceeding,
00495     Early,
00496     Confirmed,
00497 
00498     FirstState = Terminated,
00499     LastState = Confirmed
00500   };
00501   friend States operator++(States & state) { return (state = (States)(state+1)); }
00502   friend States operator--(States & state) { return (state = (States)(state-1)); }
00503   static PString GetStateName(States state);
00504   PString GetStateName() const { return GetStateName(m_state); }
00505 
00506   enum Events {
00507     NoEvent = -1,
00508     Cancelled,
00509     Rejected,
00510     Replaced,
00511     LocalBye,
00512     RemoteBye,
00513     Error,
00514     Timeout,
00515 
00516     FirstEvent = Cancelled,
00517     LastEvent = Timeout
00518   };
00519   friend Events operator++(Events & evt) { return (evt = (Events)(evt+1)); }
00520   friend Events operator--(Events & evt) { return (evt = (Events)(evt-1)); }
00521   static PString GetEventName(Events state);
00522   PString GetEventName() const { return GetEventName(m_eventType); }
00523 
00524   enum Rendering {
00525     RenderingUnknown = -1,
00526     NotRenderingMedia,
00527     RenderingMedia
00528   };
00529 
00530   PString  m_entity;
00531   PString  m_dialogId;
00532   PString  m_callId;
00533   bool     m_initiator;
00534   States   m_state;
00535   Events   m_eventType;
00536   unsigned m_eventCode;
00537   struct Participant {
00538     Participant() : m_appearance(-1), m_byeless(false), m_rendering(RenderingUnknown) { }
00539     PString   m_URI;
00540     PString   m_dialogTag;
00541     PString   m_identity;
00542     PString   m_display;
00543     int       m_appearance;
00544     bool      m_byeless;
00545     Rendering m_rendering;
00546   } m_local, m_remote;
00547 
00548   SIPDialogNotification(const PString & entity = PString::Empty());
00549 
00550   void PrintOn(ostream & strm) const;
00551 };
00552 
00553 
00554 #endif // OPAL_SIP
00555 
00556 #endif // OPAL_SIP_HANDLERS_H

Generated on Mon Aug 3 20:50:21 2009 for OPAL by  doxygen 1.5.1