]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/javascript/opensrf.js
6f93379319dc8eb54a0e0d87580b399017d2be8b
[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 }
65
66 OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_XHR; /* default to XHR */
67 OpenSRF.Session.cache = {};
68 OpenSRF.Session.find_session = function(thread_trace) {
69     return OpenSRF.Session.cache[thread_trace];
70 }
71 OpenSRF.Session.prototype.cleanup = function() {
72     delete OpenSRF.Session.cache[this.thread];
73 }
74
75 OpenSRF.Session.prototype.send = function(osrf_msg, args) {
76     switch(OpenSRF.Session.transport) {
77         case OSRF_TRANSPORT_TYPE_XHR:
78             return this.send_xhr(osrf_msg, args);
79         case OSRF_TRANSPORT_TYPE_XMPP:
80             return this.send_xmpp(osrf_msg, args);
81     }
82 }
83
84 OpenSRF.Session.prototype.send_xhr = function(osrf_msg, args) {
85     args.thread = this.thread;
86     args.rcpt = this.remote_id;
87     args.rcpt_service = this.service;
88     new OpenSRF.XHRequest(osrf_msg, args).send();
89 }
90
91 OpenSRF.Session.prototype.send_xmpp = function(osrf_msg, args) {
92     alert('xmpp transport not yet implemented');
93 }
94
95
96 /* client sessions make requests */
97 OpenSRF.ClientSession = function(service) {
98     this.service = service
99     this.remote_id = null;
100     this.locale = 'en-US';
101     this.last_id = 0;
102     this.thread = Math.random() + '' + new Date().getTime();
103     this.do_connect = false;
104     this.requests = [];
105     OpenSRF.Session.cache[this.thread] = this;
106 }
107 OpenSRF.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session');
108
109
110 OpenSRF.ClientSession.prototype.request = function(args) {
111     var req = new OpenSRF.Request(this, this.last_id++, args);
112     this.requests.push(req);
113     req.send();
114 }
115
116 OpenSRF.ClientSession.prototype.find_request = function(reqid) {
117     for(var i = 0; i < this.requests.length; i++) {
118         var req = this.requests[i];
119         if(req.reqid == reqid)
120             return req;
121     }
122     return null;
123 }
124
125 OpenSRF.Request = function(session, reqid, args) {
126     this.session = session;
127     this.reqid = reqid;
128     this.onresponse = args.onresponse;
129     this.onerror = args.onerror;
130     this.oncomplete = args.oncomplete;
131     this.method = args.method;
132     this.params = args.params;
133     this.timeout = args.timeout;
134     this.response_queue = [];
135     this.complete = false;
136 }
137
138 OpenSRF.Request.prototype.recv = function(timeout) {
139     if(this.response_queue.length > 0)
140         return this.response_queue.shift();
141 }
142
143 OpenSRF.Request.prototype.send = function() {
144     method = new osrfMethod({'method':this.method, 'params':this.params});
145     message = new osrfMessage({
146         'threadTrace' : this.reqid, 
147         'type' : OSRF_MESSAGE_TYPE_REQUEST, 
148         'payload' : method, 
149         'locale' : this.session.locale
150     });
151
152     this.session.send(message, {
153         'timeout' : this.timeout,
154         'onresponse' : this.onresponse,
155         'oncomplete' : this.oncomplete,
156         'onerror' : this.onerror
157     });
158 }
159
160 OpenSRF.NetMessage = function(to, from, thread, body) {
161     this.to = to;
162     this.from = from;
163     this.thread = thread;
164     this.body = body;
165 }
166
167 OpenSRF.Stack = function() {
168 }
169
170 OpenSRF.Stack.push = function(net_msg, stack_callback) {
171     var ses = OpenSRF.Session.find_session(net_msg.thread); 
172     if(!ses) return;
173     ses.remote_id = net_msg.sender;
174     osrf_msgs = JSON2js(net_msg.body);
175     for(var i = 0; i < osrf_msgs.length; i++) 
176         OpenSRF.Stack.handle_message(ses, osrf_msgs[i], stack_callback);        
177 }
178
179 OpenSRF.Stack.handle_message = function(ses, osrf_msg, stack_callback) {
180     
181     var req = null;
182
183     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
184         var payload = osrf_msg.payload();
185         var status = payload.statusCode();
186         var status_text = payload.status();
187
188         if(status == OSRF_STATUS_NOTFOUND) {
189             alert('status = ' + status_text);
190         }
191     }
192
193     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
194         req = ses.find_request(osrf_msg.threadTrace());
195         if(req) {
196             req.response_queue.push(osrf_msg.payload());
197         }
198     }
199
200     if(stack_callback)
201         stack_callback(ses, req);
202 }
203
204 /* The following classes map directly to network-serializable opensrf objects */
205
206 function osrfMessage(hash) {
207     this.hash = hash;
208     this._encodehash = true;
209 }
210 osrfMessage.prototype.threadTrace = function(d) { 
211     if(arguments.length == 1) 
212         this.hash.threadTrace = d; 
213     return this.hash.threadTrace; 
214 }
215 osrfMessage.prototype.type = function(d) { 
216     if(arguments.length == 1) 
217         this.hash.type = d; 
218     return this.hash.type; 
219 }
220 osrfMessage.prototype.payload = function(d) { 
221     if(arguments.length == 1) 
222         this.hash.payload = d; 
223     return this.hash.payload; 
224 }
225 osrfMessage.prototype.locale = function(d) { 
226     if(arguments.length == 1) 
227         this.hash.locale = d; 
228     return this.hash.locale; 
229 }
230 osrfMessage.prototype.serialize = function() {
231     return {
232         "__c":"osrfMessage",
233         "__p": {
234             'threadTrace' : this.hash.threadTrace,
235             'type' : this.hash.type,
236             'payload' : this.hash.payload.serialize(),
237             'locale' : this.hash.locale
238         }
239     };
240 }
241
242 function osrfMethod(hash) {
243     this.hash = hash;
244     this._encodehash = true;
245
246 osrfMethod.prototype.method = function() {
247     if(arguments.length == 1) 
248         this.hash.method = d; 
249     return this.hash.method; 
250 }
251 osrfMethod.prototype.params = function() {
252     if(arguments.length == 1) 
253         this.hash.params = d; 
254     return this.hash.params; 
255 }
256 osrfMethod.prototype.serialize = function() {
257     return {
258         "__c":"osrfMethod",
259         "__p": {
260             'method' : this.hash.method,
261             'params' : this.hash.params
262         }
263     };
264 }
265
266 function osrfMethodException(hash) {
267     this.hash = hash;
268     this._encodehash = true;
269 }
270 osrfMethodException.prototype.status = function() {
271     if(arguments.length == 1) 
272         this.hash.status = d; 
273     return this.hash.status; 
274 }
275 osrfMethodException.prototype.statusCode = function() {
276     if(arguments.length == 1) 
277         this.hash.statusCode = d; 
278     return this.hash.statusCode; 
279 }
280 function osrfConnectStatus(hash) { 
281     this.hash = hash;
282     this._encodehash = true;
283 }
284 osrfConnectStatus.prototype.status = function() {
285     if(arguments.length == 1) 
286         this.hash.status = d; 
287     return this.hash.status; 
288 }
289 osrfConnectStatus.prototype.statusCode = function() {
290     if(arguments.length == 1) 
291         this.hash.statusCode = d; 
292     return this.hash.statusCode; 
293 }
294 function osrfResult(hash) {
295     this.hash = hash;
296     this._encodehash = true;
297 }
298 osrfResult.prototype.status = function() {
299     if(arguments.length == 1) 
300         this.hash.status = d; 
301     return this.hash.status; 
302 }
303 osrfResult.prototype.statusCode = function() {
304     if(arguments.length == 1) 
305         this.hash.statusCode = d; 
306     return this.hash.statusCode; 
307 }
308 osrfResult.prototype.content = function() {
309     if(arguments.length == 1) 
310         this.hash.content = d; 
311     return this.hash.content; 
312 }
313
314
315