]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/javascript/opensrf.js
dfff52a78581adae6d3d2b2ab4985209d6382026
[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 /* The following classes map directly to network-serializable opensrf objects */
50
51 function osrfMessage(hash) {
52     this.hash = hash;
53     if(!this.hash.locale)
54         this.hash.locale = OpenSRF.locale || 'en-US';
55     this._encodehash = true;
56 }
57 osrfMessage.prototype.threadTrace = function(d) { 
58     if(arguments.length == 1) 
59         this.hash.threadTrace = d; 
60     return this.hash.threadTrace; 
61 };
62 osrfMessage.prototype.type = function(d) { 
63     if(arguments.length == 1) 
64         this.hash.type = d; 
65     return this.hash.type; 
66 };
67 osrfMessage.prototype.payload = function(d) { 
68     if(arguments.length == 1) 
69         this.hash.payload = d; 
70     return this.hash.payload; 
71 };
72 osrfMessage.prototype.locale = function(d) { 
73     if(arguments.length == 1) 
74         this.hash.locale = d; 
75     return this.hash.locale; 
76 };
77 osrfMessage.prototype.serialize = function() {
78     return {
79         "__c":"osrfMessage",
80         "__p": {
81             'threadTrace' : this.hash.threadTrace,
82             'type' : this.hash.type,
83             'payload' : (this.hash.payload) ? this.hash.payload.serialize() : 'null',
84             'locale' : this.hash.locale
85         }
86     };
87 };
88
89 function osrfMethod(hash) {
90     this.hash = hash;
91     this._encodehash = true;
92 }
93 osrfMethod.prototype.method = function(d) {
94     if(arguments.length == 1) 
95         this.hash.method = d; 
96     return this.hash.method; 
97 };
98 osrfMethod.prototype.params = function(d) {
99     if(arguments.length == 1) 
100         this.hash.params = d; 
101     return this.hash.params; 
102 };
103 osrfMethod.prototype.serialize = function() {
104     return {
105         "__c":"osrfMethod",
106         "__p": {
107             'method' : this.hash.method,
108             'params' : this.hash.params
109         }
110     };
111 };
112
113 function osrfMethodException(hash) {
114     this.hash = hash;
115     this._encodehash = true;
116 }
117 osrfMethodException.prototype.status = function(d) {
118     if(arguments.length == 1) 
119         this.hash.status = d; 
120     return this.hash.status; 
121 };
122 osrfMethodException.prototype.statusCode = function(d) {
123     if(arguments.length == 1) 
124         this.hash.statusCode = d; 
125     return this.hash.statusCode; 
126 };
127 function osrfConnectStatus(hash) { 
128     this.hash = hash;
129     this._encodehash = true;
130 }
131 osrfConnectStatus.prototype.status = function(d) {
132     if(arguments.length == 1) 
133         this.hash.status = d; 
134     return this.hash.status; 
135 };
136 osrfConnectStatus.prototype.statusCode = function(d) {
137     if(arguments.length == 1) 
138         this.hash.statusCode = d; 
139     return this.hash.statusCode; 
140 };
141 function osrfResult(hash) {
142     this.hash = hash;
143     this._encodehash = true;
144 }
145 osrfResult.prototype.status = function(d) {
146     if(arguments.length == 1) 
147         this.hash.status = d; 
148     return this.hash.status; 
149 };
150 osrfResult.prototype.statusCode = function(d) {
151     if(arguments.length == 1) 
152         this.hash.statusCode = d; 
153     return this.hash.statusCode; 
154 };
155 osrfResult.prototype.content = function(d) {
156     if(arguments.length == 1) 
157         this.hash.content = d; 
158     return this.hash.content; 
159 };
160 function osrfServerError(hash) { 
161     this.hash = hash;
162     this._encodehash = true;
163 }
164 osrfServerError.prototype.status = function(d) {
165     if(arguments.length == 1) 
166         this.hash.status = d; 
167     return this.hash.status; 
168 };
169 osrfServerError.prototype.statusCode = function(d) {
170     if(arguments.length == 1) 
171         this.hash.statusCode = d; 
172     return this.hash.statusCode; 
173 };
174 function osrfContinueStatus(hash) { 
175     this.hash = hash;
176     this._encodehash = true;
177 }
178 osrfContinueStatus.prototype.status = function(d) {
179     if(arguments.length == 1) 
180         this.hash.status = d; 
181     return this.hash.status; 
182 };
183 osrfContinueStatus.prototype.statusCode = function(d) {
184     if(arguments.length == 1) 
185         this.hash.statusCode = d; 
186     return this.hash.statusCode; 
187 };
188
189 OpenSRF = {};
190 OpenSRF.locale = null;
191
192 /* makes cls a subclass of pcls */
193 OpenSRF.set_subclass = function(cls, pcls) {
194     var str = cls+'.prototype = new '+pcls+'();';
195     str += cls+'.prototype.constructor = '+cls+';';
196     str += cls+'.baseClass = '+pcls+'.prototype.constructor;';
197     str += cls+'.prototype["super"] = '+pcls+'.prototype;';
198     eval(str);
199 };
200
201
202 /* general session superclass */
203 OpenSRF.Session = function() {
204     this.remote_id = null;
205     this.state = OSRF_APP_SESSION_DISCONNECTED;
206 };
207
208 OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_XHR; /* default to XHR */
209 OpenSRF.Session.cache = {};
210 OpenSRF.Session.find_session = function(thread_trace) {
211     return OpenSRF.Session.cache[thread_trace];
212 };
213 OpenSRF.Session.prototype.cleanup = function() {
214     delete OpenSRF.Session.cache[this.thread];
215 };
216
217 OpenSRF.Session.prototype.send = function(osrf_msg, args) {
218     args = (args) ? args : {};
219     switch(OpenSRF.Session.transport) {
220         case OSRF_TRANSPORT_TYPE_XHR:
221             return this.send_xhr(osrf_msg, args);
222         case OSRF_TRANSPORT_TYPE_XMPP:
223             return this.send_xmpp(osrf_msg, args);
224     }
225 };
226
227 OpenSRF.Session.prototype.send_xhr = function(osrf_msg, args) {
228     args.thread = this.thread;
229     args.rcpt = this.remote_id;
230     args.rcpt_service = this.service;
231     new OpenSRF.XHRequest(osrf_msg, args).send();
232 };
233
234 OpenSRF.Session.prototype.send_xmpp = function(osrf_msg, args) {
235     alert('xmpp transport not yet implemented');
236 };
237
238
239 /* client sessions make requests */
240 OpenSRF.ClientSession = function(service) {
241     this.service = service;
242     this.remote_id = null;
243     this.locale = OpenSRF.locale || 'en-US';
244     this.last_id = 0;
245     this.thread = Math.random() + '' + new Date().getTime();
246     this.requests = [];
247     this.onconnect = null;
248     OpenSRF.Session.cache[this.thread] = this;
249 };
250 OpenSRF.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session');
251
252
253 OpenSRF.ClientSession.prototype.connect = function(args) {
254     args = (args) ? args : {};
255     this.remote_id = null;
256
257     if(args.onconnect)
258         this.onconnect = args.onconnect;
259
260     /* if no handler is provided, make this a synchronous call */
261     if(!this.onconnect) 
262         this.timeout = (args.timeout) ? args.timeout : 5;
263
264     message = new osrfMessage({
265         'threadTrace' : this.reqid, 
266         'type' : OSRF_MESSAGE_TYPE_CONNECT
267     });
268
269     this.send(message, {'timeout' : this.timeout});
270
271     if(this.onconnect || this.state == OSRF_APP_SESSION_CONNECTED)
272         return true;
273     return false;
274 };
275
276 OpenSRF.ClientSession.prototype.disconnect = function(args) {
277     this.send(
278         new osrfMessage({
279             'threadTrace' : this.reqid, 
280             'type' : OSRF_MESSAGE_TYPE_DISCONNECT
281         })
282     );
283     this.remote_id = null;
284 };
285
286
287 OpenSRF.ClientSession.prototype.request = function(args) {
288     
289     if(this.state != OSRF_APP_SESSION_CONNECTED)
290         this.remote_id = null;
291         
292     if(typeof args == 'string') { 
293         params = [];
294         for(var i = 1; i < arguments.length; i++)
295             params.push(arguments[i]);
296
297         args = {
298             method : args, 
299             params : params
300         };
301     } else {
302         if(typeof args == 'undefined')
303             args = {};
304     }
305
306     var req = new OpenSRF.Request(this, this.last_id++, args);
307     this.requests.push(req);
308     return req;
309 };
310
311 OpenSRF.ClientSession.prototype.find_request = function(reqid) {
312     for(var i = 0; i < this.requests.length; i++) {
313         var req = this.requests[i];
314         if(req.reqid == reqid)
315             return req;
316     }
317     return null;
318 };
319
320 OpenSRF.Request = function(session, reqid, args) {
321     this.session = session;
322     this.reqid = reqid;
323
324     /* callbacks */
325     this.onresponse = args.onresponse;
326     this.oncomplete = args.oncomplete;
327     this.onerror = args.onerror;
328     this.onmethoderror = args.onmethoderror;
329     this.ontransporterror = args.ontransporterror;
330
331     this.method = args.method;
332     this.params = args.params;
333     this.timeout = args.timeout;
334     this.response_queue = [];
335     this.complete = false;
336 };
337
338 OpenSRF.Request.prototype.peek_last = function(timeout) {
339     if(this.response_queue.length > 0) {
340         var x = this.response_queue.pop();
341         this.response_queue.push(x);
342         return x;
343     }
344     return null;
345 };
346
347 OpenSRF.Request.prototype.peek = function(timeout) {
348     if(this.response_queue.length > 0)
349         return this.response_queue[0];
350     return null;
351 };
352
353 OpenSRF.Request.prototype.recv = function(timeout) {
354     if(this.response_queue.length > 0)
355         return this.response_queue.shift();
356     return null;
357 };
358
359 OpenSRF.Request.prototype.send = function() {
360     method = new osrfMethod({'method':this.method, 'params':this.params});
361     message = new osrfMessage({
362         'threadTrace' : this.reqid, 
363         'type' : OSRF_MESSAGE_TYPE_REQUEST, 
364         'payload' : method, 
365         'locale' : this.session.locale
366     });
367
368     this.session.send(message, {
369         'timeout' : this.timeout,
370         'onresponse' : this.onresponse,
371         'oncomplete' : this.oncomplete,
372         'onerror' : this.onerror,
373         'onmethoderror' : this.onmethoderror,
374         'ontransporterror' : this.ontransporterror
375     });
376 };
377
378 OpenSRF.NetMessage = function(to, from, thread, body) {
379     this.to = to;
380     this.from = from;
381     this.thread = thread;
382     this.body = body;
383 };
384
385 OpenSRF.Stack = function() {
386 };
387
388 // global inbound message queue
389 OpenSRF.Stack.queue = [];
390
391 OpenSRF.Stack.push = function(net_msg, callbacks) {
392     var ses = OpenSRF.Session.find_session(net_msg.thread); 
393     if(!ses) return;
394     ses.remote_id = net_msg.from;
395     osrf_msgs = JSON2js(net_msg.body);
396
397     // push the latest responses onto the end of the inbound message queue
398     for(var i = 0; i < osrf_msgs.length; i++)
399         OpenSRF.Stack.queue.push({msg : osrf_msgs[i], callbacks : callbacks, ses : ses});
400
401     // continue processing responses, oldest to newest
402     while(OpenSRF.Stack.queue.length) {
403         var data = OpenSRF.Stack.queue.shift();
404         OpenSRF.Stack.handle_message(data.ses, data.msg, data.callbacks);
405     }
406 };
407
408 OpenSRF.Stack.handle_message = function(ses, osrf_msg, callbacks) {
409     
410     var req = null;
411
412     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
413
414         var payload = osrf_msg.payload();
415         var status = payload.statusCode();
416         var status_text = payload.status();
417
418         if(status == OSRF_STATUS_COMPLETE) {
419             req = ses.find_request(osrf_msg.threadTrace());
420             if(req) {
421                 req.complete = true;
422                 if(callbacks.oncomplete && !req.oncomplete_called) {
423                     req.oncomplete_called = true;
424                     return callbacks.oncomplete(req);
425                 }
426             }
427         }
428
429         if(status == OSRF_STATUS_OK) {
430             ses.state = OSRF_APP_SESSION_CONNECTED;
431
432             /* call the connect callback */
433             if(ses.onconnect && !ses.onconnect_called) {
434                 ses.onconnect_called = true;
435                 return ses.onconnect();
436             }
437         }
438
439         if(status == OSRF_STATUS_NOTFOUND || status == OSRF_STATUS_INTERNALSERVERERROR) {
440             req = ses.find_request(osrf_msg.threadTrace());
441             if(callbacks.onmethoderror) 
442                 return callbacks.onmethoderror(req, status, status_text);
443         }
444     }
445
446     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
447         req = ses.find_request(osrf_msg.threadTrace());
448         if(req) {
449             req.response_queue.push(osrf_msg.payload());
450             if(callbacks.onresponse) 
451                 return callbacks.onresponse(req);
452         }
453     }
454 };
455
456