OpenSRF.Request = function(session, reqid, args) {
this.session = session;
this.reqid = reqid;
+
+ /* callbacks */
this.onresponse = args.onresponse;
- this.onerror = args.onerror;
this.oncomplete = args.oncomplete;
+ this.onerror = args.onerror;
+ this.onmethoderror = args.onmethoderror;
+ this.ontransporterror = args.ontransporterror;
+
this.method = args.method;
this.params = args.params;
this.timeout = args.timeout;
'timeout' : this.timeout,
'onresponse' : this.onresponse,
'oncomplete' : this.oncomplete,
- 'onerror' : this.onerror
+ 'onerror' : this.onerror,
+ 'onmethoderror' : this.onmethoderror,
+ 'ontransporterror' : this.ontransporterror
});
}
OpenSRF.Stack = function() {
}
-OpenSRF.Stack.push = function(net_msg, stack_callback) {
+OpenSRF.Stack.push = function(net_msg, callbacks) {
var ses = OpenSRF.Session.find_session(net_msg.thread);
if(!ses) return;
ses.remote_id = net_msg.sender;
osrf_msgs = JSON2js(net_msg.body);
for(var i = 0; i < osrf_msgs.length; i++)
- OpenSRF.Stack.handle_message(ses, osrf_msgs[i], stack_callback);
+ OpenSRF.Stack.handle_message(ses, osrf_msgs[i], callbacks);
}
-OpenSRF.Stack.handle_message = function(ses, osrf_msg, stack_callback) {
+OpenSRF.Stack.handle_message = function(ses, osrf_msg, callbacks) {
var req = null;
if(status == OSRF_STATUS_COMPLETE) {
req = ses.find_request(osrf_msg.threadTrace());
- if(req) req.complete = true;
+ if(req) {
+ req.complete = true;
+ if(callbacks.oncomplete && !req.oncomplete_called) {
+ req.oncomplete_called = true;
+ return callbacks.oncomplete(req);
+ }
+ }
}
if(status == OSRF_STATUS_OK) {
ses.state = OSRF_APP_SESSION_CONNECTED;
+
+ /* call the connect callback */
+ if(ses.onconnect && !ses.onconnect_called) {
+ ses.onconnect_called = true;
+ return ses.onconnect();
+ }
}
if(status == OSRF_STATUS_NOTFOUND) {
- alert('NOT_FOUND: ' + status_text);
- return;
+ req = ses.find_request(osrf_msg.threadTrace());
+ if(callbacks.onmethoderror)
+ return callbacks.onmethoderror(req, status, status_text);
}
}
if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
req = ses.find_request(osrf_msg.threadTrace());
- if(req)
+ if(req) {
req.response_queue.push(osrf_msg.payload());
+ if(callbacks.onresponse)
+ return callbacks.onresponse(req);
+ }
}
-
- if(stack_callback)
- stack_callback(ses, req);
}
/* The following classes map directly to network-serializable opensrf objects */
return this;
}
-
OpenSRF.XHRequest.prototype.core_handler = function() {
sender = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_FROM);
thread = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_THREAD);
json = this.xreq.responseText;
stat = this.xreq.status;
- var xhr = this;
+ if(stat >= 400)
+ return this.transport_error_handler();
+
OpenSRF.Stack.push(
new OpenSRF.NetMessage(null, sender, thread, json),
+ {
+ onresponse : this.args.onresponse,
+ oncomplete : this.args.oncomplete,
+ onerror : this.args.onerror,
+ onmethoderror : this.method_error_handler()
+ }
+ );
+}
- function(ses, req) {
- if(ses) {
- if(ses.state == OSRF_APP_SESSION_CONNECTED &&
- ses.onconnect && !ses.onconnect_called) {
- ses.onconnect_called = true;
- ses.onconnect();
- }
- }
- if(req) {
- if(req.response_queue.length > 0 && xhr.args.onresponse)
- return xhr.args.onresponse(req);
+OpenSRF.XHRequest.prototype.method_error_handler = function() {
+ var xhr = this;
+ return function(req, status, status_text) {
+ if(xhr.args.onmethoderror)
+ xhr.args.onmethoderror(req, status, status_text);
+ if(xhr.args.onerror)
+ xhr.args.onerror(xhr.message, xhr.args.rcpt || xhr.args.rcpt_service, xhr.args.thread);
+ }
+}
- if(req.complete && xhr.args.oncomplete && !xhr.args.oncomplete_called) {
- xhr.args.oncomplete_called = true;
- return xhr.args.oncomplete(req);
- }
- }
- }
- );
+OpenSRF.XHRequest.prototype.transport_error_handler = function() {
+ if(this.args.ontransporterror)
+ this.args.ontransporterror(this.xreq);
+ if(this.args.onerror)
+ this.args.onerror(this.message, this.args.rcpt || this.args.rcpt_service, this.args.thread);
}
+