]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/javascript/opensrf.js
LP#1485371: Use client-supplied TZ
[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_COMPLETE = 205;
49 var OSRF_STATUS_REDIRECTED = 307;
50 var OSRF_STATUS_BADREQUEST = 400;
51 var OSRF_STATUS_UNAUTHORIZED = 401;
52 var OSRF_STATUS_FORBIDDEN = 403;
53 var OSRF_STATUS_NOTFOUND = 404;
54 var OSRF_STATUS_NOTALLOWED = 405;
55 var OSRF_STATUS_TIMEOUT = 408;
56 var OSRF_STATUS_EXPFAILED = 417;
57 var OSRF_STATUS_INTERNALSERVERERROR = 500;
58 var OSRF_STATUS_NOTIMPLEMENTED = 501;
59 var OSRF_STATUS_VERSIONNOTSUPPORTED = 505;
60
61 // TODO: get path from ./configure prefix
62 var SHARED_WORKER_LIB = '/js/dojo/opensrf/opensrf_ws_shared.js'; 
63
64 /* The following classes map directly to network-serializable opensrf objects */
65
66 function osrfMessage(hash) {
67     this.hash = hash;
68     if(!this.hash.locale)
69         this.hash.locale = OpenSRF.locale || 'en-US';
70     if(!this.hash.tz)
71         this.hash.tz = jstz.determine().name()
72     this._encodehash = true;
73 }
74 osrfMessage.prototype.threadTrace = function(d) { 
75     if(arguments.length == 1) 
76         this.hash.threadTrace = d; 
77     return this.hash.threadTrace; 
78 };
79 osrfMessage.prototype.type = function(d) { 
80     if(arguments.length == 1) 
81         this.hash.type = d; 
82     return this.hash.type; 
83 };
84 osrfMessage.prototype.payload = function(d) { 
85     if(arguments.length == 1) 
86         this.hash.payload = d; 
87     return this.hash.payload; 
88 };
89 osrfMessage.prototype.locale = function(d) { 
90     if(arguments.length == 1) 
91         this.hash.locale = d; 
92     return this.hash.locale; 
93 };
94 osrfMessage.prototype.tz = function(d) { 
95     if(arguments.length == 1) 
96         this.hash.tz = d; 
97     return this.hash.tz; 
98 };
99 osrfMessage.prototype.api_level = function(d) { 
100     if(arguments.length == 1) 
101         this.hash.api_level = d; 
102     return this.hash.api_level; 
103 };
104 osrfMessage.prototype.serialize = function() {
105     return {
106         "__c":"osrfMessage",
107         "__p": {
108             'threadTrace' : this.hash.threadTrace,
109             'type' : this.hash.type,
110             'payload' : (this.hash.payload) ? this.hash.payload.serialize() : 'null',
111             'locale' : this.hash.locale,
112             'tz' : this.hash.tz,
113             'api_level' : this.hash.api_level
114         }
115     };
116 };
117
118 function osrfMethod(hash) {
119     this.hash = hash;
120     this._encodehash = true;
121 }
122 osrfMethod.prototype.method = function(d) {
123     if(arguments.length == 1) 
124         this.hash.method = d; 
125     return this.hash.method; 
126 };
127 osrfMethod.prototype.params = function(d) {
128     if(arguments.length == 1) 
129         this.hash.params = d; 
130     return this.hash.params; 
131 };
132 osrfMethod.prototype.serialize = function() {
133     return {
134         "__c":"osrfMethod",
135         "__p": {
136             'method' : this.hash.method,
137             'params' : this.hash.params
138         }
139     };
140 };
141
142 function osrfMethodException(hash) {
143     this.hash = hash;
144     this._encodehash = true;
145 }
146 osrfMethodException.prototype.status = function(d) {
147     if(arguments.length == 1) 
148         this.hash.status = d; 
149     return this.hash.status; 
150 };
151 osrfMethodException.prototype.statusCode = function(d) {
152     if(arguments.length == 1) 
153         this.hash.statusCode = d; 
154     return this.hash.statusCode; 
155 };
156 function osrfConnectStatus(hash) { 
157     this.hash = hash;
158     this._encodehash = true;
159 }
160 osrfConnectStatus.prototype.status = function(d) {
161     if(arguments.length == 1) 
162         this.hash.status = d; 
163     return this.hash.status; 
164 };
165 osrfConnectStatus.prototype.statusCode = function(d) {
166     if(arguments.length == 1) 
167         this.hash.statusCode = d; 
168     return this.hash.statusCode; 
169 };
170 function osrfResult(hash) {
171     this.hash = hash;
172     this._encodehash = true;
173 }
174 osrfResult.prototype.status = function(d) {
175     if(arguments.length == 1) 
176         this.hash.status = d; 
177     return this.hash.status; 
178 };
179 osrfResult.prototype.statusCode = function(d) {
180     if(arguments.length == 1) 
181         this.hash.statusCode = d; 
182     return this.hash.statusCode; 
183 };
184 osrfResult.prototype.content = function(d) {
185     if(arguments.length == 1) 
186         this.hash.content = d; 
187     return this.hash.content; 
188 };
189 function osrfServerError(hash) { 
190     this.hash = hash;
191     this._encodehash = true;
192 }
193 osrfServerError.prototype.status = function(d) {
194     if(arguments.length == 1) 
195         this.hash.status = d; 
196     return this.hash.status; 
197 };
198 osrfServerError.prototype.statusCode = function(d) {
199     if(arguments.length == 1) 
200         this.hash.statusCode = d; 
201     return this.hash.statusCode; 
202 };
203 function osrfContinueStatus(hash) { 
204     this.hash = hash;
205     this._encodehash = true;
206 }
207 osrfContinueStatus.prototype.status = function(d) {
208     if(arguments.length == 1) 
209         this.hash.status = d; 
210     return this.hash.status; 
211 };
212 osrfContinueStatus.prototype.statusCode = function(d) {
213     if(arguments.length == 1) 
214         this.hash.statusCode = d; 
215     return this.hash.statusCode; 
216 };
217
218 OpenSRF = {};
219 OpenSRF.tz = jstz.determine().name();
220 OpenSRF.locale = null;
221 OpenSRF.api_level = 1;
222
223 /* makes cls a subclass of pcls */
224 OpenSRF.set_subclass = function(cls, pcls) {
225     var str = cls+'.prototype = new '+pcls+'();';
226     str += cls+'.prototype.constructor = '+cls+';';
227     str += cls+'.baseClass = '+pcls+'.prototype.constructor;';
228     str += cls+'.prototype["super"] = '+pcls+'.prototype;';
229     eval(str);
230 };
231
232
233 /* general session superclass */
234 OpenSRF.Session = function() {
235     this.remote_id = null;
236     this.state = OSRF_APP_SESSION_DISCONNECTED;
237 };
238
239 OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_XHR;
240 OpenSRF.Session.cache = {};
241
242 OpenSRF.Session.find_session = function(thread_trace) {
243     return OpenSRF.Session.cache[thread_trace];
244 };
245 OpenSRF.Session.prototype.cleanup = function() {
246     delete OpenSRF.Session.cache[this.thread];
247 };
248
249 OpenSRF.Session.prototype.send = function(osrf_msg, args) {
250     args = (args) ? args : {};
251     switch(OpenSRF.Session.transport) {
252         case OSRF_TRANSPORT_TYPE_WS:
253             return this.send_ws(osrf_msg);
254         case OSRF_TRANSPORT_TYPE_WS_SHARED:
255             return this.send_ws_shared(osrf_msg);
256         case OSRF_TRANSPORT_TYPE_XHR:
257             return this.send_xhr(osrf_msg, args);
258         case OSRF_TRANSPORT_TYPE_XMPP:
259             return this.send_xmpp(osrf_msg, args);
260     }
261 };
262
263 OpenSRF.Session.prototype.send_xhr = function(osrf_msg, args) {
264     args.thread = this.thread;
265     args.rcpt = this.remote_id;
266     args.rcpt_service = this.service;
267     new OpenSRF.XHRequest(osrf_msg, args).send();
268 };
269
270 OpenSRF.websocketConnected = function() {
271     return OpenSRF.sharedWebsocketConnected || (
272         OpenSRF.websocketConnection && 
273         OpenSRF.websocketConnection.connected()
274     );
275 }
276
277 OpenSRF.Session.prototype.send_ws = function(osrf_msg) {
278
279     // XXX there appears to be a bug in Chromium where loading the
280     // same page multiple times (without a refresh or cache clear)
281     // causes the SharedWorker to fail to instantiate on 
282     // every other page load.  Disabling SharedWorker's entirely
283     // for now.
284     if (false /* ^-- */ && typeof SharedWorker == 'function' 
285
286         /*
287          * https://bugzilla.mozilla.org/show_bug.cgi?id=504553#c73
288          * Firefox does not yet support WebSockets in worker threads
289          */
290         && !navigator.userAgent.match(/Firefox/)
291     ) {
292         // vanilla websockets requested, but this browser supports
293         // shared workers, so use those instead.
294         return this.send_ws_shared(osrf_msg);
295     }
296
297     // otherwise, use a per-tab connection
298
299     if (!OpenSRF.websocketConnection) {
300         this.setup_single_ws();
301     }
302
303     var json = js2JSON({
304         service : this.service,
305         thread : this.thread,
306         osrf_msg : [osrf_msg.serialize()]
307     });
308
309     OpenSRF.websocketConnection.send(json);
310 };
311
312 OpenSRF.Session.prototype.setup_single_ws = function() {
313     OpenSRF.websocketConnection = new OpenSRF.WebSocket();
314
315     OpenSRF.websocketConnection.onmessage = function(msg) {
316         try {
317             var msg = JSON2js(msg);
318         } catch(E) {
319             console.error(
320                 "Error parsing JSON in shared WS response: " + msg);
321             throw E;
322         }
323         OpenSRF.Stack.push(                                                        
324             new OpenSRF.NetMessage(                                                
325                null, null, msg.thread, null, msg.osrf_msg)                        
326         ); 
327
328         return;
329     }
330 }
331
332 OpenSRF.Session.setup_shared_ws = function() {
333     OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_WS_SHARED;
334
335     OpenSRF.sharedWSWorker = new SharedWorker(SHARED_WORKER_LIB);
336
337     OpenSRF.sharedWSWorker.port.addEventListener('message', function(e) {                          
338         var data = e.data;
339
340         if (data.action == 'message') {
341             // pass all inbound message up the opensrf stack
342
343             OpenSRF.sharedWebsocketConnected = true;
344             var msg;
345             try {
346                 msg = JSON2js(data.message);
347             } catch(E) {
348                 console.error(
349                     "Error parsing JSON in shared WS response: " + msg);
350                 throw E;
351             }
352             OpenSRF.Stack.push(                                                        
353                 new OpenSRF.NetMessage(                                                
354                    null, null, msg.thread, null, msg.osrf_msg)                        
355             ); 
356
357             return;
358         }
359
360
361         if (data.action == 'event') {
362             if (data.type.match(/onclose|onerror/)) {
363                 OpenSRF.sharedWebsocketConnected = false;
364                 if (OpenSRF.onWebSocketClosed)
365                     OpenSRF.onWebSocketClosed();
366                 if (data.type.match(/onerror/)) 
367                     throw new Error(data.message);
368             }
369         }
370     });
371
372     OpenSRF.sharedWSWorker.port.start();   
373 }
374
375 OpenSRF.Session.prototype.send_ws_shared = function(message) {
376
377     if (!OpenSRF.sharedWSWorker) 
378         OpenSRF.Session.setup_shared_ws();
379
380     var json = js2JSON({
381         service : this.service,
382         thread : this.thread,
383         osrf_msg : [message.serialize()]
384     });
385
386     OpenSRF.sharedWSWorker.port.postMessage({
387         action : 'message', 
388         // pass the thread additionally as a stand-alone value so the
389         // worker can more efficiently inspect it.
390         thread : this.thread,
391         message : json
392     });
393 }
394
395
396 OpenSRF.Session.prototype.send_xmpp = function(osrf_msg, args) {
397     alert('xmpp transport not implemented');
398 };
399
400
401 /* client sessions make requests */
402 OpenSRF.ClientSession = function(service) {
403     this.service = service;
404     this.remote_id = null;
405     this.locale = OpenSRF.locale || 'en-US';
406     this.tz = OpenSRF.tz;
407     this.last_id = 0;
408     this.requests = [];
409     this.onconnect = null;
410     this.thread = Math.random() + '' + new Date().getTime();
411     OpenSRF.Session.cache[this.thread] = this;
412 };
413 OpenSRF.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session');
414
415
416 OpenSRF.ClientSession.prototype.connect = function(args) {
417     args = (args) ? args : {};
418     this.remote_id = null;
419
420     if (this.state == OSRF_APP_SESSION_CONNECTED) {
421         if (args.onconnect) args.onconnect();
422         return true;
423     }
424
425     if(args.onconnect) {
426         this.onconnect = args.onconnect;
427
428     } else {
429         /* if no handler is provided, make this a synchronous call */
430         this.timeout = (args.timeout) ? args.timeout : 5;
431     }
432
433     message = new osrfMessage({
434         'threadTrace' : this.last_id++, 
435         'type' : OSRF_MESSAGE_TYPE_CONNECT
436     });
437
438     this.send(message, {'timeout' : this.timeout});
439
440     if(this.onconnect || this.state == OSRF_APP_SESSION_CONNECTED)
441         return true;
442
443     return false;
444 };
445
446 OpenSRF.ClientSession.prototype.disconnect = function(args) {
447
448     if (this.state == OSRF_APP_SESSION_CONNECTED) {
449         this.send(
450             new osrfMessage({
451                 'threadTrace' : this.last_id++,
452                 'type' : OSRF_MESSAGE_TYPE_DISCONNECT
453             })
454         );
455     }
456
457     this.remote_id = null;
458     this.state = OSRF_APP_SESSION_DISCONNECTED;
459 };
460
461
462 OpenSRF.ClientSession.prototype.request = function(args) {
463     
464     if(this.state != OSRF_APP_SESSION_CONNECTED)
465         this.remote_id = null;
466         
467     if(typeof args == 'string') { 
468         params = [];
469         for(var i = 1; i < arguments.length; i++)
470             params.push(arguments[i]);
471
472         args = {
473             method : args, 
474             params : params
475         };
476     } else {
477         if(typeof args == 'undefined')
478             args = {};
479     }
480
481     var req = new OpenSRF.Request(this, this.last_id++, args);
482     this.requests.push(req);
483     return req;
484 };
485
486 OpenSRF.ClientSession.prototype.find_request = function(reqid) {
487     for(var i = 0; i < this.requests.length; i++) {
488         var req = this.requests[i];
489         if(req.reqid == reqid)
490             return req;
491     }
492     return null;
493 };
494
495 OpenSRF.Request = function(session, reqid, args) {
496     this.session = session;
497     this.reqid = reqid;
498
499     /* callbacks */
500     this.onresponse = args.onresponse;
501     this.oncomplete = args.oncomplete;
502     this.onerror = args.onerror;
503     this.onmethoderror = args.onmethoderror;
504     this.ontransporterror = args.ontransporterror;
505
506     this.method = args.method;
507     this.params = args.params;
508     this.timeout = args.timeout;
509     this.api_level = args.api_level || OpenSRF.api_level;
510     this.response_queue = [];
511     this.complete = false;
512 };
513
514 OpenSRF.Request.prototype.peek_last = function(timeout) {
515     if(this.response_queue.length > 0) {
516         var x = this.response_queue.pop();
517         this.response_queue.push(x);
518         return x;
519     }
520     return null;
521 };
522
523 OpenSRF.Request.prototype.peek = function(timeout) {
524     if(this.response_queue.length > 0)
525         return this.response_queue[0];
526     return null;
527 };
528
529 OpenSRF.Request.prototype.recv = function(timeout) {
530     if(this.response_queue.length > 0)
531         return this.response_queue.shift();
532     return null;
533 };
534
535 OpenSRF.Request.prototype.send = function() {
536     method = new osrfMethod({'method':this.method, 'params':this.params});
537     message = new osrfMessage({
538         'threadTrace' : this.reqid, 
539         'type' : OSRF_MESSAGE_TYPE_REQUEST, 
540         'payload' : method, 
541         'locale' : this.session.locale,
542         'tz' : this.session.tz,
543         'api_level' : this.api_level
544     });
545
546     this.session.send(message, {
547         'timeout' : this.timeout,
548         'onresponse' : this.onresponse,
549         'oncomplete' : this.oncomplete,
550         'onerror' : this.onerror,
551         'onmethoderror' : this.onmethoderror,
552         'ontransporterror' : this.ontransporterror
553     });
554 };
555
556 OpenSRF.NetMessage = function(to, from, thread, body, osrf_msg) {
557     this.to = to;
558     this.from = from;
559     this.thread = thread;
560     this.body = body;
561     this.osrf_msg = osrf_msg;
562 };
563
564 OpenSRF.Stack = function() {
565 };
566
567 // global inbound message queue
568 OpenSRF.Stack.queue = [];
569
570 // XXX testing
571 function log(msg) {
572     try {
573         dump(msg + '\n'); // xulrunner
574     } catch(E) {
575         console.log(msg);
576     }
577 }
578
579 // ses may be passed to us by the network handler
580 OpenSRF.Stack.push = function(net_msg, callbacks) {
581     var ses = OpenSRF.Session.find_session(net_msg.thread); 
582     if (!ses) return;
583     ses.remote_id = net_msg.from;
584
585     // NetMessage's from websocket connections are parsed before they get here
586     osrf_msgs = net_msg.osrf_msg;
587
588     if (!osrf_msgs) {
589
590         try {
591             osrf_msgs = JSON2js(net_msg.body);
592
593             // TODO: pretty sure we don't need this..
594             if (OpenSRF.Session.transport == OSRF_TRANSPORT_TYPE_WS) {
595                 // WebSocketRequests wrap the content
596                 osrf_msgs = osrf_msgs.osrf_msg;
597             }
598
599         } catch(E) {
600             log('Error parsing OpenSRF message body as JSON: ' + net_msg.body + '\n' + E);
601
602             /** UGH
603               * For unknown reasons, the Content-Type header will occasionally
604               * be included in the XHR.responseText for multipart/mixed messages.
605               * When this happens, strip the header and newlines from the message
606               * body and re-parse.
607               */
608             net_msg.body = net_msg.body.replace(/^.*\n\n/, '');
609             log('Cleaning up and retrying...');
610
611             try {
612                 osrf_msgs = JSON2js(net_msg.body);
613             } catch(E2) {
614                 log('Unable to clean up message, giving up: ' + net_msg.body);
615                 return;
616             }
617         }
618     }
619
620     // push the latest responses onto the end of the inbound message queue
621     for(var i = 0; i < osrf_msgs.length; i++)
622         OpenSRF.Stack.queue.push({msg : osrf_msgs[i], ses : ses});
623
624     // continue processing responses, oldest to newest
625     while(OpenSRF.Stack.queue.length) {
626         var data = OpenSRF.Stack.queue.shift();
627         OpenSRF.Stack.handle_message(data.ses, data.msg);
628     }
629 };
630
631 OpenSRF.Stack.handle_message = function(ses, osrf_msg) {
632     
633     var req = ses.find_request(osrf_msg.threadTrace());
634
635     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
636
637         var payload = osrf_msg.payload();
638         var status = payload.statusCode();
639         var status_text = payload.status();
640
641         if(status == OSRF_STATUS_COMPLETE) {
642             if(req) {
643                 req.complete = true;
644                 if(req.oncomplete && !req.oncomplete_called) {
645                     req.oncomplete_called = true;
646                     return req.oncomplete(req);
647                 }
648             }
649         }
650
651         if(status == OSRF_STATUS_OK) {
652             ses.state = OSRF_APP_SESSION_CONNECTED;
653
654             /* call the connect callback */
655             if(ses.onconnect && !ses.onconnect_called) {
656                 ses.onconnect_called = true;
657                 return ses.onconnect();
658             }
659         }
660
661         // capture all 400's and 500's as method errors
662         if ((status+'').match(/^4/) || (status+'').match(/^5/)) {
663             if(req && req.onmethoderror) 
664                 return req.onmethoderror(req, status, status_text);
665         }
666     }
667
668     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
669         if(req) {
670             req.response_queue.push(osrf_msg.payload());
671             if(req.onresponse) {
672                 return req.onresponse(req);
673             }
674         }
675     }
676 };
677
678