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;
177 this.onresponse = args.onresponse;
178 this.onerror = args.onerror;
179 this.oncomplete = args.oncomplete;
180 this.method = args.method;
181 this.params = args.params;
182 this.timeout = args.timeout;
183 this.response_queue = [];
184 this.complete = false;
187 OpenSRF.Request.prototype.recv = function(timeout) {
188 if(this.response_queue.length > 0)
189 return this.response_queue.shift();
193 OpenSRF.Request.prototype.send = function() {
194 method = new osrfMethod({'method':this.method, 'params':this.params});
195 message = new osrfMessage({
196 'threadTrace' : this.reqid,
197 'type' : OSRF_MESSAGE_TYPE_REQUEST,
199 'locale' : this.session.locale
202 this.session.send(message, {
203 'timeout' : this.timeout,
204 'onresponse' : this.onresponse,
205 'oncomplete' : this.oncomplete,
206 'onerror' : this.onerror
210 OpenSRF.NetMessage = function(to, from, thread, body) {
213 this.thread = thread;
217 OpenSRF.Stack = function() {
220 OpenSRF.Stack.push = function(net_msg, stack_callback) {
221 var ses = OpenSRF.Session.find_session(net_msg.thread);
223 ses.remote_id = net_msg.sender;
224 osrf_msgs = JSON2js(net_msg.body);
225 for(var i = 0; i < osrf_msgs.length; i++)
226 OpenSRF.Stack.handle_message(ses, osrf_msgs[i], stack_callback);
229 OpenSRF.Stack.handle_message = function(ses, osrf_msg, stack_callback) {
233 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
235 var payload = osrf_msg.payload();
236 var status = payload.statusCode();
237 var status_text = payload.status();
239 if(status == OSRF_STATUS_COMPLETE) {
240 req = ses.find_request(osrf_msg.threadTrace());
241 if(req) req.complete = true;
244 if(status == OSRF_STATUS_OK) {
245 ses.state = OSRF_APP_SESSION_CONNECTED;
248 if(status == OSRF_STATUS_NOTFOUND) {
249 alert('NOT_FOUND: ' + status_text);
254 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
255 req = ses.find_request(osrf_msg.threadTrace());
257 req.response_queue.push(osrf_msg.payload());
261 stack_callback(ses, req);
264 /* The following classes map directly to network-serializable opensrf objects */
266 function osrfMessage(hash) {
268 this._encodehash = true;
270 osrfMessage.prototype.threadTrace = function(d) {
271 if(arguments.length == 1)
272 this.hash.threadTrace = d;
273 return this.hash.threadTrace;
275 osrfMessage.prototype.type = function(d) {
276 if(arguments.length == 1)
278 return this.hash.type;
280 osrfMessage.prototype.payload = function(d) {
281 if(arguments.length == 1)
282 this.hash.payload = d;
283 return this.hash.payload;
285 osrfMessage.prototype.locale = function(d) {
286 if(arguments.length == 1)
287 this.hash.locale = d;
288 return this.hash.locale;
290 osrfMessage.prototype.serialize = function() {
294 'threadTrace' : this.hash.threadTrace,
295 'type' : this.hash.type,
296 'payload' : (this.hash.payload) ? this.hash.payload.serialize() : 'null',
297 'locale' : this.hash.locale
302 function osrfMethod(hash) {
304 this._encodehash = true;
306 osrfMethod.prototype.method = function() {
307 if(arguments.length == 1)
308 this.hash.method = d;
309 return this.hash.method;
311 osrfMethod.prototype.params = function() {
312 if(arguments.length == 1)
313 this.hash.params = d;
314 return this.hash.params;
316 osrfMethod.prototype.serialize = function() {
320 'method' : this.hash.method,
321 'params' : this.hash.params
326 function osrfMethodException(hash) {
328 this._encodehash = true;
330 osrfMethodException.prototype.status = function() {
331 if(arguments.length == 1)
332 this.hash.status = d;
333 return this.hash.status;
335 osrfMethodException.prototype.statusCode = function() {
336 if(arguments.length == 1)
337 this.hash.statusCode = d;
338 return this.hash.statusCode;
340 function osrfConnectStatus(hash) {
342 this._encodehash = true;
344 osrfConnectStatus.prototype.status = function() {
345 if(arguments.length == 1)
346 this.hash.status = d;
347 return this.hash.status;
349 osrfConnectStatus.prototype.statusCode = function() {
350 if(arguments.length == 1)
351 this.hash.statusCode = d;
352 return this.hash.statusCode;
354 function osrfResult(hash) {
356 this._encodehash = true;
358 osrfResult.prototype.status = function() {
359 if(arguments.length == 1)
360 this.hash.status = d;
361 return this.hash.status;
363 osrfResult.prototype.statusCode = function() {
364 if(arguments.length == 1)
365 this.hash.statusCode = d;
366 return this.hash.statusCode;
368 osrfResult.prototype.content = function() {
369 if(arguments.length == 1)
370 this.hash.content = d;
371 return this.hash.content;