]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/javascript/opensrf.js
LP#1268619: OpenSRF JS websockets plugin
[OpenSRF.git] / src / javascript / opensrf.js
1 /* -----------------------------------------------------------------------
2  * Copyright (C) 2008  Georgia Public Library Service
3  * Bill Erickson <erickson@esilibrary.com>
4  *  
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * ----------------------------------------------------------------------- */
15
16 /* session states */
17 var OSRF_APP_SESSION_CONNECTED = 0;
18 var OSRF_APP_SESSION_CONNECTING = 1;
19 var OSRF_APP_SESSION_DISCONNECTED = 2;
20
21 /* types of transport layers */
22 var OSRF_TRANSPORT_TYPE_XHR = 1;
23 var OSRF_TRANSPORT_TYPE_XMPP = 2;
24 var OSRF_TRANSPORT_TYPE_WS = 3;
25
26 /* message types */
27 var OSRF_MESSAGE_TYPE_REQUEST = 'REQUEST';
28 var OSRF_MESSAGE_TYPE_STATUS = 'STATUS';
29 var OSRF_MESSAGE_TYPE_RESULT = 'RESULT';
30 var OSRF_MESSAGE_TYPE_CONNECT = 'CONNECT';
31 var OSRF_MESSAGE_TYPE_DISCONNECT = 'DISCONNECT';
32
33 /* message statuses */
34 var OSRF_STATUS_CONTINUE = 100;
35 var OSRF_STATUS_OK = 200;
36 var OSRF_STATUS_ACCEPTED = 202;
37 var OSRF_STATUS_COMPLETE = 205;
38 var OSRF_STATUS_REDIRECTED = 307;
39 var OSRF_STATUS_BADREQUEST = 400;
40 var OSRF_STATUS_UNAUTHORIZED = 401;
41 var OSRF_STATUS_FORBIDDEN = 403;
42 var OSRF_STATUS_NOTFOUND = 404;
43 var OSRF_STATUS_NOTALLOWED = 405;
44 var OSRF_STATUS_TIMEOUT = 408;
45 var OSRF_STATUS_EXPFAILED = 417;
46 var OSRF_STATUS_INTERNALSERVERERROR = 500;
47 var OSRF_STATUS_NOTIMPLEMENTED = 501;
48 var OSRF_STATUS_VERSIONNOTSUPPORTED = 505;
49
50 /* The following classes map directly to network-serializable opensrf objects */
51
52 function osrfMessage(hash) {
53     this.hash = hash;
54     if(!this.hash.locale)
55         this.hash.locale = OpenSRF.locale || 'en-US';
56     this._encodehash = true;
57 }
58 osrfMessage.prototype.threadTrace = function(d) { 
59     if(arguments.length == 1) 
60         this.hash.threadTrace = d; 
61     return this.hash.threadTrace; 
62 };
63 osrfMessage.prototype.type = function(d) { 
64     if(arguments.length == 1) 
65         this.hash.type = d; 
66     return this.hash.type; 
67 };
68 osrfMessage.prototype.payload = function(d) { 
69     if(arguments.length == 1) 
70         this.hash.payload = d; 
71     return this.hash.payload; 
72 };
73 osrfMessage.prototype.locale = function(d) { 
74     if(arguments.length == 1) 
75         this.hash.locale = d; 
76     return this.hash.locale; 
77 };
78 osrfMessage.prototype.serialize = function() {
79     return {
80         "__c":"osrfMessage",
81         "__p": {
82             'threadTrace' : this.hash.threadTrace,
83             'type' : this.hash.type,
84             'payload' : (this.hash.payload) ? this.hash.payload.serialize() : 'null',
85             'locale' : this.hash.locale
86         }
87     };
88 };
89
90 function osrfMethod(hash) {
91     this.hash = hash;
92     this._encodehash = true;
93 }
94 osrfMethod.prototype.method = function(d) {
95     if(arguments.length == 1) 
96         this.hash.method = d; 
97     return this.hash.method; 
98 };
99 osrfMethod.prototype.params = function(d) {
100     if(arguments.length == 1) 
101         this.hash.params = d; 
102     return this.hash.params; 
103 };
104 osrfMethod.prototype.serialize = function() {
105     return {
106         "__c":"osrfMethod",
107         "__p": {
108             'method' : this.hash.method,
109             'params' : this.hash.params
110         }
111     };
112 };
113
114 function osrfMethodException(hash) {
115     this.hash = hash;
116     this._encodehash = true;
117 }
118 osrfMethodException.prototype.status = function(d) {
119     if(arguments.length == 1) 
120         this.hash.status = d; 
121     return this.hash.status; 
122 };
123 osrfMethodException.prototype.statusCode = function(d) {
124     if(arguments.length == 1) 
125         this.hash.statusCode = d; 
126     return this.hash.statusCode; 
127 };
128 function osrfConnectStatus(hash) { 
129     this.hash = hash;
130     this._encodehash = true;
131 }
132 osrfConnectStatus.prototype.status = function(d) {
133     if(arguments.length == 1) 
134         this.hash.status = d; 
135     return this.hash.status; 
136 };
137 osrfConnectStatus.prototype.statusCode = function(d) {
138     if(arguments.length == 1) 
139         this.hash.statusCode = d; 
140     return this.hash.statusCode; 
141 };
142 function osrfResult(hash) {
143     this.hash = hash;
144     this._encodehash = true;
145 }
146 osrfResult.prototype.status = function(d) {
147     if(arguments.length == 1) 
148         this.hash.status = d; 
149     return this.hash.status; 
150 };
151 osrfResult.prototype.statusCode = function(d) {
152     if(arguments.length == 1) 
153         this.hash.statusCode = d; 
154     return this.hash.statusCode; 
155 };
156 osrfResult.prototype.content = function(d) {
157     if(arguments.length == 1) 
158         this.hash.content = d; 
159     return this.hash.content; 
160 };
161 function osrfServerError(hash) { 
162     this.hash = hash;
163     this._encodehash = true;
164 }
165 osrfServerError.prototype.status = function(d) {
166     if(arguments.length == 1) 
167         this.hash.status = d; 
168     return this.hash.status; 
169 };
170 osrfServerError.prototype.statusCode = function(d) {
171     if(arguments.length == 1) 
172         this.hash.statusCode = d; 
173     return this.hash.statusCode; 
174 };
175 function osrfContinueStatus(hash) { 
176     this.hash = hash;
177     this._encodehash = true;
178 }
179 osrfContinueStatus.prototype.status = function(d) {
180     if(arguments.length == 1) 
181         this.hash.status = d; 
182     return this.hash.status; 
183 };
184 osrfContinueStatus.prototype.statusCode = function(d) {
185     if(arguments.length == 1) 
186         this.hash.statusCode = d; 
187     return this.hash.statusCode; 
188 };
189
190 OpenSRF = {};
191 OpenSRF.locale = null;
192
193 /* makes cls a subclass of pcls */
194 OpenSRF.set_subclass = function(cls, pcls) {
195     var str = cls+'.prototype = new '+pcls+'();';
196     str += cls+'.prototype.constructor = '+cls+';';
197     str += cls+'.baseClass = '+pcls+'.prototype.constructor;';
198     str += cls+'.prototype["super"] = '+pcls+'.prototype;';
199     eval(str);
200 };
201
202
203 /* general session superclass */
204 OpenSRF.Session = function() {
205     this.remote_id = null;
206     this.state = OSRF_APP_SESSION_DISCONNECTED;
207 };
208
209 OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_WS;
210 if (true || typeof WebSocket == 'undefined')
211     OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_XHR;
212
213 OpenSRF.Session.cache = {};
214 OpenSRF.Session.find_session = function(thread_trace) {
215     return OpenSRF.Session.cache[thread_trace];
216 };
217 OpenSRF.Session.prototype.cleanup = function() {
218     delete OpenSRF.Session.cache[this.thread];
219 };
220
221 OpenSRF.Session.prototype.send = function(osrf_msg, args) {
222     args = (args) ? args : {};
223     switch(OpenSRF.Session.transport) {
224         case OSRF_TRANSPORT_TYPE_WS:
225             return this.send_ws(osrf_msg, args);
226         case OSRF_TRANSPORT_TYPE_XHR:
227             return this.send_xhr(osrf_msg, args);
228         case OSRF_TRANSPORT_TYPE_XMPP:
229             return this.send_xmpp(osrf_msg, args);
230     }
231 };
232
233 OpenSRF.Session.prototype.send_xhr = function(osrf_msg, args) {
234     args.thread = this.thread;
235     args.rcpt = this.remote_id;
236     args.rcpt_service = this.service;
237     new OpenSRF.XHRequest(osrf_msg, args).send();
238 };
239
240 OpenSRF.Session.prototype.send_ws = function(osrf_msg, args) {
241     args.session = this;
242     if (this.websocket) {
243         this.websocket.args = args; // callbacks
244         this.websocket.send(osrf_msg);
245     } else {
246         this.websocket = new OpenSRF.WSRequest(
247             this, args, function(wsreq) {
248                 wsreq.send(osrf_msg);
249             }
250         );
251     }
252 };
253
254 OpenSRF.Session.prototype.send_xmpp = function(osrf_msg, args) {
255     alert('xmpp transport not implemented');
256 };
257
258
259 /* client sessions make requests */
260 OpenSRF.ClientSession = function(service) {
261     this.service = service;
262     this.remote_id = null;
263     this.locale = OpenSRF.locale || 'en-US';
264     this.last_id = 0;
265     this.thread = Math.random() + '' + new Date().getTime();
266     this.requests = [];
267     this.onconnect = null;
268     OpenSRF.Session.cache[this.thread] = this;
269 };
270 OpenSRF.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session');
271
272
273 OpenSRF.ClientSession.prototype.connect = function(args) {
274     args = (args) ? args : {};
275     this.remote_id = null;
276
277     if (this.state == OSRF_APP_SESSION_CONNECTED) {
278         if (args.onconnect) args.onconnect();
279         return true;
280     }
281
282     if(args.onconnect) {
283         this.onconnect = args.onconnect;
284
285     } else {
286         /* if no handler is provided, make this a synchronous call */
287         this.timeout = (args.timeout) ? args.timeout : 5;
288     }
289
290     message = new osrfMessage({
291         'threadTrace' : this.last_id++, 
292         'type' : OSRF_MESSAGE_TYPE_CONNECT
293     });
294
295     this.send(message, {'timeout' : this.timeout});
296
297     if(this.onconnect || this.state == OSRF_APP_SESSION_CONNECTED)
298         return true;
299
300     return false;
301 };
302
303 OpenSRF.ClientSession.prototype.disconnect = function(args) {
304
305     if (this.state == OSRF_APP_SESSION_CONNECTED) {
306         this.send(
307             new osrfMessage({
308                 'threadTrace' : this.last_id++,
309                 'type' : OSRF_MESSAGE_TYPE_DISCONNECT
310             })
311         );
312     }
313
314     this.remote_id = null;
315     this.state = OSRF_APP_SESSION_DISCONNECTED;
316
317     if (this.websocket) {
318         this.websocket.close();
319         delete this.websocket;
320     }
321 };
322
323
324 OpenSRF.ClientSession.prototype.request = function(args) {
325     
326     if(this.state != OSRF_APP_SESSION_CONNECTED)
327         this.remote_id = null;
328         
329     if(typeof args == 'string') { 
330         params = [];
331         for(var i = 1; i < arguments.length; i++)
332             params.push(arguments[i]);
333
334         args = {
335             method : args, 
336             params : params
337         };
338     } else {
339         if(typeof args == 'undefined')
340             args = {};
341     }
342
343     var req = new OpenSRF.Request(this, this.last_id++, args);
344     this.requests.push(req);
345     return req;
346 };
347
348 OpenSRF.ClientSession.prototype.find_request = function(reqid) {
349     for(var i = 0; i < this.requests.length; i++) {
350         var req = this.requests[i];
351         if(req.reqid == reqid)
352             return req;
353     }
354     return null;
355 };
356
357 OpenSRF.Request = function(session, reqid, args) {
358     this.session = session;
359     this.reqid = reqid;
360
361     /* callbacks */
362     this.onresponse = args.onresponse;
363     this.oncomplete = args.oncomplete;
364     this.onerror = args.onerror;
365     this.onmethoderror = args.onmethoderror;
366     this.ontransporterror = args.ontransporterror;
367
368     this.method = args.method;
369     this.params = args.params;
370     this.timeout = args.timeout;
371     this.response_queue = [];
372     this.complete = false;
373 };
374
375 OpenSRF.Request.prototype.peek_last = function(timeout) {
376     if(this.response_queue.length > 0) {
377         var x = this.response_queue.pop();
378         this.response_queue.push(x);
379         return x;
380     }
381     return null;
382 };
383
384 OpenSRF.Request.prototype.peek = function(timeout) {
385     if(this.response_queue.length > 0)
386         return this.response_queue[0];
387     return null;
388 };
389
390 OpenSRF.Request.prototype.recv = function(timeout) {
391     if(this.response_queue.length > 0)
392         return this.response_queue.shift();
393     return null;
394 };
395
396 OpenSRF.Request.prototype.send = function() {
397     method = new osrfMethod({'method':this.method, 'params':this.params});
398     message = new osrfMessage({
399         'threadTrace' : this.reqid, 
400         'type' : OSRF_MESSAGE_TYPE_REQUEST, 
401         'payload' : method, 
402         'locale' : this.session.locale
403     });
404
405     this.session.send(message, {
406         'timeout' : this.timeout,
407         'onresponse' : this.onresponse,
408         'oncomplete' : this.oncomplete,
409         'onerror' : this.onerror,
410         'onmethoderror' : this.onmethoderror,
411         'ontransporterror' : this.ontransporterror
412     });
413 };
414
415 OpenSRF.NetMessage = function(to, from, thread, body) {
416     this.to = to;
417     this.from = from;
418     this.thread = thread;
419     this.body = body;
420 };
421
422 OpenSRF.Stack = function() {
423 };
424
425 // global inbound message queue
426 OpenSRF.Stack.queue = [];
427
428 // XXX testing
429 function log(msg) {
430     try {
431         dump(msg + '\n'); // xulrunner
432     } catch(E) {
433         console.log(msg);
434     }
435 }
436
437 // ses may be passed to us by the network handler
438 OpenSRF.Stack.push = function(net_msg, callbacks, ses) {
439     if (!ses) ses = OpenSRF.Session.find_session(net_msg.thread); 
440     if (!ses) return;
441     ses.remote_id = net_msg.from;
442     osrf_msgs = [];
443
444     try {
445         osrf_msgs = JSON2js(net_msg.body);
446
447     } catch(E) {
448         log('Error parsing OpenSRF message body as JSON: ' + net_msg.body + '\n' + E);
449
450         /** UGH
451           * For unknown reasons, the Content-Type header will occasionally
452           * be included in the XHR.responseText for multipart/mixed messages.
453           * When this happens, strip the header and newlines from the message
454           * body and re-parse.
455           */
456         net_msg.body = net_msg.body.replace(/^.*\n\n/, '');
457         log('Cleaning up and retrying...');
458
459         try {
460             osrf_msgs = JSON2js(net_msg.body);
461         } catch(E2) {
462             log('Unable to clean up message, giving up: ' + net_msg.body);
463             return;
464         }
465     }
466
467     // push the latest responses onto the end of the inbound message queue
468     for(var i = 0; i < osrf_msgs.length; i++)
469         OpenSRF.Stack.queue.push({msg : osrf_msgs[i], callbacks : callbacks, ses : ses});
470
471     // continue processing responses, oldest to newest
472     while(OpenSRF.Stack.queue.length) {
473         var data = OpenSRF.Stack.queue.shift();
474         OpenSRF.Stack.handle_message(data.ses, data.msg, data.callbacks);
475     }
476 };
477
478 OpenSRF.Stack.handle_message = function(ses, osrf_msg, callbacks) {
479     
480     var req = null;
481
482     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
483
484         var payload = osrf_msg.payload();
485         var status = payload.statusCode();
486         var status_text = payload.status();
487
488         if(status == OSRF_STATUS_COMPLETE) {
489             req = ses.find_request(osrf_msg.threadTrace());
490             if(req) {
491                 req.complete = true;
492                 if(callbacks.oncomplete && !req.oncomplete_called) {
493                     req.oncomplete_called = true;
494                     return callbacks.oncomplete(req);
495                 }
496             }
497         }
498
499         if(status == OSRF_STATUS_OK) {
500             ses.state = OSRF_APP_SESSION_CONNECTED;
501
502             /* call the connect callback */
503             if(ses.onconnect && !ses.onconnect_called) {
504                 ses.onconnect_called = true;
505                 return ses.onconnect();
506             }
507         }
508
509         if(status == OSRF_STATUS_NOTFOUND || status == OSRF_STATUS_INTERNALSERVERERROR) {
510             req = ses.find_request(osrf_msg.threadTrace());
511             if(callbacks.onmethoderror) 
512                 return callbacks.onmethoderror(req, status, status_text);
513         }
514     }
515
516     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
517         req = ses.find_request(osrf_msg.threadTrace());
518         if(req) {
519             req.response_queue.push(osrf_msg.payload());
520             if(callbacks.onresponse) 
521                 return callbacks.onresponse(req);
522         }
523     }
524 };
525
526