]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/javascript/opensrf.js
LP#1418613 per-tab websocket send() JS thinko repair
[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 var OSRF_TRANSPORT_TYPE_WS_SHARED = 4;
26
27 /* message types */
28 var OSRF_MESSAGE_TYPE_REQUEST = 'REQUEST';
29 var OSRF_MESSAGE_TYPE_STATUS = 'STATUS';
30 var OSRF_MESSAGE_TYPE_RESULT = 'RESULT';
31 var OSRF_MESSAGE_TYPE_CONNECT = 'CONNECT';
32 var OSRF_MESSAGE_TYPE_DISCONNECT = 'DISCONNECT';
33
34 /* message statuses */
35 var OSRF_STATUS_CONTINUE = 100;
36 var OSRF_STATUS_OK = 200;
37 var OSRF_STATUS_ACCEPTED = 202;
38 var OSRF_STATUS_COMPLETE = 205;
39 var OSRF_STATUS_REDIRECTED = 307;
40 var OSRF_STATUS_BADREQUEST = 400;
41 var OSRF_STATUS_UNAUTHORIZED = 401;
42 var OSRF_STATUS_FORBIDDEN = 403;
43 var OSRF_STATUS_NOTFOUND = 404;
44 var OSRF_STATUS_NOTALLOWED = 405;
45 var OSRF_STATUS_TIMEOUT = 408;
46 var OSRF_STATUS_EXPFAILED = 417;
47 var OSRF_STATUS_INTERNALSERVERERROR = 500;
48 var OSRF_STATUS_NOTIMPLEMENTED = 501;
49 var OSRF_STATUS_VERSIONNOTSUPPORTED = 505;
50
51 // TODO: get path from ./configure prefix
52 var SHARED_WORKER_LIB = '/js/dojo/opensrf/opensrf_ws_shared.js'; 
53
54 /* The following classes map directly to network-serializable opensrf objects */
55
56 function osrfMessage(hash) {
57     this.hash = hash;
58     if(!this.hash.locale)
59         this.hash.locale = OpenSRF.locale || 'en-US';
60     this._encodehash = true;
61 }
62 osrfMessage.prototype.threadTrace = function(d) { 
63     if(arguments.length == 1) 
64         this.hash.threadTrace = d; 
65     return this.hash.threadTrace; 
66 };
67 osrfMessage.prototype.type = function(d) { 
68     if(arguments.length == 1) 
69         this.hash.type = d; 
70     return this.hash.type; 
71 };
72 osrfMessage.prototype.payload = function(d) { 
73     if(arguments.length == 1) 
74         this.hash.payload = d; 
75     return this.hash.payload; 
76 };
77 osrfMessage.prototype.locale = function(d) { 
78     if(arguments.length == 1) 
79         this.hash.locale = d; 
80     return this.hash.locale; 
81 };
82 osrfMessage.prototype.api_level = function(d) { 
83     if(arguments.length == 1) 
84         this.hash.api_level = d; 
85     return this.hash.api_level; 
86 };
87 osrfMessage.prototype.serialize = function() {
88     return {
89         "__c":"osrfMessage",
90         "__p": {
91             'threadTrace' : this.hash.threadTrace,
92             'type' : this.hash.type,
93             'payload' : (this.hash.payload) ? this.hash.payload.serialize() : 'null',
94             'locale' : this.hash.locale,
95             'api_level' : this.hash.api_level
96         }
97     };
98 };
99
100 function osrfMethod(hash) {
101     this.hash = hash;
102     this._encodehash = true;
103 }
104 osrfMethod.prototype.method = function(d) {
105     if(arguments.length == 1) 
106         this.hash.method = d; 
107     return this.hash.method; 
108 };
109 osrfMethod.prototype.params = function(d) {
110     if(arguments.length == 1) 
111         this.hash.params = d; 
112     return this.hash.params; 
113 };
114 osrfMethod.prototype.serialize = function() {
115     return {
116         "__c":"osrfMethod",
117         "__p": {
118             'method' : this.hash.method,
119             'params' : this.hash.params
120         }
121     };
122 };
123
124 function osrfMethodException(hash) {
125     this.hash = hash;
126     this._encodehash = true;
127 }
128 osrfMethodException.prototype.status = function(d) {
129     if(arguments.length == 1) 
130         this.hash.status = d; 
131     return this.hash.status; 
132 };
133 osrfMethodException.prototype.statusCode = function(d) {
134     if(arguments.length == 1) 
135         this.hash.statusCode = d; 
136     return this.hash.statusCode; 
137 };
138 function osrfConnectStatus(hash) { 
139     this.hash = hash;
140     this._encodehash = true;
141 }
142 osrfConnectStatus.prototype.status = function(d) {
143     if(arguments.length == 1) 
144         this.hash.status = d; 
145     return this.hash.status; 
146 };
147 osrfConnectStatus.prototype.statusCode = function(d) {
148     if(arguments.length == 1) 
149         this.hash.statusCode = d; 
150     return this.hash.statusCode; 
151 };
152 function osrfResult(hash) {
153     this.hash = hash;
154     this._encodehash = true;
155 }
156 osrfResult.prototype.status = function(d) {
157     if(arguments.length == 1) 
158         this.hash.status = d; 
159     return this.hash.status; 
160 };
161 osrfResult.prototype.statusCode = function(d) {
162     if(arguments.length == 1) 
163         this.hash.statusCode = d; 
164     return this.hash.statusCode; 
165 };
166 osrfResult.prototype.content = function(d) {
167     if(arguments.length == 1) 
168         this.hash.content = d; 
169     return this.hash.content; 
170 };
171 function osrfServerError(hash) { 
172     this.hash = hash;
173     this._encodehash = true;
174 }
175 osrfServerError.prototype.status = function(d) {
176     if(arguments.length == 1) 
177         this.hash.status = d; 
178     return this.hash.status; 
179 };
180 osrfServerError.prototype.statusCode = function(d) {
181     if(arguments.length == 1) 
182         this.hash.statusCode = d; 
183     return this.hash.statusCode; 
184 };
185 function osrfContinueStatus(hash) { 
186     this.hash = hash;
187     this._encodehash = true;
188 }
189 osrfContinueStatus.prototype.status = function(d) {
190     if(arguments.length == 1) 
191         this.hash.status = d; 
192     return this.hash.status; 
193 };
194 osrfContinueStatus.prototype.statusCode = function(d) {
195     if(arguments.length == 1) 
196         this.hash.statusCode = d; 
197     return this.hash.statusCode; 
198 };
199
200 OpenSRF = {};
201 OpenSRF.locale = null;
202 OpenSRF.api_level = 1;
203
204 /* makes cls a subclass of pcls */
205 OpenSRF.set_subclass = function(cls, pcls) {
206     var str = cls+'.prototype = new '+pcls+'();';
207     str += cls+'.prototype.constructor = '+cls+';';
208     str += cls+'.baseClass = '+pcls+'.prototype.constructor;';
209     str += cls+'.prototype["super"] = '+pcls+'.prototype;';
210     eval(str);
211 };
212
213
214 /* general session superclass */
215 OpenSRF.Session = function() {
216     this.remote_id = null;
217     this.state = OSRF_APP_SESSION_DISCONNECTED;
218 };
219
220 OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_XHR;
221 OpenSRF.Session.cache = {};
222
223 OpenSRF.Session.find_session = function(thread_trace) {
224     return OpenSRF.Session.cache[thread_trace];
225 };
226 OpenSRF.Session.prototype.cleanup = function() {
227     delete OpenSRF.Session.cache[this.thread];
228 };
229
230 OpenSRF.Session.prototype.send = function(osrf_msg, args) {
231     args = (args) ? args : {};
232     switch(OpenSRF.Session.transport) {
233         case OSRF_TRANSPORT_TYPE_WS:
234             return this.send_ws(osrf_msg);
235         case OSRF_TRANSPORT_TYPE_WS_SHARED:
236             return this.send_ws_shared(osrf_msg);
237         case OSRF_TRANSPORT_TYPE_XHR:
238             return this.send_xhr(osrf_msg, args);
239         case OSRF_TRANSPORT_TYPE_XMPP:
240             return this.send_xmpp(osrf_msg, args);
241     }
242 };
243
244 OpenSRF.Session.prototype.send_xhr = function(osrf_msg, args) {
245     args.thread = this.thread;
246     args.rcpt = this.remote_id;
247     args.rcpt_service = this.service;
248     new OpenSRF.XHRequest(osrf_msg, args).send();
249 };
250
251 OpenSRF.websocketConnected = function() {
252     return OpenSRF.sharedWebsocketConnected || (
253         OpenSRF.websocketConnection && 
254         OpenSRF.websocketConnection.connected()
255     );
256 }
257
258 OpenSRF.Session.prototype.send_ws = function(osrf_msg) {
259
260     // XXX there appears to be a bug in Chromium where loading the
261     // same page multiple times (without a refresh or cache clear)
262     // causes the SharedWorker to fail to instantiate on 
263     // every other page load.  Disabling SharedWorker's entirely
264     // for now.
265     if (false /* ^-- */ && typeof SharedWorker == 'function' 
266
267         /*
268          * https://bugzilla.mozilla.org/show_bug.cgi?id=504553#c73
269          * Firefox does not yet support WebSockets in worker threads
270          */
271         && !navigator.userAgent.match(/Firefox/)
272     ) {
273         // vanilla websockets requested, but this browser supports
274         // shared workers, so use those instead.
275         return this.send_ws_shared(osrf_msg);
276     }
277
278     // otherwise, use a per-tab connection
279
280     if (!OpenSRF.websocketConnection) {
281         this.setup_single_ws();
282     }
283
284     var json = js2JSON({
285         service : this.service,
286         thread : this.thread,
287         osrf_msg : [osrf_msg.serialize()]
288     });
289
290     OpenSRF.websocketConnection.send(json);
291 };
292
293 OpenSRF.Session.prototype.setup_single_ws = function() {
294     OpenSRF.websocketConnection = new OpenSRF.WebSocket();
295
296     OpenSRF.websocketConnection.onmessage = function(msg) {
297         try {
298             var msg = JSON2js(msg);
299         } catch(E) {
300             console.error(
301                 "Error parsing JSON in shared WS response: " + msg);
302             throw E;
303         }
304         OpenSRF.Stack.push(                                                        
305             new OpenSRF.NetMessage(                                                
306                null, null, msg.thread, null, msg.osrf_msg)                        
307         ); 
308
309         return;
310     }
311 }
312
313 OpenSRF.Session.setup_shared_ws = function() {
314     OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_WS_SHARED;
315
316     OpenSRF.sharedWSWorker = new SharedWorker(SHARED_WORKER_LIB);
317
318     OpenSRF.sharedWSWorker.port.addEventListener('message', function(e) {                          
319         var data = e.data;
320
321         if (data.action == 'message') {
322             // pass all inbound message up the opensrf stack
323
324             OpenSRF.sharedWebsocketConnected = true;
325             var msg;
326             try {
327                 msg = JSON2js(data.message);
328             } catch(E) {
329                 console.error(
330                     "Error parsing JSON in shared WS response: " + msg);
331                 throw E;
332             }
333             OpenSRF.Stack.push(                                                        
334                 new OpenSRF.NetMessage(                                                
335                    null, null, msg.thread, null, msg.osrf_msg)                        
336             ); 
337
338             return;
339         }
340
341
342         if (data.action == 'event') {
343             if (data.type.match(/onclose|onerror/)) {
344                 OpenSRF.sharedWebsocketConnected = false;
345                 if (OpenSRF.onWebSocketClosed)
346                     OpenSRF.onWebSocketClosed();
347                 if (data.type.match(/onerror/)) 
348                     throw new Error(data.message);
349             }
350         }
351     });
352
353     OpenSRF.sharedWSWorker.port.start();   
354 }
355
356 OpenSRF.Session.prototype.send_ws_shared = function(message) {
357
358     if (!OpenSRF.sharedWSWorker) 
359         OpenSRF.Session.setup_shared_ws();
360
361     var json = js2JSON({
362         service : this.service,
363         thread : this.thread,
364         osrf_msg : [message.serialize()]
365     });
366
367     OpenSRF.sharedWSWorker.port.postMessage({
368         action : 'message', 
369         // pass the thread additionally as a stand-alone value so the
370         // worker can more efficiently inspect it.
371         thread : this.thread,
372         message : json
373     });
374 }
375
376
377 OpenSRF.Session.prototype.send_xmpp = function(osrf_msg, args) {
378     alert('xmpp transport not implemented');
379 };
380
381
382 /* client sessions make requests */
383 OpenSRF.ClientSession = function(service) {
384     this.service = service;
385     this.remote_id = null;
386     this.locale = OpenSRF.locale || 'en-US';
387     this.last_id = 0;
388     this.requests = [];
389     this.onconnect = null;
390     this.thread = Math.random() + '' + new Date().getTime();
391     OpenSRF.Session.cache[this.thread] = this;
392 };
393 OpenSRF.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session');
394
395
396 OpenSRF.ClientSession.prototype.connect = function(args) {
397     args = (args) ? args : {};
398     this.remote_id = null;
399
400     if (this.state == OSRF_APP_SESSION_CONNECTED) {
401         if (args.onconnect) args.onconnect();
402         return true;
403     }
404
405     if(args.onconnect) {
406         this.onconnect = args.onconnect;
407
408     } else {
409         /* if no handler is provided, make this a synchronous call */
410         this.timeout = (args.timeout) ? args.timeout : 5;
411     }
412
413     message = new osrfMessage({
414         'threadTrace' : this.last_id++, 
415         'type' : OSRF_MESSAGE_TYPE_CONNECT
416     });
417
418     this.send(message, {'timeout' : this.timeout});
419
420     if(this.onconnect || this.state == OSRF_APP_SESSION_CONNECTED)
421         return true;
422
423     return false;
424 };
425
426 OpenSRF.ClientSession.prototype.disconnect = function(args) {
427
428     if (this.state == OSRF_APP_SESSION_CONNECTED) {
429         this.send(
430             new osrfMessage({
431                 'threadTrace' : this.last_id++,
432                 'type' : OSRF_MESSAGE_TYPE_DISCONNECT
433             })
434         );
435     }
436
437     this.remote_id = null;
438     this.state = OSRF_APP_SESSION_DISCONNECTED;
439 };
440
441
442 OpenSRF.ClientSession.prototype.request = function(args) {
443     
444     if(this.state != OSRF_APP_SESSION_CONNECTED)
445         this.remote_id = null;
446         
447     if(typeof args == 'string') { 
448         params = [];
449         for(var i = 1; i < arguments.length; i++)
450             params.push(arguments[i]);
451
452         args = {
453             method : args, 
454             params : params
455         };
456     } else {
457         if(typeof args == 'undefined')
458             args = {};
459     }
460
461     var req = new OpenSRF.Request(this, this.last_id++, args);
462     this.requests.push(req);
463     return req;
464 };
465
466 OpenSRF.ClientSession.prototype.find_request = function(reqid) {
467     for(var i = 0; i < this.requests.length; i++) {
468         var req = this.requests[i];
469         if(req.reqid == reqid)
470             return req;
471     }
472     return null;
473 };
474
475 OpenSRF.Request = function(session, reqid, args) {
476     this.session = session;
477     this.reqid = reqid;
478
479     /* callbacks */
480     this.onresponse = args.onresponse;
481     this.oncomplete = args.oncomplete;
482     this.onerror = args.onerror;
483     this.onmethoderror = args.onmethoderror;
484     this.ontransporterror = args.ontransporterror;
485
486     this.method = args.method;
487     this.params = args.params;
488     this.timeout = args.timeout;
489     this.api_level = args.api_level || OpenSRF.api_level;
490     this.response_queue = [];
491     this.complete = false;
492 };
493
494 OpenSRF.Request.prototype.peek_last = function(timeout) {
495     if(this.response_queue.length > 0) {
496         var x = this.response_queue.pop();
497         this.response_queue.push(x);
498         return x;
499     }
500     return null;
501 };
502
503 OpenSRF.Request.prototype.peek = function(timeout) {
504     if(this.response_queue.length > 0)
505         return this.response_queue[0];
506     return null;
507 };
508
509 OpenSRF.Request.prototype.recv = function(timeout) {
510     if(this.response_queue.length > 0)
511         return this.response_queue.shift();
512     return null;
513 };
514
515 OpenSRF.Request.prototype.send = function() {
516     method = new osrfMethod({'method':this.method, 'params':this.params});
517     message = new osrfMessage({
518         'threadTrace' : this.reqid, 
519         'type' : OSRF_MESSAGE_TYPE_REQUEST, 
520         'payload' : method, 
521         'locale' : this.session.locale,
522         'api_level' : this.api_level
523     });
524
525     this.session.send(message, {
526         'timeout' : this.timeout,
527         'onresponse' : this.onresponse,
528         'oncomplete' : this.oncomplete,
529         'onerror' : this.onerror,
530         'onmethoderror' : this.onmethoderror,
531         'ontransporterror' : this.ontransporterror
532     });
533 };
534
535 OpenSRF.NetMessage = function(to, from, thread, body, osrf_msg) {
536     this.to = to;
537     this.from = from;
538     this.thread = thread;
539     this.body = body;
540     this.osrf_msg = osrf_msg;
541 };
542
543 OpenSRF.Stack = function() {
544 };
545
546 // global inbound message queue
547 OpenSRF.Stack.queue = [];
548
549 // XXX testing
550 function log(msg) {
551     try {
552         dump(msg + '\n'); // xulrunner
553     } catch(E) {
554         console.log(msg);
555     }
556 }
557
558 // ses may be passed to us by the network handler
559 OpenSRF.Stack.push = function(net_msg, callbacks) {
560     var ses = OpenSRF.Session.find_session(net_msg.thread); 
561     if (!ses) return;
562     ses.remote_id = net_msg.from;
563
564     // NetMessage's from websocket connections are parsed before they get here
565     osrf_msgs = net_msg.osrf_msg;
566
567     if (!osrf_msgs) {
568
569         try {
570             osrf_msgs = JSON2js(net_msg.body);
571
572             // TODO: pretty sure we don't need this..
573             if (OpenSRF.Session.transport == OSRF_TRANSPORT_TYPE_WS) {
574                 // WebSocketRequests wrap the content
575                 osrf_msgs = osrf_msgs.osrf_msg;
576             }
577
578         } catch(E) {
579             log('Error parsing OpenSRF message body as JSON: ' + net_msg.body + '\n' + E);
580
581             /** UGH
582               * For unknown reasons, the Content-Type header will occasionally
583               * be included in the XHR.responseText for multipart/mixed messages.
584               * When this happens, strip the header and newlines from the message
585               * body and re-parse.
586               */
587             net_msg.body = net_msg.body.replace(/^.*\n\n/, '');
588             log('Cleaning up and retrying...');
589
590             try {
591                 osrf_msgs = JSON2js(net_msg.body);
592             } catch(E2) {
593                 log('Unable to clean up message, giving up: ' + net_msg.body);
594                 return;
595             }
596         }
597     }
598
599     // push the latest responses onto the end of the inbound message queue
600     for(var i = 0; i < osrf_msgs.length; i++)
601         OpenSRF.Stack.queue.push({msg : osrf_msgs[i], ses : ses});
602
603     // continue processing responses, oldest to newest
604     while(OpenSRF.Stack.queue.length) {
605         var data = OpenSRF.Stack.queue.shift();
606         OpenSRF.Stack.handle_message(data.ses, data.msg);
607     }
608 };
609
610 OpenSRF.Stack.handle_message = function(ses, osrf_msg) {
611     
612     var req = ses.find_request(osrf_msg.threadTrace());
613
614     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
615
616         var payload = osrf_msg.payload();
617         var status = payload.statusCode();
618         var status_text = payload.status();
619
620         if(status == OSRF_STATUS_COMPLETE) {
621             if(req) {
622                 req.complete = true;
623                 if(req.oncomplete && !req.oncomplete_called) {
624                     req.oncomplete_called = true;
625                     return req.oncomplete(req);
626                 }
627             }
628         }
629
630         if(status == OSRF_STATUS_OK) {
631             ses.state = OSRF_APP_SESSION_CONNECTED;
632
633             /* call the connect callback */
634             if(ses.onconnect && !ses.onconnect_called) {
635                 ses.onconnect_called = true;
636                 return ses.onconnect();
637             }
638         }
639
640         // capture all 400's and 500's as method errors
641         if ((status+'').match(/^4/) || (status+'').match(/^5/)) {
642             if(req && req.onmethoderror) 
643                 return req.onmethoderror(req, status, status_text);
644         }
645     }
646
647     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
648         if(req) {
649             req.response_queue.push(osrf_msg.payload());
650             if(req.onresponse) {
651                 return req.onresponse(req);
652             }
653         }
654     }
655 };
656
657