Project

General

Profile

SipMSRPApi » History » Version 1

Tijmen de Mes, 05/08/2012 06:06 PM

1 1 Tijmen de Mes
h1. MSRP API
2
3
4
5
6
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.
7
8
* MSRP sessions are defined in "RFC 4975":http://tools.ietf.org/html/rfc4975
9
* MSRP relay extension for NAT traversal is defined in "RFC 4976":http://tools.ietf.org/html/rfc4976
10
11
The MSRP protocol stack is implemented by "python-msrplib":http://download.ag-projects.com/SipClient package. 
12
13
@msrplib@ is based upon "twisted":http://twistedmatrix.com and "eventlet":http://download.ag-projects.com/SipClient/ and provides a set of classes for establishing and managing MSRP connections.
14
15
The library consists of the following modules:
16
17
*msrplib.transport*
18
 Defines @MSRPTransport@ class, which provides low level control over MSRP connections.
19
20
*msrplib.connect*
21
 Defines means to establish a connection, bind it, and provide an initialized @MSRPTransport@ instance.
22
23
*msrplib.session*
24
 Defines @MSRPSession@ class, which provides high level control over a MSRP connection.
25
26
*msrplib.protocol*
27
 Provides representation and parsing of MSRP entities - chunks and MSRP URIs.  
28
29
30
h2. URI
31
32
33
This class is implemented in the @msrplib.protocol@ module and is used to represent an MSRP URI.
34
35
36
h4. methods
37
38
39
*<notextile>__init__</notextile>*(_self_, *host*=@None@, *use_tls*=@False@, *user*=@None@, *port*=@None@, *session_id*=@None@, *parameters*=@None@, *credentials*=@None@)
40
>Constructs a new @URI@. All the arguments specified here are also attributes on the object. For more information on these attributes, see RFC4975.
41
  
42
>+_host_+:
43
44
>The hostname or IP address which forms the URI.
45
  
46
>+_use_tls_+:
47
48
>Whether this identifies an msrps or msrp URI.
49
  
50
>+_user_+:
51
52
>The user part of the URI.
53
  
54
>+_port_+:
55
56
>The port in the URI.
57
  
58
>+_session_id_+:
59
60
>The session identifier part of the URI.
61
  
62
>+_parameters_+:
63
64
>A @dict@ containing the parameters which will be appended to the URI.
65
  
66
>+_credentials_+:
67
68
>A @gnutls.interfaces.twisted.X509Credentials@ object which will be used if this identifies a TLS URI to authenticate to the other endpoint.
69
70
71
h2. MSRPRelaySettings
72
73
74
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).
75
76
77
h4. methods
78
79
80
*<notextile>__init__</notextile>*(_self_, *domain*, *username*, *password*, *host*=@None@, *use_tls*=@False@, *port*=@None@, *credentials*=@None@)
81
>Constructs a new @URI@. All the arguments specified here are also attributes on the object. For more information on these attributes, see RFC4975.
82
  
83
>+_domain_+:
84
85
>The DNS domain in which to search for a MSRP relay using SRV queries.
86
  
87
>+_username_+:
88
89
>The username which will be used to authenticate to the relay.
90
  
91
>+_password_+:
92
93
>The password which will be used to authenticate to the relay.
94
  
95
>+_host_+:
96
97
>The hostname or IP address of the MSRP relay.
98
  
99
>+_use_tls_+:
100
101
>Whether this identifies an msrps or msrp URI.
102
  
103
>+_port_+:
104
105
>The port in the URI.
106
  
107
>+_credentials_+:
108
109
>A @gnutls.interfaces.twisted.X509Credentials@ object which will be used to authenticate to the relay if TLS is used.
110
111
112
h2. ConnectorDirect
113
114
115
This class is implemented in the @msrplib.connect@ module and is used for outbound MSRP connections without a relay.
116
117
118
h4. methods
119
120
121
*<notextile>__init__</notextile>*(_self_, *loger*=@None@, *use_sessmatch*=@False@)
122
 Constructs a new ConnectorDirect.
123
 
