00001
00024 #ifndef __YATEJINGLE_H
00025 #define __YATEJINGLE_H
00026
00027 #include <yateclass.h>
00028 #include <yatejabber.h>
00029
00033 namespace TelEngine {
00034
00035 class JGAudio;
00036 class JGAudioList;
00037 class JGTransport;
00038 class JGSession;
00039 class JGEvent;
00040 class JGEngine;
00041 class JGSentStanza;
00042
00043
00048 class YJINGLE_API JGAudio : public GenObject
00049 {
00050 public:
00059 inline JGAudio(const char* _id, const char* _name, const char* _clockrate,
00060 const char* _bitrate, const char* _synonym)
00061 { set(_id,_name,_clockrate,_bitrate,_synonym); }
00062
00067 inline JGAudio(XMLElement* xml)
00068 { fromXML(xml); }
00069
00073 inline JGAudio(const JGAudio& src)
00074 { set(src.id,src.name,src.clockrate,src.bitrate,src.synonym); }
00075
00084 inline void set(const char* _id, const char* _name, const char* _clockrate,
00085 const char* _bitrate, const char* _synonym) {
00086 id = _id;
00087 name = _name;
00088 clockrate = _clockrate;
00089 bitrate = _bitrate;
00090 synonym = _synonym;
00091 }
00092
00097 virtual const String& toString() const
00098 { return id; }
00099
00104 XMLElement* toXML();
00105
00110 void fromXML(XMLElement* xml);
00111
00115 String id;
00116
00120 String name;
00121
00125 String clockrate;
00126
00130 String bitrate;
00131
00135 String synonym;
00136 };
00137
00138
00143 class JGAudioList : public ObjList
00144 {
00145 public:
00154 inline void add(const char* id, const char* name, const char* clockrate,
00155 const char* bitrate, const char* synonym)
00156 { append(new JGAudio(id,name,clockrate,bitrate,synonym)); }
00157
00163 JGAudio* findSynonym(const String& value);
00164
00170 XMLElement* toXML(bool telEvent = true);
00171
00176 void fromXML(XMLElement* xml);
00177
00185 bool createList(String& dest, bool synonym, const char* sep = ",");
00186 };
00187
00188
00193 class YJINGLE_API JGTransport : public RefObject
00194 {
00195 public:
00199 inline JGTransport()
00200 {}
00201
00205 JGTransport(const JGTransport& src);
00206
00211 inline JGTransport(XMLElement* element)
00212 { fromXML(element); }
00213
00217 virtual ~JGTransport()
00218 {}
00219
00224 static XMLElement* createTransport();
00225
00230 XMLElement* toXML();
00231
00236 void fromXML(XMLElement* element);
00237
00242 inline void addTo(XMLElement* transport)
00243 { if (transport) transport->addChild(toXML()); }
00244
00245
00246 String name;
00247 String address;
00248 String port;
00249 String preference;
00250 String username;
00251 String protocol;
00252 String generation;
00253 String password;
00254 String type;
00255 String network;
00256 };
00257
00258
00263 class YJINGLE_API JGSession : public RefObject, public Mutex
00264 {
00265 friend class JGEvent;
00266 friend class JGEngine;
00267 public:
00271 enum State {
00272 Idle = 0,
00273 Pending = 1,
00274 Active = 2,
00275 Ending = 3,
00276 Destroy = 4,
00277 };
00278
00282 enum Action {
00283 ActAccept,
00284 ActInitiate,
00285 ActReject,
00286 ActTerminate,
00287 ActTransport,
00288 ActTransportInfo,
00289 ActTransportCandidates,
00290 ActTransportAccept,
00291 ActContentInfo,
00292 ActDtmf,
00293 ActDtmfMethod,
00294 ActCount,
00295 };
00296
00300 enum TransportType {
00301 TransportUnknown,
00302 TransportInfo,
00303 TransportCandidates,
00304 };
00305
00309 virtual ~JGSession();
00310
00315 inline bool outgoing() const
00316 { return m_outgoing; }
00317
00322 const String& sid() const
00323 { return m_sid; }
00324
00329 const JabberID& local() const
00330 { return m_localJID; }
00331
00336 const JabberID& remote() const
00337 { return m_remoteJID; }
00338
00343 inline State state() const
00344 { return m_state; }
00345
00350 inline const JBStream* stream() const
00351 { return m_stream; }
00352
00357 inline void* userData()
00358 { return m_private; }
00359
00364 inline void userData(void* userdata)
00365 { m_private = userdata; }
00366
00373 bool accept(XMLElement* description);
00374
00382 bool hangup(bool reject = false, const char* msg = 0);
00383
00394 bool confirm(XMLElement* xml, XMPPError::Type error = XMPPError::NoError,
00395 const char* text = 0, XMPPError::ErrorType type = XMPPError::TypeModify);
00396
00404 bool sendDtmf(const char* dtmf, bool buttonUp = true);
00405
00411 bool sendDtmfMethod(const char* method);
00412
00418 bool denyDtmfMethod(XMLElement* element);
00419
00426 inline bool acceptTransport(JGTransport* transport = 0)
00427 { return sendTransport(transport,ActTransportAccept); }
00428
00435 inline bool sendTransport(JGTransport* transport)
00436 { return sendTransport(transport,ActTransport); }
00437
00444 inline bool sendMessage(const char* msg) {
00445 return sendStanza(JBMessage::createMessage(JBMessage::Chat,
00446 m_localJID,m_remoteJID,0,msg),false);
00447 }
00448
00449 protected:
00461 JGSession(JGEngine* engine, JBStream* stream,
00462 const String& callerJID, const String& calledJID,
00463 XMLElement* media, XMLElement* transport,
00464 bool sid, const char* msg = 0);
00465
00473 JGSession(JGEngine* engine, JBEvent* event, const String& id, bool sid);
00474
00478 virtual void destroyed();
00479
00485 void enqueue(JBEvent* event);
00486
00493 JGEvent* getEvent(u_int64_t time);
00494
00501 bool sendStanza(XMLElement* stanza, bool confirmation = true);
00502
00508 JGEvent* decodeJingle(JBEvent* jbev);
00509
00517 XMLElement* createJingle(Action action, XMLElement* element1 = 0, XMLElement* element2 = 0);
00518
00525 bool sendTransport(JGTransport* transport, Action act);
00526
00531 static const char* lookupState(int state)
00532 { return lookup(state,s_states); }
00533
00534 private:
00535 static TokenDict s_states[];
00536 static TokenDict s_actions[];
00537
00538 JGSession() {}
00539
00540 void eventTerminated(JGEvent* event);
00541
00542 void changeState(State newState);
00543
00544
00545 State m_state;
00546 TransportType m_transportType;
00547
00548 JGEngine* m_engine;
00549 JBStream* m_stream;
00550
00551 bool m_outgoing;
00552 String m_sid;
00553 JabberID m_localJID;
00554 JabberID m_remoteJID;
00555 String m_sidAttr;
00556
00557 ObjList m_events;
00558 JGEvent* m_lastEvent;
00559 void* m_private;
00560
00561 String m_localSid;
00562 u_int32_t m_stanzaId;
00563 ObjList m_sentStanza;
00564 };
00565
00570 class YJINGLE_API JGEvent
00571 {
00572 friend class JGSession;
00573 public:
00577 enum Type {
00578 Jingle,
00579
00580
00581
00582
00583
00584
00585
00586 Terminated,
00587
00588 Destroy,
00589 };
00590
00594 virtual ~JGEvent();
00595
00600 inline Type type() const
00601 { return m_type; }
00602
00607 inline JGSession* session() const
00608 { return m_session; }
00609
00614 inline XMLElement* element() const
00615 { return m_element; }
00616
00621 inline JGSession::Action action() const
00622 { return m_action; }
00623
00628 inline JGAudioList& audio()
00629 { return m_audio; }
00630
00635 inline ObjList& transport()
00636 { return m_transport; }
00637
00642 inline const String& id() const
00643 { return m_id; }
00644
00649 inline const String& reason() const
00650 { return m_reason; }
00651
00656 inline const String& text() const
00657 { return m_text; }
00658
00663 inline XMLElement* releaseXML() {
00664 XMLElement* tmp = m_element;
00665 m_element = 0;
00666 return tmp;
00667 }
00668
00673 inline bool final() const
00674 { return m_type == Terminated || m_type == Destroy; }
00675
00676 protected:
00685 inline JGEvent(Type type, JGSession* session, XMLElement* element = 0,
00686 const char* reason = 0, const char* text = 0)
00687 : m_type(type), m_session(0), m_element(element), m_action(JGSession::ActCount),
00688 m_reason(reason), m_text(text)
00689 { init(session); }
00690
00699 inline JGEvent(JGSession::Action act, JGSession* session, XMLElement* element,
00700 const char* reason = 0, const char* text = 0)
00701 : m_type(Jingle), m_session(0), m_element(element), m_action(act),
00702 m_reason(reason), m_text(text)
00703 { init(session); }
00704
00705 private:
00706 JGEvent() {}
00707 void init(JGSession* session);
00708
00709 Type m_type;
00710 JGSession* m_session;
00711 XMLElement* m_element;
00712
00713 JGSession::Action m_action;
00714 JGAudioList m_audio;
00715 ObjList m_transport;
00716 String m_id;
00717 String m_reason;
00718 String m_text;
00719 };
00720
00726 class YJINGLE_API JGEngine : public JBService, public JBThreadList
00727 {
00728 friend class JGSession;
00729 public:
00736 JGEngine(JBEngine* engine, const NamedList* params, int prio = 0);
00737
00741 virtual ~JGEngine();
00742
00747 inline u_int64_t stanzaTimeout() const
00748 { return m_stanzaTimeout; }
00749
00754 virtual void initialize(const NamedList& params);
00755
00762 JGEvent* getEvent(u_int64_t time);
00763
00774 JGSession* call(const String& callerName, const String& remoteJID,
00775 XMLElement* media, XMLElement* transport, const char* msg = 0);
00776
00781 void defProcessEvent(JGEvent* event);
00782
00788 virtual void processEvent(JGEvent* event);
00789
00790 protected:
00798 virtual bool accept(JBEvent* event, bool& processed, bool& insert);
00799
00800 private:
00801
00802 void createSessionId(String& id);
00803
00804 ObjList m_sessions;
00805 Mutex m_sessionIdMutex;
00806 u_int32_t m_sessionId;
00807 u_int64_t m_stanzaTimeout;
00808 bool m_useSidAttr;
00809 };
00810
00811
00816 class YJINGLE_API JGSentStanza : public String
00817 {
00818 public:
00824 JGSentStanza(const char* id, u_int64_t time)
00825 : String(id), m_time(time)
00826 {}
00827
00832 inline bool timeout(u_int64_t time) const
00833 { return time > m_time; }
00834
00835 private:
00836 u_int64_t m_time;
00837 };
00838
00839 }
00840
00841 #endif
00842
00843