OPAL  Version 3.14.3
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: 31411 $
27  * $Author: rjongbloed $
28  * $Date: 2014-02-07 18:56:03 +1100 (Fri, 07 Feb 2014) $
29  */
30 
31 #ifndef OPAL_IM_MSRP_H
32 #define OPAL_IM_MSRP_H
33 
34 #include <ptlib.h>
35 #include <opal_config.h>
36 
37 #if OPAL_HAS_MSRP
38 
39 #include <opal/mediastrm.h>
40 #include <opal/mediasession.h>
41 #include <ptclib/url.h>
42 #include <ptclib/inetprot.h>
43 
44 
45 class OpalManager;
46 
47 
49 //
50 // Ancestor for all MSRP encoding types
51 //
52 
54 };
55 
56 
58 //
59 // Ancestor for MSRP protocol
60 //
61 
62 class MSRPProtocol : public PInternetProtocol
63 {
64  public:
65  enum Commands {
66  SEND,
69  };
70 
71  static const unsigned MaximumMessageLength = 1024;
72 
73  class Message
74  {
75  public:
76  struct Chunk {
77  Chunk(const PString & id, unsigned from, unsigned len)
78  : m_chunkId(id), m_rangeFrom(from + 1), m_rangeTo(from + len) { }
79 
80  PString m_chunkId;
81  unsigned m_rangeFrom;
82  unsigned m_rangeTo;
83  };
84  typedef std::vector<Chunk> ChunkList;
86 
87  PString m_id;
88  PURL m_fromURL;
89  PURL m_toURL;
90  PString m_contentType;
91  unsigned m_length;
92  };
93 
94  MSRPProtocol();
95 
96  bool SendSEND(
97  const PURL & from,
98  const PURL & to,
99  const PString & text,
100  const PString & contentType,
101  PString & messageId
102  );
103 
104  bool SendChunk(
105  const PString & transactionId,
106  const PString toUrl,
107  const PString fromUrl,
108  const PMIMEInfo & mime,
109  const PString & body
110  );
111 
112  bool SendResponse(const PString & chunkId,
113  unsigned response,
114  const PString & text,
115  const PString & toUrl,
116  const PString & fromUrl);
117 
118  bool SendREPORT(const PString & chunkId,
119  const PString & toUrl,
120  const PString & fromUrl,
121  const PMIMEInfo & mime);
122 
123  bool ReadMessage(
124  int & command,
125  PString & chunkId,
126  PMIMEInfo & mime,
127  PString & body
128  );
129 
130  //typedef std::map<std::string, Message> MessageMap;
131  //MessageMap m_messageMap;
132  PMutex m_mutex;
133 };
134 
136 //
137 //
138 //
139 
140 class OpalMSRPManager : public PObject
141 {
142  public:
143  enum {
144  DefaultPort = 2855
145  };
146 
147  //
148  // Create an MSRP manager. This is a singleton class
149  //
150  OpalMSRPManager(OpalManager & opal, WORD port = DefaultPort);
152 
153  //
154  // Get the local port for the MSRP manager
155  //
156  bool GetLocalPort(WORD & port);
157 
158  //
159  // Information about a connection to another MSRP manager
160  //
161  class Connection : public PSafeObject {
162  public:
163  Connection(OpalMSRPManager & manager, const std::string & key, MSRPProtocol * protocol = NULL);
164  ~Connection();
165 
166  //
167  // Start handler thread for a connection
168  //
169  void StartHandler();
170 
171  //
172  // Handler thread for a connection
173  //
174  void HandlerThread();
175 
177  std::string m_key;
179  bool m_running;
180  PThread * m_handlerThread;
182  PAtomicInteger m_refCount;
183  };
184 
185  //
186  // Get the connection to use for communicating with a remote URL
187  //
188  PSafePtr<Connection> OpenConnection(
189  const PURL & localURL,
190  const PURL & remoteURL
191  );
192 
193  //
194  // close a connection
195  //
196  bool CloseConnection(
197  PSafePtr<OpalMSRPManager::Connection> & connection
198  );
199 
200  //
201  // Create a new MSRP session ID
202  //
203  std::string CreateSessionID();
204 
205  //
206  // return session ID as a path
207  //
208  PURL SessionIDToURL(const OpalTransportAddress & addr, const std::string & id);
209 
210  //
211  // Main listening thread for new connections
212  //
213  void ListenerThread();
214 
215  struct IncomingMSRP {
217  PString m_chunkId;
218  PMIMEInfo m_mime;
219  PString m_body;
220  PSafePtr<Connection> m_connection;
221  };
222 
223  //
224  // dispatch an incoming MSRP message to the correct callback
225  //
226  void DispatchMessage(
227  IncomingMSRP & incomingMsg
228  );
229 
230  typedef PNotifierTemplate<IncomingMSRP &> CallBack;
231 
232  void SetNotifier(
233  const PURL & localUrl,
234  const PURL & remoteURL,
235  const CallBack & notifier
236  );
237 
238  void RemoveNotifier(
239  const PURL & localUrl,
240  const PURL & remoteURL
241  );
242 
244 
245  protected:
248  PMutex mutex;
249  PAtomicInteger lastID;
250  PTCPSocket m_listenerSocket;
251  PThread * m_listenerThread;
252 
254  typedef std::map<PString, PSafePtr<Connection> > ConnectionInfoMapType;
256 
257  typedef std::map<PString, CallBack> CallBackMap;
260 
261  private:
262  static OpalMSRPManager * msrp;
263 };
264 
266 
270 {
272  public:
273  static const PCaselessString & TCP_MSRP();
274 
275  OpalMSRPMediaSession(const Init & init);
277 
278  virtual PObject * Clone() const { return new OpalMSRPMediaSession(*this); }
279 
280  virtual const PCaselessString & GetSessionType() const { return TCP_MSRP(); }
281  virtual bool Open(const PString & localInterface, const OpalTransportAddress & remoteAddress, bool isMediaAddress);
282  virtual bool Close();
283  virtual OpalTransportAddress GetLocalAddress(bool isMediaAddress = true) const;
284  virtual OpalTransportAddress GetRemoteAddress(bool isMediaAddress = true) const;
285  virtual bool SetRemoteAddress(const OpalTransportAddress & remoteAddress, bool isMediaAddress = true);
286 #if OPAL_SDP
287  virtual SDPMediaDescription * CreateSDPMediaDescription();
288 #endif
289 
290  PURL GetLocalURL() const { return m_localUrl; }
291  PURL GetRemoteURL() const { return m_remoteUrl; }
292  void SetRemoteURL(const PURL & url) { m_remoteUrl = url; }
293 
294  virtual bool WritePacket(
295  RTP_DataFrame & frame
296  );
297 
298  PBoolean ReadData(
299  BYTE * data,
300  PINDEX length,
301  PINDEX & read
302  );
303 
305  const OpalMediaFormat & mediaFormat,
306  unsigned sessionID,
307  PBoolean isSource
308  );
309 
311 
312  bool OpenMSRP(const PURL & remoteUrl);
313  void CloseMSRP();
314 
315  void SetConnection(PSafePtr<OpalMSRPManager::Connection> & conn);
316 
319  std::string m_localMSRPSessionId;
322  PSafePtr<OpalMSRPManager::Connection> m_connectionPtr;
324 
325  private:
327  : OpalMediaSession(Init(other.m_connection, other.m_sessionId, other.m_mediaType, false)), m_manager(other.m_manager) { }
328  void operator=(const OpalMSRPMediaSession &) { }
329 };
330 
331 
333 
335 {
336  public:
338  OpalConnection & conn,
339  const OpalMediaFormat & mediaFormat,
340  unsigned sessionID,
341  bool isSource,
342  OpalMSRPMediaSession & msrpSession
343 
344  );
345 
347 
348  virtual PBoolean IsSynchronous() const { return false; }
349  virtual PBoolean RequiresPatchThread() const { return IsSink(); }
350 
354  virtual PBoolean ReadPacket(
355  RTP_DataFrame & frame
356  );
357 
361  virtual PBoolean WritePacket(
362  RTP_DataFrame & frame
363  );
364 
365  virtual bool Open();
366 
367  PURL GetRemoteURL() const { return m_msrpSession.GetRemoteURL(); }
368  void SetRemoteURL(const PURL & url) { m_msrpSession.SetRemoteURL(url); }
369 
372 
373  protected:
374  virtual void InternalClose() { }
375 
377  PString m_remoteParty;
378 };
379 
380 
381 #endif // OPAL_HAS_MSRP
382 
383 #endif // OPAL_IM_MSRP_H
384