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;
51 /* makes cls a subclass of pcls */
52 OpenSRF.set_subclass = function(cls, pcls) {
53 var str = cls+'.prototype = new '+pcls+'();';
54 str += cls+'.prototype.constructor = '+cls+';';
55 str += cls+'.baseClass = '+pcls+'.prototype.constructor;';
56 str += cls+'.prototype.super = '+pcls+'.prototype;';
61 /* general session superclass */
62 OpenSRF.Session = function() {
63 this.remote_id = null;
64 this.state = OSRF_APP_SESSION_DISCONNECTED;
67 OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_XHR; /* default to XHR */
68 OpenSRF.Session.cache = {};
69 OpenSRF.Session.find_session = function(thread_trace) {
70 return OpenSRF.Session.cache[thread_trace];
72 OpenSRF.Session.prototype.cleanup = function() {
73 delete OpenSRF.Session.cache[this.thread];
76 OpenSRF.Session.prototype.send = function(osrf_msg, args) {
77 args = (args) ? args : {};
78 switch(OpenSRF.Session.transport) {
79 case OSRF_TRANSPORT_TYPE_XHR:
80 return this.send_xhr(osrf_msg, args);
81 case OSRF_TRANSPORT_TYPE_XMPP:
82 return this.send_xmpp(osrf_msg, args);
86 OpenSRF.Session.prototype.send_xhr = function(osrf_msg, args) {
87 args.thread = this.thread;
88 args.rcpt = this.remote_id;
89 args.rcpt_service = this.service;
90 new OpenSRF.XHRequest(osrf_msg, args).send();
93 OpenSRF.Session.prototype.send_xmpp = function(osrf_msg, args) {
94 alert('xmpp transport not yet implemented');
98 /* client sessions make requests */
99 OpenSRF.ClientSession = function(service) {
100 this.service = service
101 this.remote_id = null;
102 this.locale = 'en-US';
104 this.thread = Math.random() + '' + new Date().getTime();
106 this.onconnect = null;
107 OpenSRF.Session.cache[this.thread] = this;
109 OpenSRF.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session');
112 OpenSRF.ClientSession.prototype.connect = function(args) {
113 args = (args) ? args : {};
116 this.onconnect = args.onconnect;
118 /* if no handler is provided, make this a synchronous call */
120 this.timeout = (args.timeout) ? args.timeout : 5;
122 message = new osrfMessage({
123 'threadTrace' : this.reqid,
124 'type' : OSRF_MESSAGE_TYPE_CONNECT,
127 this.send(message, {'timeout' : this.timeout});
129 if(this.onconnect || this.state == OSRF_APP_SESSION_CONNECTED)
134 OpenSRF.ClientSession.prototype.disconnect = function(args) {
137 'threadTrace' : this.reqid,
138 'type' : OSRF_MESSAGE_TYPE_DISCONNECT,
144 OpenSRF.ClientSession.prototype.request = function(args) {
146 if(typeof args == 'string') {
148 for(var i = 1; i < arguments.length; i++)
149 params.push(arguments[i]);
156 if(typeof args == 'undefined')
160 var req = new OpenSRF.Request(this, this.last_id++, args);
161 this.requests.push(req);
165 OpenSRF.ClientSession.prototype.find_request = function(reqid) {
166 for(var i = 0; i < this.requests.length; i++) {
167 var req = this.requests[i];
168 if(req.reqid == reqid)
174 OpenSRF.Request = function(session, reqid, args) {
175 this.session = session;
179 this.onresponse = args.onresponse;
180 this.oncomplete = args.oncomplete;
181 this.onerror = args.onerror;
182 this.onmethoderror = args.onmethoderror;
183 this.ontransporterror = args.ontransporterror;
185 this.method = args.method;
186 this.params = args.params;
187 this.timeout = args.timeout;
188 this.response_queue = [];
189 this.complete = false;
192 OpenSRF.Request.prototype.recv = function(timeout) {
193 if(this.response_queue.length > 0)
194 return this.response_queue.shift();
198 OpenSRF.Request.prototype.send = function() {
199 method = new osrfMethod({'method':this.method, 'params':this.params});
200 message = new osrfMessage({
201 'threadTrace' : this.reqid,
202 'type' : OSRF_MESSAGE_TYPE_REQUEST,
204 'locale' : this.session.locale
207 this.session.send(message, {
208 'timeout' : this.timeout,
209 'onresponse' : this.onresponse,
210 'oncomplete' : this.oncomplete,
211 'onerror' : this.onerror,
212 'onmethoderror' : this.onmethoderror,
213 'ontransporterror' : this.ontransporterror
217 OpenSRF.NetMessage = function(to, from, thread, body) {
220 this.thread = thread;
224 OpenSRF.Stack = function() {
227 OpenSRF.Stack.push = function(net_msg, callbacks) {
228 var ses = OpenSRF.Session.find_session(net_msg.thread);
230 ses.remote_id = net_msg.sender;
231 osrf_msgs = JSON2js(net_msg.body);
232 for(var i = 0; i < osrf_msgs.length; i++)
233 OpenSRF.Stack.handle_message(ses, osrf_msgs[i], callbacks);
236 OpenSRF.Stack.handle_message = function(ses, osrf_msg, callbacks) {
240 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
242 var payload = osrf_msg.payload();
243 var status = payload.statusCode();
244 var status_text = payload.status();
246 if(status == OSRF_STATUS_COMPLETE) {
247 req = ses.find_request(osrf_msg.threadTrace());
250 if(callbacks.oncomplete && !req.oncomplete_called) {
251 req.oncomplete_called = true;
252 return callbacks.oncomplete(req);
257 if(status == OSRF_STATUS_OK) {
258 ses.state = OSRF_APP_SESSION_CONNECTED;
260 /* call the connect callback */
261 if(ses.onconnect && !ses.onconnect_called) {
262 ses.onconnect_called = true;
263 return ses.onconnect();
267 if(status == OSRF_STATUS_NOTFOUND) {
268 req = ses.find_request(osrf_msg.threadTrace());
269 if(callbacks.onmethoderror)
270 return callbacks.onmethoderror(req, status, status_text);
274 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
275 req = ses.find_request(osrf_msg.threadTrace());
277 req.response_queue.push(osrf_msg.payload());
278 if(callbacks.onresponse)
279 return callbacks.onresponse(req);
284 /* The following classes map directly to network-serializable opensrf objects */
286 function osrfMessage(hash) {
288 this._encodehash = true;
290 osrfMessage.prototype.threadTrace = function(d) {
291 if(arguments.length == 1)
292 this.hash.threadTrace = d;
293 return this.hash.threadTrace;
295 osrfMessage.prototype.type = function(d) {
296 if(arguments.length == 1)
298 return this.hash.type;
300 osrfMessage.prototype.payload = function(d) {
301 if(arguments.length == 1)
302 this.hash.payload = d;
303 return this.hash.payload;
305 osrfMessage.prototype.locale = function(d) {
306 if(arguments.length == 1)
307 this.hash.locale = d;
308 return this.hash.locale;
310 osrfMessage.prototype.serialize = function() {
314 'threadTrace' : this.hash.threadTrace,
315 'type' : this.hash.type,
316 'payload' : (this.hash.payload) ? this.hash.payload.serialize() : 'null',
317 'locale' : this.hash.locale
322 function osrfMethod(hash) {
324 this._encodehash = true;
326 osrfMethod.prototype.method = function() {
327 if(arguments.length == 1)
328 this.hash.method = d;
329 return this.hash.method;
331 osrfMethod.prototype.params = function() {
332 if(arguments.length == 1)
333 this.hash.params = d;
334 return this.hash.params;
336 osrfMethod.prototype.serialize = function() {
340 'method' : this.hash.method,
341 'params' : this.hash.params
346 function osrfMethodException(hash) {
348 this._encodehash = true;
350 osrfMethodException.prototype.status = function() {
351 if(arguments.length == 1)
352 this.hash.status = d;
353 return this.hash.status;
355 osrfMethodException.prototype.statusCode = function() {
356 if(arguments.length == 1)
357 this.hash.statusCode = d;
358 return this.hash.statusCode;
360 function osrfConnectStatus(hash) {
362 this._encodehash = true;
364 osrfConnectStatus.prototype.status = function() {
365 if(arguments.length == 1)
366 this.hash.status = d;
367 return this.hash.status;
369 osrfConnectStatus.prototype.statusCode = function() {
370 if(arguments.length == 1)
371 this.hash.statusCode = d;
372 return this.hash.statusCode;
374 function osrfResult(hash) {
376 this._encodehash = true;
378 osrfResult.prototype.status = function() {
379 if(arguments.length == 1)
380 this.hash.status = d;
381 return this.hash.status;
383 osrfResult.prototype.statusCode = function() {
384 if(arguments.length == 1)
385 this.hash.statusCode = d;
386 return this.hash.statusCode;
388 osrfResult.prototype.content = function() {
389 if(arguments.length == 1)
390 this.hash.content = d;
391 return this.hash.content;