]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/javascript/opensrf.js
3f5cb0f1ede75658de2fe6ff687397e17d91a191
[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     this.onresponse = args.onresponse;
178     this.onerror = args.onerror;
179     this.oncomplete = args.oncomplete;
180     this.method = args.method;
181     this.params = args.params;
182     this.timeout = args.timeout;
183     this.response_queue = [];
184     this.complete = false;
185 }
186
187 OpenSRF.Request.prototype.recv = function(timeout) {
188     if(this.response_queue.length > 0)
189         return this.response_queue.shift();
190     return null;
191 }
192
193 OpenSRF.Request.prototype.send = function() {
194     method = new osrfMethod({'method':this.method, 'params':this.params});
195     message = new osrfMessage({
196         'threadTrace' : this.reqid, 
197         'type' : OSRF_MESSAGE_TYPE_REQUEST, 
198         'payload' : method, 
199         'locale' : this.session.locale
200     });
201
202     this.session.send(message, {
203         'timeout' : this.timeout,
204         'onresponse' : this.onresponse,
205         'oncomplete' : this.oncomplete,
206         'onerror' : this.onerror
207     });
208 }
209
210 OpenSRF.NetMessage = function(to, from, thread, body) {
211     this.to = to;
212     this.from = from;
213     this.thread = thread;
214     this.body = body;
215 }
216
217 OpenSRF.Stack = function() {
218 }
219
220 OpenSRF.Stack.push = function(net_msg, stack_callback) {
221     var ses = OpenSRF.Session.find_session(net_msg.thread); 
222     if(!ses) return;
223     ses.remote_id = net_msg.sender;
224     osrf_msgs = JSON2js(net_msg.body);
225     for(var i = 0; i < osrf_msgs.length; i++) 
226         OpenSRF.Stack.handle_message(ses, osrf_msgs[i], stack_callback);        
227 }
228
229 OpenSRF.Stack.handle_message = function(ses, osrf_msg, stack_callback) {
230     
231     var req = null;
232
233     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
234
235         var payload = osrf_msg.payload();
236         var status = payload.statusCode();
237         var status_text = payload.status();
238
239         if(status == OSRF_STATUS_COMPLETE) {
240             req = ses.find_request(osrf_msg.threadTrace());
241             if(req) req.complete = true;
242         }
243
244         if(status == OSRF_STATUS_OK) {
245             ses.state = OSRF_APP_SESSION_CONNECTED;
246         }
247
248         if(status == OSRF_STATUS_NOTFOUND) {
249             alert('NOT_FOUND: ' + status_text);
250             return;
251         }
252     }
253
254     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
255         req = ses.find_request(osrf_msg.threadTrace());
256         if(req) 
257             req.response_queue.push(osrf_msg.payload());
258     }
259
260     if(stack_callback)
261         stack_callback(ses, req);
262 }
263
264 /* The following classes map directly to network-serializable opensrf objects */
265
266 function osrfMessage(hash) {
267     this.hash = hash;
268     this._encodehash = true;
269 }
270 osrfMessage.prototype.threadTrace = function(d) { 
271     if(arguments.length == 1) 
272         this.hash.threadTrace = d; 
273     return this.hash.threadTrace; 
274 }
275 osrfMessage.prototype.type = function(d) { 
276     if(arguments.length == 1) 
277         this.hash.type = d; 
278     return this.hash.type; 
279 }
280 osrfMessage.prototype.payload = function(d) { 
281     if(arguments.length == 1) 
282         this.hash.payload = d; 
283     return this.hash.payload; 
284 }
285 osrfMessage.prototype.locale = function(d) { 
286     if(arguments.length == 1) 
287         this.hash.locale = d; 
288     return this.hash.locale; 
289 }
290 osrfMessage.prototype.serialize = function() {
291     return {
292         "__c":"osrfMessage",
293         "__p": {
294             'threadTrace' : this.hash.threadTrace,
295             'type' : this.hash.type,
296             'payload' : (this.hash.payload) ? this.hash.payload.serialize() : 'null',
297             'locale' : this.hash.locale
298         }
299     };
300 }
301
302 function osrfMethod(hash) {
303     this.hash = hash;
304     this._encodehash = true;
305
306 osrfMethod.prototype.method = function() {
307     if(arguments.length == 1) 
308         this.hash.method = d; 
309     return this.hash.method; 
310 }
311 osrfMethod.prototype.params = function() {
312     if(arguments.length == 1) 
313         this.hash.params = d; 
314     return this.hash.params; 
315 }
316 osrfMethod.prototype.serialize = function() {
317     return {
318         "__c":"osrfMethod",
319         "__p": {
320             'method' : this.hash.method,
321             'params' : this.hash.params
322         }
323     };
324 }
325
326 function osrfMethodException(hash) {
327     this.hash = hash;
328     this._encodehash = true;
329 }
330 osrfMethodException.prototype.status = function() {
331     if(arguments.length == 1) 
332         this.hash.status = d; 
333     return this.hash.status; 
334 }
335 osrfMethodException.prototype.statusCode = function() {
336     if(arguments.length == 1) 
337         this.hash.statusCode = d; 
338     return this.hash.statusCode; 
339 }
340 function osrfConnectStatus(hash) { 
341     this.hash = hash;
342     this._encodehash = true;
343 }
344 osrfConnectStatus.prototype.status = function() {
345     if(arguments.length == 1) 
346         this.hash.status = d; 
347     return this.hash.status; 
348 }
349 osrfConnectStatus.prototype.statusCode = function() {
350     if(arguments.length == 1) 
351         this.hash.statusCode = d; 
352     return this.hash.statusCode; 
353 }
354 function osrfResult(hash) {
355     this.hash = hash;
356     this._encodehash = true;
357 }
358 osrfResult.prototype.status = function() {
359     if(arguments.length == 1) 
360         this.hash.status = d; 
361     return this.hash.status; 
362 }
363 osrfResult.prototype.statusCode = function() {
364     if(arguments.length == 1) 
365         this.hash.statusCode = d; 
366     return this.hash.statusCode; 
367 }
368 osrfResult.prototype.content = function() {
369     if(arguments.length == 1) 
370         this.hash.content = d; 
371     return this.hash.content; 
372 }
373
374
375