]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/javascript/opensrf.js
e52c14540256af65a659ef428a302b2494fb1fb2
[OpenSRF.git] / src / javascript / opensrf.js
1 /* -----------------------------------------------------------------------
2  * Copyright (C) 2008  Georgia Public Library Service
3  * Bill Erickson <erickson@esilibrary.com>
4  *  
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.
9  * 
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  * ----------------------------------------------------------------------- */
15
16 /* session states */
17 var OSRF_APP_SESSION_CONNECTED = 0;
18 var OSRF_APP_SESSION_CONNECTING = 1;
19 var OSRF_APP_SESSION_DISCONNECTED = 2;
20
21 /* types of transport layers */
22 var OSRF_TRANSPORT_TYPE_XHR = 1;
23 var OSRF_TRANSPORT_TYPE_XMPP = 2;
24
25 /* message types */
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';
31
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;
48
49 var OpenSRF = {};
50
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;';
57     eval(str);
58 }
59
60
61 /* general session superclass */
62 OpenSRF.Session = function() {
63     this.remote_id = null;
64 }
65
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];
70 }
71 OpenSRF.Session.prototype.cleanup = function() {
72     delete OpenSRF.Session.cache[this.thread];
73 }
74
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);
81     }
82 }
83
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();
89 }
90
91 OpenSRF.Session.prototype.send_xmpp = function(osrf_msg, args) {
92     alert('xmpp transport not yet implemented');
93 }
94
95
96 /* client sessions make requests */
97 OpenSRF.ClientSession = function(service) {
98     this.service = service
99     this.remote_id = null;
100     this.locale = 'en-US';
101     this.last_id = 0;
102     this.thread = Math.random() + '' + new Date().getTime();
103     this.do_connect = false;
104     this.requests = [];
105     OpenSRF.Session.cache[this.thread] = this;
106 }
107 OpenSRF.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session');
108
109
110 OpenSRF.ClientSession.prototype.request = function(args) {
111
112     if(typeof args == 'string') { 
113         params = [];
114         for(var i = 1; i < arguments.length; i++)
115             params.push(arguments[i]);
116
117         args = {
118             method : args, 
119             params : params
120         };
121     }
122
123     var req = new OpenSRF.Request(this, this.last_id++, args);
124     this.requests.push(req);
125     return req;
126 }
127
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)
132             return req;
133     }
134     return null;
135 }
136
137 OpenSRF.Request = function(session, reqid, args) {
138     this.session = session;
139     this.reqid = reqid;
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;
148 }
149
150 OpenSRF.Request.prototype.recv = function(timeout) {
151     if(this.response_queue.length > 0)
152         return this.response_queue.shift();
153 }
154
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, 
160         'payload' : method, 
161         'locale' : this.session.locale
162     });
163
164     this.session.send(message, {
165         'timeout' : this.timeout,
166         'onresponse' : this.onresponse,
167         'oncomplete' : this.oncomplete,
168         'onerror' : this.onerror
169     });
170 }
171
172 OpenSRF.NetMessage = function(to, from, thread, body) {
173     this.to = to;
174     this.from = from;
175     this.thread = thread;
176     this.body = body;
177 }
178
179 OpenSRF.Stack = function() {
180 }
181
182 OpenSRF.Stack.push = function(net_msg, stack_callback) {
183     var ses = OpenSRF.Session.find_session(net_msg.thread); 
184     if(!ses) return;
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);        
189 }
190
191 OpenSRF.Stack.handle_message = function(ses, osrf_msg, stack_callback) {
192     
193     var req = null;
194
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();
199
200         if(status == OSRF_STATUS_NOTFOUND) {
201             alert('status = ' + status_text);
202         }
203     }
204
205     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
206         req = ses.find_request(osrf_msg.threadTrace());
207         if(req) {
208             req.response_queue.push(osrf_msg.payload());
209         }
210     }
211
212     if(stack_callback)
213         stack_callback(ses, req);
214 }
215
216 /* The following classes map directly to network-serializable opensrf objects */
217
218 function osrfMessage(hash) {
219     this.hash = hash;
220     this._encodehash = true;
221 }
222 osrfMessage.prototype.threadTrace = function(d) { 
223     if(arguments.length == 1) 
224         this.hash.threadTrace = d; 
225     return this.hash.threadTrace; 
226 }
227 osrfMessage.prototype.type = function(d) { 
228     if(arguments.length == 1) 
229         this.hash.type = d; 
230     return this.hash.type; 
231 }
232 osrfMessage.prototype.payload = function(d) { 
233     if(arguments.length == 1) 
234         this.hash.payload = d; 
235     return this.hash.payload; 
236 }
237 osrfMessage.prototype.locale = function(d) { 
238     if(arguments.length == 1) 
239         this.hash.locale = d; 
240     return this.hash.locale; 
241 }
242 osrfMessage.prototype.serialize = function() {
243     return {
244         "__c":"osrfMessage",
245         "__p": {
246             'threadTrace' : this.hash.threadTrace,
247             'type' : this.hash.type,
248             'payload' : this.hash.payload.serialize(),
249             'locale' : this.hash.locale
250         }
251     };
252 }
253
254 function osrfMethod(hash) {
255     this.hash = hash;
256     this._encodehash = true;
257
258 osrfMethod.prototype.method = function() {
259     if(arguments.length == 1) 
260         this.hash.method = d; 
261     return this.hash.method; 
262 }
263 osrfMethod.prototype.params = function() {
264     if(arguments.length == 1) 
265         this.hash.params = d; 
266     return this.hash.params; 
267 }
268 osrfMethod.prototype.serialize = function() {
269     return {
270         "__c":"osrfMethod",
271         "__p": {
272             'method' : this.hash.method,
273             'params' : this.hash.params
274         }
275     };
276 }
277
278 function osrfMethodException(hash) {
279     this.hash = hash;
280     this._encodehash = true;
281 }
282 osrfMethodException.prototype.status = function() {
283     if(arguments.length == 1) 
284         this.hash.status = d; 
285     return this.hash.status; 
286 }
287 osrfMethodException.prototype.statusCode = function() {
288     if(arguments.length == 1) 
289         this.hash.statusCode = d; 
290     return this.hash.statusCode; 
291 }
292 function osrfConnectStatus(hash) { 
293     this.hash = hash;
294     this._encodehash = true;
295 }
296 osrfConnectStatus.prototype.status = function() {
297     if(arguments.length == 1) 
298         this.hash.status = d; 
299     return this.hash.status; 
300 }
301 osrfConnectStatus.prototype.statusCode = function() {
302     if(arguments.length == 1) 
303         this.hash.statusCode = d; 
304     return this.hash.statusCode; 
305 }
306 function osrfResult(hash) {
307     this.hash = hash;
308     this._encodehash = true;
309 }
310 osrfResult.prototype.status = function() {
311     if(arguments.length == 1) 
312         this.hash.status = d; 
313     return this.hash.status; 
314 }
315 osrfResult.prototype.statusCode = function() {
316     if(arguments.length == 1) 
317         this.hash.statusCode = d; 
318     return this.hash.statusCode; 
319 }
320 osrfResult.prototype.content = function() {
321     if(arguments.length == 1) 
322         this.hash.content = d; 
323     return this.hash.content; 
324 }
325
326
327