124
>+_logger_+:
125
126
 The logger which will be used for this MSRP connection. See the [[SipMSRPApi#Logging|Logging]] section for more information.
127
 
128
>+_use_sessmatch_+:
129
130
 Indicates if the connector should use the session matching mechanism defined by the "sessmatch draft version 10":http://tools.ietf.org/html/draft-ietf-simple-msrp-sessmatch-10
131
132
*prepare*(_self_, *local_uri*=@None@)
133
 This method returns a full local path - list of @protocol.URI@ instances, suitable to be put in SDP @'a:path'@ attribute.
134
 
135
>+_local_uri_+:
136
137
 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
138
 will be automatically generated
139
 
140
*complete*(_self_, *full_remote_path*)
141
 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.
142
 
143
>+_full_remote_path_+:
144
145
 A list of @protocol.URI@ instances, obtained by parsing @'a:path'@ put in SDP by the remote party.
146
147
*cleanup*(_self_)
148
 This method cleans up after @prepare()@; it should be called if @complete()@ will not be called for whatever reason.
149
150
151
h2. AcceptorDirect
152
153
154
This class is implemented in the @msrplib.connect@ module and is used for inbound MSRP connections without using a relay.
155
156
157
h4. methods
158
159
160
*<notextile>__init__</notextile>*(_self_, *loger*=@None@, *use_sessmatch*=@False@)
161
>Constructs a new AcceptorDirect.
162
 
163
>+_logger_+:
164
165
 The logger which will be used for this MSRP connection. See the [[SipMSRPApi#Logging|Logging]] section for more information.
166
 
167
>+_use_sessmatch_+:
168
169
 Indicates if the connector should use the session matching mechanism defined by the "sessmatch draft version 10":http://tools.ietf.org/html/draft-ietf-simple-msrp-sessmatch-10
170
171
*prepare*(_self_, *local_uri*=@None@)
172
 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.
173
 
174
>+_local_uri_+:
175
176
 This attribute will be used to construct the local path and to listen for incomming connections. If not provided, one
177
 will be automatically generated. Note that the object may be changed in place: for example, if the port specified is 0,
178
 a random one will be selected and the object will be updated accordingly.
179
 
180
*complete*(_self_, *full_remote_path*)
181
 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.
182
 
183
>+_full_remote_path_+:
184
185
 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.
186
187
*cleanup*(_self_)
188
 This method cleans up after @prepare()@; it should be called if @complete()@ will not be called for whatever reason.
189
190
191
192
h2. RelayConnection
193
194
195
This class is implemented in the @msrplib.connect@ module and is used for inbound and outbound MSRP connections using a relay.
196
197
198
h4. methods
199
200
201
*<notextile>__init__</notextile>*(_self_, *relay*, *mode*, *loger*=@None@, *use_sessmatch*=@False@)
202
 Constructs a new RelayConnection.
203
 
204
>+_relay_+:
205
206
 An instance of @MSRPRelaySettings@ which identifies the relay which is to be used.
207
 
208
>+_mode_+:
209
210
 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.
211
 
212
>+_logger_+:
213
214
 The logger which will be used for this MSRP connection. See the [[SipMSRPApi#Logging|Logging]] section for more information.
215
 
216
>+_use_sessmatch_+:
217
218
 Indicates if the connector should use the session matching mechanism defined by the "sessmatch draft version 10":http://tools.ietf.org/html/draft-ietf-simple-msrp-sessmatch-10
219
220
*prepare*(_self_, *local_uri*=@None@)
221
 This method returns a full local path - list of @protocol.URI@ instances, suitable to be put in SDP @'a:path'@ attribute.
222
 
223
>+_local_uri_+:
224
225
 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
226
 will be automatically generated
227
 
228
*complete*(_self_, *full_remote_path*)
229
 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.
230
 
231
>+_full_remote_path_+:
232
233
 A list of @protocol.URI@ instances, obtained by parsing @'a:path'@ put in SDP by the remote party.
234
235
*cleanup*(_self_)
236
 This method cleans up after @prepare()@; it should be called if @complete()@ will not be called for whatever reason.
237
238
239
240
h2. MSRPTransport
241
242
243
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.
244
245
246
h4. methods
247
248
249
*make_chunk*(_self_, *transaction_id*=@None@, *method*=@'SEND'@, *code*=@None@, *comment*=@None@, *data*=@''@, *contflag*=@None@, *start*=@1@, *end*=@None@, *length*=@None@, *message_id*=@None@)
250
 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.
251
 
252
>+_transaction_id_+:
253
254
 The transaction id which will be put in the chunk. If it is not provided, one will be randomly generated.
255
 
256
>+_method_+:
257
258
 The method of the new MSRP request, or @None@ if a response is required.
259
 
260
>+_code_+:
261
262
 The code of the new MSRP response, or @None@ if a request is required.
263
 
264
>+_code_+:
265
266
 The comment of the new MSRP response, or @None@ if a request is required or a comment on the response is not.
267
 
268
>+_data_+:
269
270
 The payload of the new chunk, or an empty string if no payload is required.
271
 
272
>+_contflag_+:
273
274
 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.
275
 
276
>+_start_+:
277
278
 The first byte's index within the whole message payload this chunk will carry as its own payload.
279
 
280
>+_end_+:
281
282
 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@.
283
 
284
>+_length_+:
285
286
 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.
287
 
288
>+_message_id_+:
289
290
 The ID of the message this chunk is part of. If it is not provided, one will be randomly generated.
291
292
*write_chunk*(_self_, *chunk*, *wait*=@True@)
293
 Writes the chunk provided to the underlying socket.
294
 
295
>+_chunk_+:
296
297
 The chunk which is to be written, an instance of @MSRPData@.
298
 
299
>+_wait_+:
300
301
 If @True@, waits for the operation to complete.
302
303
*write_response*(_self_, *chunk*, *code*, *comment*, *wait*=@True@):
304
 Creates a response which is suitable as a reply to the specified chunk and writes it to the underlying socket.
305
 
306
>+_chunk_+:
307
308
 The chunk for which to create a response.
309
 
310
>+_code_+:
311
312
 The status code of the response which is to be created.
313
 
314
>+_comment_+:
315
316
 The comment of the response which is to be created.
317
 
318
>+_wait_+:
319
320
 If @True@, waits for the operation to complete.
321
322
*read_chunk*(_self_, *size*=@None@)
323
 Waits for a new chunk and returns it.
324
 If there was an error, closes the connection and raises @ChunkParseError@.
325
326
 In case of unintelligible input, loses the connection and returns @None@.
327
 When the connection is closed, raises the reason of the closure (e.g. @ConnectionDone@).
328
329
 If the data already read exceeds _size_, stops reading the data and returns
330
 a "virtual" chunk, i.e. the one that does not actually correspond the the real
331
 MSRP chunk. Such chunks have Byte-Range header changed to match the number of
332
 bytes read and continuation that is @'+'@; they also possess @segment@ attribute,
333
 an integer, starting with 1 and increasing with every new segment of the chunk.
334
335
 Note, that _size_ only hints when to interrupt the segment but does not affect
336
 how the data is read from socket. You may have segments bigger than _size_ and it's
337
 legal to set _size_ to zero (which would mean return a chunk as long as you get
338
 some data, regardless how small).
339
 
340
 
341
>+_size_+:
342
343
 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.
344
345
*check_incoming_SEND_chunk*(_self_, *chunk*)
346
 Checks the 'To-Path' and 'From-Path' of the incoming SEND chunk.
347
 Returns None is the paths are valid for this connection.
348
 If an error is detected an MSRPError is created and returned.
349
350
351
h2. MSRPData
352
353
354
This class is implemented in the @msrplib.protocol@ module and represents an MSRP chunk, either a request or a response.
355
356
357
h4. attributes
358
359
360
 The following attributes are read-only.
361
362
*content_type*
363
>The MIME type of the payload carried by this chunk, which is stored in the Content-Type header.
364
365
*message_id*
366
>The ID of the message this chunk is part of, which is stored in the Message-ID header.
367
368
*byte_range*
369
>A 3-tuple containing the start, end and length values (in this order) from the Byte-Range header.
370
371
*status*
372
>The value of the Status header.
373
374
*failure_report*
375
>The value of the Failure-Report header, whether it exists or it is implied; one of @'yes'@, @'no'@, @'partial'@.
376
377
*success_report*
378
>The value of the Success-Report header, whether it exists or it is implied; one of @'yes'@ or @'no'@.
379
380
*size*
381
>The size of the payload of this chunk, in bytes.
382
383
384
h4. methods
385
386
387
*<notextile>__init__</notextile>*(_self_, *transaction_id*, *method*=@None@, *code*=@None@, *comment*=@None@, *headers*=@None@, *data*=@''@, *contflag*=@'$'@)
388
>Initializes a new MSRPData instance. All the arguments are also available as attributes.
389
  
390
>+_transaction_id_+:
391
392
>The transaction identified of this chunk.
393
  
394
>+_method_+:
395
396
>The method of this chunk if it a request and @None@ if it is a response.
397
  
398
>+_code_+:
399
400
>The status code of this chunk if it is a response and @None@ if it is a request.
401
  
402
>+_comment_+:
403
404
>The comment of this chunk if it is a response and @None@ if it is a request.
405
  
406
>+_headers_+:
407
408
>A dict containing the headers which are to be added to this chunk, or @None@ if no headers are to be added.
409
  
410
>+_data_+:
411
412
>The payload of this chunk, or an empty string if no payload is to be added.
413
  
414
>+_contflag_+:
415
416
>The MSRP continuation flag of this chunk, one of @'$'@, @'#'@ or @'+'@.
417
418
*copy*(_self_)
419
>Returns a new MSRPData instance with exactly the same attributes as this object.
420
421
*add_header*(_self_, *header*)
422
>Add a MSRP header to this chunk.
423
  
424
>+_header_+:
425
426
>The header object which is to be added to this chunk.
427
428
*verify_headers*(_self_)
429
>Verifies that the chunk contains a To-Path and a From-Path header and that all the headers are valid.
430
431
*encode_start*(_self_)
432
>Returns a string containing the MSRP header of this chunk.
433
434
*encode_end*(_self_, *continuation*)
435
>Returns a string containing the MSRP end line of this chunk: 7 commas followed by the transaction identifier and the continuation flag specified.
436
  
437
>+_continuation_+:
438
439
>The continuation flag which is to be used in the end line.
440
441
*encode*(_self_):
442
>Returns a string containing the whole of this MSRP chunk.
443
444
445
446
h2. OutgoingFile
447
448
449
This class is implemented in the @msrplib.session@ module and represents a file which is to be sent via MSRP.
450
451
452
h4. attributes
453
454
455
*headers*
456
>A dict which maps header names to header objects. These headers will be sent in the MSRP message used to send the file.
457
458
459
h4. methods
460
461
462
*<notextile>__init__</notextile>*(_self_, *fileobj*, *size*, *content_type*=@None@, *position*=@0@, *message_id*=@None@)
463
>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.
464
  
465
>+_fileobj_+:
466
467
>A file-like object which will be used for reading the data to be sent.
468
  
469
>+_size_+:
470
471
>The size of the whole file.
472
  
473
>+_content_type_+:
474
475
>The MIME type associated with the file's data. If provided, it will be added as a Content-Type header.
476
  
477
>+_position_+:
478
479
>The position within the file from which data will be sent. The file object must already be seeked to this position.
480
  
481
>+_message_id_+:
482
483
>The message ID which will be used for sending this file.
484
485
486
h2. MSRPSession
487
488
489
This class is implemented in the @msrplib.session@ module and provides a high level API for MSRP sessions.
490
491
492
h4. methods
493
494
495
*<notextile>__init__</notextile>*(_self_, *msrptransport*, *accept_types*=@['*']@, *on_incoming_cb*=@None@)
496
 Initializes MSRPSession instance over the specified transport. The incoming chunks are reported through the _on_incoming_cb_ callback.
497
 
498
>+_msrptransport_+:
499
500
 An instance of @MSRPTransport@ over which this session will operate.
501
 
502
>+_accept_types_+:
503
504
 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.
505
 
506
>+_on_incoming_cb_+:
507
508
 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.
509
510
*send_chunk*(_self_, *chunk*, *response_cb*=@None@)
511
 Sends the specified chunk and reports the result via the _response_cb_ callback.
512
513
 When _response_cb_ argument is present, it will be used to report
514
 the transaction response to the caller. When a response is received or generated
515
 locally, _response_cb_ is called with one argument. The function must do something
516
 quickly and must not block, because otherwise it would block the reader greenlet.
517
518
 If no response was received after @RESPONSE_TIMEOUT@ seconds,
519
* 408 response is generated if Failure-Report was @'yes'@ or absent
520
* 200 response is generated if Failure-Report was @'partial'@ or @'no'@
521
522
 Note that it's rather wasteful to provide _response_cb_ argument other than @None@
523
 for chunks with Failure-Report='no' since it will always fire 30 seconds later
524
 with 200 result (unless the other party is broken and ignores Failure-Report header)
525
526
 If sending is not possible, @MSRPSessionError@ is raised.
527
528
 
529
>+_chunk_+:
530
531
 The chunk which is to be sent. It must be an instance of @MSRPData@ which represents a request.
532
 
533
>+_response_cb_+:
534
535
 A function receiving a single argument which will be the response received for the sent chunk.
536
537
*deliver_chunk*(_self_, *chunk*, *event*=@None@)
538
 Sends the specified chunk and waits for the transaction response (if Failure-Report header is not @'no'@).
539
 Returns the transaction response if it's a success or raise @MSRPTransactionError@ if it's not.
540
541
 If chunk's Failure-Report is @'no'@, returns @None@ immediately.
542
543
 
544
>+_chunk_+:
545
546
 The chunk which is to be sent. It must be an intance of @MSRPData@ which represents a request.
547
 
548
>+_event_+:
549
550
 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.
551
 
552
*send_file*(_self_, *outgoing_file*)
553
 Adds the specified file to the queue of files to be sent. The method returns immediately.
554
 
555
>+_outgoing_file_+:
556
557
 An instance of @OutgoingFile@ which represents the file to be sent.
558
559
*shutdown*(_self_, *sync*=@True@)
560
 Sends the messages already in queue then closes the connection.
561
562
563
h2. MSRPServer
564
565
566
This class is implemented in the @msrplib.connect@ module.
567
    
568
MSRPServer solves the problem with AcceptorDirect: concurrent using of 2
569
or more AcceptorDirect instances on the same non-zero port is not possible.
570
If you initialize() those instances, one after another, one will listen on
571
the socket and another will get BindError.
572
573
MSRPServer avoids the problem by sharing the listening socket between multiple connections.
574
It has a slightly different interface from AcceptorDirect, so it cannot be considered a drop-in
575
replacement.
576
577
It manages the listening sockets and binds incoming requests.
578
579
580
h4. methods
581
582
583
*<notextile>__init__</notextile>*(_self_, *logger*)
584
 Constructs a new MSRPServer which will be using the specifed logger for its connections.
585
 
586
>+_logger_+:
587
588
 The default logger which will be used for this object's MSRP connections. See the [[SipMSRPApi#Logging|Logging]] section for more information.
589
590
*prepare*(_self_, _local_uri_=@None@, _logger_=@None@)
591
 Starts a listening port specified by _local_uri_ if there isn't one on that port/interface already.
592
 Adds _local_uri_ to the list of expected URIs, so that incoming connections featuring this URI won't be rejected.
593
 If _logger_ is provided, it uses it for this connection instead of the one specified in @__init___@.
594
 
595
>+_local_uri_+:
596
597
 The URI which will be added to the list of expected URIs. Connections from this URI will be accepted.
598
 
599
>+_logger_+:
600
601
 The logger which will be used for connections from the specified URI. See the [[SipMSRPApi#Logging|Logging]] section for more information.
602
603
*complete*(_self_, _full_remote_path_)
604
 Waits until one of the incoming connections binds using the provided _full_remote_path_.
605
 Returns the connected and bound @MSRPTransport@ instance.
606
 If no such binding was made within @MSRPBindSessionTimeout.seconds@, @MSRPBindSessionTimeout@ is raised.
607
 
608
>+_full_remote_path_+:
609
610
 A list of @protocol.URI@ instances, obtained by parsing @'a:path'@ put in SDP by the remote party.
611
612
*cleanup*(_self_, _local_uri_)
613
 Removes _local_uri_ from the list of expected URIs.
614
 
615
>+_local_uri_+:
616
617
 The URI which is to be removed from the list of expected URIs.
618
619
620
h2. Headers
621
622
623
The MSRP headers are represented using objects from the @msrplib.protocol module@. All MSRP header object provide three properties:
624
 _name_:
625
626
 The name of the header, as it appears in MSRP chunks.
627
 
628
>+_encode_+:
629
630
 The MSRP encoded header value, as it appears in MSRP chunks.
631
 
632
>+_decode_+:
633
634
 The high-level object representing the header value.
635
636
All headers can be constructed by passing either the encoded or decoded objects to @__init__@. The following headers are provided:
637
638
639
h3. ToPathHeader
640
641
642
The name of the header is @'To-Path'@ and the decoded value is a deque of @URI@ objects.
643
644
645
h3. FromPathHeader
646
647
648
The name of the header is @'From-Path'@ and the decoded value is a deque of @URI@ objects.
649
650
651
h3. MessageIDHeader
652
653
654
The name of the header is @'Message-ID'@ and the decoded value is the string containing the message ID.
655
656
657
h3. SuccessReportHeader
658
659
660
The name of the header is @'Success-Report'@ and the decoded value is one of @'yes'@ or @'no'@.
661
662
663
h3. FailureReportHeader
664
665
666
The name of the header is @'Failure-Report'@ and the decoded value is one of @'yes'@, @'partial'@ or @'no'@.
667
668
669
h3. ByteRangeHeader
670
671
672
The name of the header is @'Byte-Range'@ and the decoded value is a 3-tuple containing the start, end and length values.
673
674
675
h4. attributes
676
677
678
*fro*
679
>The start value of the header: the index within the whole message payload where a chunk's payload starts.
680
*end*
681
>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.
682
*length*
683
>The total size of the message payload.
684
685
686
h3. StatusHeader
687
688
689
The name of the header is @'Status'@ and the decoded value is a 2-tuple containing the status code and comment.
690
691
692
h4. attributes
693
694
695
*code*
696
>The code component of the header.
697
*comment*
698
>The comment component of the header.
699
700
701
h3. ExpiresHeader
702
703
704
The name of the header is @'Expires'@ and the decoded value is an integer.
705
706
707
h3. MinExpiresHeader
708
709
710
The name of the header is @'Min-Expires'@ and the decoded value is an integer.
711
712
713
h3. MaxExpiresHeader
714
715
716
The name of the header is @'Max-Expires'@ and the decoded value is an integer.
717
718
719
h3. UsePathHeader
720
721
722
The name of the header is @'Use-Path'@ and the decoded value is a deque of @URI@ objects.
723
724
725
h3. WWWAuthenticateHeader
726
727
728
The name of the header is @'WWW-Authenticate'@ and the decoded value is a dictionary of the parameters within the header.
729
730
731
h3. AuthorizationHeader
732
733
734
The name of the header is @'Authorization'@ and the decoded value is a dictionary of the parameters within the header.
735
736
737
h3. AuthenticationInfoHeader
738
739
740
The name of the header is @'Authentication-Info'@ and the decoded value is a dictionary of the parameters within the header.
741
742
743
h3. ContentTypeHeader
744
745
746
The name of the header is @'Content-Type'@ and the decoded value is the string representing the MIME type.
747
748
749
h3. ContentIDHeader
750
751
752
The name of the header is @'Content-ID'@ and the decoded value is the string with the value of the header.
753
754
755
h3. ContentDescriptionHeader
756
757
758
The name of the header is @'Content-Description'@ and the decoded value is the string with the value of the header.
759
760
761
h3. ContentDispositionHeader
762
763
764
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.
765
766
767
768
h2. Logging
769
770
771
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:
772
773
774
h4. methods
775
776
777
*received_new_chunk*(*data*, *transport*, *chunk*)
778
>This method is called when the start of a new chunk is received.
779
  
780
>+_data_+:
781
782
>The data which came along with the start of the chunk.
783
  
784
>+_transport_+:
785
786
>The @MSRPTransport@ instance on which the chunk was received.
787
  
788
>+_chunk_+:
789
790
>The actual chunk object.
791
792
*received_chunk_data*(*data*, *transport*, *transaction_id*)
793
>This method is called when data is received as part of a chunk previously announced via *received_new_chunk*.
794
  
795
>+_data_+:
796
797
>The data received as part of the chunk.
798
  
799
>+_transport_+:
800
801
>The @MSRPTransport@ instance on which the chunk was received.
802
  
803
>+_transaction_id_+:
804
805
>The transaction ID which identifies the chunk for which data was received.
806
807
*received_chunk_end*(*data*, *transport*, *transaction_id*)
808
>This method is called when the last data of a chunk is received. The chunk was previously announced via *received_new_chunk*.
809
  
810
>+_data_+:
811
812
>The last data received as part of the chunk.
813
  
814
>+_transport_+:
815
816
>The @MSRPTransport@ instance on which the chunk was received.
817
  
818
>+_transaction_id_+:
819
820
>The transaction ID which identifies the chunk which was ended.
821
822
*sent_new_chunk*(*data*, *transport*, *chunk*)
823
>This method is called when the start of a new chunk is sent.
824
  
825
>+_data_+:
826
827
>The data which was sent along with the start of the chunk.
828
  
829
>+_transport_+:
830
831
>The @MSRPTransport@ instance on which the chunk was sent.
832
  
833
>+_chunk_+:
834
835
>The actual chunk object.
836
837
*sent_chunk_data*(*data*, *transport*, *transaction_id*)
838
>This method is called when data is sent as part of a chunk previously announced via *sent_new_chunk*.
839
  
840
>+_data_+:
841
842
>The data sent as part of the chunk.
843
  
844
>+_transport_+:
845
846
>The @MSRPTransport@ instance on which the chunk was sent.
847
  
848
>+_transaction_id_+:
849
850
>The transaction ID which identifies the chunk for which data was sent.
851
852
*sent_chunk_end*(*data*, *transport*, *transaction_id*)
853
>This method is called when the last data of a chunk is sent. The chunk was previously announced via *sent_new_chunk*.
854
  
855
>+_data_+:
856
857
>The last data sent as part of the chunk.
858
  
859
>+_transport_+:
860
861
>The @MSRPTransport@ instance on which the chunk was sent.
862
  
863
>+_transaction_id_+:
864
865
>The transaction ID which identifies the chunk which was ended.
866
867
*debug*(*message*)
868
>This method is called when a debug level message is sent by the library.
869
870
*info*(*message*)
871
>This method is called when a info level message is sent by the library.
872
873
*warn*(*message*)
874
>This method is called when a warning level message is sent by the library.
875
876
*error*(*message*)
877
>This method is called when a error level message is sent by the library.
878
879
*fatal*(*message*)
880
>This method is called when a fatal level message is sent by the library.
881
882
883
h2. Examples
884
885
886
887
h3. Creating an outbound connection
888
889
890
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:
891
892
<pre>
893
from msrplib.connect import ConnectorDirect
894
from msrplib.session import MSRPSession
895
896
connector = ConnectorDirect()
897
full_local_path = connector.prepare()
898
try:
899
    # put full_local_path in SDP 'a:path' attribute of offer
900
    # get full_remote_path from remote's 'a:path: attribute of reply
901
    msrp_transport = connector.complete(full_remote_path)
902
except:
903
    connector.cleanup()
904
    raise
905
906
def handle_incoming(chunk=None, error=None):
907
    if error is not None:
908
       print 'Error: %s' % error
909
       session.shutdown()
910
    elif chunk is not None:
911
       print 'Got data: %s' % chunk.data
912
913
session = MSRPSession(msrp_transport, on_incoming_cb=handle_incoming)
914
session.send_chunk(msrp_transport.make_chunk(data='Hello world!'))
915
</pre>
916
917
918
h3. Waiting for an inbound connection
919
920
921
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:
922
923
<pre>
924
from msrplib.connect import RelayConnection, MSRPRelaySettings
925
from msrplib.session import MSRPSession
926
927
relay = MSRPRelaySettings(domain='example.org', username='user', password='pass')
928
connector = RelayConnection(relay, 'passive')
929
full_local_path = connector.prepare()
930
try:
931
    # get full_remote_path from remote's 'a:path: attribute of offer
932
    # put full_local_path in SDP 'a:path' attribute of reply
933
    msrp_transport = connector.complete(full_remote_path)
934
except:
935
    connector.cleanup()
936
    raise
937
938
def handle_incoming(chunk=None, error=None):
939
    if error is not None:
940
       print 'Error: %s' % error
941
       session.shutdown()
942
    elif chunk is not None:
943
       print 'Got data: %s' % chunk.data
944
945
session = MSRPSession(msrp_transport, on_incoming_cb=handle_incoming)
946
session.send_chunk(msrp_transport.make_chunk(data='Hello world!'))
947
</pre>