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;
66 OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_XHR; /* default to XHR */
67 OpenSRF.Session.cache = {};
68 OpenSRF.Session.find_session = function(thread_trace) {
69 return OpenSRF.Session.cache[thread_trace];
71 OpenSRF.Session.prototype.cleanup = function() {
72 delete OpenSRF.Session.cache[this.thread];
75 OpenSRF.Session.prototype.send = function(osrf_msg, args) {
76 switch(OpenSRF.Session.transport) {
77 case OSRF_TRANSPORT_TYPE_XHR:
78 return this.send_xhr(osrf_msg, args);
79 case OSRF_TRANSPORT_TYPE_XMPP:
80 return this.send_xmpp(osrf_msg, args);
84 OpenSRF.Session.prototype.send_xhr = function(osrf_msg, args) {
85 args.thread = this.thread;
86 args.rcpt = this.remote_id;
87 args.rcpt_service = this.service;
88 new OpenSRF.XHRequest(osrf_msg, args).send();
91 OpenSRF.Session.prototype.send_xmpp = function(osrf_msg, args) {
92 alert('xmpp transport not yet implemented');
96 /* client sessions make requests */
97 OpenSRF.ClientSession = function(service) {
98 this.service = service
99 this.remote_id = null;
100 this.locale = 'en-US';
102 this.thread = Math.random() + '' + new Date().getTime();
103 this.do_connect = false;
105 OpenSRF.Session.cache[this.thread] = this;
107 OpenSRF.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session');
110 OpenSRF.ClientSession.prototype.request = function(args) {
112 if(typeof args == 'string') {
114 for(var i = 1; i < arguments.length; i++)
115 params.push(arguments[i]);
123 var req = new OpenSRF.Request(this, this.last_id++, args);
124 this.requests.push(req);
128 OpenSRF.ClientSession.prototype.find_request = function(reqid) {
129 for(var i = 0; i < this.requests.length; i++) {
130 var req = this.requests[i];
131 if(req.reqid == reqid)
137 OpenSRF.Request = function(session, reqid, args) {
138 this.session = session;
140 this.onresponse = args.onresponse;
141 this.onerror = args.onerror;
142 this.oncomplete = args.oncomplete;
143 this.method = args.method;
144 this.params = args.params;
145 this.timeout = args.timeout;
146 this.response_queue = [];
147 this.complete = false;
150 OpenSRF.Request.prototype.recv = function(timeout) {
151 if(this.response_queue.length > 0)
152 return this.response_queue.shift();
155 OpenSRF.Request.prototype.send = function() {
156 method = new osrfMethod({'method':this.method, 'params':this.params});
157 message = new osrfMessage({
158 'threadTrace' : this.reqid,
159 'type' : OSRF_MESSAGE_TYPE_REQUEST,
161 'locale' : this.session.locale
164 this.session.send(message, {
165 'timeout' : this.timeout,
166 'onresponse' : this.onresponse,
167 'oncomplete' : this.oncomplete,
168 'onerror' : this.onerror
172 OpenSRF.NetMessage = function(to, from, thread, body) {
175 this.thread = thread;
179 OpenSRF.Stack = function() {
182 OpenSRF.Stack.push = function(net_msg, stack_callback) {
183 var ses = OpenSRF.Session.find_session(net_msg.thread);
185 ses.remote_id = net_msg.sender;
186 osrf_msgs = JSON2js(net_msg.body);
187 for(var i = 0; i < osrf_msgs.length; i++)
188 OpenSRF.Stack.handle_message(ses, osrf_msgs[i], stack_callback);
191 OpenSRF.Stack.handle_message = function(ses, osrf_msg, stack_callback) {
195 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
196 var payload = osrf_msg.payload();
197 var status = payload.statusCode();
198 var status_text = payload.status();
200 if(status == OSRF_STATUS_NOTFOUND) {
201 alert('status = ' + status_text);
205 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
206 req = ses.find_request(osrf_msg.threadTrace());
208 req.response_queue.push(osrf_msg.payload());
213 stack_callback(ses, req);
216 /* The following classes map directly to network-serializable opensrf objects */
218 function osrfMessage(hash) {
220 this._encodehash = true;
222 osrfMessage.prototype.threadTrace = function(d) {
223 if(arguments.length == 1)
224 this.hash.threadTrace = d;
225 return this.hash.threadTrace;
227 osrfMessage.prototype.type = function(d) {
228 if(arguments.length == 1)
230 return this.hash.type;
232 osrfMessage.prototype.payload = function(d) {
233 if(arguments.length == 1)
234 this.hash.payload = d;
235 return this.hash.payload;
237 osrfMessage.prototype.locale = function(d) {
238 if(arguments.length == 1)
239 this.hash.locale = d;
240 return this.hash.locale;
242 osrfMessage.prototype.serialize = function() {
246 'threadTrace' : this.hash.threadTrace,
247 'type' : this.hash.type,
248 'payload' : this.hash.payload.serialize(),
249 'locale' : this.hash.locale
254 function osrfMethod(hash) {
256 this._encodehash = true;
258 osrfMethod.prototype.method = function() {
259 if(arguments.length == 1)
260 this.hash.method = d;
261 return this.hash.method;
263 osrfMethod.prototype.params = function() {
264 if(arguments.length == 1)
265 this.hash.params = d;
266 return this.hash.params;
268 osrfMethod.prototype.serialize = function() {
272 'method' : this.hash.method,
273 'params' : this.hash.params
278 function osrfMethodException(hash) {
280 this._encodehash = true;
282 osrfMethodException.prototype.status = function() {
283 if(arguments.length == 1)
284 this.hash.status = d;
285 return this.hash.status;
287 osrfMethodException.prototype.statusCode = function() {
288 if(arguments.length == 1)
289 this.hash.statusCode = d;
290 return this.hash.statusCode;
292 function osrfConnectStatus(hash) {
294 this._encodehash = true;
296 osrfConnectStatus.prototype.status = function() {
297 if(arguments.length == 1)
298 this.hash.status = d;
299 return this.hash.status;
301 osrfConnectStatus.prototype.statusCode = function() {
302 if(arguments.length == 1)
303 this.hash.statusCode = d;
304 return this.hash.statusCode;
306 function osrfResult(hash) {
308 this._encodehash = true;
310 osrfResult.prototype.status = function() {
311 if(arguments.length == 1)
312 this.hash.status = d;
313 return this.hash.status;
315 osrfResult.prototype.statusCode = function() {
316 if(arguments.length == 1)
317 this.hash.statusCode = d;
318 return this.hash.statusCode;
320 osrfResult.prototype.content = function() {
321 if(arguments.length == 1)
322 this.hash.content = d;
323 return this.hash.content;