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