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