e18efa530d336545bcd66cb3f4250a2265291bd0
[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 /* -----------------------------------------------------------------------
17  * Portions of this file are Copyright (c) Jon Nylander
18  *
19  * jsTimezoneDetect is released under the MIT License
20  *  - http://www.opensource.org/licenses/mit-license.php
21  *
22  * For usage and examples, visit: http://pellepim.bitbucket.org/jstz/
23  * ----------------------------------------------------------------------- */
24 (function(e){var t=function(){"use strict";var e="s",n=function(e){var t=-e.getTimezoneOffset();return t!==null?t:0},r=function(e,t,n){var r=new Date;return e!==undefined&&r.setFullYear(e),r.setDate(n),r.setMonth(t),r},i=function(e){return n(r(e,0,2))},s=function(e){return n(r(e,5,2))},o=function(e){var t=e.getMonth()>7?s(e.getFullYear()):i(e.getFullYear()),r=n(e);return t-r!==0},u=function(){var t=i(),n=s(),r=i()-s();return r<0?t+",1":r>0?n+",1,"+e:t+",0"},a=function(){var e=u();return new t.TimeZone(t.olson.timezones[e])},f=function(e){var t=new Date(2010,6,15,1,0,0,0),n={"America/Denver":new Date(2011,2,13,3,0,0,0),"America/Mazatlan":new Date(2011,3,3,3,0,0,0),"America/Chicago":new Date(2011,2,13,3,0,0,0),"America/Mexico_City":new Date(2011,3,3,3,0,0,0),"America/Asuncion":new Date(2012,9,7,3,0,0,0),"America/Santiago":new Date(2012,9,3,3,0,0,0),"America/Campo_Grande":new Date(2012,9,21,5,0,0,0),"America/Montevideo":new Date(2011,9,2,3,0,0,0),"America/Sao_Paulo":new Date(2011,9,16,5,0,0,0),"America/Los_Angeles":new Date(2011,2,13,8,0,0,0),"America/Santa_Isabel":new Date(2011,3,5,8,0,0,0),"America/Havana":new Date(2012,2,10,2,0,0,0),"America/New_York":new Date(2012,2,10,7,0,0,0),"Asia/Beirut":new Date(2011,2,27,1,0,0,0),"Europe/Helsinki":new Date(2011,2,27,4,0,0,0),"Europe/Istanbul":new Date(2011,2,28,5,0,0,0),"Asia/Damascus":new Date(2011,3,1,2,0,0,0),"Asia/Jerusalem":new Date(2011,3,1,6,0,0,0),"Asia/Gaza":new Date(2009,2,28,0,30,0,0),"Africa/Cairo":new Date(2009,3,25,0,30,0,0),"Pacific/Auckland":new Date(2011,8,26,7,0,0,0),"Pacific/Fiji":new Date(2010,11,29,23,0,0,0),"America/Halifax":new Date(2011,2,13,6,0,0,0),"America/Goose_Bay":new Date(2011,2,13,2,1,0,0),"America/Miquelon":new Date(2011,2,13,5,0,0,0),"America/Godthab":new Date(2011,2,27,1,0,0,0),"Europe/Moscow":t,"Asia/Yekaterinburg":t,"Asia/Omsk":t,"Asia/Krasnoyarsk":t,"Asia/Irkutsk":t,"Asia/Yakutsk":t,"Asia/Vladivostok":t,"Asia/Kamchatka":t,"Europe/Minsk":t,"Australia/Perth":new Date(2008,10,1,1,0,0,0)};return n[e]};return{determine:a,date_is_dst:o,dst_start_for:f}}();t.TimeZone=function(e){"use strict";var n={"America/Denver":["America/Denver","America/Mazatlan"],"America/Chicago":["America/Chicago","America/Mexico_City"],"America/Santiago":["America/Santiago","America/Asuncion","America/Campo_Grande"],"America/Montevideo":["America/Montevideo","America/Sao_Paulo"],"Asia/Beirut":["Asia/Beirut","Europe/Helsinki","Europe/Istanbul","Asia/Damascus","Asia/Jerusalem","Asia/Gaza"],"Pacific/Auckland":["Pacific/Auckland","Pacific/Fiji"],"America/Los_Angeles":["America/Los_Angeles","America/Santa_Isabel"],"America/New_York":["America/Havana","America/New_York"],"America/Halifax":["America/Goose_Bay","America/Halifax"],"America/Godthab":["America/Miquelon","America/Godthab"],"Asia/Dubai":["Europe/Moscow"],"Asia/Dhaka":["Asia/Yekaterinburg"],"Asia/Jakarta":["Asia/Omsk"],"Asia/Shanghai":["Asia/Krasnoyarsk","Australia/Perth"],"Asia/Tokyo":["Asia/Irkutsk"],"Australia/Brisbane":["Asia/Yakutsk"],"Pacific/Noumea":["Asia/Vladivostok"],"Pacific/Tarawa":["Asia/Kamchatka"],"Africa/Johannesburg":["Asia/Gaza","Africa/Cairo"],"Asia/Baghdad":["Europe/Minsk"]},r=e,i=function(){var e=n[r],i=e.length,s=0,o=e[0];for(;s<i;s+=1){o=e[s];if(t.date_is_dst(t.dst_start_for(o))){r=o;return}}},s=function(){return typeof n[r]!="undefined"};return s()&&i(),{name:function(){return r}}},t.olson={},t.olson.timezones={"-720,0":"Etc/GMT+12","-660,0":"Pacific/Pago_Pago","-600,1":"America/Adak","-600,0":"Pacific/Honolulu","-570,0":"Pacific/Marquesas","-540,0":"Pacific/Gambier","-540,1":"America/Anchorage","-480,1":"America/Los_Angeles","-480,0":"Pacific/Pitcairn","-420,0":"America/Phoenix","-420,1":"America/Denver","-360,0":"America/Guatemala","-360,1":"America/Chicago","-360,1,s":"Pacific/Easter","-300,0":"America/Bogota","-300,1":"America/New_York","-270,0":"America/Caracas","-240,1":"America/Halifax","-240,0":"America/Santo_Domingo","-240,1,s":"America/Santiago","-210,1":"America/St_Johns","-180,1":"America/Godthab","-180,0":"America/Argentina/Buenos_Aires","-180,1,s":"America/Montevideo","-120,0":"Etc/GMT+2","-120,1":"Etc/GMT+2","-60,1":"Atlantic/Azores","-60,0":"Atlantic/Cape_Verde","0,0":"Etc/UTC","0,1":"Europe/London","60,1":"Europe/Berlin","60,0":"Africa/Lagos","60,1,s":"Africa/Windhoek","120,1":"Asia/Beirut","120,0":"Africa/Johannesburg","180,0":"Asia/Baghdad","180,1":"Europe/Moscow","210,1":"Asia/Tehran","240,0":"Asia/Dubai","240,1":"Asia/Baku","270,0":"Asia/Kabul","300,1":"Asia/Yekaterinburg","300,0":"Asia/Karachi","330,0":"Asia/Kolkata","345,0":"Asia/Kathmandu","360,0":"Asia/Dhaka","360,1":"Asia/Omsk","390,0":"Asia/Rangoon","420,1":"Asia/Krasnoyarsk","420,0":"Asia/Jakarta","480,0":"Asia/Shanghai","480,1":"Asia/Irkutsk","525,0":"Australia/Eucla","525,1,s":"Australia/Eucla","540,1":"Asia/Yakutsk","540,0":"Asia/Tokyo","570,0":"Australia/Darwin","570,1,s":"Australia/Adelaide","600,0":"Australia/Brisbane","600,1":"Asia/Vladivostok","600,1,s":"Australia/Sydney","630,1,s":"Australia/Lord_Howe","660,1":"Asia/Kamchatka","660,0":"Pacific/Noumea","690,0":"Pacific/Norfolk","720,1,s":"Pacific/Auckland","720,0":"Pacific/Tarawa","765,1,s":"Pacific/Chatham","780,0":"Pacific/Tongatapu","780,1,s":"Pacific/Apia","840,0":"Pacific/Kiritimati"},typeof exports!="undefined"?exports.jstz=t:e.jstz=t})(this);
25
26 /* session states */
27 var OSRF_APP_SESSION_CONNECTED = 0;
28 var OSRF_APP_SESSION_CONNECTING = 1;
29 var OSRF_APP_SESSION_DISCONNECTED = 2;
30
31 /* types of transport layers */
32 var OSRF_TRANSPORT_TYPE_XHR = 1;
33 var OSRF_TRANSPORT_TYPE_XMPP = 2;
34 var OSRF_TRANSPORT_TYPE_WS = 3;
35 var OSRF_TRANSPORT_TYPE_WS_SHARED = 4;
36
37 /* message types */
38 var OSRF_MESSAGE_TYPE_REQUEST = 'REQUEST';
39 var OSRF_MESSAGE_TYPE_STATUS = 'STATUS';
40 var OSRF_MESSAGE_TYPE_RESULT = 'RESULT';
41 var OSRF_MESSAGE_TYPE_CONNECT = 'CONNECT';
42 var OSRF_MESSAGE_TYPE_DISCONNECT = 'DISCONNECT';
43
44 /* message statuses */
45 var OSRF_STATUS_CONTINUE = 100;
46 var OSRF_STATUS_OK = 200;
47 var OSRF_STATUS_ACCEPTED = 202;
48 var OSRF_STATUS_NOCONTENT = 204;
49 var OSRF_STATUS_COMPLETE = 205;
50 var OSRF_STATUS_PARTIAL = 206;
51 var OSRF_STATUS_REDIRECTED = 307;
52 var OSRF_STATUS_BADREQUEST = 400;
53 var OSRF_STATUS_UNAUTHORIZED = 401;
54 var OSRF_STATUS_FORBIDDEN = 403;
55 var OSRF_STATUS_NOTFOUND = 404;
56 var OSRF_STATUS_NOTALLOWED = 405;
57 var OSRF_STATUS_TIMEOUT = 408;
58 var OSRF_STATUS_EXPFAILED = 417;
59 var OSRF_STATUS_INTERNALSERVERERROR = 500;
60 var OSRF_STATUS_NOTIMPLEMENTED = 501;
61 var OSRF_STATUS_VERSIONNOTSUPPORTED = 505;
62
63 // TODO: get path from ./configure prefix
64 var SHARED_WORKER_LIB = '/js/dojo/opensrf/opensrf_ws_shared.js'; 
65
66 /* The following classes map directly to network-serializable opensrf objects */
67
68 function osrfMessage(hash) {
69     this.hash = hash;
70     if(!this.hash.locale)
71         this.hash.locale = OpenSRF.locale || 'en-US';
72     if(!this.hash.tz)
73         this.hash.tz = jstz.determine().name()
74     this._encodehash = true;
75 }
76 osrfMessage.prototype.threadTrace = function(d) { 
77     if(arguments.length == 1) 
78         this.hash.threadTrace = d; 
79     return this.hash.threadTrace; 
80 };
81 osrfMessage.prototype.type = function(d) { 
82     if(arguments.length == 1) 
83         this.hash.type = d; 
84     return this.hash.type; 
85 };
86 osrfMessage.prototype.payload = function(d) { 
87     if(arguments.length == 1) 
88         this.hash.payload = d; 
89     return this.hash.payload; 
90 };
91 osrfMessage.prototype.locale = function(d) { 
92     if(arguments.length == 1) 
93         this.hash.locale = d; 
94     return this.hash.locale; 
95 };
96 osrfMessage.prototype.tz = function(d) { 
97     if(arguments.length == 1) 
98         this.hash.tz = d; 
99     return this.hash.tz; 
100 };
101 osrfMessage.prototype.api_level = function(d) { 
102     if(arguments.length == 1) 
103         this.hash.api_level = d; 
104     return this.hash.api_level; 
105 };
106 osrfMessage.prototype.serialize = function() {
107     return {
108         "__c":"osrfMessage",
109         "__p": {
110             'threadTrace' : this.hash.threadTrace,
111             'type' : this.hash.type,
112             'payload' : (this.hash.payload) ? this.hash.payload.serialize() : 'null',
113             'locale' : this.hash.locale,
114             'tz' : this.hash.tz,
115             'api_level' : this.hash.api_level
116         }
117     };
118 };
119
120 function osrfMethod(hash) {
121     this.hash = hash;
122     this._encodehash = true;
123 }
124 osrfMethod.prototype.method = function(d) {
125     if(arguments.length == 1) 
126         this.hash.method = d; 
127     return this.hash.method; 
128 };
129 osrfMethod.prototype.params = function(d) {
130     if(arguments.length == 1) 
131         this.hash.params = d; 
132     return this.hash.params; 
133 };
134 osrfMethod.prototype.serialize = function() {
135     return {
136         "__c":"osrfMethod",
137         "__p": {
138             'method' : this.hash.method,
139             'params' : this.hash.params
140         }
141     };
142 };
143
144 function osrfMethodException(hash) {
145     this.hash = hash;
146     this._encodehash = true;
147 }
148 osrfMethodException.prototype.status = function(d) {
149     if(arguments.length == 1) 
150         this.hash.status = d; 
151     return this.hash.status; 
152 };
153 osrfMethodException.prototype.statusCode = function(d) {
154     if(arguments.length == 1) 
155         this.hash.statusCode = d; 
156     return this.hash.statusCode; 
157 };
158 function osrfConnectStatus(hash) { 
159     this.hash = hash;
160     this._encodehash = true;
161 }
162 osrfConnectStatus.prototype.status = function(d) {
163     if(arguments.length == 1) 
164         this.hash.status = d; 
165     return this.hash.status; 
166 };
167 osrfConnectStatus.prototype.statusCode = function(d) {
168     if(arguments.length == 1) 
169         this.hash.statusCode = d; 
170     return this.hash.statusCode; 
171 };
172 function osrfResult(hash) {
173     this.hash = hash;
174     this._encodehash = true;
175 }
176 osrfResult.prototype.status = function(d) {
177     if(arguments.length == 1) 
178         this.hash.status = d; 
179     return this.hash.status; 
180 };
181 osrfResult.prototype.statusCode = function(d) {
182     if(arguments.length == 1) 
183         this.hash.statusCode = d; 
184     return this.hash.statusCode; 
185 };
186 osrfResult.prototype.content = function(d) {
187     if(arguments.length == 1) 
188         this.hash.content = d; 
189     return this.hash.content; 
190 };
191 function osrfResultPartial(hash) {
192     this.hash = hash;
193     this._encodehash = true;
194 }
195 osrfResultPartial.prototype.status = function(d) {
196     if(arguments.length == 1) 
197         this.hash.status = d; 
198     return this.hash.status; 
199 };
200 osrfResultPartial.prototype.statusCode = function(d) {
201     if(arguments.length == 1) 
202         this.hash.statusCode = d; 
203     return this.hash.statusCode; 
204 };
205 osrfResultPartial.prototype.content = function(d) {
206     if(arguments.length == 1) 
207         this.hash.content = d; 
208     return this.hash.content; 
209 };
210 function osrfResultPartialComplete(hash) {
211     this.hash = hash;
212     this._encodehash = true;
213 }
214 osrfResultPartialComplete.prototype.status = function(d) {
215     if(arguments.length == 1) 
216         this.hash.status = d; 
217     return this.hash.status; 
218 };
219 osrfResultPartialComplete.prototype.statusCode = function(d) {
220     if(arguments.length == 1) 
221         this.hash.statusCode = d; 
222     return this.hash.statusCode; 
223 };
224 osrfResultPartialComplete.prototype.content = function(d) {
225     if(arguments.length == 1) 
226         this.hash.content = d; 
227     return this.hash.content; 
228 };
229
230 function osrfServerError(hash) { 
231     this.hash = hash;
232     this._encodehash = true;
233 }
234 osrfServerError.prototype.status = function(d) {
235     if(arguments.length == 1) 
236         this.hash.status = d; 
237     return this.hash.status; 
238 };
239 osrfServerError.prototype.statusCode = function(d) {
240     if(arguments.length == 1) 
241         this.hash.statusCode = d; 
242     return this.hash.statusCode; 
243 };
244 function osrfContinueStatus(hash) { 
245     this.hash = hash;
246     this._encodehash = true;
247 }
248 osrfContinueStatus.prototype.status = function(d) {
249     if(arguments.length == 1) 
250         this.hash.status = d; 
251     return this.hash.status; 
252 };
253 osrfContinueStatus.prototype.statusCode = function(d) {
254     if(arguments.length == 1) 
255         this.hash.statusCode = d; 
256     return this.hash.statusCode; 
257 };
258
259 OpenSRF = {};
260 OpenSRF.tz = jstz.determine().name();
261 OpenSRF.locale = null;
262 OpenSRF.api_level = 1;
263
264 /* makes cls a subclass of pcls */
265 OpenSRF.set_subclass = function(cls, pcls) {
266     var str = cls+'.prototype = new '+pcls+'();';
267     str += cls+'.prototype.constructor = '+cls+';';
268     str += cls+'.baseClass = '+pcls+'.prototype.constructor;';
269     str += cls+'.prototype["super"] = '+pcls+'.prototype;';
270     eval(str);
271 };
272
273
274 /* general session superclass */
275 OpenSRF.Session = function() {
276     this.remote_id = null;
277     this.state = OSRF_APP_SESSION_DISCONNECTED;
278 };
279
280 OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_XHR;
281 OpenSRF.Session.cache = {};
282
283 OpenSRF.Session.find_session = function(thread_trace) {
284     return OpenSRF.Session.cache[thread_trace];
285 };
286 OpenSRF.Session.prototype.cleanup = function() {
287     delete OpenSRF.Session.cache[this.thread];
288 };
289
290 OpenSRF.Session.prototype.send = function(osrf_msg, args) {
291     args = (args) ? args : {};
292     switch(OpenSRF.Session.transport) {
293         case OSRF_TRANSPORT_TYPE_WS:
294             return this.send_ws(osrf_msg);
295         case OSRF_TRANSPORT_TYPE_WS_SHARED:
296             return this.send_ws_shared(osrf_msg);
297         case OSRF_TRANSPORT_TYPE_XHR:
298             return this.send_xhr(osrf_msg, args);
299         case OSRF_TRANSPORT_TYPE_XMPP:
300             return this.send_xmpp(osrf_msg, args);
301     }
302 };
303
304 OpenSRF.Session.prototype.send_xhr = function(osrf_msg, args) {
305     args.thread = this.thread;
306     args.rcpt = this.remote_id;
307     args.rcpt_service = this.service;
308     new OpenSRF.XHRequest(osrf_msg, args).send();
309 };
310
311 OpenSRF.websocketConnected = function() {
312     return OpenSRF.sharedWebsocketConnected || (
313         OpenSRF.websocketConnection && 
314         OpenSRF.websocketConnection.connected()
315     );
316 }
317
318 OpenSRF.Session.prototype.send_ws = function(osrf_msg) {
319
320     // XXX there appears to be a bug in Chromium where loading the
321     // same page multiple times (without a refresh or cache clear)
322     // causes the SharedWorker to fail to instantiate on 
323     // every other page load.  Disabling SharedWorker's entirely
324     // for now.
325     if (false /* ^-- */ && typeof SharedWorker == 'function' 
326
327         /*
328          * https://bugzilla.mozilla.org/show_bug.cgi?id=504553#c73
329          * Firefox does not yet support WebSockets in worker threads
330          */
331         && !navigator.userAgent.match(/Firefox/)
332     ) {
333         // vanilla websockets requested, but this browser supports
334         // shared workers, so use those instead.
335         return this.send_ws_shared(osrf_msg);
336     }
337
338     // otherwise, use a per-tab connection
339
340     if (!OpenSRF.websocketConnection) {
341         this.setup_single_ws();
342     }
343
344     var json = js2JSON({
345         service : this.service,
346         thread : this.thread,
347         osrf_msg : [osrf_msg.serialize()]
348     });
349
350     OpenSRF.websocketConnection.send(json);
351 };
352
353 OpenSRF.Session.prototype.setup_single_ws = function() {
354     OpenSRF.websocketConnection = new OpenSRF.WebSocket();
355
356     OpenSRF.websocketConnection.onmessage = function(msg) {
357         try {
358             var msg = JSON2js(msg);
359         } catch(E) {
360             console.error(
361                 "Error parsing JSON in shared WS response: " + msg);
362             throw E;
363         }
364
365         if (msg.transport_error) {
366             // Websockets gateway returns bounced messages (e.g. for
367             // requets to unavailable services) with a transport_error
368             // flag set.  
369             console.error(
370                 'Websocket request failed with a transport error', msg);
371
372             var ses = OpenSRF.Session.find_session(msg.thread); 
373             if (ses) {
374                 if (msg.osrf_msg && msg.osrf_msg[0]) {
375                     var req = ses.find_request(msg.osrf_msg[0].threadTrace());
376                     if (req) {
377                         var handler = req.ontransporterror || req.onerror;
378                         if (handler) {
379                             handler('Service ' + ses.service + ' unavailable');
380                         }
381                     }
382                 }
383             }
384             return; // No viable error handlers
385         }
386
387         OpenSRF.Stack.push(                                                        
388             new OpenSRF.NetMessage(                                                
389                null, null, msg.thread, null, msg.osrf_msg)                        
390         ); 
391
392         return;
393     }
394 }
395
396 OpenSRF.Session.setup_shared_ws = function() {
397     OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_WS_SHARED;
398
399     OpenSRF.sharedWSWorker = new SharedWorker(SHARED_WORKER_LIB);
400
401     OpenSRF.sharedWSWorker.port.addEventListener('message', function(e) {                          
402         var data = e.data;
403
404         if (data.action == 'message') {
405             // pass all inbound message up the opensrf stack
406
407             OpenSRF.sharedWebsocketConnected = true;
408             var msg;
409             try {
410                 msg = JSON2js(data.message);
411             } catch(E) {
412                 console.error(
413                     "Error parsing JSON in shared WS response: " + msg);
414                 throw E;
415             }
416             OpenSRF.Stack.push(                                                        
417                 new OpenSRF.NetMessage(                                                
418                    null, null, msg.thread, null, msg.osrf_msg)                        
419             ); 
420
421             return;
422         }
423
424
425         if (data.action == 'event') {
426             if (data.type.match(/onclose|onerror/)) {
427                 OpenSRF.sharedWebsocketConnected = false;
428                 if (OpenSRF.onWebSocketClosed)
429                     OpenSRF.onWebSocketClosed();
430                 if (data.type.match(/onerror/)) 
431                     throw new Error(data.message);
432             }
433         }
434     });
435
436     OpenSRF.sharedWSWorker.port.start();   
437 }
438
439 OpenSRF.Session.prototype.send_ws_shared = function(message) {
440
441     if (!OpenSRF.sharedWSWorker) 
442         OpenSRF.Session.setup_shared_ws();
443
444     var json = js2JSON({
445         service : this.service,
446         thread : this.thread,
447         osrf_msg : [message.serialize()]
448     });
449
450     OpenSRF.sharedWSWorker.port.postMessage({
451         action : 'message', 
452         // pass the thread additionally as a stand-alone value so the
453         // worker can more efficiently inspect it.
454         thread : this.thread,
455         message : json
456     });
457 }
458
459
460 OpenSRF.Session.prototype.send_xmpp = function(osrf_msg, args) {
461     alert('xmpp transport not implemented');
462 };
463
464
465 /* client sessions make requests */
466 OpenSRF.ClientSession = function(service) {
467     this.service = service;
468     this.remote_id = null;
469     this.locale = OpenSRF.locale || 'en-US';
470     this.tz = OpenSRF.tz;
471     this.last_id = 0;
472     this.requests = [];
473     this.onconnect = null;
474     this.thread = Math.random() + '' + new Date().getTime();
475     OpenSRF.Session.cache[this.thread] = this;
476 };
477 OpenSRF.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session');
478
479
480 OpenSRF.ClientSession.prototype.connect = function(args) {
481     args = (args) ? args : {};
482     this.remote_id = null;
483
484     if (this.state == OSRF_APP_SESSION_CONNECTED) {
485         if (args.onconnect) args.onconnect();
486         return true;
487     }
488
489     if(args.onconnect) {
490         this.onconnect = args.onconnect;
491
492     } else {
493         /* if no handler is provided, make this a synchronous call */
494         this.timeout = (args.timeout) ? args.timeout : 5;
495     }
496
497     message = new osrfMessage({
498         'threadTrace' : this.last_id++, 
499         'type' : OSRF_MESSAGE_TYPE_CONNECT
500     });
501
502     this.send(message, {'timeout' : this.timeout});
503
504     if(this.onconnect || this.state == OSRF_APP_SESSION_CONNECTED)
505         return true;
506
507     return false;
508 };
509
510 OpenSRF.ClientSession.prototype.disconnect = function(args) {
511
512     if (this.state == OSRF_APP_SESSION_CONNECTED) {
513         this.send(
514             new osrfMessage({
515                 'threadTrace' : this.last_id++,
516                 'type' : OSRF_MESSAGE_TYPE_DISCONNECT
517             })
518         );
519     }
520
521     this.remote_id = null;
522     this.state = OSRF_APP_SESSION_DISCONNECTED;
523 };
524
525
526 OpenSRF.ClientSession.prototype.request = function(args) {
527     
528     if(this.state != OSRF_APP_SESSION_CONNECTED)
529         this.remote_id = null;
530         
531     if(typeof args == 'string') { 
532         params = [];
533         for(var i = 1; i < arguments.length; i++)
534             params.push(arguments[i]);
535
536         args = {
537             method : args, 
538             params : params
539         };
540     } else {
541         if(typeof args == 'undefined')
542             args = {};
543     }
544
545     var req = new OpenSRF.Request(this, this.last_id++, args);
546     this.requests.push(req);
547     return req;
548 };
549
550 OpenSRF.ClientSession.prototype.find_request = function(reqid) {
551     for(var i = 0; i < this.requests.length; i++) {
552         var req = this.requests[i];
553         if(req.reqid == reqid)
554             return req;
555     }
556     return null;
557 };
558
559 OpenSRF.Request = function(session, reqid, args) {
560     this.session = session;
561     this.reqid = reqid;
562
563     /* callbacks */
564     this.onresponse = args.onresponse;
565     this.oncomplete = args.oncomplete;
566     this.onerror = args.onerror;
567     this.onmethoderror = args.onmethoderror;
568     this.ontransporterror = args.ontransporterror;
569
570     this.method = args.method;
571     this.params = args.params;
572     this.timeout = args.timeout;
573     this.api_level = args.api_level || OpenSRF.api_level;
574     this.response_queue = [];
575     this.part_response_buffer = '';
576     this.complete = false;
577 };
578
579 OpenSRF.Request.prototype.peek_last = function(timeout) {
580     if(this.response_queue.length > 0) {
581         var x = this.response_queue.pop();
582         this.response_queue.push(x);
583         return x;
584     }
585     return null;
586 };
587
588 OpenSRF.Request.prototype.peek = function(timeout) {
589     if(this.response_queue.length > 0)
590         return this.response_queue[0];
591     return null;
592 };
593
594 OpenSRF.Request.prototype.recv = function(timeout) {
595     if(this.response_queue.length > 0)
596         return this.response_queue.shift();
597     return null;
598 };
599
600 OpenSRF.Request.prototype.send = function() {
601     method = new osrfMethod({'method':this.method, 'params':this.params});
602     message = new osrfMessage({
603         'threadTrace' : this.reqid, 
604         'type' : OSRF_MESSAGE_TYPE_REQUEST, 
605         'payload' : method, 
606         'locale' : this.session.locale,
607         'tz' : this.session.tz,
608         'api_level' : this.api_level
609     });
610
611     this.session.send(message, {
612         'timeout' : this.timeout,
613         'onresponse' : this.onresponse,
614         'oncomplete' : this.oncomplete,
615         'onerror' : this.onerror,
616         'onmethoderror' : this.onmethoderror,
617         'ontransporterror' : this.ontransporterror
618     });
619 };
620
621 OpenSRF.NetMessage = function(to, from, thread, body, osrf_msg) {
622     this.to = to;
623     this.from = from;
624     this.thread = thread;
625     this.body = body;
626     this.osrf_msg = osrf_msg;
627 };
628
629 OpenSRF.Stack = function() {
630 };
631
632 // global inbound message queue
633 OpenSRF.Stack.queue = [];
634
635 // XXX testing
636 function log(msg) {
637     try {
638         dump(msg + '\n'); // xulrunner
639     } catch(E) {
640         console.log(msg);
641     }
642 }
643
644 // ses may be passed to us by the network handler
645 OpenSRF.Stack.push = function(net_msg, callbacks) {
646     var ses = OpenSRF.Session.find_session(net_msg.thread); 
647     if (!ses) return;
648     ses.remote_id = net_msg.from;
649
650     // NetMessage's from websocket connections are parsed before they get here
651     osrf_msgs = net_msg.osrf_msg;
652
653     if (!osrf_msgs) {
654
655         try {
656             osrf_msgs = JSON2js(net_msg.body);
657
658             // TODO: pretty sure we don't need this..
659             if (OpenSRF.Session.transport == OSRF_TRANSPORT_TYPE_WS) {
660                 // WebSocketRequests wrap the content
661                 osrf_msgs = osrf_msgs.osrf_msg;
662             }
663
664         } catch(E) {
665             log('Error parsing OpenSRF message body as JSON: ' + net_msg.body + '\n' + E);
666
667             /** UGH
668               * For unknown reasons, the Content-Type header will occasionally
669               * be included in the XHR.responseText for multipart/mixed messages.
670               * When this happens, strip the header and newlines from the message
671               * body and re-parse.
672               */
673             net_msg.body = net_msg.body.replace(/^.*\n\n/, '');
674             log('Cleaning up and retrying...');
675
676             try {
677                 osrf_msgs = JSON2js(net_msg.body);
678             } catch(E2) {
679                 log('Unable to clean up message, giving up: ' + net_msg.body);
680                 return;
681             }
682         }
683     }
684
685     // push the latest responses onto the end of the inbound message queue
686     for(var i = 0; i < osrf_msgs.length; i++)
687         OpenSRF.Stack.queue.push({msg : osrf_msgs[i], ses : ses});
688
689     // continue processing responses, oldest to newest
690     while(OpenSRF.Stack.queue.length) {
691         var data = OpenSRF.Stack.queue.shift();
692         OpenSRF.Stack.handle_message(data.ses, data.msg);
693     }
694 };
695
696 OpenSRF.Stack.handle_message = function(ses, osrf_msg) {
697     
698     var req = ses.find_request(osrf_msg.threadTrace());
699
700     var payload = osrf_msg.payload();
701     var status = payload.statusCode();
702     var status_text = payload.status();
703
704     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
705
706
707         if(status == OSRF_STATUS_COMPLETE) {
708             if(req) {
709                 req.complete = true;
710                 if(req.oncomplete && !req.oncomplete_called) {
711                     req.oncomplete_called = true;
712                     return req.oncomplete(req);
713                 }
714             }
715         }
716
717         if(status == OSRF_STATUS_OK) {
718             ses.state = OSRF_APP_SESSION_CONNECTED;
719
720             /* call the connect callback */
721             if(ses.onconnect && !ses.onconnect_called) {
722                 ses.onconnect_called = true;
723                 return ses.onconnect();
724             }
725         }
726
727         // capture all 400's and 500's as method errors
728         if ((status+'').match(/^4/) || (status+'').match(/^5/)) {
729             if(req && req.onmethoderror) 
730                 return req.onmethoderror(req, status, status_text);
731         }
732     }
733
734     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
735         req = ses.find_request(osrf_msg.threadTrace());
736         if(req) {
737             if (status == OSRF_STATUS_PARTIAL) {
738                 req.part_response_buffer += payload.content()
739                 return; // we're just collecting a big chunked payload
740             } else if (status == OSRF_STATUS_NOCONTENT) {
741                 payload.content( JSON2js(req.part_response_buffer) );
742                 payload.statusCode( OSRF_STATUS_OK );
743                 req.part_response_buffer = '';
744             }
745             req.response_queue.push(payload);
746             if(req.onresponse) 
747                 return req.onresponse(req);
748         }
749     }
750 };
751
752