1 /* -----------------------------------------------------------------------
2 * Copyright (C) 2008 Georgia Public Library Service
3 * Bill Erickson <erickson@esilibrary.com>
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.
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 * ----------------------------------------------------------------------- */
17 var OSRF_APP_SESSION_CONNECTED = 0;
18 var OSRF_APP_SESSION_CONNECTING = 1;
19 var OSRF_APP_SESSION_DISCONNECTED = 2;
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;
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';
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;
51 /* The following classes map directly to network-serializable opensrf objects */
53 function osrfMessage(hash) {
56 this.hash.locale = OpenSRF.locale || 'en-US';
57 this._encodehash = true;
59 osrfMessage.prototype.threadTrace = function(d) {
60 if(arguments.length == 1)
61 this.hash.threadTrace = d;
62 return this.hash.threadTrace;
64 osrfMessage.prototype.type = function(d) {
65 if(arguments.length == 1)
67 return this.hash.type;
69 osrfMessage.prototype.payload = function(d) {
70 if(arguments.length == 1)
71 this.hash.payload = d;
72 return this.hash.payload;
74 osrfMessage.prototype.locale = function(d) {
75 if(arguments.length == 1)
77 return this.hash.locale;
79 osrfMessage.prototype.serialize = function() {
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
91 function osrfMethod(hash) {
93 this._encodehash = true;
95 osrfMethod.prototype.method = function(d) {
96 if(arguments.length == 1)
98 return this.hash.method;
100 osrfMethod.prototype.params = function(d) {
101 if(arguments.length == 1)
102 this.hash.params = d;
103 return this.hash.params;
105 osrfMethod.prototype.serialize = function() {
109 'method' : this.hash.method,
110 'params' : this.hash.params
115 function osrfMethodException(hash) {
117 this._encodehash = true;
119 osrfMethodException.prototype.status = function(d) {
120 if(arguments.length == 1)
121 this.hash.status = d;
122 return this.hash.status;
124 osrfMethodException.prototype.statusCode = function(d) {
125 if(arguments.length == 1)
126 this.hash.statusCode = d;
127 return this.hash.statusCode;
129 function osrfConnectStatus(hash) {
131 this._encodehash = true;
133 osrfConnectStatus.prototype.status = function(d) {
134 if(arguments.length == 1)
135 this.hash.status = d;
136 return this.hash.status;
138 osrfConnectStatus.prototype.statusCode = function(d) {
139 if(arguments.length == 1)
140 this.hash.statusCode = d;
141 return this.hash.statusCode;
143 function osrfResult(hash) {
145 this._encodehash = true;
147 osrfResult.prototype.status = function(d) {
148 if(arguments.length == 1)
149 this.hash.status = d;
150 return this.hash.status;
152 osrfResult.prototype.statusCode = function(d) {
153 if(arguments.length == 1)
154 this.hash.statusCode = d;
155 return this.hash.statusCode;
157 osrfResult.prototype.content = function(d) {
158 if(arguments.length == 1)
159 this.hash.content = d;
160 return this.hash.content;
162 function osrfServerError(hash) {
164 this._encodehash = true;
166 osrfServerError.prototype.status = function(d) {
167 if(arguments.length == 1)
168 this.hash.status = d;
169 return this.hash.status;
171 osrfServerError.prototype.statusCode = function(d) {
172 if(arguments.length == 1)
173 this.hash.statusCode = d;
174 return this.hash.statusCode;
176 function osrfContinueStatus(hash) {
178 this._encodehash = true;
180 osrfContinueStatus.prototype.status = function(d) {
181 if(arguments.length == 1)
182 this.hash.status = d;
183 return this.hash.status;
185 osrfContinueStatus.prototype.statusCode = function(d) {
186 if(arguments.length == 1)
187 this.hash.statusCode = d;
188 return this.hash.statusCode;
192 OpenSRF.locale = null;
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;';
204 /* general session superclass */
205 OpenSRF.Session = function() {
206 this.remote_id = null;
207 this.state = OSRF_APP_SESSION_DISCONNECTED;
210 //OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_WS;
211 OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_XHR;
213 OpenSRF.Session.cache = {};
214 OpenSRF.Session.find_session = function(thread_trace) {
215 return OpenSRF.Session.cache[thread_trace];
217 OpenSRF.Session.prototype.cleanup = function() {
218 delete OpenSRF.Session.cache[this.thread];
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);
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();
242 OpenSRF.Session.prototype.send_ws = function(osrf_msg) {
243 new OpenSRF.WebSocketRequest(
245 function(wsreq) {wsreq.send(osrf_msg)} // onopen
249 OpenSRF.Session.setup_shared_ws = function(onconnect) {
251 OpenSRF.sharedWSWorker = new SharedWorker('opensrf_ws_shared.js');
253 OpenSRF.sharedWSWorker.port.addEventListener('message', function(e) {
255 console.log('sharedWSWorker received message ' + data.action);
257 if (data.action == 'message') {
258 // pass all inbound message up the opensrf stack
260 var msg = JSON2js(data.message); // TODO: json error handling
262 new OpenSRF.NetMessage(
263 null, null, msg.thread, null, msg.osrf_msg)
269 if (data.action == 'socket_connected') {
270 if (onconnect) onconnect();
274 if (data.action == 'error') {
275 throw new Error(data.message);
279 OpenSRF.sharedWSWorker.port.start();
282 OpenSRF.Session.prototype.send_ws_shared = function(message) {
285 service : this.service,
286 thread : this.thread,
287 osrf_msg : [message.serialize()]
290 OpenSRF.sharedWSWorker.port.postMessage({
292 // pass the thread additionally as a stand-alone value so the
293 // worker can more efficiently inspect it.
294 thread : this.thread,
300 OpenSRF.Session.prototype.send_xmpp = function(osrf_msg, args) {
301 alert('xmpp transport not implemented');
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';
312 this.onconnect = null;
313 this.thread = Math.random() + '' + new Date().getTime();
314 OpenSRF.Session.cache[this.thread] = this;
316 OpenSRF.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session');
319 OpenSRF.ClientSession.prototype.connect = function(args) {
320 args = (args) ? args : {};
321 this.remote_id = null;
323 if (this.state == OSRF_APP_SESSION_CONNECTED) {
324 if (args.onconnect) args.onconnect();
329 this.onconnect = args.onconnect;
332 /* if no handler is provided, make this a synchronous call */
333 this.timeout = (args.timeout) ? args.timeout : 5;
336 message = new osrfMessage({
337 'threadTrace' : this.last_id++,
338 'type' : OSRF_MESSAGE_TYPE_CONNECT
341 this.send(message, {'timeout' : this.timeout});
343 if(this.onconnect || this.state == OSRF_APP_SESSION_CONNECTED)
349 OpenSRF.ClientSession.prototype.disconnect = function(args) {
351 if (this.state == OSRF_APP_SESSION_CONNECTED) {
354 'threadTrace' : this.last_id++,
355 'type' : OSRF_MESSAGE_TYPE_DISCONNECT
360 this.remote_id = null;
361 this.state = OSRF_APP_SESSION_DISCONNECTED;
365 OpenSRF.ClientSession.prototype.request = function(args) {
367 if(this.state != OSRF_APP_SESSION_CONNECTED)
368 this.remote_id = null;
370 if(typeof args == 'string') {
372 for(var i = 1; i < arguments.length; i++)
373 params.push(arguments[i]);
380 if(typeof args == 'undefined')
384 var req = new OpenSRF.Request(this, this.last_id++, args);
385 this.requests.push(req);
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)
398 OpenSRF.Request = function(session, reqid, args) {
399 this.session = session;
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;
409 this.method = args.method;
410 this.params = args.params;
411 this.timeout = args.timeout;
412 this.response_queue = [];
413 this.complete = false;
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);
425 OpenSRF.Request.prototype.peek = function(timeout) {
426 if(this.response_queue.length > 0)
427 return this.response_queue[0];
431 OpenSRF.Request.prototype.recv = function(timeout) {
432 if(this.response_queue.length > 0)
433 return this.response_queue.shift();
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,
443 'locale' : this.session.locale
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
456 OpenSRF.NetMessage = function(to, from, thread, body, osrf_msg) {
459 this.thread = thread;
461 this.osrf_msg = osrf_msg;
464 OpenSRF.Stack = function() {
467 // global inbound message queue
468 OpenSRF.Stack.queue = [];
473 dump(msg + '\n'); // xulrunner
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);
483 ses.remote_id = net_msg.from;
485 // NetMessage's from websocket connections are parsed before they get here
486 osrf_msgs = net_msg.osrf_msg;
491 osrf_msgs = JSON2js(net_msg.body);
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;
500 log('Error parsing OpenSRF message body as JSON: ' + net_msg.body + '\n' + E);
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
508 net_msg.body = net_msg.body.replace(/^.*\n\n/, '');
509 log('Cleaning up and retrying...');
512 osrf_msgs = JSON2js(net_msg.body);
514 log('Unable to clean up message, giving up: ' + net_msg.body);
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});
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);
531 OpenSRF.Stack.handle_message = function(ses, osrf_msg) {
533 var req = ses.find_request(osrf_msg.threadTrace());
535 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
537 var payload = osrf_msg.payload();
538 var status = payload.statusCode();
539 var status_text = payload.status();
541 if(status == OSRF_STATUS_COMPLETE) {
544 if(req.oncomplete && !req.oncomplete_called) {
545 req.oncomplete_called = true;
546 return req.oncomplete(req);
551 if(status == OSRF_STATUS_OK) {
552 ses.state = OSRF_APP_SESSION_CONNECTED;
554 /* call the connect callback */
555 if(ses.onconnect && !ses.onconnect_called) {
556 ses.onconnect_called = true;
557 return ses.onconnect();
561 if(status == OSRF_STATUS_NOTFOUND || status == OSRF_STATUS_INTERNALSERVERERROR) {
562 if(req && req.onmethoderror)
563 return req.onmethoderror(req, status, status_text);
567 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
569 req.response_queue.push(osrf_msg.payload());
571 return req.onresponse(req);