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