2 var XML_HTTP_GATEWAY = "gateway";
3 var XML_HTTP_SERVER = "gapines.org";
4 var XML_HTTP_MAX_TRIES = 3;
8 /* true if we've been absorbed by a XUL app */
19 /* ----------------------------------------------------------------------- */
22 /* keeping all requests in a global cache allows us to manage request
23 resends effectively */
25 /* Array of globally pending requests */
26 RemoteRequest.pending = new Array();
28 /* cleans requests (and null entries) from the pending array */
29 RemoteRequest.prunePending = function(id) {
30 var tmpArray = new Array();
31 for( var x in RemoteRequest.pending ) {
32 if( RemoteRequest.pending[x] != null ) {
33 if( RemoteRequest.pending[x].id != id )
34 tmpArray.push(RemoteRequest.pending[x]);
37 RemoteRequest.pending = tmpArray;
40 /* returns the number of pending requests */
41 RemoteRequest.numPending = function() {
42 return RemoteRequest.pending.length;
45 RemoteRequest.cancelAll = function() {
46 for( var x in RemoteRequest.pending ) {
47 if( RemoteRequest.pending[x] != null ) {
48 debug("Cancelling request...");
49 RemoteRequest.pending[x].cancelled = true;
55 /* ----------------------------------------------------------------------- */
56 /* Generic request manager */
57 function RequestBatch() {
58 this.requests = new Array();
60 RequestBatch.prototype.add = function(request) {
61 this.requests.push(request);
64 RequestBatch.prototype.remove = function(request) {
65 var newArray = new Array();
66 for( var i in this.requests ) {
67 if( this.requests[i] != null &&
68 this.requests[i].id != request.id )
69 newArray.push(this.requests[i]);
71 this.requests = newArray;
74 RequestBatch.prototype.pending = function() {
75 return this.requests.length;
78 /* cancels all requests in this batch that have not already been sent */
79 RequestBatch.prototype.cancel = function() {
80 for(var i in this.requests) {
81 if(this.requests[i] != null)
82 this.requests.cancelled = true;
86 /* ----------------------------------------------------------------------- */
88 function RemoteRequest( service, method ) {
90 this.service = service;
96 this.type = "POST"; /* default */
97 this.id = service + method + Math.random();
98 this.cancelled = false;
103 while(i < arguments.length) {
104 var object = js2JSON(arguments[i++]);
105 this.params += "&__param=" + encodeURIComponent(object);
108 if(!this.params) { this.params = ""; }
109 this.param_string = "service=" + service + "&method=" + method + this.params;
111 if( ! this.type || ! this.service || ! this.method ) {
112 alert( "ERROR IN REQUEST PARAMS");
116 if( this.buildXMLRequest() == null )
117 alert("NEWER BROWSER");
120 /* constructs our XMLHTTPRequest object */
121 RemoteRequest.prototype.buildXMLRequest = function() {
124 this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
127 this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
129 this.xmlhttp = false;
133 if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
134 this.xmlhttp = new XMLHttpRequest();
138 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
143 this.setCompleteCallback( this.callback );
149 /* define the callback we use when this request has received
151 RemoteRequest.prototype.setCompleteCallback = function(callback) {
153 if(this.cancelled) return;
156 var obj = this.xmlhttp;
157 this.callback = callback;
159 this.xmlhttp.onreadystatechange = function() {
160 if( obj.readyState == 4 ) {
163 if(object.cancelled) return;
168 debug("Processing Error in complete callback: [" + E + "]");
170 /* if we receive a communication error, retry the request up
171 to XML_HTTP_MAX_TRIES attempts */
172 if( instanceOf(E, EXCommunication) ) {
174 debug("Communication Error: [" + E + "]");
175 if(object.sendCount >= XML_HTTP_MAX_TRIES ) {
179 alert("Arrrgghh, Matey! Error communicating:\n" +
180 E + "\n" + object.param_string);
183 object.buildXMLRequest();
188 /* any other exception is alerted for now */
189 RemoteRequest.prunePending(object.id);
190 //alert("Exception: " + E);
195 /* on success, remove the request from the pending cache */
196 RemoteRequest.prunePending(object.id);
202 /* http by default. This makes it https. *ONLY works when
203 embedded in a XUL app. */
204 RemoteRequest.prototype.setSecure = function(bool) {
209 * By default, all calls are asynchronous. if 'blocking' is
210 * set to true, then the call will block until a response
211 * is received. If blocking, callbacks will not be called.
212 * In other words, you can assume the data is avaiable
213 * (getResponseObject()) as soon as the send call returns.
215 RemoteRequest.prototype.send = function(blocking) {
217 if(this.cancelled) return;
219 if( this.sendCount == 0)
220 RemoteRequest.pending.push(this);
222 debug("Resending request with id " + this.id
223 + " and send count " + this.sendCount);
225 /* determine the xmlhttp server dynamically */
226 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
230 url = "https://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
232 //url = "http://" + XML_HTTP_SERVER + ":8080/" + XML_HTTP_GATEWAY;
233 url = "http://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
238 if( this.type == 'GET' ) {
239 url += "?" + this.param_string;
243 this.xmlhttp.open(this.type, url, false);
245 this.xmlhttp.open(this.type, url, true);
249 if( this.type == 'POST' ) {
250 data = this.param_string;
251 this.xmlhttp.setRequestHeader('Content-Type',
252 'application/x-www-form-urlencoded');
255 debug("Remote Request sending..." + data);
256 this.xmlhttp.send( data );
258 debug("Remote Request done sending");
262 /* returns the actual response text from the request */
263 RemoteRequest.prototype.getText = function() {
264 return this.xmlhttp.responseText;
267 RemoteRequest.prototype.isReady = function() {
268 return this.xmlhttp.readyState == 4;
272 /* returns the JSON->js result object */
273 RemoteRequest.prototype.getResultObject = function() {
274 if(this.cancelled) return null;
276 var text = this.xmlhttp.responseText;
277 var obj = JSON2js(text);
280 debug("received null response");
285 debug("Something's Wrong: " + js2JSON(obj));
286 throw new EXCommunication(obj.err_msg);
289 if( obj[0] != null && obj[1] == null )
292 /* these are user level exceptions from the server code */
293 if(instanceOf(obj, ex)) {
294 debug("Received user level exception: " + obj.err_msg());
301 /* adds a new parameter to the request */
302 RemoteRequest.prototype.addParam = function(param) {
303 var string = encodeURIComponent(js2JSON(param));
304 this.param_string += "&__param=" + string;