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