]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/javascript/opensrf.js
994fbe60005bdf24d89cca98d452f41b41623df3
[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 /* The following classes map directly to network-serializable opensrf objects */
52
53 function osrfMessage(hash) {
54     this.hash = hash;
55     if(!this.hash.locale)
56         this.hash.locale = OpenSRF.locale || 'en-US';
57     this._encodehash = true;
58 }
59 osrfMessage.prototype.threadTrace = function(d) { 
60     if(arguments.length == 1) 
61         this.hash.threadTrace = d; 
62     return this.hash.threadTrace; 
63 };
64 osrfMessage.prototype.type = function(d) { 
65     if(arguments.length == 1) 
66         this.hash.type = d; 
67     return this.hash.type; 
68 };
69 osrfMessage.prototype.payload = function(d) { 
70     if(arguments.length == 1) 
71         this.hash.payload = d; 
72     return this.hash.payload; 
73 };
74 osrfMessage.prototype.locale = function(d) { 
75     if(arguments.length == 1) 
76         this.hash.locale = d; 
77     return this.hash.locale; 
78 };
79 osrfMessage.prototype.serialize = function() {
80     return {
81         "__c":"osrfMessage",
82         "__p": {
83             'threadTrace' : this.hash.threadTrace,
84             'type' : this.hash.type,
85             'payload' : (this.hash.payload) ? this.hash.payload.serialize() : 'null',
86             'locale' : this.hash.locale
87         }
88     };
89 };
90
91 function osrfMethod(hash) {
92     this.hash = hash;
93     this._encodehash = true;
94 }
95 osrfMethod.prototype.method = function(d) {
96     if(arguments.length == 1) 
97         this.hash.method = d; 
98     return this.hash.method; 
99 };
100 osrfMethod.prototype.params = function(d) {
101     if(arguments.length == 1) 
102         this.hash.params = d; 
103     return this.hash.params; 
104 };
105 osrfMethod.prototype.serialize = function() {
106     return {
107         "__c":"osrfMethod",
108         "__p": {
109             'method' : this.hash.method,
110             'params' : this.hash.params
111         }
112     };
113 };
114
115 function osrfMethodException(hash) {
116     this.hash = hash;
117     this._encodehash = true;
118 }
119 osrfMethodException.prototype.status = function(d) {
120     if(arguments.length == 1) 
121         this.hash.status = d; 
122     return this.hash.status; 
123 };
124 osrfMethodException.prototype.statusCode = function(d) {
125     if(arguments.length == 1) 
126         this.hash.statusCode = d; 
127     return this.hash.statusCode; 
128 };
129 function osrfConnectStatus(hash) { 
130     this.hash = hash;
131     this._encodehash = true;
132 }
133 osrfConnectStatus.prototype.status = function(d) {
134     if(arguments.length == 1) 
135         this.hash.status = d; 
136     return this.hash.status; 
137 };
138 osrfConnectStatus.prototype.statusCode = function(d) {
139     if(arguments.length == 1) 
140         this.hash.statusCode = d; 
141     return this.hash.statusCode; 
142 };
143 function osrfResult(hash) {
144     this.hash = hash;
145     this._encodehash = true;
146 }
147 osrfResult.prototype.status = function(d) {
148     if(arguments.length == 1) 
149         this.hash.status = d; 
150     return this.hash.status; 
151 };
152 osrfResult.prototype.statusCode = function(d) {
153     if(arguments.length == 1) 
154         this.hash.statusCode = d; 
155     return this.hash.statusCode; 
156 };
157 osrfResult.prototype.content = function(d) {
158     if(arguments.length == 1) 
159         this.hash.content = d; 
160     return this.hash.content; 
161 };
162 function osrfServerError(hash) { 
163     this.hash = hash;
164     this._encodehash = true;
165 }
166 osrfServerError.prototype.status = function(d) {
167     if(arguments.length == 1) 
168         this.hash.status = d; 
169     return this.hash.status; 
170 };
171 osrfServerError.prototype.statusCode = function(d) {
172     if(arguments.length == 1) 
173         this.hash.statusCode = d; 
174     return this.hash.statusCode; 
175 };
176 function osrfContinueStatus(hash) { 
177     this.hash = hash;
178     this._encodehash = true;
179 }
180 osrfContinueStatus.prototype.status = function(d) {
181     if(arguments.length == 1) 
182         this.hash.status = d; 
183     return this.hash.status; 
184 };
185 osrfContinueStatus.prototype.statusCode = function(d) {
186     if(arguments.length == 1) 
187         this.hash.statusCode = d; 
188     return this.hash.statusCode; 
189 };
190
191 OpenSRF = {};
192 OpenSRF.locale = null;
193
194 /* makes cls a subclass of pcls */
195 OpenSRF.set_subclass = function(cls, pcls) {
196     var str = cls+'.prototype = new '+pcls+'();';
197     str += cls+'.prototype.constructor = '+cls+';';
198     str += cls+'.baseClass = '+pcls+'.prototype.constructor;';
199     str += cls+'.prototype["super"] = '+pcls+'.prototype;';
200     eval(str);
201 };
202
203
204 /* general session superclass */
205 OpenSRF.Session = function() {
206     this.remote_id = null;
207     this.state = OSRF_APP_SESSION_DISCONNECTED;
208 };
209
210 //OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_WS;
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);
226         case OSRF_TRANSPORT_TYPE_WS_SHARED:
227             return this.send_ws_shared(osrf_msg);
228         case OSRF_TRANSPORT_TYPE_XHR:
229             return this.send_xhr(osrf_msg, args);
230         case OSRF_TRANSPORT_TYPE_XMPP:
231             return this.send_xmpp(osrf_msg, args);
232     }
233 };
234
235 OpenSRF.Session.prototype.send_xhr = function(osrf_msg, args) {
236     args.thread = this.thread;
237     args.rcpt = this.remote_id;
238     args.rcpt_service = this.service;
239     new OpenSRF.XHRequest(osrf_msg, args).send();
240 };
241
242 OpenSRF.Session.prototype.send_ws = function(osrf_msg) {
243     new OpenSRF.WebSocketRequest(
244         this, 
245         function(wsreq) {wsreq.send(osrf_msg)} // onopen
246     );
247 };
248
249 OpenSRF.Session.setup_shared_ws = function(onconnect) {
250     // TODO path
251     OpenSRF.sharedWSWorker = new SharedWorker('opensrf_ws_shared.js'); 
252
253     OpenSRF.sharedWSWorker.port.addEventListener('message', function(e) {                          
254         var data = e.data;
255         console.log('sharedWSWorker received message ' + data.action);
256
257         if (data.action == 'message') {
258             // pass all inbound message up the opensrf stack
259
260             var msg = JSON2js(data.message); // TODO: json error handling
261             OpenSRF.Stack.push(                                                        
262                 new OpenSRF.NetMessage(                                                
263                    null, null, msg.thread, null, msg.osrf_msg)                        
264             ); 
265
266             return;
267         }
268
269         if (data.action == 'socket_connected') {
270             if (onconnect) onconnect();
271             return;
272         }
273
274         if (data.action == 'error') {
275             throw new Error(data.message);
276         }
277     });
278
279     OpenSRF.sharedWSWorker.port.start();   
280 }
281
282 OpenSRF.Session.prototype.send_ws_shared = function(message) {
283
284     var json = js2JSON({
285         service : this.service,
286         thread : this.thread,
287         osrf_msg : [message.serialize()]
288     });
289
290     OpenSRF.sharedWSWorker.port.postMessage({
291         action : 'message', 
292         // pass the thread additionally as a stand-alone value so the
293         // worker can more efficiently inspect it.
294         thread : this.thread,
295         message : json
296     });
297 }
298
299
300 OpenSRF.Session.prototype.send_xmpp = function(osrf_msg, args) {
301     alert('xmpp transport not implemented');
302 };
303
304
305 /* client sessions make requests */
306 OpenSRF.ClientSession = function(service) {
307     this.service = service;
308     this.remote_id = null;
309     this.locale = OpenSRF.locale || 'en-US';
310     this.last_id = 0;
311     this.requests = [];
312     this.onconnect = null;
313     this.thread = Math.random() + '' + new Date().getTime();
314     OpenSRF.Session.cache[this.thread] = this;
315 };
316 OpenSRF.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session');
317
318
319 OpenSRF.ClientSession.prototype.connect = function(args) {
320     args = (args) ? args : {};
321     this.remote_id = null;
322
323     if (this.state == OSRF_APP_SESSION_CONNECTED) {
324         if (args.onconnect) args.onconnect();
325         return true;
326     }
327
328     if(args.onconnect) {
329         this.onconnect = args.onconnect;
330
331     } else {
332         /* if no handler is provided, make this a synchronous call */
333         this.timeout = (args.timeout) ? args.timeout : 5;
334     }
335
336     message = new osrfMessage({
337         'threadTrace' : this.last_id++, 
338         'type' : OSRF_MESSAGE_TYPE_CONNECT
339     });
340
341     this.send(message, {'timeout' : this.timeout});
342
343     if(this.onconnect || this.state == OSRF_APP_SESSION_CONNECTED)
344         return true;
345
346     return false;
347 };
348
349 OpenSRF.ClientSession.prototype.disconnect = function(args) {
350
351     if (this.state == OSRF_APP_SESSION_CONNECTED) {
352         this.send(
353             new osrfMessage({
354                 'threadTrace' : this.last_id++,
355                 'type' : OSRF_MESSAGE_TYPE_DISCONNECT
356             })
357         );
358     }
359
360     this.remote_id = null;
361     this.state = OSRF_APP_SESSION_DISCONNECTED;
362 };
363
364
365 OpenSRF.ClientSession.prototype.request = function(args) {
366     
367     if(this.state != OSRF_APP_SESSION_CONNECTED)
368         this.remote_id = null;
369         
370     if(typeof args == 'string') { 
371         params = [];
372         for(var i = 1; i < arguments.length; i++)
373             params.push(arguments[i]);
374
375         args = {
376             method : args, 
377             params : params
378         };
379     } else {
380         if(typeof args == 'undefined')
381             args = {};
382     }
383
384     var req = new OpenSRF.Request(this, this.last_id++, args);
385     this.requests.push(req);
386     return req;
387 };
388
389 OpenSRF.ClientSession.prototype.find_request = function(reqid) {
390     for(var i = 0; i < this.requests.length; i++) {
391         var req = this.requests[i];
392         if(req.reqid == reqid)
393             return req;
394     }
395     return null;
396 };
397
398 OpenSRF.Request = function(session, reqid, args) {
399     this.session = session;
400     this.reqid = reqid;
401
402     /* callbacks */
403     this.onresponse = args.onresponse;
404     this.oncomplete = args.oncomplete;
405     this.onerror = args.onerror;
406     this.onmethoderror = args.onmethoderror;
407     this.ontransporterror = args.ontransporterror;
408
409     this.method = args.method;
410     this.params = args.params;
411     this.timeout = args.timeout;
412     this.response_queue = [];
413     this.complete = false;
414 };
415
416 OpenSRF.Request.prototype.peek_last = function(timeout) {
417     if(this.response_queue.length > 0) {
418         var x = this.response_queue.pop();
419         this.response_queue.push(x);
420         return x;
421     }
422     return null;
423 };
424
425 OpenSRF.Request.prototype.peek = function(timeout) {
426     if(this.response_queue.length > 0)
427         return this.response_queue[0];
428     return null;
429 };
430
431 OpenSRF.Request.prototype.recv = function(timeout) {
432     if(this.response_queue.length > 0)
433         return this.response_queue.shift();
434     return null;
435 };
436
437 OpenSRF.Request.prototype.send = function() {
438     method = new osrfMethod({'method':this.method, 'params':this.params});
439     message = new osrfMessage({
440         'threadTrace' : this.reqid, 
441         'type' : OSRF_MESSAGE_TYPE_REQUEST, 
442         'payload' : method, 
443         'locale' : this.session.locale
444     });
445
446     this.session.send(message, {
447         'timeout' : this.timeout,
448         'onresponse' : this.onresponse,
449         'oncomplete' : this.oncomplete,
450         'onerror' : this.onerror,
451         'onmethoderror' : this.onmethoderror,
452         'ontransporterror' : this.ontransporterror
453     });
454 };
455
456 OpenSRF.NetMessage = function(to, from, thread, body, osrf_msg) {
457     this.to = to;
458     this.from = from;
459     this.thread = thread;
460     this.body = body;
461     this.osrf_msg = osrf_msg;
462 };
463
464 OpenSRF.Stack = function() {
465 };
466
467 // global inbound message queue
468 OpenSRF.Stack.queue = [];
469
470 // XXX testing
471 function log(msg) {
472     try {
473         dump(msg + '\n'); // xulrunner
474     } catch(E) {
475         console.log(msg);
476     }
477 }
478
479 // ses may be passed to us by the network handler
480 OpenSRF.Stack.push = function(net_msg, callbacks) {
481     var ses = OpenSRF.Session.find_session(net_msg.thread); 
482     if (!ses) return;
483     ses.remote_id = net_msg.from;
484
485     // NetMessage's from websocket connections are parsed before they get here
486     osrf_msgs = net_msg.osrf_msg;
487
488     if (!osrf_msgs) {
489
490         try {
491             osrf_msgs = JSON2js(net_msg.body);
492
493             // TODO: pretty sure we don't need this..
494             if (OpenSRF.Session.transport == OSRF_TRANSPORT_TYPE_WS) {
495                 // WebSocketRequests wrap the content
496                 osrf_msgs = osrf_msgs.osrf_msg;
497             }
498
499         } catch(E) {
500             log('Error parsing OpenSRF message body as JSON: ' + net_msg.body + '\n' + E);
501
502             /** UGH
503               * For unknown reasons, the Content-Type header will occasionally
504               * be included in the XHR.responseText for multipart/mixed messages.
505               * When this happens, strip the header and newlines from the message
506               * body and re-parse.
507               */
508             net_msg.body = net_msg.body.replace(/^.*\n\n/, '');
509             log('Cleaning up and retrying...');
510
511             try {
512                 osrf_msgs = JSON2js(net_msg.body);
513             } catch(E2) {
514                 log('Unable to clean up message, giving up: ' + net_msg.body);
515                 return;
516             }
517         }
518     }
519
520     // push the latest responses onto the end of the inbound message queue
521     for(var i = 0; i < osrf_msgs.length; i++)
522         OpenSRF.Stack.queue.push({msg : osrf_msgs[i], ses : ses});
523
524     // continue processing responses, oldest to newest
525     while(OpenSRF.Stack.queue.length) {
526         var data = OpenSRF.Stack.queue.shift();
527         OpenSRF.Stack.handle_message(data.ses, data.msg);
528     }
529 };
530
531 OpenSRF.Stack.handle_message = function(ses, osrf_msg) {
532     
533     var req = ses.find_request(osrf_msg.threadTrace());
534
535     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
536
537         var payload = osrf_msg.payload();
538         var status = payload.statusCode();
539         var status_text = payload.status();
540
541         if(status == OSRF_STATUS_COMPLETE) {
542             if(req) {
543                 req.complete = true;
544                 if(req.oncomplete && !req.oncomplete_called) {
545                     req.oncomplete_called = true;
546                     return req.oncomplete(req);
547                 }
548             }
549         }
550
551         if(status == OSRF_STATUS_OK) {
552             ses.state = OSRF_APP_SESSION_CONNECTED;
553
554             /* call the connect callback */
555             if(ses.onconnect && !ses.onconnect_called) {
556                 ses.onconnect_called = true;
557                 return ses.onconnect();
558             }
559         }
560
561         if(status == OSRF_STATUS_NOTFOUND || status == OSRF_STATUS_INTERNALSERVERERROR) {
562             if(req && req.onmethoderror) 
563                 return req.onmethoderror(req, status, status_text);
564         }
565     }
566
567     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
568         if(req) {
569             req.response_queue.push(osrf_msg.payload());
570             if(req.onresponse) {
571                 return req.onresponse(req);
572             }
573         }
574     }
575 };
576
577