]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/javascript/opensrf.js
07aacdd3478ac581da44825ff018a292c830a03e
[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 }
143
144
145 OpenSRF.ClientSession.prototype.request = function(args) {
146
147     if(typeof args == 'string') { 
148         params = [];
149         for(var i = 1; i < arguments.length; i++)
150             params.push(arguments[i]);
151
152         args = {
153             method : args, 
154             params : params
155         };
156     } else {
157         if(typeof args == 'undefined')
158             args = {};
159     }
160
161     var req = new OpenSRF.Request(this, this.last_id++, args);
162     this.requests.push(req);
163     return req;
164 }
165
166 OpenSRF.ClientSession.prototype.find_request = function(reqid) {
167     for(var i = 0; i < this.requests.length; i++) {
168         var req = this.requests[i];
169         if(req.reqid == reqid)
170             return req;
171     }
172     return null;
173 }
174
175 OpenSRF.Request = function(session, reqid, args) {
176     this.session = session;
177     this.reqid = reqid;
178
179     /* callbacks */
180     this.onresponse = args.onresponse;
181     this.oncomplete = args.oncomplete;
182     this.onerror = args.onerror;
183     this.onmethoderror = args.onmethoderror;
184     this.ontransporterror = args.ontransporterror;
185
186     this.method = args.method;
187     this.params = args.params;
188     this.timeout = args.timeout;
189     this.response_queue = [];
190     this.complete = false;
191 }
192
193 OpenSRF.Request.prototype.recv = function(timeout) {
194     if(this.response_queue.length > 0)
195         return this.response_queue.shift();
196     return null;
197 }
198
199 OpenSRF.Request.prototype.send = function() {
200     method = new osrfMethod({'method':this.method, 'params':this.params});
201     message = new osrfMessage({
202         'threadTrace' : this.reqid, 
203         'type' : OSRF_MESSAGE_TYPE_REQUEST, 
204         'payload' : method, 
205         'locale' : this.session.locale
206     });
207
208     this.session.send(message, {
209         'timeout' : this.timeout,
210         'onresponse' : this.onresponse,
211         'oncomplete' : this.oncomplete,
212         'onerror' : this.onerror,
213         'onmethoderror' : this.onmethoderror,
214         'ontransporterror' : this.ontransporterror
215     });
216 }
217
218 OpenSRF.NetMessage = function(to, from, thread, body) {
219     this.to = to;
220     this.from = from;
221     this.thread = thread;
222     this.body = body;
223 }
224
225 OpenSRF.Stack = function() {
226 }
227
228 OpenSRF.Stack.push = function(net_msg, callbacks) {
229     var ses = OpenSRF.Session.find_session(net_msg.thread); 
230     if(!ses) return;
231     ses.remote_id = net_msg.from;
232     osrf_msgs = JSON2js(net_msg.body);
233     for(var i = 0; i < osrf_msgs.length; i++) 
234         OpenSRF.Stack.handle_message(ses, osrf_msgs[i], callbacks);        
235 }
236
237 OpenSRF.Stack.handle_message = function(ses, osrf_msg, callbacks) {
238     
239     var req = null;
240
241     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
242
243         var payload = osrf_msg.payload();
244         var status = payload.statusCode();
245         var status_text = payload.status();
246
247         if(status == OSRF_STATUS_COMPLETE) {
248             req = ses.find_request(osrf_msg.threadTrace());
249             if(req) {
250                 req.complete = true;
251                 if(callbacks.oncomplete && !req.oncomplete_called) {
252                     req.oncomplete_called = true;
253                     return callbacks.oncomplete(req);
254                 }
255             }
256         }
257
258         if(status == OSRF_STATUS_OK) {
259             ses.state = OSRF_APP_SESSION_CONNECTED;
260
261             /* call the connect callback */
262             if(ses.onconnect && !ses.onconnect_called) {
263                 ses.onconnect_called = true;
264                 return ses.onconnect();
265             }
266         }
267
268         if(status == OSRF_STATUS_NOTFOUND) {
269             req = ses.find_request(osrf_msg.threadTrace());
270             if(callbacks.onmethoderror) 
271                 return callbacks.onmethoderror(req, status, status_text);
272         }
273     }
274
275     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
276         req = ses.find_request(osrf_msg.threadTrace());
277         if(req) {
278             req.response_queue.push(osrf_msg.payload());
279             if(callbacks.onresponse) 
280                 return callbacks.onresponse(req);
281         }
282     }
283 }
284
285 /* The following classes map directly to network-serializable opensrf objects */
286
287 function osrfMessage(hash) {
288     this.hash = hash;
289     if(!this.hash.locale)
290         this.hash.locale = OpenSRF.locale || 'en-US';
291     this._encodehash = true;
292 }
293 osrfMessage.prototype.threadTrace = function(d) { 
294     if(arguments.length == 1) 
295         this.hash.threadTrace = d; 
296     return this.hash.threadTrace; 
297 }
298 osrfMessage.prototype.type = function(d) { 
299     if(arguments.length == 1) 
300         this.hash.type = d; 
301     return this.hash.type; 
302 }
303 osrfMessage.prototype.payload = function(d) { 
304     if(arguments.length == 1) 
305         this.hash.payload = d; 
306     return this.hash.payload; 
307 }
308 osrfMessage.prototype.locale = function(d) { 
309     if(arguments.length == 1) 
310         this.hash.locale = d; 
311     return this.hash.locale; 
312 }
313 osrfMessage.prototype.serialize = function() {
314     return {
315         "__c":"osrfMessage",
316         "__p": {
317             'threadTrace' : this.hash.threadTrace,
318             'type' : this.hash.type,
319             'payload' : (this.hash.payload) ? this.hash.payload.serialize() : 'null',
320             'locale' : this.hash.locale
321         }
322     };
323 }
324
325 function osrfMethod(hash) {
326     this.hash = hash;
327     this._encodehash = true;
328
329 osrfMethod.prototype.method = function() {
330     if(arguments.length == 1) 
331         this.hash.method = d; 
332     return this.hash.method; 
333 }
334 osrfMethod.prototype.params = function() {
335     if(arguments.length == 1) 
336         this.hash.params = d; 
337     return this.hash.params; 
338 }
339 osrfMethod.prototype.serialize = function() {
340     return {
341         "__c":"osrfMethod",
342         "__p": {
343             'method' : this.hash.method,
344             'params' : this.hash.params
345         }
346     };
347 }
348
349 function osrfMethodException(hash) {
350     this.hash = hash;
351     this._encodehash = true;
352 }
353 osrfMethodException.prototype.status = function() {
354     if(arguments.length == 1) 
355         this.hash.status = d; 
356     return this.hash.status; 
357 }
358 osrfMethodException.prototype.statusCode = function() {
359     if(arguments.length == 1) 
360         this.hash.statusCode = d; 
361     return this.hash.statusCode; 
362 }
363 function osrfConnectStatus(hash) { 
364     this.hash = hash;
365     this._encodehash = true;
366 }
367 osrfConnectStatus.prototype.status = function() {
368     if(arguments.length == 1) 
369         this.hash.status = d; 
370     return this.hash.status; 
371 }
372 osrfConnectStatus.prototype.statusCode = function() {
373     if(arguments.length == 1) 
374         this.hash.statusCode = d; 
375     return this.hash.statusCode; 
376 }
377 function osrfResult(hash) {
378     this.hash = hash;
379     this._encodehash = true;
380 }
381 osrfResult.prototype.status = function() {
382     if(arguments.length == 1) 
383         this.hash.status = d; 
384     return this.hash.status; 
385 }
386 osrfResult.prototype.statusCode = function() {
387     if(arguments.length == 1) 
388         this.hash.statusCode = d; 
389     return this.hash.statusCode; 
390 }
391 osrfResult.prototype.content = function() {
392     if(arguments.length == 1) 
393         this.hash.content = d; 
394     return this.hash.content; 
395 }
396
397
398