Project

General

Profile

SipMSRPApi » History » Version 57

Adrian Georgescu, 04/16/2010 09:11 AM

1 1 Adrian Georgescu
= MSRP API =
2
3 48 Luci Stanescu
[[TOC(SipMSRPApi, SipDeveloperGuide, depth=2)]]
4 1 Adrian Georgescu
5 57 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).  MSRP can be used for any application that requires a reliable stream of data, for example File Transfer or Desktop Sharing.
6 1 Adrian Georgescu
7 25 Adrian Georgescu
 * MSRP sessions are defined in [http://tools.ietf.org/html/rfc4975 RFC 4975]
8 54 Adrian Georgescu
 * MSRP relay extension for NAT traversal is defined in [http://tools.ietf.org/html/rfc4976 RFC 4976]
9 1 Adrian Georgescu
10 56 Adrian Georgescu
The MSRP protocol stack is implemented by [http://download.ag-projects.com/SipClient python-msrplib] package. 
11 4 Oliver Bril
12 33 Adrian Georgescu
{{{msrplib}}} is based upon [http://twistedmatrix.com twisted] and [http://download.ag-projects.com/SipClient/ eventlet] and provides a set of classes for establishing and managing MSRP connections.
13 1 Adrian Georgescu
14 28 Adrian Georgescu
The library consists of the following modules:
15 1 Adrian Georgescu
16
 '''msrplib.transport'''::
17 27 Adrian Georgescu
 Defines {{{MSRPTransport}}} class, which provides low level control over MSRP connections.
18 2 Redmine Admin
19 1 Adrian Georgescu
 '''msrplib.connect'''::
20
 Defines means to establish a connection, bind it, and provide an initialized {{{MSRPTransport}}} instance.
21
22
 '''msrplib.session'''::
23
 Defines {{{MSRPSession}}} class, which provides high level control over a MSRP connection.
24
25
 '''msrplib.protocol'''::
26 42 Luci Stanescu
 Provides representation and parsing of MSRP entities - chunks and MSRP URIs.  
27 1 Adrian Georgescu
28 48 Luci Stanescu
== URI ==
29 39 Luci Stanescu
30 44 Luci Stanescu
This class is implemented in the {{{msrplib.protocol}}} module and is used to represent an MSRP URI.
31 1 Adrian Georgescu
32 49 Adrian Georgescu
==== methods ====
33 1 Adrian Georgescu
34 39 Luci Stanescu
 '''!__init!__'''(''self'', '''host'''={{{None}}}, '''use_tls'''={{{False}}}, '''user'''={{{None}}}, '''port'''={{{None}}}, '''session_id'''={{{None}}}, '''parameters'''={{{None}}}, '''credentials'''={{{None}}})::
35
  Constructs a new {{{URI}}}. All the arguments specified here are also attributes on the object. For more information on these attributes, see RFC4975.
36
  [[BR]]''host'':[[BR]]
37
  The hostname or IP address which forms the URI.
38
  [[BR]]''use_tls'':[[BR]]
39
  Whether this identifies an msrps or msrp URI.
40
  [[BR]]''user'':[[BR]]
41
  The user part of the URI.
42
  [[BR]]''port'':[[BR]]
43
  The port in the URI.
44
  [[BR]]''session_id'':[[BR]]
45
  The session identifier part of the URI.
46 1 Adrian Georgescu
  [[BR]]''parameters'':[[BR]]
47 39 Luci Stanescu
  A {{{dict}}} containing the parameters which will be appended to the URI.
48
  [[BR]]''credentials'':[[BR]]
49 1 Adrian Georgescu
  A {{{gnutls.interfaces.twisted.X509Credentials}}} object which will be used if this identifies a TLS URI to authenticate to the other endpoint.
50 39 Luci Stanescu
51 48 Luci Stanescu
== MSRPRelaySettings ==
52 1 Adrian Georgescu
53 45 Luci Stanescu
This class is implemented in the {{{msrplib.connect}}} module and is used to specify the MSRP relay which will be used when connecting via a relay (using the {{{ConnectorRelay}}} or {{{AcceptorRelay}}} classes).
54 39 Luci Stanescu
55 49 Adrian Georgescu
==== methods ====
56 39 Luci Stanescu
57
 '''!__init!__'''(''self'', '''domain''', '''username''', '''password''', '''host'''={{{None}}}, '''use_tls'''={{{False}}}, '''port'''={{{None}}}, '''credentials'''={{{None}}})::
58
  Constructs a new {{{URI}}}. All the arguments specified here are also attributes on the object. For more information on these attributes, see RFC4975.
59
  [[BR]]''domain'':[[BR]]
60
  The DNS domain in which to search for a MSRP relay using SRV queries.
61
  [[BR]]''username'':[[BR]]
62
  The username which will be used to authenticate to the relay.
63
  [[BR]]''password'':[[BR]]
64
  The password which will be used to authenticate to the relay.
65
  [[BR]]''host'':[[BR]]
66
  The hostname or IP address of the MSRP relay.
67 1 Adrian Georgescu
  [[BR]]''use_tls'':[[BR]]
68 39 Luci Stanescu
  Whether this identifies an msrps or msrp URI.
69
  [[BR]]''port'':[[BR]]
70 1 Adrian Georgescu
  The port in the URI.
71 39 Luci Stanescu
  [[BR]]''credentials'':[[BR]]
72
  A {{{gnutls.interfaces.twisted.X509Credentials}}} object which will be used to authenticate to the relay if TLS is used.
73
74 48 Luci Stanescu
== ConnectorDirect ==
75 39 Luci Stanescu
76 44 Luci Stanescu
This class is implemented in the {{{msrplib.connect}}} module and is used for outbound MSRP connections without a relay.
77 39 Luci Stanescu
78 49 Adrian Georgescu
==== methods ====
79 39 Luci Stanescu
80
 '''!__init!__'''(''self'', '''loger'''={{{None}}})::
81 44 Luci Stanescu
 Constructs a new ConnectorDirect.
82 39 Luci Stanescu
 [[BR]]''logger'':[[BR]]
83
 The logger which will be used for this MSRP connection. See the [wiki:SipMSRPApi#Logging Logging] section for more information.
84
85
 '''prepare'''(''self'', '''local_uri'''={{{None}}})::
86
 This method returns a full local path - list of {{{protocol.URI}}} instances, suitable to be put in SDP {{{'a:path'}}} attribute.
87
 [[BR]]''local_uri'':[[BR]]
88
 This attribute will be used to construct the local path, but other than that it is not used anywhere else in case of the ConnectorDirect. If not provided, one
89
 will be automatically generated
90 1 Adrian Georgescu
 
91
 '''complete'''(''self'', '''full_remote_path''')::
92 39 Luci Stanescu
 This method establishes the connection and binds it (sends an empty chunk to verify each other's From-Path and To-Path). It returns {{{transport.MSRPTransport}}} instance, ready to read and send chunks.
93 1 Adrian Georgescu
 [[BR]]''full_remote_path'':[[BR]]
94 39 Luci Stanescu
 A list of {{{protocol.URI}}} instances, obtained by parsing {{{'a:path'}}} put in SDP by the remote party.
95 1 Adrian Georgescu
96 39 Luci Stanescu
 '''cleanup'''(''self'')::
97
 This method cleans up after {{{prepare()}}}; it should be called if {{{complete()}}} will not be called for whatever reason.
98 1 Adrian Georgescu
99 48 Luci Stanescu
== ConnectorRelay ==
100 1 Adrian Georgescu
101 44 Luci Stanescu
This class is implemented in the {{{msrplib.connect}}} module and is used for outbound MSRP connections using a relay.
102 1 Adrian Georgescu
103 49 Adrian Georgescu
==== methods ====
104 39 Luci Stanescu
105
 '''!__init!__'''(''self'', '''relay''', '''loger'''={{{None}}})::
106
 Constructs a new ConnectorRelay.
107
 [[BR]]''relay'':[[BR]]
108
 An instance of {{{MSRPRelaySettings}}} which identifies the relay which is to be used.
109
 [[BR]]''logger'':[[BR]]
110
 The logger which will be used for this MSRP connection. See the [wiki:SipMSRPApi#Logging Logging] section for more information.
111
112
 '''prepare'''(''self'', '''local_uri'''={{{None}}})::
113
 This method returns a full local path - list of {{{protocol.URI}}} instances, suitable to be put in SDP {{{'a:path'}}} attribute.
114
 [[BR]]''local_uri'':[[BR]]
115 1 Adrian Georgescu
 This attribute will be used to construct the local path, but other than that it is not used anywhere else in case of the ConnectorRelay. If not provided, one
116 39 Luci Stanescu
 will be automatically generated
117
 
118 1 Adrian Georgescu
 '''complete'''(''self'', '''full_remote_path''')::
119 39 Luci Stanescu
 This method establishes the connection to the relay and binds it (sends an empty chunk to verify each other's From-Path and To-Path). It returns {{{transport.MSRPTransport}}} instance, ready to read and send chunks.
120
 [[BR]]''full_remote_path'':[[BR]]
121
 A list of {{{protocol.URI}}} instances, obtained by parsing {{{'a:path'}}} put in SDP by the remote party.
122
123 1 Adrian Georgescu
 '''cleanup'''(''self'')::
124 39 Luci Stanescu
 This method cleans up after {{{prepare()}}}; it should be called if {{{complete()}}} will not be called for whatever reason.
125 1 Adrian Georgescu
126 48 Luci Stanescu
== AcceptorDirect ==
127 1 Adrian Georgescu
128 44 Luci Stanescu
This class is implemented in the {{{msrplib.connect}}} module and is used for inbound MSRP connections without using a relay.
129 1 Adrian Georgescu
130 49 Adrian Georgescu
==== methods ====
131 39 Luci Stanescu
132
 '''!__init!__'''(''self'', '''loger'''={{{None}}})::
133
  Constructs a new AcceptorDirect.
134
 [[BR]]''logger'':[[BR]]
135
 The logger which will be used for this MSRP connection. See the [wiki:SipMSRPApi#Logging Logging] section for more information.
136
137
 '''prepare'''(''self'', '''local_uri'''={{{None}}})::
138 1 Adrian Georgescu
 This method starts listening on a socket identified by the parameters in the {{{local_uri}}} argument. It returns a full local path - list of {{{protocol.URI}}} instances, suitable to be put in SDP {{{'a:path'}}} attribute.
139 39 Luci Stanescu
 [[BR]]''local_uri'':[[BR]]
140
 This attribute will be used to construct the local path and to listen for incomming connections. If not provided, one
141 52 Luci Stanescu
 will be automatically generated. Note that the object may be changed in place: for example, if the port specified is 0,
142
 a random one will be selected and the object will be updated accordingly.
143 39 Luci Stanescu
 
144
 '''complete'''(''self'', '''full_remote_path''')::
145
 This method waits for an incoming connection and a chunk sent by the other party. It returns {{{transport.MSRPTransport}}} instance, ready to read and send chunks.
146
 [[BR]]''full_remote_path'':[[BR]]
147
 A list of {{{protocol.URI}}} instances, obtained by parsing {{{'a:path'}}} put in SDP by the remote party. This is checked agains the From-Path header in the binding chunk.
148
149
 '''cleanup'''(''self'')::
150
 This method cleans up after {{{prepare()}}}; it should be called if {{{complete()}}} will not be called for whatever reason.
151
152 48 Luci Stanescu
== AcceptorRelay ==
153 39 Luci Stanescu
154 44 Luci Stanescu
This class is implemented in the {{{msrplib.connect}}} module and is used for inbound MSRP connections using a relay.
155 39 Luci Stanescu
156 49 Adrian Georgescu
==== methods ====
157 39 Luci Stanescu
158
 '''!__init!__'''(''self'', '''relay''', '''loger'''={{{None}}})::
159
 Constructs a new AcceptorRelay.
160
 [[BR]]''relay'':[[BR]]
161
 An instance of {{{MSRPRelaySettings}}} which identifies the relay which is to be used.
162
 [[BR]]''logger'':[[BR]]
163 1 Adrian Georgescu
 The logger which will be used for this MSRP connection. See the [wiki:SipMSRPApi#Logging Logging] section for more information.
164 39 Luci Stanescu
165
 '''prepare'''(''self'', '''local_uri'''={{{None}}})::
166 1 Adrian Georgescu
 This method connects to the relay specified in {{{__init__}}}. It returns a full local path - list of {{{protocol.URI}}} instances, suitable to be put in SDP {{{'a:path'}}} attribute.
167 39 Luci Stanescu
 [[BR]]''local_uri'':[[BR]]
168
 This attribute will be used to construct the local path. If not provided, one will be automatically generated
169
 
170
 '''complete'''(''self'', '''full_remote_path''')::
171
 This method waits for an incoming chunk sent by the other party. It returns {{{transport.MSRPTransport}}} instance, ready to read and send chunks.
172
 [[BR]]''full_remote_path'':[[BR]]
173
 A list of {{{protocol.URI}}} instances, obtained by parsing {{{'a:path'}}} put in SDP by the remote party. This is checked agains the From-Path header in the binding chunk.
174
175
 '''cleanup'''(''self'')::
176
 This method cleans up after {{{prepare()}}}; it should be called if {{{complete()}}} will not be called for whatever reason.
177
178 53 Luci Stanescu
== Headers ==
179
180
The MSRP headers are represented using objects from the {{{msrplib.protocol module}}}. All MSRP header object provide three properties:
181
 ''name'':[[BR]]
182
 The name of the header, as it appears in MSRP chunks.
183
 [[BR]]''encode'':[[BR]]
184
 The MSRP encoded header value, as it appears in MSRP chunks.
185
 [[BR]]''decode'':[[BR]]
186
 The high-level object representing the header value.
187
188
All headers can be constructed by passing either the encoded or decoded objects to {{{__init__}}}. The following headers are provided:
189
190
=== ToPathHeader ===
191
192
The name of the header is {{{'To-Path'}}} and the decoded value is a deque of {{{URI}}} objects.
193
194
=== FromPathHeader ===
195
196
The name of the header is {{{'From-Path'}}} and the decoded value is a deque of {{{URI}}} objects.
197
198
=== MessageIDHeader ===
199
200
The name of the header is {{{'Message-ID'}}} and the decoded value is the string containing the message ID.
201
202
=== SuccessReportHeader ===
203
204
The name of the header is {{{'Success-Report'}}} and the decoded value is one of {{{'yes'}}} or {{{'no'}}}.
205
206
=== FailureReportHeader ===
207
208
The name of the header is {{{'Failure-Report'}}} and the decoded value is one of {{{'yes'}}}, {{{'partial'}}} or {{{'no'}}}.
209
210
=== ByteRangeHeader ===
211
212
The name of the header is {{{'Byte-Range'}}} and the decoded value is a 3-tuple containing the start, end and length values.
213
214
==== attributes ====
215
216
 '''fro'''::
217
  The start value of the header: the index within the whole message payload where a chunk's payload starts.
218
 '''end'''::
219
  The end value of the header: the index within the whole message payload where a chunk's payload ends. Note that this index is inclusive.
220
 '''length'''::
221
  The total size of the message payload.
222
223
=== StatusHeader ===
224
225
The name of the header is {{{'Status'}}} and the decoded value is a 2-tuple containing the status code and comment.
226
227
==== attributes ====
228
229
 '''code'''::
230
  The code component of the header.
231
 '''comment'''::
232
  The comment component of the header.
233
234
=== ExpiresHeader ===
235
236
The name of the header is {{{'Expires'}}} and the decoded value is an integer.
237
238
=== MinExpiresHeader ===
239
240
The name of the header is {{{'Min-Expires'}}} and the decoded value is an integer.
241
242
=== MaxExpiresHeader ===
243
244
The name of the header is {{{'Max-Expires'}}} and the decoded value is an integer.
245
246
=== UsePathHeader ===
247
248
The name of the header is {{{'Use-Path'}}} and the decoded value is a deque of {{{URI}}} objects.
249
250
=== WWWAuthenticateHeader ===
251
252
The name of the header is {{{'WWW-Authenticate'}}} and the decoded value is a dictionary of the parameters within the header.
253
254
=== AuthorizationHeader ===
255
256
The name of the header is {{{'Authorization'}}} and the decoded value is a dictionary of the parameters within the header.
257
258
=== AuthenticationInfoHeader ===
259
260
The name of the header is {{{'Authentication-Info'}}} and the decoded value is a dictionary of the parameters within the header.
261
262
=== ContentTypeHeader ===
263
264
The name of the header is {{{'Content-Type'}}} and the decoded value is the string representing the MIME type.
265
266
=== ContentIDHeader ===
267
268
The name of the header is {{{'Content-ID'}}} and the decoded value is the string with the value of the header.
269
270
=== ContentDescriptionHeader ===
271
272
The name of the header is {{{'Content-Description'}}} and the decoded value is the string with the value of the header.
273
274
=== ContentDispositionHeader ===
275
276
The name of the header is {{{'Content-Disposition'}}} and the decoded value is a list with two elements: the disposition and a dict with the parameters.
277
278 51 Luci Stanescu
== MSRPData ==
279
280
This class is implemented in the {{{msrplib.protocol}}} module and represents an MSRP chunk, either a request or a response.
281
282
==== attributes ====
283
284
 The following attributes are read-only.
285
286
 '''content_type'''::
287
  The MIME type of the payload carried by this chunk, which is stored in the Content-Type header.
288
289
 '''message_id'''::
290
  The ID of the message this chunk is part of, which is stored in the Message-ID header.
291
292
 '''byte_range'''::
293
  A 3-tuple containing the start, end and length values (in this order) from the Byte-Range header.
294
295
 '''status'''::
296
  The value of the Status header.
297
298
 '''failure_report'''::
299
  The value of the Failure-Report header, whether it exists or it is implied; one of {{{'yes'}}}, {{{'no'}}}, {{{'partial'}}}.
300
301
 '''success_report'''::
302
  The value of the Success-Report header, whether it exists or it is implied; one of {{{'yes'}}} or {{{'no'}}}.
303
304
 '''size'''::
305
  The size of the payload of this chunk, in bytes.
306
307
==== methods ====
308
309
 '''!__init!__'''(''self'', '''transaction_id''', '''method'''={{{None}}}, '''code'''={{{None}}}, '''comment'''={{{None}}}, '''headers'''={{{None}}}, '''data'''={{{''}}}, '''contflag'''={{{'$'}}})::
310
  Initializes a new MSRPData instance. All the arguments are also available as attributes.
311
  [[BR]]''transaction_id'':[[BR]]
312
  The transaction identified of this chunk.
313
  [[BR]]''method'':[[BR]]
314
  The method of this chunk if it a request and {{{None}}} if it is a response.
315
  [[BR]]''code'':[[BR]]
316
  The status code of this chunk if it is a response and {{{None}}} if it is a request.
317
  [[BR]]''comment'':[[BR]]
318
  The comment of this chunk if it is a response and {{{None}}} if it is a request.
319
  [[BR]]''headers'':[[BR]]
320
  A dict containing the headers which are to be added to this chunk, or {{{None}}} if no headers are to be added.
321
  [[BR]]''data'':[[BR]]
322
  The payload of this chunk, or an empty string if no payload is to be added.
323
  [[BR]]''contflag'':[[BR]]
324
  The MSRP continuation flag of this chunk, one of {{{'$'}}}, {{{'#'}}} or {{{'+'}}}.
325
326
 '''copy'''(''self'')::
327
  Returns a new MSRPData instance with exactly the same attributes as this object.
328
329
 '''add_header'''(''self'', '''header''')::
330
  Add a MSRP header to this chunk.
331
  [[BR]]''header'':[[BR]]
332
  The header object which is to be added to this chunk.
333
334
 '''verify_headers'''(''self'')::
335
  Verifies that the chunk contains a To-Path and a From-Path header and that all the headers are valid.
336
337
 '''encode_start'''(''self'')::
338
  Returns a string containing the MSRP header of this chunk.
339
340
 '''encode_end'''(''self'', '''continuation''')::
341
  Returns a string containing the MSRP end line of this chunk: 7 commas followed by the transaction identifier and the continuation flag specified.
342
  [[BR]]''continuation'':[[BR]]
343
  The continuation flag which is to be used in the end line.
344
345
 '''encode'''(''self''):
346
  Returns a string containing the whole of this MSRP chunk.
347
348
349 14 Oliver Bril
== MSRPTransport ==
350 1 Adrian Georgescu
351 51 Luci Stanescu
This class is implemented in the {{{msrplib.transport}}} module and provies low level access to the MSRP connection. This class should not be constructed directly, but rather its intances should be obtained by using the various connector/acceptor classes documented above.
352 1 Adrian Georgescu
353
==== methods ====
354
355
 '''make_chunk'''(''self'', '''transaction_id'''={{{None}}}, '''method'''={{{'SEND'}}}, '''code'''={{{None}}}, '''comment'''={{{None}}}, '''data'''={{{''}}}, '''contflag'''={{{None}}}, '''start'''={{{1}}}, '''end'''={{{None}}}, '''length'''={{{None}}}, '''message_id'''={{{None}}})::
356 51 Luci Stanescu
 Creates a new chunk ({{{protocol.MSRPData}}} instance), which is either an MSRP request or a response. Proper {{{From-Path}}}, {{{To-Path}}}, {{{Byte-Range}}} and {{{Message-ID}}} headers are added based on MSRPTransport's state and the parameters provided. Use ''data'' for payload, and ''start''/''end''/''length'' to generate the {{{Byte-Range}}} header.
357
 [[BR]]''transaction_id'':[[BR]]
358
 The transaction id which will be put in the chunk. If it is not provided, one will be randomly generated.
359
 [[BR]]''method'':[[BR]]
360
 The method of the new MSRP request, or {{{None}}} if a response is required.
361
 [[BR]]''code'':[[BR]]
362
 The code of the new MSRP response, or {{{None}}} if a request is required.
363
 [[BR]]''code'':[[BR]]
364
 The comment of the new MSRP response, or {{{None}}} if a request is required or a comment on the response is not.
365
 [[BR]]''data'':[[BR]]
366
 The payload of the new chunk, or an empty string if no payload is required.
367 1 Adrian Georgescu
 [[BR]]''contflag'':[[BR]]
368 51 Luci Stanescu
 MSRP chunk's continuation flag ({{{'$'}}}, {{{'+'}}} or {{{'#'}}}). Default is {{{'$'}}} for a full message, unless a partial {{{SEND}}} chunk required, in which case it should be set to {{{'+'}}}. If {{{None}}} is provided, either {{{'$'}}} or {{{'+'}}} will be used depending on whether this chunk seems to carry the last part of the message data.
369
 [[BR]]''start'':[[BR]]
370
 The first byte's index within the whole message payload this chunk will carry as its own payload.
371
 [[BR]]''end'':[[BR]]
372
 The last byte's index within the whole message payload this chunk will carry as its own payload. Note that this is an inclusive index. If this is not provided, it's computed based on the number of bytes in data and {{{start}}}.
373
 [[BR]]''length'':[[BR]]
374
 The total number of bytes of the whole message payload. If this is not provided, it is computed as if this chunk carries that last part of the message payload.
375
 [[BR]]''message_id'':[[BR]]
376
 The ID of the message this chunk is part of. If it is not provided, one will be randomly generated.
377 1 Adrian Georgescu
378 51 Luci Stanescu
 '''write_chunk'''(''self'', '''chunk''', '''wait'''={{{True}}})::
379
 Writes the chunk provided to the underlying socket.
380
 [[BR]]''chunk'':[[BR]]
381
 The chunk which is to be written, an instance of {{{MSRPData}}}.
382
 [[BR]]''wait'':[[BR]]
383
 If {{{True}}}, waits for the operation to complete.
384 1 Adrian Georgescu
385 51 Luci Stanescu
 '''write_response'''(''self'', '''chunk''', '''code''', '''comment''', '''wait'''={{{True}}}):
386
 Creates a response which is suitable as a reply to the specified chunk and writes it to the underlying socket.
387
 [[BR]]''chunk'':[[BR]]
388
 The chunk for which to create a response.
389
 [[BR]]''code'':[[BR]]
390
 The status code of the response which is to be created.
391
 [[BR]]''comment'':[[BR]]
392
 The comment of the response which is to be created.
393
 [[BR]]''wait'':[[BR]]
394
 If {{{True}}}, waits for the operation to complete.
395
396 27 Adrian Georgescu
 '''read_chunk'''(''self'', '''size'''={{{None}}})::
397 1 Adrian Georgescu
 Waits for a new chunk and returns it.
398 15 Oliver Bril
 If there was an error, closes the connection and raises {{{ChunkParseError}}}.
399 27 Adrian Georgescu
400 15 Oliver Bril
 In case of unintelligible input, loses the connection and returns {{{None}}}.
401 39 Luci Stanescu
 When the connection is closed, raises the reason of the closure (e.g. {{{ConnectionDone}}}).
402 15 Oliver Bril
403 1 Adrian Georgescu
 If the data already read exceeds ''size'', stops reading the data and returns
404 15 Oliver Bril
 a "virtual" chunk, i.e. the one that does not actually correspond the the real
405 1 Adrian Georgescu
 MSRP chunk. Such chunks have Byte-Range header changed to match the number of
406 48 Luci Stanescu
 bytes read and continuation that is {{{'+'}}}; they also possess {{{segment}}} attribute,
407 15 Oliver Bril
 an integer, starting with 1 and increasing with every new segment of the chunk.
408 1 Adrian Georgescu
409
 Note, that ''size'' only hints when to interrupt the segment but does not affect
410
 how the data is read from socket. You may have segments bigger than ''size'' and it's
411 44 Luci Stanescu
 legal to set ''size'' to zero (which would mean return a chunk as long as you get
412 39 Luci Stanescu
 some data, regardless how small).
413 51 Luci Stanescu
 
414
 [[BR]]''size'':[[BR]]
415
 The hint towards how much to read from the socket. If the data already read is larger, then all the data will be returned, even if it exceeds {{{size}}} bytes.
416 41 Luci Stanescu
417 1 Adrian Georgescu
 '''check_incoming_SEND_chunk'''(''self'', '''chunk''')::
418
 Checks the 'To-Path' and 'From-Path' of the incoming SEND chunk.
419
 Returns None is the paths are valid for this connection.
420
 If an error is detected an MSRPError is created and returned.
421
422 51 Luci Stanescu
== OutgoingFile ==
423
424
This class is implemented in the {{{msrplib.session}}} module and represents a file which is to be sent via MSRP.
425
426
==== attributes ====
427
428
 '''headers'''::
429
  A dict which maps header names to header objects. These headers will be sent in the MSRP message used to send the file.
430
431
==== methods ====
432
433
 '''!__init!__'''(''self'', '''fileobj''', '''size''', '''content_type'''={{{None}}}, '''position'''={{{0}}}, '''message_id'''={{{None}}})::
434
  Initializes a new OutgoingFile using the specified file object. All arguments are available as attributes, except for {{{content_type}}} which will be part of the {{{headers}}} attribute.
435
  [[BR]]''fileobj'':[[BR]]
436
  A file-like object which will be used for reading the data to be sent.
437
  [[BR]]''size'':[[BR]]
438
  The size of the whole file.
439
  [[BR]]''content_type'':[[BR]]
440
  The MIME type associated with the file's data. If provided, it will be added as a Content-Type header.
441
  [[BR]]''position'':[[BR]]
442
  The position within the file from which data will be sent. The file object must already be seeked to this position.
443
  [[BR]]''message_id'':[[BR]]
444
  The message ID which will be used for sending this file.
445
446 1 Adrian Georgescu
== MSRPSession ==
447
448
This class is implemented in the {{{msrplib.session}}} module and provides a high level API for MSRP sessions.
449
450
==== methods ====
451
452
 '''!__init!__'''(''self'', '''msrptransport''', '''accept_types'''={{{['*']}}}, '''on_incoming_cb'''={{{None}}})::
453 51 Luci Stanescu
 Initializes MSRPSession instance over the specified transport. The incoming chunks are reported through the ''on_incoming_cb'' callback.
454
 [[BR]]''msrptransport'':[[BR]]
455
 An instance of {{{MSRPTransport}}} over which this session will operate.
456
 [[BR]]''accept_types'':[[BR]]
457
 A list of MIME types which are acceptable over this session. If data is received with a Content-Type header which doesn't match this list, a negative response is sent back and the application does not get the received data. The special strings {{{'*'}}} and {{{'<type>/*'}}} can be used to match multiple MIME types.
458
 [[BR]]''on_incoming_cb'':[[BR]]
459
 A function which receives two arguments, both optional with default values of {{{None}}}: ''chunk'' and ''error''. This will be called when a new chunk is received.
460 1 Adrian Georgescu
461
 '''send_chunk'''(''self'', '''chunk''', '''response_cb'''={{{None}}})::
462 51 Luci Stanescu
 Sends the specified chunk and reports the result via the ''response_cb'' callback.
463 1 Adrian Georgescu
464
 When ''response_cb'' argument is present, it will be used to report
465
 the transaction response to the caller. When a response is received or generated
466
 locally, ''response_cb'' is called with one argument. The function must do something
467 38 Adrian Georgescu
 quickly and must not block, because otherwise it would block the reader greenlet.
468 39 Luci Stanescu
469 1 Adrian Georgescu
 If no response was received after {{{RESPONSE_TIMEOUT}}} seconds,
470
 * 408 response is generated if Failure-Report was {{{'yes'}}} or absent
471
 * 200 response is generated if Failure-Report was {{{'partial'}}} or {{{'no'}}}
472
473
 Note that it's rather wasteful to provide ''response_cb'' argument other than {{{None}}}
474
 for chunks with Failure-Report='no' since it will always fire 30 seconds later
475
 with 200 result (unless the other party is broken and ignores Failure-Report header)
476
477 51 Luci Stanescu
 If sending is not possible, {{{MSRPSessionError}}} is raised.
478 1 Adrian Georgescu
479 51 Luci Stanescu
 [[BR]]''chunk'':[[BR]]
480
 The chunk which is to be sent. It must be an instance of {{{MSRPData}}} which represents a request.
481
 [[BR]]''response_cb'':[[BR]]
482
 A function receiving a single argument which will be the response received for the sent chunk.
483
484 1 Adrian Georgescu
 '''deliver_chunk'''(''self'', '''chunk''', '''event'''={{{None}}})::
485 51 Luci Stanescu
 Sends the specified chunk and waits for the transaction response (if Failure-Report header is not {{{'no'}}}).
486
 Returns the transaction response if it's a success or raise {{{MSRPTransactionError}}} if it's not.
487 1 Adrian Georgescu
488
 If chunk's Failure-Report is {{{'no'}}}, returns {{{None}}} immediately.
489
490 51 Luci Stanescu
 [[BR]]''chunk'':[[BR]]
491
 The chunk which is to be sent. It must be an intance of {{{MSRPData}}} which represents a request.
492
 [[BR]]''event'':[[BR]]
493
 The {{{eventlet.coros.event}}} object which will be used to wait for a response. It's send method will be called when a response is received. If it is not provided, one will be constructed automatically and used.
494
 
495
 '''send_file'''(''self'', '''outgoing_file''')::
496
 Adds the specified file to the queue of files to be sent. The method returns immediately.
497
 [[BR]]''outgoing_file'':[[BR]]
498
 An instance of {{{OutgoingFile}}} which represents the file to be sent.
499
500 44 Luci Stanescu
 '''shutdown'''(''self'', '''sync'''={{{True}}})::
501 1 Adrian Georgescu
 Sends the messages already in queue then closes the connection.
502 44 Luci Stanescu
503
== MSRPServer ==
504
505 1 Adrian Georgescu
This class is implemented in the {{{msrplib.connect}}} module.
506 44 Luci Stanescu
    
507 39 Luci Stanescu
MSRPServer solves the problem with AcceptorDirect: concurrent using of 2
508
or more AcceptorDirect instances on the same non-zero port is not possible.
509 49 Adrian Georgescu
If you initialize() those instances, one after another, one will listen on
510 1 Adrian Georgescu
the socket and another will get BindError.
511
512
MSRPServer avoids the problem by sharing the listening socket between multiple connections.
513 39 Luci Stanescu
It has a slightly different interface from AcceptorDirect, so it cannot be considered a drop-in
514 46 Luci Stanescu
replacement.
515
516 1 Adrian Georgescu
It manages the listening sockets and binds incoming requests.
517
518
==== methods ====
519
520
 '''!__init!__'''(''self'', '''logger''')::
521 51 Luci Stanescu
 Constructs a new MSRPServer which will be using the specifed logger for its connections.
522
 [[BR]]''logger'':[[BR]]
523
 The default logger which will be used for this object's MSRP connections. See the [wiki:SipMSRPApi#Logging Logging] section for more information.
524 1 Adrian Georgescu
525
 '''prepare'''(''self'', ''local_uri''={{{None}}}, ''logger''={{{None}}})::
526
 Starts a listening port specified by ''local_uri'' if there isn't one on that port/interface already.
527
 Adds ''local_uri'' to the list of expected URIs, so that incoming connections featuring this URI won't be rejected.
528 51 Luci Stanescu
 If ''logger'' is provided, it uses it for this connection instead of the one specified in {{{__init___}}}.
529
 [[BR]]''local_uri'':[[BR]]
530
 The URI which will be added to the list of expected URIs. Connections from this URI will be accepted.
531
 [[BR]]''logger'':[[BR]]
532
 The logger which will be used for connections from the specified URI. See the [wiki:SipMSRPApi#Logging Logging] section for more information.
533 15 Oliver Bril
534 1 Adrian Georgescu
 '''complete'''(''self'', ''full_remote_path'')::
535 51 Luci Stanescu
 Waits until one of the incoming connections binds using the provided ''full_remote_path''.
536
 Returns the connected and bound {{{MSRPTransport}}} instance.
537
 If no such binding was made within {{{MSRPBindSessionTimeout.seconds}}}, {{{MSRPBindSessionTimeout}}} is raised.
538
 [[BR]]''full_remote_path'':[[BR]]
539
 A list of {{{protocol.URI}}} instances, obtained by parsing {{{'a:path'}}} put in SDP by the remote party.
540 38 Adrian Georgescu
541
 '''cleanup'''(''self'', ''local_uri'')::
542 46 Luci Stanescu
 Removes ''local_uri'' from the list of expected URIs.
543 51 Luci Stanescu
 [[BR]]''local_uri'':[[BR]]
544
 The URI which is to be removed from the list of expected URIs.
545 49 Adrian Georgescu
546 46 Luci Stanescu
== Logging ==
547
548
Logging is done throughout the library using objects defined by the application, called loggers. If logging is not desired, the {{{application.python.Null}}} object of {{{python-application}}} can be used. These loggers must define the following methods:
549
550
==== methods ====
551
552
 '''received_new_chunk'''('''data''', '''transport''', '''chunk''')::
553
  This method is called when the start of a new chunk is received.
554
  [[BR]]''data'':[[BR]]
555
  The data which came along with the start of the chunk.
556
  [[BR]]''transport'':[[BR]]
557
  The {{{MSRPTransport}}} instance on which the chunk was received.
558
  [[BR]]''chunk'':[[BR]]
559
  The actual chunk object.
560
561
 '''received_chunk_data'''('''data''', '''transport''', '''transaction_id''')::
562
  This method is called when data is received as part of a chunk previously announced via '''received_new_chunk'''.
563
  [[BR]]''data'':[[BR]]
564
  The data received as part of the chunk.
565 47 Luci Stanescu
  [[BR]]''transport'':[[BR]]
566 46 Luci Stanescu
  The {{{MSRPTransport}}} instance on which the chunk was received.
567
  [[BR]]''transaction_id'':[[BR]]
568
  The transaction ID which identifies the chunk for which data was received.
569
570
 '''received_chunk_end'''('''data''', '''transport''', '''transaction_id''')::
571
  This method is called when the last data of a chunk is received. The chunk was previously announced via '''received_new_chunk'''.
572
  [[BR]]''data'':[[BR]]
573
  The last data received as part of the chunk.
574
  [[BR]]''transport'':[[BR]]
575
  The {{{MSRPTransport}}} instance on which the chunk was received.
576
  [[BR]]''transaction_id'':[[BR]]
577
  The transaction ID which identifies the chunk which was ended.
578
579
 '''sent_new_chunk'''('''data''', '''transport''', '''chunk''')::
580
  This method is called when the start of a new chunk is sent.
581
  [[BR]]''data'':[[BR]]
582
  The data which was sent along with the start of the chunk.
583
  [[BR]]''transport'':[[BR]]
584
  The {{{MSRPTransport}}} instance on which the chunk was sent.
585
  [[BR]]''chunk'':[[BR]]
586
  The actual chunk object.
587
588
 '''sent_chunk_data'''('''data''', '''transport''', '''transaction_id''')::
589
  This method is called when data is sent as part of a chunk previously announced via '''sent_new_chunk'''.
590
  [[BR]]''data'':[[BR]]
591
  The data sent as part of the chunk.
592 47 Luci Stanescu
  [[BR]]''transport'':[[BR]]
593 46 Luci Stanescu
  The {{{MSRPTransport}}} instance on which the chunk was sent.
594
  [[BR]]''transaction_id'':[[BR]]
595
  The transaction ID which identifies the chunk for which data was sent.
596
597
 '''sent_chunk_end'''('''data''', '''transport''', '''transaction_id''')::
598
  This method is called when the last data of a chunk is sent. The chunk was previously announced via '''sent_new_chunk'''.
599
  [[BR]]''data'':[[BR]]
600
  The last data sent as part of the chunk.
601
  [[BR]]''transport'':[[BR]]
602
  The {{{MSRPTransport}}} instance on which the chunk was sent.
603
  [[BR]]''transaction_id'':[[BR]]
604
  The transaction ID which identifies the chunk which was ended.
605
606
 '''debug'''('''message''')::
607
  This method is called when a debug level message is sent by the library.
608
609
 '''info'''('''message''')::
610
  This method is called when a info level message is sent by the library.
611
612
 '''warn'''('''message''')::
613
  This method is called when a warning level message is sent by the library.
614
615 38 Adrian Georgescu
 '''error'''('''message''')::
616 39 Luci Stanescu
  This method is called when a error level message is sent by the library.
617 38 Adrian Georgescu
618
 '''fatal'''('''message''')::
619 1 Adrian Georgescu
  This method is called when a fatal level message is sent by the library.
620
621 52 Luci Stanescu
== Examples ==
622 1 Adrian Georgescu
623 52 Luci Stanescu
=== Creating an outbound connection ===
624 1 Adrian Georgescu
625 52 Luci Stanescu
When creating an outbound connection, a relay is not usually used because NAT traversal is not a problem for the endpoing creating the connection. If one is nevertheless desired, a {{{ConnectorRelay}}} can be used instead:
626 1 Adrian Georgescu
627 52 Luci Stanescu
{{{
628
from msrplib.connect import ConnectorDirect
629
from msrplib.session import MSRPSession
630 1 Adrian Georgescu
631 52 Luci Stanescu
connector = ConnectorDirect()
632
full_local_path = connector.prepare()
633
try:
634
    # put full_local_path in SDP 'a:path' attribute of offer
635
    # get full_remote_path from remote's 'a:path: attribute of reply
636
    msrp_transport = connector.complete(full_remote_path)
637
except:
638
    connector.cleanup()
639
    raise
640 1 Adrian Georgescu
641 52 Luci Stanescu
def handle_incoming(chunk=None, error=None):
642
    if error is not None:
643
       print 'Error: %s' % error
644
       session.shutdown()
645
    elif chunk is not None:
646
       print 'Got data: %s' % chunk.data
647 1 Adrian Georgescu
648 52 Luci Stanescu
session = MSRPSession(msrp_transport, on_incoming_cb=handle_incoming)
649
session.send_chunk(msrp_transport.make_chunk(data='Hello world!'))
650
}}}
651 38 Adrian Georgescu
652 52 Luci Stanescu
=== Waiting for an inbound connection ===
653
654
When creating an inbound connection, a relay must be used for NAT traversal. However, if one is not desired, an {{{AceptorDirect}}} can be used instead:
655
656 38 Adrian Georgescu
{{{
657 52 Luci Stanescu
from msrplib.connect import AcceptorRelay, MSRPRelaySettings
658
from msrplib.session import MSRPSession
659
660
relay = MSRPRelaySettings(domain='example.org', username='user', password='pass')
661
connector = AcceptorRelay(relay)
662 1 Adrian Georgescu
full_local_path = connector.prepare()
663
try:
664 52 Luci Stanescu
    # get full_remote_path from remote's 'a:path: attribute of offer
665
    # put full_local_path in SDP 'a:path' attribute of reply
666
    msrp_transport = connector.complete(full_remote_path)
667
except:
668 1 Adrian Georgescu
    connector.cleanup()
669 52 Luci Stanescu
    raise
670 1 Adrian Georgescu
671 52 Luci Stanescu
def handle_incoming(chunk=None, error=None):
672
    if error is not None:
673
       print 'Error: %s' % error
674
       session.shutdown()
675
    elif chunk is not None:
676
       print 'Got data: %s' % chunk.data
677 1 Adrian Georgescu
678 52 Luci Stanescu
session = MSRPSession(msrp_transport, on_incoming_cb=handle_incoming)
679
session.send_chunk(msrp_transport.make_chunk(data='Hello world!'))
680
}}}