Skip to content
Browse files

removed dependency on fibers. service methods now support async callb…

…acks.
  • Loading branch information...
1 parent 65c1688 commit 3a8a6617a0187ebc528d1caadc720571e5a01a14 Vinay Pulim committed
Showing with 71 additions and 44 deletions.
  1. +58 −23 lib/server.js
  2. +1 −3 test/ip2tele/fake_server.js
  3. +2 −3 test/ip2tele/ip2tele.js
  4. +10 −15 test/vac/vac.js
View
81 lib/server.js
@@ -7,7 +7,6 @@ function findKey(obj, val) {
for (var n in obj) if (obj[n] === val) return n;
}
-require('fibers');
var url = require('url'),
compress = null;
@@ -68,17 +67,19 @@ Server.prototype._requestListener = function(req, res) {
gunzip.end();
gunzip = null
}
- Fiber(function() {
try {
- result = self._process(xml, req.url);
- self.logger_res && self.logger_res(result,res,req);
+ self._process(xml, req.url, function(result) {
+ self.logger_res && self.logger_res(result,res,req);
+ res.write(result);
+ res.end();
+ });
}
- catch (err) {
- result = err.stack;
+ catch(err) {
+ err = err.stack || err;
+ self.logger_res && self.logger_res(err,res,req);
+ res.write(err);
+ res.end();
}
- res.write(result);
- res.end();
- }).run();
});
}
else {
@@ -86,7 +87,7 @@ Server.prototype._requestListener = function(req, res) {
}
}
-Server.prototype._process = function(input,URL) {
+Server.prototype._process = function(input, URL, callback) {
var pathname = url.parse(URL).pathname.replace(/\/$/,'');
var self = this,
obj = this.wsdl.xmlToObject(input),
@@ -110,32 +111,66 @@ Server.prototype._process = function(input,URL) {
})(this);
methods = binding.methods;
+
if(binding.style === 'rpc') {
methodName = Object.keys(obj)[0];
- return self._executeMethod(serviceName, portName, methodName, methodName+'Response', obj[methodName], 'rpc');
+ self._executeMethod({
+ serviceName: serviceName,
+ portName: portName,
+ methodName: methodName,
+ outputName: methodName + 'Response',
+ args: obj[methodName],
+ style: 'rpc'
+ }, callback);
} else {
var messageElemName = Object.keys(obj)[0];
var pair = binding.topElements[messageElemName];
- return self._executeMethod(serviceName, portName, pair.methodName, pair.outputName, obj[messageElemName], 'document');
+ self._executeMethod({
+ serviceName: serviceName,
+ portName: portName,
+ methodName: pair.methodName,
+ outputName: pair.outputName,
+ args: obj[messageElemName],
+ style: 'document'
+ }, callback);
}
-
- return '';
}
-Server.prototype._executeMethod = function(serviceName, portName, methodName, outputName, args, style) {
- var method, body;
+Server.prototype._executeMethod = function(options, callback) {
+ options = options || {};
+ var self = this,
+ method, body,
+ serviceName = options.serviceName,
+ portName = options.portName,
+ methodName = options.methodName,
+ outputName = options.outputName,
+ args = options.args,
+ style = options.style,
+ handled = false;
+
try {
method = this.services[serviceName][portName][methodName];
} catch(e) {
- return this._envelope(body);
+ return callback(this._envelope(''));
}
- if(style==='rpc') {
- body = this.wsdl.objectToRpcXML(outputName, method(args), '', this.wsdl.definitions.$targetNamespace);
- } else {
- var element = this.wsdl.definitions.services[serviceName].ports[portName].binding.methods[methodName].output;
- body = this.wsdl.objectToDocumentXML(outputName, method(args), element.targetNSAlias, element.targetNamespace);
+
+ function handleResult(result) {
+ if (handled) return;
+ handled = true;
+
+ if(style==='rpc') {
+ body = self.wsdl.objectToRpcXML(outputName, result, '', self.wsdl.definitions.$targetNamespace);
+ } else {
+ var element = self.wsdl.definitions.services[serviceName].ports[portName].binding.methods[methodName].output;
+ body = self.wsdl.objectToDocumentXML(outputName, result, element.targetNSAlias, element.targetNamespace);
+ }
+ callback(self._envelope(body));
+ }
+
+ var result = method(args, handleResult);
+ if (typeof result !== 'undefined') {
+ handleResult(result);
}
- return this._envelope(body);
}
Server.prototype._envelope = function(body) {
View
4 test/ip2tele/fake_server.js
@@ -4,14 +4,12 @@ var path = require('path');
var http = require('http');
var QueryString = require('querystring');
var Request = require('request');
-var moment = require('moment');
var fs = require('fs');
var SoapServices = {
'QueryUserInfoServiceApply': {
'QueryUserInfoServiceApplyHttpPort': {
'QueryUserInfoServiceApply': function(args) {
- var fiber = Fiber.current;
console.log('received args = ');
console.log(args);
return ({
@@ -37,7 +35,7 @@ server.listen(cfg.fake_port);
var wsdl_string = require('fs').readFileSync(path.resolve(cfg.wsdl_file), 'utf8');
var soap_server = soap.listen(server, cfg.path, SoapServices, wsdl_string);
soap_server.logger_req = function(xml, req, res) {
- req.__time = moment().format('MMDD-HHmmss');
+ req.__time = +new Date;
var cip = req.connection.remoteAddress;
var filename = 'logs/svr-' + req.__time + '-' + cip + '-req.log.xml';
var ws = fs.createWriteStream(filename);
View
5 test/ip2tele/ip2tele.js
@@ -1,6 +1,5 @@
var http = require('http');
-var soap = require('node-soap-ly');
-var moment = require('moment');
+var soap = require('../..');
var inspect = require('util').inspect;
var url = require('url');
var path = require('path');
@@ -37,7 +36,7 @@ var web_server = http.createServer(function(req, res) {
},
'ServerInfo': {
'ServerID': cfg.ServerID,
- 'TimeStamp': moment().format('YYYYMMDDHHmmss')
+ 'TimeStamp': +new Date
}
}
console.log(QueryUserInfoRequest);
View
25 test/vac/vac.js
@@ -4,12 +4,11 @@ var cfg = {
service_port: 8007,
psp_url: 'http://61.181.22.71:81/admin/if_vac_h'
}
-var soap = require('node-soap-ly');
+var soap = require('../..');
var path = require('path');
var http = require('http');
var QueryString = require('querystring');
var Request = require('request');
-var moment = require('moment');
var fs = require('fs');
var reqSeq = 0;
@@ -18,8 +17,7 @@ var defLog = false;
var SoapServices = {
'ESyncNotifySPServiceService': {
'ESyncNotifySP': {
- 'eOrderRelationUpdateNotify': function(args) {
- var fiber = Fiber.current;
+ 'eOrderRelationUpdateNotify': function(args, callback) {
var oraReqNV = args["eOrderRelationUpdateNotifyRequest"];
oraReqNV.SubInfo.split(',').forEach(function(line) {
if (!line) return;
@@ -37,14 +35,14 @@ var SoapServices = {
},
function(error, response, body) {
if (!error && response.statusCode === 200) {
- fiber.run({
+ callback({
'eOrderRelationUpdateNotifyResponse': {
'RecordSequenceID': oraReqNV.RecordSequenceID,
'ResultCode': body
}
});
} else {
- fiber.run({
+ callback({
'eOrderRelationUpdateNotifyResponse': {
'RecordSequenceID': oraReqNV.RecordSequenceID,
'ResultCode': 1
@@ -52,10 +50,8 @@ var SoapServices = {
});
}
});
- return yield();
},
- 'eMemOrderRelationUpdateNotify': function(args) {
- var fiber = Fiber.current;
+ 'eMemOrderRelationUpdateNotify': function(args, callback) {
args = args["eMemOrderRelationUpdateNotifyRequest"];
var oraReqNV = {
'billnum': args.EUserId,
@@ -75,14 +71,14 @@ var SoapServices = {
},
function(error, response, body) {
if (!error && response.statusCode === 200) {
- fiber.run({
+ callback({
'eMemOrderRelationUpdateNotifyResponse': {
'RecordSequenceID': args.RecordSequenceID,
'ResultCode': body
}
});
} else {
- fiber.run({
+ callback({
'eMemOrderRelationUpdateNotifyResponse': {
'RecordSequenceID': args.RecordSequenceID,
'ResultCode': 1
@@ -90,7 +86,6 @@ var SoapServices = {
});
}
});
- return yield();
}
}
}
@@ -104,7 +99,7 @@ var wsdl_string = require('fs').readFileSync(path.resolve(cfg.wsdl_file), 'utf8'
var soap_server = soap.listen(server, cfg.service_url_path, SoapServices, wsdl_string);
soap_server.logger_req = function(xml, req, res) {
- req.__time = moment().format('MMDD-HHmmss');
+ req.__time = +new Date;
var cip = req.connection.remoteAddress;
var filename = 'logs/srv-' + req.__time + '-' + (++reqSeq) + '-' + cip + '-req.log.xml';
var ws = fs.createWriteStream(filename);
@@ -155,14 +150,14 @@ setTimeout(function() {
var logSeq = 0;
function do_test(client) {
client.logger_req = function(xml) {
- var time = moment().format('MMDD-HHmmss');
+ var time = +new Date;
var filename = 'logs/cli-' + time + '-' + (++logSeq) + '-req.log.xml';
var ws = fs.createWriteStream(filename);
ws.write(xml);
ws.end();
};
client.logger_res = function(xml) {
- var time = moment().format('MMDD-HHmmss');
+ var time = +new Date;
var filename = 'logs/cli-' + time + '-' + '-' + (++logSeq) + '-res.log.xml';
var ws = fs.createWriteStream(filename);
ws.write(xml);

0 comments on commit 3a8a661

Please sign in to comment.
Something went wrong with that request. Please try again.