Project

General

Profile

SipMSRPApi » History » Version 63

Anonymous, 06/28/2011 02:28 PM

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