1 var XML_HTTP_GATEWAY = "gateway";
2 var XML_HTTP_SERVER = "dev.gapines.org";
3 var XML_HTTP_MAX_TRIES = 3;
6 function isXUL() { try { if(IAMXUL) return true;}catch(e){return false;}; }
8 /* some communication exceptions */
9 function EX(message) { this.init(message); }
10 EX.prototype.init = function(message) { this.message = message; }
11 EX.prototype.toString = function() { return "\n *** Exception Occured \n" + this.message; }
12 EXCommunication.prototype = new EX();
13 EXCommunication.prototype.constructor = EXCommunication;
14 EXCommunication.baseClass = EX.prototype.constructor;
15 function EXCommunication(message) { this.classname="EXCommunication"; this.init("EXCommunication: " + message); }
16 /* ------------------------------------------------ */
19 var _allrequests = {};
21 function cleanRemoteRequests() {
22 for( var i in _allrequests )
23 destroyRequest(_allrequests[i]);
26 function abortAllRequests() {
27 for( var i in _allrequests ) {
28 var r = _allrequests[i];
30 /* this has to come before abort() or IE will puke on you */
31 r.xmlhttp.onreadystatechange = function(){};
38 function destroyRequest(r) {
42 r.xmlhttp.onreadystatechange = function(){};
48 _allrequests[r.id] = null;
51 /* ----------------------------------------------------------------------- */
53 function RemoteRequest( service, method ) {
56 this.service = service;
62 this.type = "POST"; /* default */
63 this.id = service + method + Math.random();
64 this.cancelled = false;
66 _allrequests[this.id] = this;
71 while(i < arguments.length) {
72 var object = js2JSON(arguments[i++]);
73 this.params += "¶m=" + encodeURIComponent(object);
76 if(!this.params) { this.params = ""; }
77 this.param_string = "service=" + service + "&method=" + method + this.params;
78 if( this.buildXMLRequest() == null ) alert("Browser is not supported!");
81 RemoteRequest.prototype.abort = function() {
82 if( this.xmlhttp ) this.xmlhttp.abort();
85 /* constructs our XMLHTTPRequest object */
86 RemoteRequest.prototype.buildXMLRequest = function() {
90 x = new ActiveXObject("Msxml2.XMLHTTP");
93 x = new ActiveXObject("Microsoft.XMLHTTP");
99 if (!x && typeof XMLHttpRequest!='undefined') x = new XMLHttpRequest();
102 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
111 function _remoteRequestCallback(id) {
113 var object = _allrequests[id];
114 if(object.cancelled) return;
116 if( object.xmlhttp.readyState == 4 ) {
118 object.callback(object);
121 /* if we receive a communication error, retry the request up
122 to XML_HTTP_MAX_TRIES attempts */
123 if( E && E.classname == "EXCommunication" ) {
125 //try { dump('Communication Error: ' + E ); } catch(e){}
126 alert('Debug: Communication Error: ' + E );
128 if(object.sendCount >= XML_HTTP_MAX_TRIES ) {
129 if(isXUL()) throw object;
130 else alert("Arrrgghh, Matey! Error communicating:\n" + E + "\n" + object.param_string);
132 object.buildXMLRequest();
139 destroyRequest(object);
146 /* define the callback we use when this request has received
148 RemoteRequest.prototype.setCompleteCallback = function(callback) {
149 if(this.cancelled) return;
150 this.callback = callback;
152 this.xmlhttp.onreadystatechange = function() { _remoteRequestCallback(id); }
156 /* http by default. This makes it https. *ONLY works when
157 embedded in a XUL app. */
158 RemoteRequest.prototype.setSecure = function(bool) {
162 RemoteRequest.prototype.send = function(blocking) {
164 if(this.cancelled) return;
166 /* determine the xmlhttp server dynamically */
167 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
170 if(this.secure || url.match(/^https:/) )
171 url = "https://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
173 url = "http://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
177 if( this.type == 'GET' ) url += "?" + this.param_string;
181 if(blocking) this.xmlhttp.open(this.type, url, false);
182 else this.xmlhttp.open(this.type, url, true);
185 alert("Fatal error opening XMLHTTPRequest for URL:\n" + url + '\n');
190 if( this.type == 'POST' ) {
191 data = this.param_string;
192 this.xmlhttp.setRequestHeader('Content-Type',
193 'application/x-www-form-urlencoded');
196 try{ this.xmlhttp.send( data ); } catch(e){}
202 /* returns the actual response text from the request */
203 RemoteRequest.prototype.getText = function() {
204 return this.xmlhttp.responseText;
207 RemoteRequest.prototype.isReady = function() {
208 return this.xmlhttp.readyState == 4;
212 /* returns the JSON->js result object */
213 RemoteRequest.prototype.getResultObject = function() {
215 if(this.cancelled) return null;
216 if(!this.xmlhttp) return null;
218 var text = this.xmlhttp.responseText;
219 if(text == "" || text == " " || text == null) null;
221 var obj = JSON2js(text);
222 if(!obj) return null;
224 if( obj.status != 200 ) {
226 alert("A Server Error Occured. Debug information follows:\n" +
227 "Status: " + obj.status + '\n' + obj.debug + '\n'
228 + 'Payload: ' + js2JSON(obj.payload) + '\n');
230 dump('Remote Request Error:' +
231 '\ncode = ' + obj.status +
232 '\ndebug: ' + obj.debug +
233 '\npayload: ' + js2JSON(obj.payload));
238 var payload = obj.payload;
239 if(!payload || payload.length == 0) return null;
240 payload = (payload.length == 1) ? payload[0] : payload;
242 if(payload.__isfieldmapper && payload.classname == "perm_ex") {
243 if(!isXUL()) alert(payload.err_msg());
250 /* adds a new parameter to the request */
251 RemoteRequest.prototype.addParam = function(param) {
252 var string = encodeURIComponent(js2JSON(param));
253 this.param_string += "¶m=" + string;