Project

General

Profile

SipMSRPApi » History » Version 31

Adrian Georgescu, 11/12/2009 11:54 PM

1 1 Adrian Georgescu
= MSRP API =
2
3
[[TOC(WikiStart, Sip*, depth=3)]]
4
5 31 Adrian Georgescu
Message Session Relay Protocol (MSRP) is a protocol for transmitting a series of related Instant Messages in the context of a session. Message sessions are treated like any other media stream when set up via a rendezvous or session creation protocol such as the Session Initiation Protocol (SIP). 
6 1 Adrian Georgescu
7 25 Adrian Georgescu
 * MSRP sessions are defined in [http://tools.ietf.org/html/rfc4975 RFC 4975]
8
 * MSRP relay extension used for NAT traversal of instant messaging and file transfer sessions is defined in [http://tools.ietf.org/html/rfc4976 RFC 4976]
9 26 Adrian Georgescu
 * Common Presence and Instant Messaging (CPIM): Message Format is defined in [http://tools.ietf.org/html/rfc3862 RFC 3862]
10 1 Adrian Georgescu
11 31 Adrian Georgescu
The MSRP protocol stack is implemented by [http://devel.ag-projects.com/cgi-bin/darcsweb.cgi?r=python-msrplib;a=summary msrplib] Python package. On top of it, {{{sipsimple}}} provides higher level classes integrated into middleware notification and configuration systems to achieve Instant Messaging, File Transfer and Desktop Sharing:
12 1 Adrian Georgescu
13 31 Adrian Georgescu
 * {{{sipsimple.streams.msrp.MSRPStreamBase}}}
14
 * {{{sipsimple.streams.msrp.ChatStream}}}
15
 * {{{sipsimple.streams.msrp.FileSelector}}}
16
 * {{{sipsimple.streams.msrp.FileTransferStream}}}
17
 * {{{sipsimple.streams.msrp.VNCConnectionError}}}
18
 * {{{sipsimple.streams.msrp.DesktopSharingHandlerBase}}}
19
 * {{{sipsimple.streams.msrp.InternalVNCViewerHandler}}}
20
 * {{{sipsimple.streams.msrp.InternalVNCViewerHandler}}}
21
 * {{{sipsimple.streams.msrp.ExternalVNCViewerHandler}}}
22
 * {{{sipsimple.streams.msrp.ExternalVNCServerHandler}}}
23
 * {{{sipsimple.streams.msrp.DesktopSharingStream}}}
24
 * {{{sipsimple.streams.msrp.NotificationProxyLogger}}}
25 1 Adrian Georgescu
26 21 Adrian Georgescu
These classes are used internally by [wiki:SipMiddlewareApi#Session Session], which provides the necessary methods to access their features. The notifications posted by these classes are also handled internally by [wiki:SipMiddlewareApi#Session Session]. The notifications that are relevant to the user are then reposted by the Session instance. Refer to [wiki:SipMiddlewareApi#Session Session documentation] for details on the Session API. 
27 10 Adrian Georgescu
28 31 Adrian Georgescu
== ChatStream ==
29 1 Adrian Georgescu
30 31 Adrian Georgescu
{{{sipsimple.streams.msrp.ChatStream}}} implements Instant Messaging (IM) over MSRP for the [wiki:SipMiddlewareApi middleware]. This class performs the following functions:
31 1 Adrian Georgescu
32 5 Oliver Bril
 * automatically wraps outgoing messages with Message/CPIM if that's necessary according to accept-types
33 19 Adrian Georgescu
 * unwraps incoming Message/CPIM messages; for each incoming message, {{{MSRPChatGotMessage}}} is posted
34 5 Oliver Bril
 * plays notification sounds on received/sent message
35
36 17 Oliver Bril
=== Methods ===
37 5 Oliver Bril
38 30 Adrian Georgescu
 '''!__init!__'''(''self'', ''account'', ''direction'')::
39 27 Adrian Georgescu
 Initializes the MSRPChat instance.
40 5 Oliver Bril
41
 '''initialize'''(''self'')::
42 27 Adrian Georgescu
 Initializes the MSRP connection; connects to the relay if necessary. When done, fires MSRPChatDidInitialize (with 'sdpmedia' attribute, containing the appropriate 'SDPMedia' instance)
43 5 Oliver Bril
44
 '''start'''(''self'', ''remote_media'')::
45 27 Adrian Georgescu
 Completes the MSRP connection establishment; this includes binding the MSRP session. [[BR]]
46
 When done, fires MSRPChatDidStart notification. At this point each incoming message is posted as a {{{MSRPChatGotMessage}}} notification
47 5 Oliver Bril
48
 '''end'''(''self'')::
49 27 Adrian Georgescu
 Closes the MSRP connection or cleans up after initialize(), whatever is necessary. [[BR]]
50
 Before doing anything posts {{{MSRPChatWillEnd}}} notification.
51
 When done, posts {{{MSRPChatDidEnd}}} notification. If there was an error, posts {{{MSRPChatDidFail}}} notification. 
52
 {{{MSRPChatDidEnd}}} notification will be posted anyway.
53 5 Oliver Bril
54 6 Oliver Bril
 '''send_message'''(''self'', ''content'', ''content_type''={{{'text/plain'}}}, ''to_uri''={{{None}}}, ''dt''={{{None}}})::
55 27 Adrian Georgescu
 Sends IM message. Prefer Message/CPIM wrapper if it is supported. If called before the connection was established, the messages will be
56 6 Oliver Bril
 queued until MSRPChatDidStart notification.
57
58 27 Adrian Georgescu
 Returns the generated MSRP chunk (MSRPData instance); to get Message-ID use its 'message_id' attribute.
59 6 Oliver Bril
60
 ''content'' str:[[BR]]
61
 content of the message
62
63
 ''to_uri'' SIPURI:[[BR]]
64
 "To" header of CPIM wrapper; use to override the default supplied to {{{__init__}}}.
65
 May only differ from the one supplied in __init__ if the remote party supports private messages. If it does not, {{{MSRPChatError}}} will be raised;
66
67
 ''content_type'' str:[[BR]]
68
 Content-Type of wrapped message if Message/CPIM is used (Content-Type of MSRP message is always Message/CPIM in that case);
69
 otherwise, Content-Type of MSRP message.
70
71
 These MSRP headers are used to enable end-to-end success reports and to disable hop-to-hop successful responses:
72
{{{
73
Failure-Report: partial
74
Success-Report: yes
75
}}}
76
77 1 Adrian Georgescu
=== Notifications ===
78 21 Adrian Georgescu
79
To communicate with the middleware, MSRPChat class uses the notification system provided by the [http://pypi.python.org/pypi/python-application python-application] package.
80 1 Adrian Georgescu
81 17 Oliver Bril
 '''MSRPChatDidInitialize'''::
82 18 Adrian Georgescu
 Sent when the {{{MSRPChat}}} instance is initialized, it includes the MSRP relay reservation when requested.
83 17 Oliver Bril
 '''MSRPChatDidStart'''::
84
 Sent when the {{{MSRPChat}}} instance is started.
85
 '''MSRPChatWillEnd'''::
86
 Sent when the {{{MSRPChat}}} is entering cleanup procedure initiated by calling end().
87
 '''MSRPChatDidEnd'''::
88
 Sent when the {{{MSRPChat}}} has finished the cleanup procedure initiated by end().
89
 '''MSRPChatDidFail'''::
90
 Sent whenever the {{{MSRPChat}}} instance fails.
91
  [[BR]]''context'':[[BR]]
92
  A text string identifying the context of the failure ('initialize', 'sdp_negotiation', 'start', 'reading').
93
  [[BR]]''reason'':[[BR]]
94
  A text string describing the reason of failure.
95
  [[BR]]''failure'':[[BR]]
96
  A Failure instance providing detailed traceback information.
97
 '''MSRPChatGotMessage'''::
98
 Sent whenever a new incoming message is received,
99
  [[BR]]''content'':[[BR]]
100
  The string that the remote user has typed.
101
  [[BR]]''content_type'':[[BR]]
102
  Content-Type of the user message.
103
  [[BR]]''cpim_headers'':[[BR]]
104
  A dictionary of CPIM headers. (Empty dictionary if no CPIM wrapper was used).
105
  [[BR]]''message'':[[BR]]
106
  A {{{msrplib.protocol.MSRPData}}} instance providing all the MSRP information about the chunk.
107
 '''MSRPChatDidDeliverMessage'''::
108
 Sent when a successful report is received.
109
  [[BR]]''message_id'':[[BR]]
110
  Text identifier of the message.
111
  [[BR]]''code'':[[BR]]
112
  Integer result code.
113
  [[BR]]''reason'':[[BR]]
114
  Text comment.
115
  [[BR]]''message'':[[BR]]
116
  A {{{msrplib.protocol.MSRPData}}} instance providing all the MSRP information about the report.
117
 '''MSRPChatDidNotDeliverMessage'''::
118
 Sent when a failure report of failure transaction response is received.
119
  [[BR]]''message_id'':[[BR]]
120 1 Adrian Georgescu
  Text identifier of the message.
121 17 Oliver Bril
  [[BR]]''code'':[[BR]]
122
  Integer result code.
123
  [[BR]]''reason'':[[BR]]
124 1 Adrian Georgescu
  Text comment.
125 17 Oliver Bril
  [[BR]]''message'':[[BR]]
126
  A {{{msrplib.protocol.MSRPData}}} instance providing all the MSRP information about the report.
127 5 Oliver Bril
128 31 Adrian Georgescu
== FileTransferStream ==
129 1 Adrian Georgescu
130 22 Adrian Georgescu
=== Methods ===
131
=== Notifications ===
132
133 31 Adrian Georgescu
== DesktopSharingStream ==
134 22 Adrian Georgescu
135
=== Methods ===
136
=== Notifications ===
137 1 Adrian Georgescu
138 12 Adrian Georgescu
----------------------
139
140 24 Adrian Georgescu
== MSRP library ==
141 4 Oliver Bril
142 1 Adrian Georgescu
{{{msrplib}}} is based upon [http://twistedmatrix.com twisted] and [http://devel.ag-projects.com/~denis/eventlet/ eventlet] and provides a set of
143 27 Adrian Georgescu
classes for establishing and managing MSRP connections.
144 1 Adrian Georgescu
145 28 Adrian Georgescu
The library consists of the following modules:
146 1 Adrian Georgescu
147
 '''msrplib.transport'''::
148 27 Adrian Georgescu
 Defines {{{MSRPTransport}}} class, which provides low level control over MSRP connections.
149 2 Redmine Admin
150 1 Adrian Georgescu
 '''msrplib.connect'''::
151
 Defines means to establish a connection, bind it, and provide an initialized {{{MSRPTransport}}} instance.
152
153
 '''msrplib.session'''::
154
 Defines {{{MSRPSession}}} class, which provides high level control over a MSRP connection.
155
156
 '''msrplib.protocol'''::
157 27 Adrian Georgescu
 Provides representation and parsing of MSRP entities - chunks and MSRP URIs.
158 1 Adrian Georgescu
159
 '''msrplib.trafficlog'''::
160
 Defines {{{Logger}}} class that is used through out the library to log the connection state.
161
  
162
=== Usage ===
163 4 Oliver Bril
164 1 Adrian Georgescu
==== Establish a connection ====
165 4 Oliver Bril
166 3 Oliver Bril
{{{msrplib.connect}}} provides a number of classes to establish a connection, so the first
167 1 Adrian Georgescu
thing to do is to select which one applies to your situation:
168
169
    1. Calling endpoint, not using a relay ({{{ConnectorDirect}}})
170
    2. Answering endpoint, not using a relay ({{{AcceptorDirect}}})
171
    3. Calling endpoint, using a relay ({{{ConnectorRelay}}})
172
    4. Answering endpoint, using a relay ({{{AcceptorRelay}}})
173
174 27 Adrian Georgescu
The answering endpoint may skip using the relay if sure that it's accessible directly, e.g is not behind a NAT. To be sure it works in any network topology a called end-point should always use a relay.
175 1 Adrian Georgescu
176 27 Adrian Georgescu
The calling endpoint does not need a relay as the protocol mandates that it is establishing an outbound connection which always work from behind a NAT.
177
178 1 Adrian Georgescu
Once you have an instance of the right class (use the convenience functions
179
{{{get_connector()}}} and {{{get_acceptor()}}} to get one), the procedure to establish the
180
connection is the same:
181
182
{{{
183
full_local_path = connector.prepare()
184
try:
185
    ... put full_local_path in SDP 'a:path' attribute
186
    ... get full_remote_path from remote's 'a:path: attribute
187
    ... (the order of the above steps is reversed if you're the
188
    ... answering party, but that does not affect connector's usage)
189
    msrptransport = connector.complete(full_remote_path)
190
finally:
191
    connector.cleanup()
192
}}}
193
194 27 Adrian Georgescu
To customize connection's parameters, creates a new {{{protocol.URI}}} object and passes
195 1 Adrian Georgescu
it to prepare() function, e.g.
196
{{{
197
local_uri = protocol.URI(use_tls=False, port=5000)
198
connector.prepare(local_uri)
199
}}}
200
201
{{{prepare()}}} may update {{{local_uri}}} in place with the actual connection parameters
202
used (e.g. if you specified port=0). 'port' attribute of {{{local_uri}}} is currently
203
only respected by {{{AcceptorDirect}}}.
204
205
Note that, acceptors and connectors are one-use only. Which means, that {{{AcceptorDirect}}}
206
will open a port just to handle one incoming connection and close it right after.
207
If your application behaves more like a server, i.e. opens a port and listens on it
208
constantly, use {{{MSRPServer}}} class.
209 3 Oliver Bril
210 1 Adrian Georgescu
=== Components ===
211
212 4 Oliver Bril
==== a connector or acceptor ====
213 3 Oliver Bril
214 8 Oliver Bril
 {{{msrplib.connect}}} provides 2 connectors (with and without relay) and 2 acceptors (likewise, with or without relay). All of them have the exact same interface,
215
216 1 Adrian Georgescu
 '''prepare'''(''self'', ''local_uri''={{{None}}})::
217 8 Oliver Bril
 Depending on type of the connector, use local_uri to prepare the MSRP connection, which means:
218
 * connecting and authenticating at the relay if a relay is used ({{{ConnectorRelay}}} and {{{AcceptorRelay}}})
219 27 Adrian Georgescu
 * starts listening on a local port for DirectAcceptor
220 1 Adrian Georgescu
221 8 Oliver Bril
 ''local_uri'' is used to specify the connection parameters, e.g. local port and local ip.
222 1 Adrian Georgescu
 If not provided, suitable ''local_uri'' will be generated.
223 8 Oliver Bril
 ''local_uri'' maybe updated in place by {{{prepare()}}} method if the real settings used are different from those specified.
224
225
 {{{prepare}}} returns a full local path - list of {{{protocol.URI}}} instances, suitable to be put in SDP {{{'a:path'}}} attribute.
226 1 Adrian Georgescu
 
227 8 Oliver Bril
 '''complete'''(''self'', ''full_remote_path'')::
228 27 Adrian Georgescu
 Completes the establishment of the MSRP connection, which means:
229
 * establishes the connection if it wasn't already established ({{{ConnectorDirect}}})
230
 * binds the connection, i.e. exchanges empty chunk to verify each other's From-Path and To-Path
231 8 Oliver Bril
232
 ''full_remote_path'' should be a list of {{{protocol.URI}}} instances, obtained by parsing {{{'a:path'}}} put in SDP by the remote party.
233
234
 {{{complete}}} returns {{{transport.MSRPTransport}}} instance, ready to read and send chunks.
235 9 Oliver Bril
236
 '''cleanup'''(''self'')::
237 27 Adrian Georgescu
 Calls this method to cleanup after {{{initialize()}}} if it's impossible to call {{{complete()}}}
238 8 Oliver Bril
 
239 1 Adrian Georgescu
240 4 Oliver Bril
==== transport.MSRPTransport ====
241 1 Adrian Georgescu
242 27 Adrian Georgescu
Low level access to the MSRP connection.
243 1 Adrian Georgescu
244
 '''make_chunk'''(''self'', ''transaction_id''={{{None}}}, ''method''={{{'SEND'}}}, ''code''={{{None}}}, ''comment''={{{None}}}, ''data''={{{''}}}, ''contflag''={{{None}}}, ''start''={{{1}}}, ''end''={{{None}}}, ''length''={{{None}}}, ''message_id''={{{None}}})::
245 27 Adrian Georgescu
 Makes a new chunk ({{{protocol.MSRPData}}} instance) with proper {{{From-Path}}}, {{{To-Path}}}, {{{Byte-Range}}} and {{{Message-ID}}} headers set up based on MSRPTransport's state and the parameters provided. Use ''data'' for payload, and ''start''/''end''/''length'' to generate {{{Byte-Range}}} header. Generate new random strings for default values of ''transaction_id'' and ''message_id''. 
246 1 Adrian Georgescu
 [[BR]]''contflag'':[[BR]]
247
 MSRP chunk's continuation flag ({{{'$'}}}, {{{'+'}}} or {{{'#'}}}). Default is {{{'$'}}}, unless you have a partial {{{SEND}}} chunk, in which case it is {{{'+'}}}
248
249 14 Oliver Bril
 '''write'''(''self'', ''bytes'', ''sync''={{{True}}})::
250 27 Adrian Georgescu
 Writes ''bytes'' to the socket. If ''sync'' is true, waits for an operation to complete.
251 14 Oliver Bril
252
 '''read_chunk'''(''self'', ''size''={{{None}}})::
253 27 Adrian Georgescu
 Waits for a new chunk and returns it.
254
 If there was an error, closes the connection and raises {{{ChunkParseError}}}.
255 14 Oliver Bril
256 27 Adrian Georgescu
 In case of unintelligible input, loses the connection and returns {{{None}}}.
257
 When the connection is closed, raises the reason of the closure (e.g. {{{ConnectionDone}}}).
258 14 Oliver Bril
259 27 Adrian Georgescu
 If the data already read exceeds ''size'', stops reading the data and returns
260 14 Oliver Bril
 a "virtual" chunk, i.e. the one that does not actually correspond the the real
261
 MSRP chunk. Such chunks have Byte-Range header changed to match the number of
262
 bytes read and continuation that is {{{'+'}}}; they also possess {{{segment}}} attribute,
263
 an integer, starting with 1 and increasing with every new segment of the chunk.
264
265 1 Adrian Georgescu
 Note, that ''size'' only hints when to interrupt the segment but does not affect
266
 how the data is read from socket. You may have segments bigger than ''size'' and it's
267 14 Oliver Bril
 legal to set ''size'' to zero (which would mean return a chunk as long as you get
268 1 Adrian Georgescu
 some data, regardless how small).
269
270 15 Oliver Bril
 '''check_incoming_SEND_chunk'''(''self'', ''chunk'')::
271 27 Adrian Georgescu
 Checks the 'To-Path' and 'From-Path' of the incoming SEND chunk.
272
 Returns None is the paths are valid for this connection.
273 15 Oliver Bril
 If an error is detected an MSRPError is created and returned.
274 1 Adrian Georgescu
275 4 Oliver Bril
==== session.MSRPSession ====
276 1 Adrian Georgescu
277 15 Oliver Bril
 '''!__init!__'''(''self'', ''msrptransport'', ''accept_types''={{{['*']}}}, ''on_incoming_cb''={{{None}}})::
278 27 Adrian Georgescu
 Initializes MSRPSession instance. Reports the incoming chunks through ''on_incoming_cb'' callback.
279 1 Adrian Georgescu
280 15 Oliver Bril
 '''send_chunk'''(''self'', ''chunk'', ''response_cb''={{{None}}})::
281 27 Adrian Georgescu
 Sends ''chunk''. Report the result via ''response_cb''.
282 15 Oliver Bril
283
 When ''response_cb'' argument is present, it will be used to report
284
 the transaction response to the caller. When a response is received or generated
285
 locally, ''response_cb'' is called with one argument. The function must do something
286 29 Oliver Bril
 quickly and must not block, because otherwise it would block the reader greenlet.
287 15 Oliver Bril
288
 If no response was received after {{{RESPONSE_TIMEOUT}}} seconds,
289
 * 408 response is generated if Failure-Report was {{{'yes'}}} or absent
290 1 Adrian Georgescu
 * 200 response is generated if Failure-Report was {{{'partial'}}} or {{{'no'}}}
291 15 Oliver Bril
292 1 Adrian Georgescu
 Note that it's rather wasteful to provide ''response_cb'' argument other than {{{None}}}
293 15 Oliver Bril
 for chunks with Failure-Report='no' since it will always fire 30 seconds later
294
 with 200 result (unless the other party is broken and ignores Failure-Report header)
295
296 16 Oliver Bril
 If sending is impossible raise {{{MSRPSessionError}}}.
297 1 Adrian Georgescu
298 16 Oliver Bril
 '''deliver_chunk'''(''self'', ''chunk'', ''event''={{{None}}})::
299 27 Adrian Georgescu
 Sends the chunk, waits for the transaction response (if Failure-Report header is not {{{'no'}}}).
300
 Returns the transaction response if it's a success, raise {{{MSRPTransactionError}}} if it's not.
301 16 Oliver Bril
302 27 Adrian Georgescu
 If chunk's Failure-Report is {{{'no'}}}, returns {{{None}}} immediately.
303 16 Oliver Bril
304
 '''shutdown'''(''self'', ''sync''={{{True}}})::
305 27 Adrian Georgescu
 Sends the messages already in queue then close the connection.
306 16 Oliver Bril
307
==== session.GreenMSRPSession ====
308
309
 A subclass of MSRPSession that delivers the incoming messages to the queue.
310
311
 '''!__init!__'''(''self'', ''msrptransport'', ''accept_types''={{{['*']}}})::
312 27 Adrian Georgescu
 Initializes GreenMSRPSession instance. The messages will be delivered to the queue (available as {{{incoming}}} attribute).
313 16 Oliver Bril
314
 '''receive_chunk'''(''self'')::
315 27 Adrian Georgescu
 Returns a message from the queue.
316 16 Oliver Bril
317 1 Adrian Georgescu
318 4 Oliver Bril
==== connect.MSRPServer ====
319 27 Adrian Georgescu
 Manages the listening sockets. Binds incoming requests.
320 13 Oliver Bril
    
321
 MSRPServer solves the problem with AcceptorDirect: concurrent using of 2
322
 or more AcceptorDirect instances on the same non-zero port is not possible.
323
 If you initialize() those instances, one after another, one will listen on
324
 the socket and another will get BindError.
325
326
 MSRPServer avoids the problem by sharing the listening socket between multiple connections.
327
 It has a slightly different interface from AcceptorDirect, so it cannot be considered a drop-in
328
 replacement.
329
330
 '''prepare'''(''self'', ''local_uri''={{{None}}}, ''logger''={{{None}}})::
331 27 Adrian Georgescu
 Starts a listening port specified by ''local_uri'' if there isn't one on that port/interface already.
332
 Adds ''local_uri'' to the list of expected URIs, so that incoming connections featuring this URI won't be rejected.
333
 If ''logger'' is provided, it uses it for this connection instead of the default one.
334 13 Oliver Bril
335
 '''complete'''(''self'', ''full_remote_path'')::
336 27 Adrian Georgescu
 Waits until one of the incoming connections binds using provided ''full_remote_path''.
337
 Returns connected and bounds the {{{MSRPTransport}}} instance.
338 13 Oliver Bril
        
339
 If no such binding was made within {{{MSRPBindSessionTimeout.seconds}}}, raise {{{MSRPBindSessionTimeout}}}.
340
 ''full_remote_path'' should be a list of {{{protocol.URI}}} instances, obtained by parsing {{{'a:path'}}} put in SDP by the remote party.
341
342
 '''cleanup'''(''self'', ''local_uri'')::
343 27 Adrian Georgescu
 Removes ''local_uri'' from the list of expected URIs.