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(){};
49 _allrequests[r.id] = null;
52 /* ----------------------------------------------------------------------- */
54 function RemoteRequest( service, method ) {
57 this.service = service;
63 this.type = "POST"; /* default */
64 this.id = service + method + Math.random();
65 this.cancelled = false;
67 _allrequests[this.id] = this;
72 while(i < arguments.length) {
73 var object = js2JSON(arguments[i++]);
74 this.params += "¶m=" + encodeURIComponent(object);
77 if(!this.params) { this.params = ""; }
78 this.param_string = "service=" + service + "&method=" + method + this.params;
79 if( this.buildXMLRequest() == null ) alert("Browser is not supported!");
82 RemoteRequest.prototype.abort = function() {
83 if( this.xmlhttp ) this.xmlhttp.abort();
86 /* constructs our XMLHTTPRequest object */
87 RemoteRequest.prototype.buildXMLRequest = function() {
91 x = new ActiveXObject("Msxml2.XMLHTTP");
94 x = new ActiveXObject("Microsoft.XMLHTTP");
100 if (!x && typeof XMLHttpRequest!='undefined') x = new XMLHttpRequest();
103 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
112 function _remoteRequestCallback(id) {
114 var object = _allrequests[id];
115 if(object.cancelled) return;
117 if( object.xmlhttp.readyState == 4 ) {
119 object.callback(object);
122 /* if we receive a communication error, retry the request up
123 to XML_HTTP_MAX_TRIES attempts */
124 if( E && E.classname == "EXCommunication" ) {
126 //try { dump('Communication Error: ' + E ); } catch(e){}
127 alert('Debug: Communication Error: ' + E );
129 if(object.sendCount >= XML_HTTP_MAX_TRIES ) {
130 if(isXUL()) throw object;
131 else alert("Arrrgghh, Matey! Error communicating:\n" + E + "\n" + object.param_string);
133 object.buildXMLRequest();
140 destroyRequest(object);
147 /* define the callback we use when this request has received
149 RemoteRequest.prototype.setCompleteCallback = function(callback) {
150 if(this.cancelled) return;
151 this.callback = callback;
153 this.xmlhttp.onreadystatechange = function() { _remoteRequestCallback(id); }
157 /* http by default. This makes it https. *ONLY works when
158 embedded in a XUL app. */
159 RemoteRequest.prototype.setSecure = function(bool) {
163 RemoteRequest.prototype.send = function(blocking) {
165 if(this.cancelled) return;
167 /* determine the xmlhttp server dynamically */
168 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
171 if(this.secure || url.match(/^https:/) )
172 url = "https://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
174 url = "http://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
178 if( this.type == 'GET' ) url += "?" + this.param_string;
182 if(blocking) this.xmlhttp.open(this.type, url, false);
183 else this.xmlhttp.open(this.type, url, true);
186 alert("Fatal error opening XMLHTTPRequest for URL:\n" + url + '\n');
191 if( this.type == 'POST' ) {
192 data = this.param_string;
193 this.xmlhttp.setRequestHeader('Content-Type',
194 'application/x-www-form-urlencoded');
197 try{ this.xmlhttp.send( data ); } catch(e){}
203 /* returns the actual response text from the request */
204 RemoteRequest.prototype.getText = function() {
205 return this.xmlhttp.responseText;
208 RemoteRequest.prototype.isReady = function() {
209 return this.xmlhttp.readyState == 4;
213 /* returns the JSON->js result object */
214 RemoteRequest.prototype.getResultObject = function() {
216 if(this.cancelled) return null;
217 if(!this.xmlhttp) return null;
219 var text = this.xmlhttp.responseText;
220 if(text == "" || text == " " || text == null) null;
222 var obj = JSON2js(text);
223 if(!obj) return null;
225 if( obj.status != 200 ) {
227 alert("A Server Error Occured. Debug information follows:\n" +
228 "Status: " + obj.status + '\n' + obj.debug + '\n'
229 + 'Payload: ' + js2JSON(obj.payload) + '\n');
231 dump('Remote Request Error:' +
232 '\ncode = ' + obj.status +
233 '\ndebug: ' + obj.debug +
234 '\npayload: ' + js2JSON(obj.payload));
239 var payload = obj.payload;
240 if(!payload || payload.length == 0) return null;
241 payload = (payload.length == 1) ? payload[0] : payload;
243 if(payload.__isfieldmapper && payload.classname == "perm_ex") {
244 if(!isXUL()) alert(payload.err_msg());
251 /* adds a new parameter to the request */
252 RemoteRequest.prototype.addParam = function(param) {
253 var string = encodeURIComponent(js2JSON(param));
254 this.param_string += "¶m=" + string;