OPAL  Version 3.10.10
msrp.h
Go to the documentation of this file.
1 /*
2  * msrp.h
3  *
4  * Support for RFC 4975 Message Session Relay Protocol (MSRP)
5  *
6  * Open Phone Abstraction Library (OPAL)
7  *
8  * Copyright (c) 2008 Post Increment
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Open Phone Abstraction Library.
21  *
22  * The Initial Developer of the Original Code is Post Increment
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 27149 $
27  * $Author: rjongbloed $
28  * $Date: 2012-03-07 18:32:36 -0600 (Wed, 07 Mar 2012) $
29  */
30 
31 #ifndef OPAL_IM_MSRP_H
32 #define OPAL_IM_MSRP_H
33 
34 #include <ptlib.h>
35 #include <opal/buildopts.h>
36 #include <opal/rtpconn.h>
37 #include <opal/manager.h>
38 #include <opal/mediastrm.h>
39 #include <opal/mediatype.h>
40 #include <im/im.h>
41 #include <ptclib/inetprot.h>
42 #include <ptclib/guid.h>
43 #include <ptclib/mime.h>
44 
45 #if OPAL_SIP
46 #include <sip/sdp.h>
47 #endif
48 
49 #if OPAL_HAS_MSRP
50 
52 {
53  public:
55  virtual OpalMediaSession * CreateMediaSession(OpalConnection & conn, unsigned sessionID) const;
56 
57 #if OPAL_SIP
59 #endif
60 };
61 
62 
64 //
65 // Ancestor for all MSRP encoding types
66 //
67 
69 };
70 
71 
73 //
74 // Ancestor for MSRP protocol
75 //
76 
77 class MSRPProtocol : public PInternetProtocol
78 {
79  public:
80  enum Commands {
81  SEND,
84  };
85 
86  static const unsigned MaximumMessageLength = 1024;
87 
88  class Message
89  {
90  public:
91  struct Chunk {
92  Chunk(const PString & id, unsigned from, unsigned len)
93  : m_chunkId(id), m_rangeFrom(from + 1), m_rangeTo(from + len) { }
94 
95  PString m_chunkId;
96  unsigned m_rangeFrom;
97  unsigned m_rangeTo;
98  };
99  typedef std::vector<Chunk> ChunkList;
101 
102  PString m_id;
103  PURL m_fromURL;
104  PURL m_toURL;
105  PString m_contentType;
106  unsigned m_length;
107  };
108 
109  MSRPProtocol();
110 
111  bool SendSEND(
112  const PURL & from,
113  const PURL & to,
114  const PString & text,
115  const PString & contentType,
116  PString & messageId
117  );
118 
119  bool SendChunk(
120  const PString & transactionId,
121  const PString toUrl,
122  const PString fromUrl,
123  const PMIMEInfo & mime,
124  const PString & body
125  );
126 
127  bool SendResponse(const PString & chunkId,
128  unsigned response,
129  const PString & text,
130  const PString & toUrl,
131  const PString & fromUrl);
132 
133  bool SendREPORT(const PString & chunkId,
134  const PString & toUrl,
135  const PString & fromUrl,
136  const PMIMEInfo & mime);
137 
138  bool ReadMessage(
139  int & command,
140  PString & chunkId,
141  PMIMEInfo & mime,
142  PString & body
143  );
144 
145  //typedef std::map<std::string, Message> MessageMap;
146  //MessageMap m_messageMap;
147  PMutex m_mutex;
148 };
149 
151 //
152 //
153 //
154 
155 class OpalMSRPManager : public PObject
156 {
157  public:
158  enum {
159  DefaultPort = 2855
160  };
161 
162  //
163  // Create an MSRP manager. This is a singleton class
164  //
165  OpalMSRPManager(OpalManager & opal, WORD port = DefaultPort);
167 
168  //
169  // Get the local port for the MSRP manager
170  //
171  bool GetLocalPort(WORD & port);
172 
173  //
174  // Information about a connection to another MSRP manager
175  //
176  class Connection : public PSafeObject {
177  public:
178  Connection(OpalMSRPManager & manager, const std::string & key, MSRPProtocol * protocol = NULL);
179  ~Connection();
180 
181  //
182  // Start handler thread for a connection
183  //
184  void StartHandler();
185 
186  //
187  // Handler thread for a connection
188  //
189  void HandlerThread();
190 
192  std::string m_key;
194  bool m_running;
195  PThread * m_handlerThread;
197  PAtomicInteger m_refCount;
198  };
199 
200  //
201  // Get the connection to use for communicating with a remote URL
202  //
203  PSafePtr<Connection> OpenConnection(
204  const PURL & localURL,
205  const PURL & remoteURL
206  );
207 
208  //
209  // close a connection
210  //
211  bool CloseConnection(
212  PSafePtr<OpalMSRPManager::Connection> & connection
213  );
214 
215  //
216  // Create a new MSRP session ID
217  //
218  std::string CreateSessionID();
219 
220  //
221  // return session ID as a path
222  //
223  PURL SessionIDToURL(const OpalTransportAddress & addr, const std::string & id);
224 
225  //
226  // Main listening thread for new connections
227  //
228  void ListenerThread();
229 
230  struct IncomingMSRP {
232  PString m_chunkId;
233  PMIMEInfo m_mime;
234  PString m_body;
235  PSafePtr<Connection> m_connection;
236  };
237 
238  //
239  // dispatch an incoming MSRP message to the correct callback
240  //
241  void DispatchMessage(
242  IncomingMSRP & incomingMsg
243  );
244 
245  typedef PNotifierTemplate<IncomingMSRP &> CallBack;
246 
247  void SetNotifier(
248  const PURL & localUrl,
249  const PURL & remoteURL,
250  const CallBack & notifier
251  );
252 
253  void RemoveNotifier(
254  const PURL & localUrl,
255  const PURL & remoteURL
256  );
257 
259 
260  protected:
263  PMutex mutex;
264  PAtomicInteger lastID;
265  PTCPSocket m_listenerSocket;
266  PThread * m_listenerThread;
267 
269  typedef std::map<std::string, PSafePtr<Connection> > ConnectionInfoMapType;
271 
272  typedef std::map<std::string, CallBack> CallBackMap;
275 
276  private:
277  static OpalMSRPManager * msrp;
278 };
279 
281 
285 {
287  public:
291 
292  bool Open(const PURL & remoteParty);
293 
294  virtual void Close();
295 
296  virtual PObject * Clone() const { return new OpalMSRPMediaSession(*this); }
297 
298  virtual bool IsActive() const { return true; }
299 
300  virtual bool IsRTP() const { return false; }
301 
302  virtual bool HasFailed() const { return false; }
303 
305 
306  PURL GetLocalURL() const { return m_localUrl; }
307  PURL GetRemoteURL() const { return m_remoteUrl; }
308  void SetRemoteURL(const PURL & url) { m_remoteUrl = url; }
309 
310  virtual void SetRemoteMediaAddress(const OpalTransportAddress &, const OpalMediaFormatList & );
311 
312  virtual bool WritePacket(
313  RTP_DataFrame & frame
314  );
315 
316  PBoolean ReadData(
317  BYTE * data,
318  PINDEX length,
319  PINDEX & read
320  );
321 
322 #if OPAL_SIP
324  const OpalTransportAddress & localAddress
325  );
326 #endif
327 
329  const OpalMediaFormat & mediaFormat,
330  unsigned sessionID,
331  PBoolean isSource
332  );
333 
335 
336  bool OpenMSRP(const PURL & remoteUrl);
337  void CloseMSRP();
338 
339  void SetConnection(PSafePtr<OpalMSRPManager::Connection> & conn);
340 
343  std::string m_localMSRPSessionId;
346  PSafePtr<OpalMSRPManager::Connection> m_connectionPtr;
348 };
349 
351 
353 {
354  public:
356  OpalConnection & conn,
357  const OpalMediaFormat & mediaFormat,
358  unsigned sessionID,
359  bool isSource,
360  OpalMSRPMediaSession & msrpSession
361 
362  );
363 
365 
366  virtual PBoolean RequiresPatchThread() const { return !isSource; }
367 
371  virtual PBoolean ReadPacket(
372  RTP_DataFrame & frame
373  );
374 
378  virtual PBoolean WritePacket(
379  RTP_DataFrame & frame
380  );
381 
382  virtual bool Open();
383 
384  PURL GetRemoteURL() const { return m_msrpSession.GetRemoteURL(); }
385  void SetRemoteURL(const PURL & url) { m_msrpSession.SetRemoteURL(url); }
386 
388 
389 
391  protected:
393  PString m_remoteParty;
395 };
396 
397 // schemeName,user, passwd, host, defUser,defhost, query, params, frags, path, rel, port
398 
399 #endif // OPAL_HAS_MSRP
400 
401 #endif // OPAL_IM_MSRP_H
402 
bool m_originating
Definition: msrp.h:196
virtual void Close()
PURL m_localUrl
Definition: msrp.h:344
bool SendREPORT(const PString &chunkId, const PString &toUrl, const PString &fromUrl, const PMIMEInfo &mime)
Definition: manager.h:74
PDECLARE_NOTIFIER2(OpalMSRPManager, OpalMSRPMediaStream, OnReceiveMSRP, OpalMSRPManager::IncomingMSRP &)
std::map< std::string, CallBack > CallBackMap
Definition: msrp.h:272
OpalTransportAddress m_remoteAddress
Definition: msrp.h:347
virtual SDPMediaDescription * CreateSDPMediaDescription(const OpalTransportAddress &localAddress)
OpalMSRPManager & GetManager()
Definition: msrp.h:334
PString m_remoteParty
Definition: msrp.h:393
bool isSource
Definition: mediastrm.h:421
PMutex m_connectionInfoMapAddMutex
Definition: msrp.h:268
virtual OpalMediaSession * CreateMediaSession(OpalConnection &conn, unsigned sessionID) const
Connection(OpalMSRPManager &manager, const std::string &key, MSRPProtocol *protocol=NULL)
virtual bool IsActive() const
Definition: msrp.h:298
PAtomicInteger m_refCount
Definition: msrp.h:197
Commands
Definition: msrp.h:80
Definition: rtpconn.h:72
virtual OpalTransportAddress GetLocalMediaAddress() const
OpalMSRPMediaSession & m_msrpSession
Definition: msrp.h:392
Definition: msrp.h:155
std::string m_localMSRPSessionId
Definition: msrp.h:343
PNotifierTemplate< IncomingMSRP & > CallBack
Definition: msrp.h:245
PURL GetRemoteURL() const
Definition: msrp.h:307
void SetConnection(PSafePtr< OpalMSRPManager::Connection > &conn)
Definition: msrp.h:68
OpalMSRPMediaStream(OpalConnection &conn, const OpalMediaFormat &mediaFormat, unsigned sessionID, bool isSource, OpalMSRPMediaSession &msrpSession)
bool ReadMessage(int &command, PString &chunkId, PMIMEInfo &mime, PString &body)
virtual PBoolean WritePacket(RTP_DataFrame &frame)
Definition: msrp.h:83
bool SendResponse(const PString &chunkId, unsigned response, const PString &text, const PString &toUrl, const PString &fromUrl)
bool m_isOriginating
Definition: msrp.h:342
PURL m_remoteUrl
Definition: msrp.h:345
bool SendSEND(const PURL &from, const PURL &to, const PString &text, const PString &contentType, PString &messageId)
SDPMediaDescription * CreateSDPMediaDescription(const OpalTransportAddress &localAddress)
PURL GetRemoteURL() const
Definition: msrp.h:384
PSafePtr< Connection > m_connection
Definition: msrp.h:235
PString m_id
Definition: msrp.h:102
virtual OpalMediaStream * CreateMediaStream(const OpalMediaFormat &mediaFormat, unsigned sessionID, PBoolean isSource)
bool Open(const PURL &remoteParty)
OpalMSRPManager(OpalManager &opal, WORD port=DefaultPort)
Chunk(const PString &id, unsigned from, unsigned len)
Definition: msrp.h:92
Definition: msrp.h:77
PAtomicInteger lastID
Definition: msrp.h:264
OpalMSRPMediaSession(OpalConnection &connection, unsigned sessionId)
virtual PBoolean ReadPacket(RTP_DataFrame &frame)
PMutex m_callBacksMutex
Definition: msrp.h:274
PString m_body
Definition: msrp.h:234
PMutex mutex
Definition: msrp.h:263
Definition: msrp.h:284
PURL SessionIDToURL(const OpalTransportAddress &addr, const std::string &id)
Definition: msrp.h:82
RFC4103Context m_rfc4103Context
Definition: msrp.h:394
ChunkList m_chunks
Definition: msrp.h:100
Definition: mediafmt.h:72
PMutex m_mutex
Definition: msrp.h:147
std::vector< Chunk > ChunkList
Definition: msrp.h:99
PString m_contentType
Definition: msrp.h:105
OpalConnection & connection
Definition: rtpconn.h:105
unsigned sessionID
Definition: mediastrm.h:417
unsigned m_rangeFrom
Definition: msrp.h:96
PURL m_toURL
Definition: msrp.h:104
Definition: rtp.h:71
void SetRemoteURL(const PURL &url)
Definition: msrp.h:385
PURL GetLocalURL() const
Definition: msrp.h:306
Definition: mediafmt.h:724
virtual bool HasFailed() const
Definition: msrp.h:302
Definition: msrp.h:88
OpalMSRPManager & m_manager
Definition: msrp.h:341
void RemoveNotifier(const PURL &localUrl, const PURL &remoteURL)
Definition: sdp.h:119
Definition: im.h:399
bool GetLocalPort(WORD &port)
PThread * m_listenerThread
Definition: msrp.h:266
OpalManager & GetOpalManager()
Definition: msrp.h:258
CallBackMap m_callBacks
Definition: msrp.h:273
Definition: msrp.h:230
unsigned m_rangeTo
Definition: msrp.h:97
int m_command
Definition: msrp.h:231
Definition: msrp.h:91
void DispatchMessage(IncomingMSRP &incomingMsg)
PTCPSocket m_listenerSocket
Definition: msrp.h:265
Definition: msrp.h:159
unsigned m_length
Definition: msrp.h:106
PSafePtr< OpalMSRPManager::Connection > m_connectionPtr
Definition: msrp.h:346
void SetNotifier(const PURL &localUrl, const PURL &remoteURL, const CallBack &notifier)
std::map< std::string, PSafePtr< Connection > > ConnectionInfoMapType
Definition: msrp.h:269
bool SendChunk(const PString &transactionId, const PString toUrl, const PString fromUrl, const PMIMEInfo &mime, const PString &body)
PSafePtr< Connection > OpenConnection(const PURL &localURL, const PURL &remoteURL)
std::string m_key
Definition: msrp.h:192
Definition: msrp.h:176
PMIMEInfo m_mime
Definition: msrp.h:233
static const unsigned MaximumMessageLength
Definition: msrp.h:86
unsigned sessionId
Definition: rtpconn.h:107
std::string CreateSessionID()
PBoolean ReadData(BYTE *data, PINDEX length, PINDEX &read)
void ListenerThread()
Definition: msrp.h:51
PString m_chunkId
Definition: msrp.h:232
virtual bool Open()
Definition: mediastrm.h:111
OpalMediaFormat mediaFormat
Definition: mediastrm.h:419
Definition: im.h:74
WORD m_listenerPort
Definition: msrp.h:262
Definition: msrp.h:81
Definition: rfc4103.h:44
virtual bool IsRTP() const
Definition: msrp.h:300
Definition: connection.h:353
Definition: msrp.h:352
OpalMSRPManager & m_manager
Definition: msrp.h:191
virtual void SetRemoteMediaAddress(const OpalTransportAddress &, const OpalMediaFormatList &)
virtual bool WritePacket(RTP_DataFrame &frame)
PString m_chunkId
Definition: msrp.h:95
bool OpenMSRP(const PURL &remoteUrl)
Definition: transports.h:149
MSRPProtocol * m_protocol
Definition: msrp.h:193
virtual PObject * Clone() const
Definition: msrp.h:296
PURL m_fromURL
Definition: msrp.h:103
OpalManager & opalManager
Definition: msrp.h:261
PThread * m_handlerThread
Definition: msrp.h:195
bool m_running
Definition: msrp.h:194
ConnectionInfoMapType m_connectionInfoMap
Definition: msrp.h:270
void SetRemoteURL(const PURL &url)
Definition: msrp.h:308
bool CloseConnection(PSafePtr< OpalMSRPManager::Connection > &connection)
virtual PBoolean RequiresPatchThread() const
Definition: msrp.h:366