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;
26 var OSRF_MESSAGE_TYPE_REQUEST = 'REQUEST';
27 var OSRF_MESSAGE_TYPE_STATUS = 'STATUS';
28 var OSRF_MESSAGE_TYPE_RESULT = 'RESULT';
29 var OSRF_MESSAGE_TYPE_CONNECT = 'CONNECT';
30 var OSRF_MESSAGE_TYPE_DISCONNECT = 'DISCONNECT';
32 /* message statuses */
33 var OSRF_STATUS_CONTINUE = 100;
34 var OSRF_STATUS_OK = 200;
35 var OSRF_STATUS_ACCEPTED = 202;
36 var OSRF_STATUS_COMPLETE = 205;
37 var OSRF_STATUS_REDIRECTED = 307;
38 var OSRF_STATUS_BADREQUEST = 400;
39 var OSRF_STATUS_UNAUTHORIZED = 401;
40 var OSRF_STATUS_FORBIDDEN = 403;
41 var OSRF_STATUS_NOTFOUND = 404;
42 var OSRF_STATUS_NOTALLOWED = 405;
43 var OSRF_STATUS_TIMEOUT = 408;
44 var OSRF_STATUS_EXPFAILED = 417;
45 var OSRF_STATUS_INTERNALSERVERERROR = 500;
46 var OSRF_STATUS_NOTIMPLEMENTED = 501;
47 var OSRF_STATUS_VERSIONNOTSUPPORTED = 505;
50 OpenSRF.locale = null;
52 /* makes cls a subclass of pcls */
53 OpenSRF.set_subclass = function(cls, pcls) {
54 var str = cls+'.prototype = new '+pcls+'();';
55 str += cls+'.prototype.constructor = '+cls+';';
56 str += cls+'.baseClass = '+pcls+'.prototype.constructor;';
57 str += cls+'.prototype["super"] = '+pcls+'.prototype;';
62 /* general session superclass */
63 OpenSRF.Session = function() {
64 this.remote_id = null;
65 this.state = OSRF_APP_SESSION_DISCONNECTED;
68 OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_XHR; /* default to XHR */
69 OpenSRF.Session.cache = {};
70 OpenSRF.Session.find_session = function(thread_trace) {
71 return OpenSRF.Session.cache[thread_trace];
73 OpenSRF.Session.prototype.cleanup = function() {
74 delete OpenSRF.Session.cache[this.thread];
77 OpenSRF.Session.prototype.send = function(osrf_msg, args) {
78 args = (args) ? args : {};
79 switch(OpenSRF.Session.transport) {
80 case OSRF_TRANSPORT_TYPE_XHR:
81 return this.send_xhr(osrf_msg, args);
82 case OSRF_TRANSPORT_TYPE_XMPP:
83 return this.send_xmpp(osrf_msg, args);
87 OpenSRF.Session.prototype.send_xhr = function(osrf_msg, args) {
88 args.thread = this.thread;
89 args.rcpt = this.remote_id;
90 args.rcpt_service = this.service;
91 new OpenSRF.XHRequest(osrf_msg, args).send();
94 OpenSRF.Session.prototype.send_xmpp = function(osrf_msg, args) {
95 alert('xmpp transport not yet implemented');
99 /* client sessions make requests */
100 OpenSRF.ClientSession = function(service) {
101 this.service = service
102 this.remote_id = null;
103 this.locale = OpenSRF.locale || 'en-US';
105 this.thread = Math.random() + '' + new Date().getTime();
107 this.onconnect = null;
108 OpenSRF.Session.cache[this.thread] = this;
110 OpenSRF.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session');
113 OpenSRF.ClientSession.prototype.connect = function(args) {
114 args = (args) ? args : {};
115 this.remote_id = null;
118 this.onconnect = args.onconnect;
120 /* if no handler is provided, make this a synchronous call */
122 this.timeout = (args.timeout) ? args.timeout : 5;
124 message = new osrfMessage({
125 'threadTrace' : this.reqid,
126 'type' : OSRF_MESSAGE_TYPE_CONNECT
129 this.send(message, {'timeout' : this.timeout});
131 if(this.onconnect || this.state == OSRF_APP_SESSION_CONNECTED)
136 OpenSRF.ClientSession.prototype.disconnect = function(args) {
139 'threadTrace' : this.reqid,
140 'type' : OSRF_MESSAGE_TYPE_DISCONNECT
143 this.remote_id = null;
147 OpenSRF.ClientSession.prototype.request = function(args) {
149 if(this.state != OSRF_APP_SESSION_CONNECTED)
150 this.remote_id = null;
152 if(typeof args == 'string') {
154 for(var i = 1; i < arguments.length; i++)
155 params.push(arguments[i]);
162 if(typeof args == 'undefined')
166 var req = new OpenSRF.Request(this, this.last_id++, args);
167 this.requests.push(req);
171 OpenSRF.ClientSession.prototype.find_request = function(reqid) {
172 for(var i = 0; i < this.requests.length; i++) {
173 var req = this.requests[i];
174 if(req.reqid == reqid)
180 OpenSRF.Request = function(session, reqid, args) {
181 this.session = session;
185 this.onresponse = args.onresponse;
186 this.oncomplete = args.oncomplete;
187 this.onerror = args.onerror;
188 this.onmethoderror = args.onmethoderror;
189 this.ontransporterror = args.ontransporterror;
191 this.method = args.method;
192 this.params = args.params;
193 this.timeout = args.timeout;
194 this.response_queue = [];
195 this.complete = false;
198 OpenSRF.Request.prototype.peek_last = function(timeout) {
199 if(this.response_queue.length > 0) {
200 var x = this.response_queue.pop();
201 this.response_queue.push(x);
207 OpenSRF.Request.prototype.peek = function(timeout) {
208 if(this.response_queue.length > 0)
209 return this.response_queue[0];
213 OpenSRF.Request.prototype.recv = function(timeout) {
214 if(this.response_queue.length > 0)
215 return this.response_queue.shift();
219 OpenSRF.Request.prototype.send = function() {
220 method = new osrfMethod({'method':this.method, 'params':this.params});
221 message = new osrfMessage({
222 'threadTrace' : this.reqid,
223 'type' : OSRF_MESSAGE_TYPE_REQUEST,
225 'locale' : this.session.locale
228 this.session.send(message, {
229 'timeout' : this.timeout,
230 'onresponse' : this.onresponse,
231 'oncomplete' : this.oncomplete,
232 'onerror' : this.onerror,
233 'onmethoderror' : this.onmethoderror,
234 'ontransporterror' : this.ontransporterror
238 OpenSRF.NetMessage = function(to, from, thread, body) {
241 this.thread = thread;
245 OpenSRF.Stack = function() {
248 OpenSRF.Stack.push = function(net_msg, callbacks) {
249 var ses = OpenSRF.Session.find_session(net_msg.thread);
251 ses.remote_id = net_msg.from;
252 osrf_msgs = JSON2js(net_msg.body);
253 for(var i = 0; i < osrf_msgs.length; i++)
254 OpenSRF.Stack.handle_message(ses, osrf_msgs[i], callbacks);
257 OpenSRF.Stack.handle_message = function(ses, osrf_msg, callbacks) {
261 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
263 var payload = osrf_msg.payload();
264 var status = payload.statusCode();
265 var status_text = payload.status();
267 if(status == OSRF_STATUS_COMPLETE) {
268 req = ses.find_request(osrf_msg.threadTrace());
271 if(callbacks.oncomplete && !req.oncomplete_called) {
272 req.oncomplete_called = true;
273 return callbacks.oncomplete(req);
278 if(status == OSRF_STATUS_OK) {
279 ses.state = OSRF_APP_SESSION_CONNECTED;
281 /* call the connect callback */
282 if(ses.onconnect && !ses.onconnect_called) {
283 ses.onconnect_called = true;
284 return ses.onconnect();
288 if(status == OSRF_STATUS_NOTFOUND || status == OSRF_STATUS_INTERNALSERVERERROR) {
289 req = ses.find_request(osrf_msg.threadTrace());
290 if(callbacks.onmethoderror)
291 return callbacks.onmethoderror(req, status, status_text);
295 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
296 req = ses.find_request(osrf_msg.threadTrace());
298 req.response_queue.push(osrf_msg.payload());
299 if(callbacks.onresponse)
300 return callbacks.onresponse(req);
305 /* The following classes map directly to network-serializable opensrf objects */
307 function osrfMessage(hash) {
309 if(!this.hash.locale)
310 this.hash.locale = OpenSRF.locale || 'en-US';
311 this._encodehash = true;
313 osrfMessage.prototype.threadTrace = function(d) {
314 if(arguments.length == 1)
315 this.hash.threadTrace = d;
316 return this.hash.threadTrace;
318 osrfMessage.prototype.type = function(d) {
319 if(arguments.length == 1)
321 return this.hash.type;
323 osrfMessage.prototype.payload = function(d) {
324 if(arguments.length == 1)
325 this.hash.payload = d;
326 return this.hash.payload;
328 osrfMessage.prototype.locale = function(d) {
329 if(arguments.length == 1)
330 this.hash.locale = d;
331 return this.hash.locale;
333 osrfMessage.prototype.serialize = function() {
337 'threadTrace' : this.hash.threadTrace,
338 'type' : this.hash.type,
339 'payload' : (this.hash.payload) ? this.hash.payload.serialize() : 'null',
340 'locale' : this.hash.locale
345 function osrfMethod(hash) {
347 this._encodehash = true;
349 osrfMethod.prototype.method = function() {
350 if(arguments.length == 1)
351 this.hash.method = d;
352 return this.hash.method;
354 osrfMethod.prototype.params = function() {
355 if(arguments.length == 1)
356 this.hash.params = d;
357 return this.hash.params;
359 osrfMethod.prototype.serialize = function() {
363 'method' : this.hash.method,
364 'params' : this.hash.params
369 function osrfMethodException(hash) {
371 this._encodehash = true;
373 osrfMethodException.prototype.status = function() {
374 if(arguments.length == 1)
375 this.hash.status = d;
376 return this.hash.status;
378 osrfMethodException.prototype.statusCode = function() {
379 if(arguments.length == 1)
380 this.hash.statusCode = d;
381 return this.hash.statusCode;
383 function osrfConnectStatus(hash) {
385 this._encodehash = true;
387 osrfConnectStatus.prototype.status = function() {
388 if(arguments.length == 1)
389 this.hash.status = d;
390 return this.hash.status;
392 osrfConnectStatus.prototype.statusCode = function() {
393 if(arguments.length == 1)
394 this.hash.statusCode = d;
395 return this.hash.statusCode;
397 function osrfResult(hash) {
399 this._encodehash = true;
401 osrfResult.prototype.status = function() {
402 if(arguments.length == 1)
403 this.hash.status = d;
404 return this.hash.status;
406 osrfResult.prototype.statusCode = function() {
407 if(arguments.length == 1)
408 this.hash.statusCode = d;
409 return this.hash.statusCode;
411 osrfResult.prototype.content = function() {
412 if(arguments.length == 1)
413 this.hash.content = d;
414 return this.hash.content;
416 function osrfServerError(hash) {
418 this._encodehash = true;
420 osrfServerError.prototype.status = function() {
421 if(arguments.length == 1)
422 this.hash.status = d;
423 return this.hash.status;
425 osrfServerError.prototype.statusCode = function() {
426 if(arguments.length == 1)
427 this.hash.statusCode = d;
428 return this.hash.statusCode;