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) {
28 r.xmlhttp.onreadystatechange = function(){};
32 _allrequests[r.id] = null;
35 /* ----------------------------------------------------------------------- */
37 function RemoteRequest( service, method ) {
40 this.service = service;
46 this.type = "POST"; /* default */
47 this.id = service + method + Math.random();
48 this.cancelled = false;
50 _allrequests[this.id] = this;
55 while(i < arguments.length) {
56 var object = js2JSON(arguments[i++]);
57 this.params += "¶m=" + encodeURIComponent(object);
60 if(!this.params) { this.params = ""; }
61 this.param_string = "service=" + service + "&method=" + method + this.params;
62 if( this.buildXMLRequest() == null ) alert("Browser is not supported!");
65 /* constructs our XMLHTTPRequest object */
66 RemoteRequest.prototype.buildXMLRequest = function() {
70 x = new ActiveXObject("Msxml2.XMLHTTP");
73 x = new ActiveXObject("Microsoft.XMLHTTP");
79 if (!x && typeof XMLHttpRequest!='undefined') x = new XMLHttpRequest();
82 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
91 function _remoteRequestCallback(id) {
93 var object = _allrequests[id];
94 if(object.cancelled) return;
96 if( object.xmlhttp.readyState == 4 ) {
98 object.callback(object);
101 /* if we receive a communication error, retry the request up
102 to XML_HTTP_MAX_TRIES attempts */
103 if( E && E.classname == "EXCommunication" ) {
105 //try { dump('Communication Error: ' + E ); } catch(e){}
106 alert('Debug: Communication Error: ' + E );
108 if(object.sendCount >= XML_HTTP_MAX_TRIES ) {
109 if(isXUL()) throw object;
110 else alert("Arrrgghh, Matey! Error communicating:\n" + E + "\n" + object.param_string);
112 object.buildXMLRequest();
119 destroyRequest(object);
126 /* define the callback we use when this request has received
128 RemoteRequest.prototype.setCompleteCallback = function(callback) {
129 if(this.cancelled) return;
130 this.callback = callback;
132 this.xmlhttp.onreadystatechange = function() { _remoteRequestCallback(id); }
136 /* http by default. This makes it https. *ONLY works when
137 embedded in a XUL app. */
138 RemoteRequest.prototype.setSecure = function(bool) {
142 RemoteRequest.prototype.send = function(blocking) {
144 if(this.cancelled) return;
146 /* determine the xmlhttp server dynamically */
147 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
150 if(this.secure || url.match(/^https:/) )
151 url = "https://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
153 url = "http://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
157 if( this.type == 'GET' ) url += "?" + this.param_string;
161 if(blocking) this.xmlhttp.open(this.type, url, false);
162 else this.xmlhttp.open(this.type, url, true);
165 alert("Fatal error opening XMLHTTPRequest for URL:\n" + url + '\n');
170 if( this.type == 'POST' ) {
171 data = this.param_string;
172 this.xmlhttp.setRequestHeader('Content-Type',
173 'application/x-www-form-urlencoded');
176 try{ this.xmlhttp.send( data ); } catch(e){}
182 /* returns the actual response text from the request */
183 RemoteRequest.prototype.getText = function() {
184 return this.xmlhttp.responseText;
187 RemoteRequest.prototype.isReady = function() {
188 return this.xmlhttp.readyState == 4;
192 /* returns the JSON->js result object */
193 RemoteRequest.prototype.getResultObject = function() {
195 if(this.cancelled) return null;
196 if(!this.xmlhttp) return null;
198 var text = this.xmlhttp.responseText;
199 if(text == "" || text == " " || text == null) null;
201 var obj = JSON2js(text);
204 if( obj.status != 200 ) {
206 alert("A Server Error Occured. Debug information follows:\n",
207 "Status: " + obj.status + '\n' + obj.debug + '\n'
208 + 'Payload: ' + js2JSON(obj.payload) + '\n');
209 } else { throw obj; }
212 var payload = obj.payload;
213 payload = (payload.length == 1) ? payload[0] : payload;
215 if(payload.__isfieldmapper && payload.classname == "perm_ex") {
216 if(!isXUL()) alert(payload.err_msg());
223 /* adds a new parameter to the request */
224 RemoteRequest.prototype.addParam = function(param) {
225 var string = encodeURIComponent(js2JSON(param));
226 this.param_string += "¶m=" + string;