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 destroyRequest(r) {
30 /* this has to come before abort() or IE will puke on you */
31 r.xmlhttp.onreadystatechange = function(){};
38 _allrequests[r.id] = null;
41 /* ----------------------------------------------------------------------- */
43 function RemoteRequest( service, method ) {
46 this.service = service;
52 this.type = "POST"; /* default */
53 this.id = service + method + Math.random();
54 this.cancelled = false;
56 _allrequests[this.id] = this;
61 while(i < arguments.length) {
62 var object = js2JSON(arguments[i++]);
63 this.params += "¶m=" + encodeURIComponent(object);
66 if(!this.params) { this.params = ""; }
67 this.param_string = "service=" + service + "&method=" + method + this.params;
68 if( this.buildXMLRequest() == null ) alert("Browser is not supported!");
71 RemoteRequest.prototype.abort = function() {
72 if( this.xmlhttp ) this.xmlhttp.abort();
75 /* constructs our XMLHTTPRequest object */
76 RemoteRequest.prototype.buildXMLRequest = function() {
80 x = new ActiveXObject("Msxml2.XMLHTTP");
83 x = new ActiveXObject("Microsoft.XMLHTTP");
89 if (!x && typeof XMLHttpRequest!='undefined') x = new XMLHttpRequest();
92 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
101 function _remoteRequestCallback(id) {
103 var object = _allrequests[id];
104 if(object.cancelled) return;
106 if( object.xmlhttp.readyState == 4 ) {
108 object.callback(object);
111 /* if we receive a communication error, retry the request up
112 to XML_HTTP_MAX_TRIES attempts */
113 if( E && E.classname == "EXCommunication" ) {
115 //try { dump('Communication Error: ' + E ); } catch(e){}
116 alert('Debug: Communication Error: ' + E );
118 if(object.sendCount >= XML_HTTP_MAX_TRIES ) {
119 if(isXUL()) throw object;
120 else alert("Arrrgghh, Matey! Error communicating:\n" + E + "\n" + object.param_string);
122 object.buildXMLRequest();
129 destroyRequest(object);
136 /* define the callback we use when this request has received
138 RemoteRequest.prototype.setCompleteCallback = function(callback) {
139 if(this.cancelled) return;
140 this.callback = callback;
142 this.xmlhttp.onreadystatechange = function() { _remoteRequestCallback(id); }
146 /* http by default. This makes it https. *ONLY works when
147 embedded in a XUL app. */
148 RemoteRequest.prototype.setSecure = function(bool) {
152 RemoteRequest.prototype.send = function(blocking) {
154 if(this.cancelled) return;
156 /* determine the xmlhttp server dynamically */
157 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
160 if(this.secure || url.match(/^https:/) )
161 url = "https://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
163 url = "http://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
167 if( this.type == 'GET' ) url += "?" + this.param_string;
171 if(blocking) this.xmlhttp.open(this.type, url, false);
172 else this.xmlhttp.open(this.type, url, true);
175 alert("Fatal error opening XMLHTTPRequest for URL:\n" + url + '\n');
180 if( this.type == 'POST' ) {
181 data = this.param_string;
182 this.xmlhttp.setRequestHeader('Content-Type',
183 'application/x-www-form-urlencoded');
186 try{ this.xmlhttp.send( data ); } catch(e){}
192 /* returns the actual response text from the request */
193 RemoteRequest.prototype.getText = function() {
194 return this.xmlhttp.responseText;
197 RemoteRequest.prototype.isReady = function() {
198 return this.xmlhttp.readyState == 4;
202 /* returns the JSON->js result object */
203 RemoteRequest.prototype.getResultObject = function() {
205 if(this.cancelled) return null;
206 if(!this.xmlhttp) return null;
208 var text = this.xmlhttp.responseText;
209 if(text == "" || text == " " || text == null) null;
211 var obj = JSON2js(text);
212 if(!obj) return null;
214 if( obj.status != 200 ) {
216 alert("A Server Error Occured. Debug information follows:\n" +
217 "Status: " + obj.status + '\n' + obj.debug + '\n'
218 + 'Payload: ' + js2JSON(obj.payload) + '\n');
220 dump('Remote Request Error:' +
221 '\ncode = ' + obj.status +
222 '\ndebug: ' + obj.debug +
223 '\npayload: ' + js2JSON(obj.payload));
228 var payload = obj.payload;
229 if(!payload || payload.length == 0) return null;
230 payload = (payload.length == 1) ? payload[0] : payload;
232 if(payload.__isfieldmapper && payload.classname == "perm_ex") {
233 if(!isXUL()) alert(payload.err_msg());
240 /* adds a new parameter to the request */
241 RemoteRequest.prototype.addParam = function(param) {
242 var string = encodeURIComponent(js2JSON(param));
243 this.param_string += "¶m=" + string;