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) {
111 var req = new OpenSRF.Request(this, this.last_id++, args);
112 this.requests.push(req);
116 OpenSRF.ClientSession.prototype.find_request = function(reqid) {
117 for(var i = 0; i < this.requests.length; i++) {
118 var req = this.requests[i];
119 if(req.reqid == reqid)
125 OpenSRF.Request = function(session, reqid, args) {
126 this.session = session;
128 this.onresponse = args.onresponse;
129 this.onerror = args.onerror;
130 this.oncomplete = args.oncomplete;
131 this.method = args.method;
132 this.params = args.params;
133 this.timeout = args.timeout;
134 this.response_queue = [];
135 this.complete = false;
138 OpenSRF.Request.prototype.recv = function(timeout) {
139 if(this.response_queue.length > 0)
140 return this.response_queue.shift();
143 OpenSRF.Request.prototype.send = function() {
144 method = new osrfMethod({'method':this.method, 'params':this.params});
145 message = new osrfMessage({
146 'threadTrace' : this.reqid,
147 'type' : OSRF_MESSAGE_TYPE_REQUEST,
149 'locale' : this.session.locale
152 this.session.send(message, {
153 'timeout' : this.timeout,
154 'onresponse' : this.onresponse,
155 'oncomplete' : this.oncomplete,
156 'onerror' : this.onerror
160 OpenSRF.NetMessage = function(to, from, thread, body) {
163 this.thread = thread;
167 OpenSRF.Stack = function() {
170 OpenSRF.Stack.push = function(net_msg, stack_callback) {
171 var ses = OpenSRF.Session.find_session(net_msg.thread);
173 ses.remote_id = net_msg.sender;
174 osrf_msgs = JSON2js(net_msg.body);
175 for(var i = 0; i < osrf_msgs.length; i++)
176 OpenSRF.Stack.handle_message(ses, osrf_msgs[i], stack_callback);
179 OpenSRF.Stack.handle_message = function(ses, osrf_msg, stack_callback) {
183 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
184 var payload = osrf_msg.payload();
185 var status = payload.statusCode();
186 var status_text = payload.status();
188 if(status == OSRF_STATUS_NOTFOUND) {
189 alert('status = ' + status_text);
193 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
194 req = ses.find_request(osrf_msg.threadTrace());
196 req.response_queue.push(osrf_msg.payload());
201 stack_callback(ses, req);
204 /* The following classes map directly to network-serializable opensrf objects */
206 function osrfMessage(hash) {
208 this._encodehash = true;
210 osrfMessage.prototype.threadTrace = function(d) {
211 if(arguments.length == 1)
212 this.hash.threadTrace = d;
213 return this.hash.threadTrace;
215 osrfMessage.prototype.type = function(d) {
216 if(arguments.length == 1)
218 return this.hash.type;
220 osrfMessage.prototype.payload = function(d) {
221 if(arguments.length == 1)
222 this.hash.payload = d;
223 return this.hash.payload;
225 osrfMessage.prototype.locale = function(d) {
226 if(arguments.length == 1)
227 this.hash.locale = d;
228 return this.hash.locale;
230 osrfMessage.prototype.serialize = function() {
234 'threadTrace' : this.hash.threadTrace,
235 'type' : this.hash.type,
236 'payload' : this.hash.payload.serialize(),
237 'locale' : this.hash.locale
242 function osrfMethod(hash) {
244 this._encodehash = true;
246 osrfMethod.prototype.method = function() {
247 if(arguments.length == 1)
248 this.hash.method = d;
249 return this.hash.method;
251 osrfMethod.prototype.params = function() {
252 if(arguments.length == 1)
253 this.hash.params = d;
254 return this.hash.params;
256 osrfMethod.prototype.serialize = function() {
260 'method' : this.hash.method,
261 'params' : this.hash.params
266 function osrfMethodException(hash) {
268 this._encodehash = true;
270 osrfMethodException.prototype.status = function() {
271 if(arguments.length == 1)
272 this.hash.status = d;
273 return this.hash.status;
275 osrfMethodException.prototype.statusCode = function() {
276 if(arguments.length == 1)
277 this.hash.statusCode = d;
278 return this.hash.statusCode;
280 function osrfConnectStatus(hash) {
282 this._encodehash = true;
284 osrfConnectStatus.prototype.status = function() {
285 if(arguments.length == 1)
286 this.hash.status = d;
287 return this.hash.status;
289 osrfConnectStatus.prototype.statusCode = function() {
290 if(arguments.length == 1)
291 this.hash.statusCode = d;
292 return this.hash.statusCode;
294 function osrfResult(hash) {
296 this._encodehash = true;
298 osrfResult.prototype.status = function() {
299 if(arguments.length == 1)
300 this.hash.status = d;
301 return this.hash.status;
303 osrfResult.prototype.statusCode = function() {
304 if(arguments.length == 1)
305 this.hash.statusCode = d;
306 return this.hash.statusCode;
308 osrfResult.prototype.content = function() {
309 if(arguments.length == 1)
310 this.hash.content = d;
311 return this.hash.content;