fingerprint-container-node-sdk
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -57,6 +57,7 @@ var util = require('util'); | ||
while (true) { | ||
var bufferItem = null; | ||
var slicer = null; | ||
var headerLen = 0; | ||
var state = this.state; | ||
//console.log(this.msg); | ||
// 如果不是可选的字段,那么统一的进行处理(获取片段及长度判断) | ||
@@ -128,3 +129,4 @@ if (OPTION_STATE.indexOf(state) == -1) { | ||
var offset = i * HEADER.ROUTER_LIST_ITEM; | ||
var bufferItem = (slicer.readUInt32BE(offset) << 8) + slicer.readUInt32BE(offset + 4); | ||
bufferItem = new Buffer(HEADER.ROUTER_LIST_ITEM); | ||
slicer.copy(bufferItem, 0, offset, offset + HEADER.ROUTER_LIST_ITEM); | ||
routersList.push(bufferItem); | ||
@@ -143,4 +145,5 @@ this.offset += HEADER.ROUTER_LIST_ITEM; | ||
slicer = this.buffer.slice(this.offset, this.offset + HEADER.DSTPARAM); | ||
this.msg.dstParam = (slicer.readUInt32BE(0) << 8) + slicer.readUInt32BE(4); | ||
bufferItem = new Buffer(HEADER.DSTPARAM); | ||
this.buffer.copy(bufferItem, 0, this.offset, this.offset + HEADER.DSTPARAM); | ||
this.msg.dstParam = bufferItem; | ||
this.offset += HEADER.DSTPARAM; | ||
@@ -147,0 +150,0 @@ this.state = STATE.BODY_PARSE; |
@@ -57,6 +57,4 @@ var Buffer = require('buffer').Buffer; | ||
for (var i = 0, len = msg.routers; i < len; i++) { | ||
var item = msg.routersList[i]; | ||
buffer.writeUInt32BE(item >> 8, offset); | ||
buffer.writeUInt32BE(item & 0x00ff, offset + 4); | ||
offset += 8; | ||
msg.routersList[i].copy(buffer, offset); | ||
offset += 8 | ||
} | ||
@@ -66,6 +64,4 @@ } | ||
if (msg.dstMode > 0) { | ||
var dstParam = msg.dstParam; | ||
buffer.writeUInt32BE(dstParam >> 8, offset); | ||
buffer.writeUInt32BE(dstParam & 0x00ff, offset + 4); | ||
offset += 8; | ||
msg.dstParam.copy(buffer, offset); | ||
offset += 8 | ||
} | ||
@@ -72,0 +68,0 @@ |
{ | ||
"name": "fingerprint-container-node-sdk", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "fingerprint container's sdk", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha ./test/test.js" | ||
}, | ||
@@ -23,3 +23,5 @@ "repository": { | ||
"underscore": "^1.8.3" | ||
} | ||
}, | ||
"_from": "fingerprint-container-node-sdk@0.0.1", | ||
"_resolved": "http://registry.npm.taobao.org/fingerprint-container-node-sdk/download/fingerprint-container-node-sdk-0.0.1.tgz" | ||
} |
100
service.js
@@ -18,13 +18,8 @@ var util = require('util'); | ||
var CRC32_CONTAINNER_COMM_REGISTER = crc32.unsigned(CONTAINNER_COMM_REGISTER); | ||
var FLAG = { | ||
RPC: 0, | ||
RPC_RES: 1, | ||
EVENT: 2 | ||
}; | ||
var FLAG = require('./flag'); | ||
function Service(host, port) { | ||
function Service(host, port, name) { | ||
EventEmitter.call(this); | ||
this.accepts = {}; | ||
this.acceptMap = {}; | ||
this.rpcs = []; | ||
this.name = name; | ||
this.rand = +new Date() % 65536; | ||
this.closed = false; | ||
@@ -72,11 +67,5 @@ this.uniqid = 0; | ||
macAddress = macInts[0] << 40 | | ||
macInts[1] << 32 | | ||
macInts[2] << 24 | | ||
macInts[3] << 16 | | ||
macInts[4] << 8 | | ||
macInts[5]; | ||
body.tag = macAddress | process.pid; | ||
body.name = 'service_' + body.tag; | ||
var pid = process.pid + _me.rand; | ||
body.tag = Number('0x' + macInts.join('') + (pid % 65536).toString(16)); | ||
body.name = _me.name || 'service_' + body.tag; | ||
body.accepts = _.map(_me.accepts, function (val, key) { | ||
@@ -91,5 +80,4 @@ return { | ||
}).then(function () { | ||
var msg = _me.encoder.encode(body, _me.genMsg(CONTAINNER_COMM_REGISTER)); | ||
var msg = _me.encoder.encode(body, _me._genMsg(CONTAINNER_COMM_REGISTER)); | ||
_me.client.write(msg); | ||
console.log(+new Date()); | ||
}).fail(function (cont, error) { | ||
@@ -101,29 +89,9 @@ // TODO log error | ||
Service.prototype.rpcRequest = function (serviceName, body, callback, timeout) { | ||
var _me = this; | ||
var msg = this.genMsg(serviceName, {flag: FLAG.RPC}); | ||
Service.prototype.rpcRequest = function (body, msg) { | ||
msg = this.encoder.encode(body, msg); | ||
this.client.write(msg); | ||
var timer = setTimeout(function () { | ||
var askId = msg.askId; | ||
var rpcInfo = _.findWhere(_me.rpcs, {askId: askId}); | ||
_me.rpcs.splice(rpcInfo.index, 1); | ||
_me.emit('timeout', { | ||
serviceName: serviceName, | ||
body: body, | ||
askId: askId | ||
}); | ||
}, timeout || 1500); | ||
this.rpcs.push({ | ||
askId: msg.askId, | ||
callback: callback, | ||
timer: timer, | ||
index: this.rpcs.length | ||
}); | ||
}; | ||
Service.prototype.eventRequest = function (serviceName, body) { | ||
var msg = this.genMsg(serviceName, {flag: FLAG.EVENT}); | ||
var msg = this._genMsg(serviceName, {flag: FLAG.EVENT}); | ||
msg = this.encoder.encode(body, msg); | ||
@@ -133,19 +101,2 @@ this.client.write(msg); | ||
Service.prototype.addHandler = function (serviceName, callback, isBroadCast, isForce) { | ||
this.acceptMap[crc32.unsigned(serviceName)] = serviceName; | ||
this.accepts[serviceName] = { | ||
callback: _.isFunction(callback) ? callback : noop, | ||
broadcast: isBroadCast || false, | ||
force: isForce || false | ||
}; | ||
}; | ||
Service.prototype.removeHandler = function (serviceName) { | ||
var crc32Name = crc32.unsigned(serviceName); | ||
this.acceptMap[crc32Name] = null; | ||
this.accepts[serviceName] = null; | ||
delete this.acceptMap[crc32Name]; | ||
delete this.accepts[serviceName]; | ||
}; | ||
Service.prototype.close = function () { | ||
@@ -175,3 +126,2 @@ if (!this.closed) { | ||
Service.prototype._onDataHandler = function (buffer) { | ||
console.log(buffer.length); | ||
this.decoder.decode(buffer); | ||
@@ -187,30 +137,16 @@ }; | ||
var body = msg.body; | ||
var flag = msg.flag; | ||
var isCalled = false; | ||
if (serviceName == CRC32_CONTAINNER_COMM_PING) { | ||
this.eventRequest(CONTAINNER_COMM_PING, body); | ||
isCalled = true; | ||
} else if (serviceName == CRC32_CONTAINNER_COMM_REGISTER) { | ||
this.emit('register'); | ||
} else if (flag == FLAG.RPC_RES) { | ||
var askId = msg.askId; | ||
var rpcInfo = _.findWhere(this.rpcs, {askId: askId}); | ||
if (rpcInfo == null) { | ||
return; | ||
} | ||
isCalled = true; | ||
} | ||
clearTimeout(rpcInfo.timer); | ||
rpcInfo.callback(body); | ||
this.rpcs.splice(rpcInfo.index, 1); | ||
} else { | ||
serviceName = this.acceptMap[serviceName]; | ||
if (serviceName == null) { | ||
return; | ||
} | ||
var accept = this.accepts[serviceName]; | ||
accept.callback(body); | ||
} | ||
return isCalled; | ||
}; | ||
Service.prototype.genMsg = function (serviceName, opt) { | ||
Service.prototype._genMsg = function (serviceName, opt) { | ||
return _.extend({ | ||
@@ -224,7 +160,3 @@ service: crc32.unsigned(serviceName), | ||
function noop() { | ||
} | ||
module.exports = Service; | ||
@@ -5,2 +5,3 @@ var assert = require("assert"); | ||
var crc32 = require('buffer-crc32'); | ||
var Buffer = require('buffer').Buffer; | ||
var Encoder = require('../encoder'); | ||
@@ -25,4 +26,4 @@ var Decoder = require('../decoder'); | ||
dstMode: 1, | ||
routersList: [1, 2], | ||
dstParam: 2, | ||
routersList: [new Buffer(8), new Buffer(8)], | ||
dstParam: new Buffer(8), | ||
body: { | ||
@@ -29,0 +30,0 @@ name: 'UnitTest' |
var assert = require("assert"); | ||
var _ = require('underscore'); | ||
var crc32 = require('buffer-crc32'); | ||
var Encoder = require('../encoder'); | ||
var Buffer = require('buffer').Buffer; | ||
var Encoder = require('../fpc').Encoder; | ||
@@ -17,4 +18,4 @@ describe("Encoder", function () { | ||
dstMode: 1, | ||
routersList: [1, 2], | ||
dstParam: 2, | ||
routersList: [new Buffer(8), new Buffer(8)], | ||
dstParam: new Buffer(8), | ||
body: { | ||
@@ -21,0 +22,0 @@ name: 'UnitTest' |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
29733
21
737
2