SipMSRPApi

Version 38 (Adrian Georgescu, 04/13/2010 10:16 am)

1 1 Adrian Georgescu
= MSRP API =
2 1 Adrian Georgescu
3 34 Adrian Georgescu
[[TOC(SipMSRPApi, SipDeveloperGuide, depth=3)]]
4 1 Adrian Georgescu
5 31 Adrian Georgescu
Message Session Relay Protocol (MSRP) is a protocol for transmitting a series of related Instant Messages in the context of a session. Message sessions are treated like any other media stream when set up via a rendezvous or session creation protocol such as the Session Initiation Protocol (SIP). 
6 1 Adrian Georgescu
7 25 Adrian Georgescu
 * MSRP sessions are defined in [http://tools.ietf.org/html/rfc4975 RFC 4975]
8 25 Adrian Georgescu
 * MSRP relay extension used for NAT traversal of instant messaging and file transfer sessions is defined in [http://tools.ietf.org/html/rfc4976 RFC 4976]
9 1 Adrian Georgescu
10 32 Adrian Georgescu
The MSRP protocol stack is implemented by [http://devel.ag-projects.com/cgi-bin/darcsweb.cgi?r=python-msrplib;a=summary msrplib] Python package. 
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 1 Adrian Georgescu
 '''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 1 Adrian Georgescu
 Defines means to establish a connection, bind it, and provide an initialized {{{MSRPTransport}}} instance.
21 1 Adrian Georgescu
22 1 Adrian Georgescu
 '''msrplib.session'''::
23 1 Adrian Georgescu
 Defines {{{MSRPSession}}} class, which provides high level control over a MSRP connection.
24 1 Adrian Georgescu
25 1 Adrian Georgescu
 '''msrplib.protocol'''::
26 27 Adrian Georgescu
 Provides representation and parsing of MSRP entities - chunks and MSRP URIs.
27 1 Adrian Georgescu
28 1 Adrian Georgescu
 '''msrplib.trafficlog'''::
29 1 Adrian Georgescu
 Defines {{{Logger}}} class that is used through out the library to log the connection state.
30 1 Adrian Georgescu
  
31 1 Adrian Georgescu
32 38 Adrian Georgescu
== Components ==
33 3 Oliver Bril
34 38 Adrian Georgescu
=== a connector or acceptor ===
35 4 Oliver Bril
36 3 Oliver Bril
 {{{msrplib.connect}}} provides 2 connectors (with and without relay) and 2 acceptors (likewise, with or without relay). All of them have the exact same interface,
37 8 Oliver Bril
38 8 Oliver Bril
 '''prepare'''(''self'', ''local_uri''={{{None}}})::
39 1 Adrian Georgescu
 {{{prepare}}} returns a full local path - list of {{{protocol.URI}}} instances, suitable to be put in SDP {{{'a:path'}}} attribute.
40 35 Adrian Georgescu
41 35 Adrian Georgescu
 Depending on type of the connector, use local_uri to prepare the MSRP connection, which means:
42 8 Oliver Bril
 * connecting and authenticating at the relay if a relay is used ({{{ConnectorRelay}}} and {{{AcceptorRelay}}})
43 8 Oliver Bril
 * starts listening on a local port for DirectAcceptor
44 27 Adrian Georgescu
45 1 Adrian Georgescu
 ''local_uri'' is used to specify the connection parameters, e.g. local port and local ip.
46 8 Oliver Bril
 If not provided, suitable ''local_uri'' will be generated.
47 1 Adrian Georgescu
 ''local_uri'' maybe updated in place by {{{prepare()}}} method if the real settings used are different from those specified.
48 8 Oliver Bril
49 1 Adrian Georgescu
 
50 8 Oliver Bril
 '''complete'''(''self'', ''full_remote_path'')::
51 1 Adrian Georgescu
 {{{complete}}} returns {{{transport.MSRPTransport}}} instance, ready to read and send chunks.
52 1 Adrian Georgescu
53 35 Adrian Georgescu
 Completes the establishment of the MSRP connection, which means:
54 35 Adrian Georgescu
 * establishes the connection if it wasn't already established ({{{ConnectorDirect}}})
55 27 Adrian Georgescu
 * binds the connection, i.e. exchanges empty chunk to verify each other's From-Path and To-Path
56 27 Adrian Georgescu
57 8 Oliver Bril
 ''full_remote_path'' should be a list of {{{protocol.URI}}} instances, obtained by parsing {{{'a:path'}}} put in SDP by the remote party.
58 8 Oliver Bril
59 8 Oliver Bril
60 9 Oliver Bril
 '''cleanup'''(''self'')::
61 27 Adrian Georgescu
 Calls this method to cleanup after {{{initialize()}}} if it's impossible to call {{{complete()}}}
62 8 Oliver Bril
63 38 Adrian Georgescu
=== transport.MSRPTransport ===
64 4 Oliver Bril
65 1 Adrian Georgescu
Low level access to the MSRP connection.
66 27 Adrian Georgescu
67 1 Adrian Georgescu
 '''make_chunk'''(''self'', ''transaction_id''={{{None}}}, ''method''={{{'SEND'}}}, ''code''={{{None}}}, ''comment''={{{None}}}, ''data''={{{''}}}, ''contflag''={{{None}}}, ''start''={{{1}}}, ''end''={{{None}}}, ''length''={{{None}}}, ''message_id''={{{None}}})::
68 1 Adrian Georgescu
 Makes a new chunk ({{{protocol.MSRPData}}} instance) with proper {{{From-Path}}}, {{{To-Path}}}, {{{Byte-Range}}} and {{{Message-ID}}} headers set up based on MSRPTransport's state and the parameters provided. Use ''data'' for payload, and ''start''/''end''/''length'' to generate {{{Byte-Range}}} header. Generate new random strings for default values of ''transaction_id'' and ''message_id''. 
69 27 Adrian Georgescu
 [[BR]]''contflag'':[[BR]]
70 1 Adrian Georgescu
 MSRP chunk's continuation flag ({{{'$'}}}, {{{'+'}}} or {{{'#'}}}). Default is {{{'$'}}}, unless you have a partial {{{SEND}}} chunk, in which case it is {{{'+'}}}
71 1 Adrian Georgescu
72 1 Adrian Georgescu
 '''write'''(''self'', ''bytes'', ''sync''={{{True}}})::
73 14 Oliver Bril
 Writes ''bytes'' to the socket. If ''sync'' is true, waits for an operation to complete.
74 27 Adrian Georgescu
75 14 Oliver Bril
 '''read_chunk'''(''self'', ''size''={{{None}}})::
76 14 Oliver Bril
 Waits for a new chunk and returns it.
77 27 Adrian Georgescu
 If there was an error, closes the connection and raises {{{ChunkParseError}}}.
78 27 Adrian Georgescu
79 14 Oliver Bril
 In case of unintelligible input, loses the connection and returns {{{None}}}.
80 1 Adrian Georgescu
 When the connection is closed, raises the reason of the closure (e.g. {{{ConnectionDone}}}).
81 27 Adrian Georgescu
82 27 Adrian Georgescu
 If the data already read exceeds ''size'', stops reading the data and returns
83 14 Oliver Bril
 a "virtual" chunk, i.e. the one that does not actually correspond the the real
84 27 Adrian Georgescu
 MSRP chunk. Such chunks have Byte-Range header changed to match the number of
85 14 Oliver Bril
 bytes read and continuation that is {{{'+'}}}; they also possess {{{segment}}} attribute,
86 14 Oliver Bril
 an integer, starting with 1 and increasing with every new segment of the chunk.
87 14 Oliver Bril
88 14 Oliver Bril
 Note, that ''size'' only hints when to interrupt the segment but does not affect
89 14 Oliver Bril
 how the data is read from socket. You may have segments bigger than ''size'' and it's
90 1 Adrian Georgescu
 legal to set ''size'' to zero (which would mean return a chunk as long as you get
91 1 Adrian Georgescu
 some data, regardless how small).
92 1 Adrian Georgescu
93 14 Oliver Bril
 '''check_incoming_SEND_chunk'''(''self'', ''chunk'')::
94 1 Adrian Georgescu
 Checks the 'To-Path' and 'From-Path' of the incoming SEND chunk.
95 15 Oliver Bril
 Returns None is the paths are valid for this connection.
96 27 Adrian Georgescu
 If an error is detected an MSRPError is created and returned.
97 27 Adrian Georgescu
98 38 Adrian Georgescu
=== session.MSRPSession ===
99 1 Adrian Georgescu
100 4 Oliver Bril
 '''!__init!__'''(''self'', ''msrptransport'', ''accept_types''={{{['*']}}}, ''on_incoming_cb''={{{None}}})::
101 1 Adrian Georgescu
 Initializes MSRPSession instance. Reports the incoming chunks through ''on_incoming_cb'' callback.
102 15 Oliver Bril
103 27 Adrian Georgescu
 '''send_chunk'''(''self'', ''chunk'', ''response_cb''={{{None}}})::
104 1 Adrian Georgescu
 Sends ''chunk''. Report the result via ''response_cb''.
105 15 Oliver Bril
106 27 Adrian Georgescu
 When ''response_cb'' argument is present, it will be used to report
107 15 Oliver Bril
 the transaction response to the caller. When a response is received or generated
108 15 Oliver Bril
 locally, ''response_cb'' is called with one argument. The function must do something
109 15 Oliver Bril
 quickly and must not block, because otherwise it would block the reader greenlet.
110 15 Oliver Bril
111 29 Oliver Bril
 If no response was received after {{{RESPONSE_TIMEOUT}}} seconds,
112 15 Oliver Bril
 * 408 response is generated if Failure-Report was {{{'yes'}}} or absent
113 15 Oliver Bril
 * 200 response is generated if Failure-Report was {{{'partial'}}} or {{{'no'}}}
114 15 Oliver Bril
115 1 Adrian Georgescu
 Note that it's rather wasteful to provide ''response_cb'' argument other than {{{None}}}
116 1 Adrian Georgescu
 for chunks with Failure-Report='no' since it will always fire 30 seconds later
117 1 Adrian Georgescu
 with 200 result (unless the other party is broken and ignores Failure-Report header)
118 1 Adrian Georgescu
119 1 Adrian Georgescu
 If sending is impossible raise {{{MSRPSessionError}}}.
120 1 Adrian Georgescu
121 1 Adrian Georgescu
 '''deliver_chunk'''(''self'', ''chunk'', ''event''={{{None}}})::
122 1 Adrian Georgescu
 Sends the chunk, waits for the transaction response (if Failure-Report header is not {{{'no'}}}).
123 1 Adrian Georgescu
 Returns the transaction response if it's a success, raise {{{MSRPTransactionError}}} if it's not.
124 1 Adrian Georgescu
125 1 Adrian Georgescu
 If chunk's Failure-Report is {{{'no'}}}, returns {{{None}}} immediately.
126 1 Adrian Georgescu
127 1 Adrian Georgescu
 '''shutdown'''(''self'', ''sync''={{{True}}})::
128 1 Adrian Georgescu
 Sends the messages already in queue then close the connection.
129 1 Adrian Georgescu
130 38 Adrian Georgescu
=== session.GreenMSRPSession ===
131 1 Adrian Georgescu
132 1 Adrian Georgescu
 A subclass of MSRPSession that delivers the incoming messages to the queue.
133 1 Adrian Georgescu
134 1 Adrian Georgescu
 '''!__init!__'''(''self'', ''msrptransport'', ''accept_types''={{{['*']}}})::
135 1 Adrian Georgescu
 Initializes GreenMSRPSession instance. The messages will be delivered to the queue (available as {{{incoming}}} attribute).
136 1 Adrian Georgescu
137 1 Adrian Georgescu
 '''receive_chunk'''(''self'')::
138 1 Adrian Georgescu
 Returns a message from the queue.
139 1 Adrian Georgescu
140 38 Adrian Georgescu
=== connect.MSRPServer ===
141 1 Adrian Georgescu
142 1 Adrian Georgescu
 Manages the listening sockets. Binds incoming requests.
143 1 Adrian Georgescu
    
144 1 Adrian Georgescu
 MSRPServer solves the problem with AcceptorDirect: concurrent using of 2
145 1 Adrian Georgescu
 or more AcceptorDirect instances on the same non-zero port is not possible.
146 1 Adrian Georgescu
 If you initialize() those instances, one after another, one will listen on
147 1 Adrian Georgescu
 the socket and another will get BindError.
148 1 Adrian Georgescu
149 1 Adrian Georgescu
 MSRPServer avoids the problem by sharing the listening socket between multiple connections.
150 1 Adrian Georgescu
 It has a slightly different interface from AcceptorDirect, so it cannot be considered a drop-in
151 1 Adrian Georgescu
 replacement.
152 1 Adrian Georgescu
153 1 Adrian Georgescu
 '''prepare'''(''self'', ''local_uri''={{{None}}}, ''logger''={{{None}}})::
154 1 Adrian Georgescu
 Starts a listening port specified by ''local_uri'' if there isn't one on that port/interface already.
155 1 Adrian Georgescu
 Adds ''local_uri'' to the list of expected URIs, so that incoming connections featuring this URI won't be rejected.
156 1 Adrian Georgescu
 If ''logger'' is provided, it uses it for this connection instead of the default one.
157 1 Adrian Georgescu
158 1 Adrian Georgescu
 '''complete'''(''self'', ''full_remote_path'')::
159 1 Adrian Georgescu
 Waits until one of the incoming connections binds using provided ''full_remote_path''.
160 1 Adrian Georgescu
 Returns connected and bounds the {{{MSRPTransport}}} instance.
161 1 Adrian Georgescu
        
162 15 Oliver Bril
 If no such binding was made within {{{MSRPBindSessionTimeout.seconds}}}, raise {{{MSRPBindSessionTimeout}}}.
163 1 Adrian Georgescu
 ''full_remote_path'' should be a list of {{{protocol.URI}}} instances, obtained by parsing {{{'a:path'}}} put in SDP by the remote party.
164 15 Oliver Bril
165 15 Oliver Bril
 '''cleanup'''(''self'', ''local_uri'')::
166 15 Oliver Bril
 Removes ''local_uri'' from the list of expected URIs.
167 38 Adrian Georgescu
168 38 Adrian Georgescu
== Usage ==
169 38 Adrian Georgescu
170 38 Adrian Georgescu
=== Establish a connection ===
171 38 Adrian Georgescu
172 38 Adrian Georgescu
{{{msrplib.connect}}} provides a number of classes to establish a connection, so the first
173 38 Adrian Georgescu
thing to do is to select which one applies to your situation:
174 38 Adrian Georgescu
175 38 Adrian Georgescu
    1. Calling endpoint, not using a relay ({{{ConnectorDirect}}})
176 38 Adrian Georgescu
    2. Answering endpoint, not using a relay ({{{AcceptorDirect}}})
177 38 Adrian Georgescu
    3. Calling endpoint, using a relay ({{{ConnectorRelay}}})
178 38 Adrian Georgescu
    4. Answering endpoint, using a relay ({{{AcceptorRelay}}})
179 38 Adrian Georgescu
180 38 Adrian Georgescu
The answering endpoint may skip using the relay if sure that it's accessible directly, e.g is not behind a NAT. To be sure it works in any network topology a called end-point should always use a relay.
181 38 Adrian Georgescu
182 38 Adrian Georgescu
The calling endpoint does not need a relay as the protocol mandates that it is establishing an outbound connection which always work from behind a NAT.
183 38 Adrian Georgescu
184 38 Adrian Georgescu
Once you have an instance of the right class (use the convenience functions
185 38 Adrian Georgescu
{{{get_connector()}}} and {{{get_acceptor()}}} to get one), the procedure to establish the
186 38 Adrian Georgescu
connection is the same:
187 38 Adrian Georgescu
188 38 Adrian Georgescu
{{{
189 38 Adrian Georgescu
full_local_path = connector.prepare()
190 38 Adrian Georgescu
try:
191 38 Adrian Georgescu
    ... put full_local_path in SDP 'a:path' attribute
192 38 Adrian Georgescu
    ... get full_remote_path from remote's 'a:path: attribute
193 38 Adrian Georgescu
    ... (the order of the above steps is reversed if you're the
194 38 Adrian Georgescu
    ... answering party, but that does not affect connector's usage)
195 38 Adrian Georgescu
    msrptransport = connector.complete(full_remote_path)
196 38 Adrian Georgescu
finally:
197 38 Adrian Georgescu
    connector.cleanup()
198 38 Adrian Georgescu
}}}
199 38 Adrian Georgescu
200 38 Adrian Georgescu
To customize connection's parameters, creates a new {{{protocol.URI}}} object and passes
201 38 Adrian Georgescu
it to prepare() function, e.g.
202 38 Adrian Georgescu
{{{
203 38 Adrian Georgescu
local_uri = protocol.URI(use_tls=False, port=5000)
204 38 Adrian Georgescu
connector.prepare(local_uri)
205 38 Adrian Georgescu
}}}
206 38 Adrian Georgescu
207 38 Adrian Georgescu
{{{prepare()}}} may update {{{local_uri}}} in place with the actual connection parameters
208 38 Adrian Georgescu
used (e.g. if you specified port=0). 'port' attribute of {{{local_uri}}} is currently
209 38 Adrian Georgescu
only respected by {{{AcceptorDirect}}}.
210 38 Adrian Georgescu
211 38 Adrian Georgescu
Note that, acceptors and connectors are one-use only. Which means, that {{{AcceptorDirect}}}
212 38 Adrian Georgescu
will open a port just to handle one incoming connection and close it right after.
213 38 Adrian Georgescu
If your application behaves more like a server, i.e. opens a port and listens on it
214 38 Adrian Georgescu
constantly, use {{{MSRPServer}}} class.