pres_ent.h

Go to the documentation of this file.
00001 /*
00002  * prese_ent.h
00003  *
00004  * Presence Entity classes for Opal
00005  *
00006  * Open Phone Abstraction Library (OPAL)
00007  *
00008  * Copyright (c) 2009 Post Increment
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 Post Increment
00023  *
00024  * Contributor(s): ______________________________________.
00025  *
00026  * $Revision: 24993 $
00027  * $Author: rjongbloed $
00028  * $Date: 2011-01-04 16:38:21 -0600 (Tue, 04 Jan 2011) $
00029  */
00030 
00031 #ifndef OPAL_IM_PRES_ENT_H
00032 #define OPAL_IM_PRES_ENT_H
00033 
00034 #include <ptlib.h>
00035 #include <opal/buildopts.h>
00036 
00037 #include <ptlib/pfactory.h>
00038 #include <ptlib/safecoll.h>
00039 #include <ptclib/url.h>
00040 #include <ptclib/guid.h>
00041 
00042 #include <list>
00043 #include <queue>
00044 
00045 class OpalManager;
00046 class OpalPresentityCommand;
00047 
00048 
00050 
00053 class OpalPresenceInfo
00054 {
00055   public:
00057     enum State {
00058       InternalError = -3,    // something bad happened
00059       Forbidden     = -2,    // access to presence information was specifically forbidden
00060       NoPresence    = -1,    // remove presence status - not the same as Unavailable or Away
00061 
00062       // basic states (from RFC 3863)
00063       Unchanged,
00064       Available,
00065       Unavailable,
00066 
00067       // extended states (from RFC 4480)
00068       // if this is changed, also change the tables in sippres.cxx and handlers.cxx - look for RFC 4480
00069       ExtendedBase    = 100,
00070       UnknownExtended = ExtendedBase,
00071       Appointment,
00072       Away,
00073       Breakfast,
00074       Busy,
00075       Dinner,
00076       Holiday,
00077       InTransit,
00078       LookingForWork,
00079       Lunch,
00080       Meal,
00081       Meeting,
00082       OnThePhone,
00083       Other,
00084       Performance,
00085       PermanentAbsence,
00086       Playing,
00087       Presentation,
00088       Shopping,
00089       Sleeping,
00090       Spectator,
00091       Steering,
00092       Travel,
00093       TV,
00094       Vacation,
00095       Working,
00096       Worship
00097     };
00098 
00099     State   m_state;    
00100     PString m_note;     
00101     PURL    m_entity;   
00102     PURL    m_target;   
00103 
00104     OpalPresenceInfo(State state = Unchanged) : m_state(state) { }
00105 
00106     static PString AsString(State state);
00107     static State FromString(const PString & str);
00108     PString AsString() const;
00109 };
00110 
00111 ostream & operator<<(ostream & strm, OpalPresenceInfo::State state);
00112 
00113 
00115 
00125 class OpalPresentity : public PSafeObject
00126 {
00127     PCLASSINFO(OpalPresentity, PSafeObject);
00128 
00131   protected:
00133     OpalPresentity();
00134 
00135   public:
00138     static OpalPresentity * Create(
00139       OpalManager & manager, 
00140       const PURL & url,      
00141       const PString & scheme = PString::Empty() 
00142     );
00144 
00156     virtual bool Open() = 0;
00157 
00160     virtual bool IsOpen() const = 0;
00161 
00164     virtual bool Close() = 0;
00166 
00171     class Attributes :  public PStringToString
00172     {
00173       public:
00175         virtual bool Has(const PString &   key   ) const { return Contains(key  ); }
00176         virtual bool Has(const PString & (*key)()) const { return Contains(key()); }
00177 
00179         virtual PString Get(const PString &   key   , const PString & deflt = PString::Empty()) const { return (*this)(key  , deflt); }
00180                 PString Get(const PString & (*key)(), const PString & deflt = PString::Empty()) const { return (*this)(key(), deflt); }
00181 
00183         virtual void Set(const PString &   key   , const PString & value) { SetAt(key  , value); }
00184                 void Set(const PString & (*key)(), const PString & value) { SetAt(key(), value); }
00185     };
00186 
00188     Attributes & GetAttributes() { return m_attributes; }
00189 
00190     static const PString & AuthNameKey();         
00191     static const PString & AuthPasswordKey();     
00192     static const PString & FullNameKey();         
00193     static const PString & SchemeKey();           
00194     static const PString & TimeToLiveKey();       
00195 
00200     const PURL & GetAOR() const { return m_aor; }
00202 
00213     virtual bool SubscribeToPresence(
00214       const PURL & presentity,      
00215       bool subscribe = true,        
00216       const PString & note = PString::Empty() 
00217     );
00218 
00227     virtual bool UnsubscribeFromPresence(
00228       const PURL & presentity    
00229     );
00230 
00232     enum Authorisation {
00233       AuthorisationPermitted,
00234       AuthorisationDenied,
00235       AuthorisationDeniedPolitely,
00236       AuthorisationConfirming,
00237       AuthorisationRemove,
00238       NumAuthorisations
00239     };
00240 
00251     virtual bool SetPresenceAuthorisation(
00252       const PURL & presentity,        
00253       Authorisation authorisation     
00254     );
00255 
00263     virtual bool SetLocalPresence(
00264       OpalPresenceInfo::State state,            
00265       const PString & note = PString::Empty()   
00266     );
00267 
00272     template <class cls>
00273     __inline cls * CreateCommand()
00274     {
00275       return dynamic_cast<cls *>(InternalCreateCommand(typeid(cls).name()));
00276     }
00277 
00289     virtual bool SendCommand(
00290       OpalPresentityCommand * cmd   
00291     );
00293 
00296     struct AuthorisationRequest
00297     {
00298       PURL    m_presentity; 
00299       PString m_note;       
00300     };
00301 
00309     virtual void OnAuthorisationRequest(
00310       const AuthorisationRequest & request  
00311     );
00312 
00313     typedef PNotifierTemplate<const AuthorisationRequest &> AuthorisationRequestNotifier;
00314 #define PDECLARE_AuthorisationRequestNotifier(cls, fn) PDECLARE_NOTIFIER2(OpalPresentity, cls, fn, const OpalPresentity::AuthorisationRequest &)
00315     #define PCREATE_AuthorisationRequestNotifier(fn) PCREATE_NOTIFIER2(fn, const OpalPresentity::AuthorisationRequest &)
00316 
00318     void SetAuthorisationRequestNotifier(
00319       const AuthorisationRequestNotifier & notifier   
00320     );
00321 
00330     virtual void OnPresenceChange(
00331       const OpalPresenceInfo & info 
00332     );
00333 
00334     typedef PNotifierTemplate<const OpalPresenceInfo &> PresenceChangeNotifier;
00335     #define PDECLARE_PresenceChangeNotifier(cls, fn) PDECLARE_NOTIFIER2(OpalPresentity, cls, fn, const OpalPresenceInfo &)
00336     #define PCREATE_PresenceChangeNotifier(fn) PCREATE_NOTIFIER2(fn, const OpalPresenceInfo &)
00337 
00339     void SetPresenceChangeNotifier(
00340       const PresenceChangeNotifier & notifier   
00341     );
00343 
00350     struct BuddyInfo {
00351       BuddyInfo(
00352         const PURL & presentity = PString::Empty(),
00353         const PString & displayName = PString::Empty()
00354       ) : m_presentity(presentity)
00355         , m_displayName(displayName)
00356       { }
00357 
00358       PURL    m_presentity;   
00359       PString m_displayName;  
00360 
00361       PString m_contentType;  
00362       PString m_rawXML;       
00363     };
00364 
00365     typedef std::list<BuddyInfo> BuddyList;
00366 
00369     virtual bool GetBuddyList(
00370       BuddyList & buddies   
00371     );
00372 
00375     virtual bool SetBuddyList(
00376       const BuddyList & buddies   
00377     );
00378 
00381     virtual bool DeleteBuddyList();
00382 
00387     virtual bool GetBuddy(
00388       BuddyInfo & buddy
00389     );
00390 
00393     virtual bool SetBuddy(
00394       const BuddyInfo & buddy
00395     );
00396 
00399     virtual bool DeleteBuddy(
00400       const PURL & presentity
00401     );
00402 
00408     virtual bool SubscribeBuddyList(
00409       bool subscribe = true
00410     );
00411 
00417     virtual bool UnsubscribeBuddyList();
00418 
00419     virtual PString GetID() const;
00421 
00422   protected:
00423     OpalPresentityCommand * InternalCreateCommand(const char * cmdName);
00424 
00425     OpalManager        * m_manager;
00426     PGloballyUniqueID    m_guid;
00427     PURL                 m_aor;
00428     Attributes           m_attributes;
00429 
00430     AuthorisationRequestNotifier m_onAuthorisationRequestNotifier;
00431     PresenceChangeNotifier       m_onPresenceChangeNotifier;
00432 
00433     PMutex m_notificationMutex;
00434     PAtomicInteger::IntegerType m_idNumber;
00435 };
00436 
00437 
00439 
00443 class OpalPresentityWithCommandThread : public OpalPresentity
00444 {
00445     PCLASSINFO(OpalPresentityWithCommandThread, OpalPresentity);
00446 
00449   protected:
00451     OpalPresentityWithCommandThread();
00452 
00453   public:
00457     ~OpalPresentityWithCommandThread();
00459 
00473     virtual bool SendCommand(
00474       OpalPresentityCommand * cmd   
00475     );
00477 
00483     void StartThread();
00484 
00490     void StopThread();
00492 
00493   protected:
00494     void ThreadMain();
00495 
00496     typedef std::queue<OpalPresentityCommand *> CommandQueue;
00497     CommandQueue   m_commandQueue;
00498     PMutex         m_commandQueueMutex;
00499     PAtomicInteger m_commandSequence;
00500     PSyncPoint     m_commandQueueSync;
00501 
00502     bool      m_threadRunning;
00503     PThread * m_thread;
00504 };
00505 
00506 
00508 
00511 class OpalPresentityCommand {
00512   public:
00513     OpalPresentityCommand(bool responseNeeded = false) 
00514       : m_responseNeeded(responseNeeded)
00515     { }
00516     virtual ~OpalPresentityCommand() { }
00517 
00521     virtual void Process(
00522       OpalPresentity & presentity
00523     ) = 0;
00524 
00525     typedef PAtomicInteger::IntegerType CmdSeqType;
00526     CmdSeqType m_sequence;
00527     bool       m_responseNeeded;
00528     PURL       m_presentity;
00529 };
00530 
00533 #define OPAL_DEFINE_COMMAND(command, entity, func) \
00534   class entity##_##command : public command \
00535   { \
00536     public: virtual void Process(OpalPresentity & presentity) { dynamic_cast<entity &>(presentity).func(*this); } \
00537   }; \
00538   static PFactory<OpalPresentityCommand>::Worker<entity##_##command> \
00539   s_entity##_##command(PDefaultPFactoryKey(entity::Class())+typeid(command).name())
00540 
00541 
00544 class OpalSubscribeToPresenceCommand : public OpalPresentityCommand {
00545   public:
00546     OpalSubscribeToPresenceCommand(bool subscribe = true) : m_subscribe(subscribe) { }
00547 
00548     bool    m_subscribe;  
00549     PString m_note;       
00550 };
00551 
00552 
00559 class OpalAuthorisationRequestCommand : public OpalPresentityCommand {
00560   public:
00561     OpalAuthorisationRequestCommand() : m_authorisation(OpalPresentity::AuthorisationPermitted) { }
00562 
00563     OpalPresentity::Authorisation m_authorisation;  
00564     PString m_note;                                 
00565 };
00566 
00567 
00573 class OpalSetLocalPresenceCommand : public OpalPresentityCommand, public OpalPresenceInfo {
00574   public:
00575     OpalSetLocalPresenceCommand(State state = NoPresence) : OpalPresenceInfo(state) { }
00576 };
00577 
00578 
00580 
00581 // Include concrete classes here so the factories are initialised
00582 #if OPAL_SIP && OPAL_PTLIB_EXPAT
00583 PFACTORY_LOAD(SIPLocal_Presentity);
00584 PFACTORY_LOAD(SIPXCAP_Presentity);
00585 PFACTORY_LOAD(SIPOMA_Presentity);
00586 #endif
00587 
00588 
00589 #endif  // OPAL_IM_PRES_ENT_H
00590 

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