Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added packet tracing #257

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/protocol/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ Connection.prototype.open = function open(options, cb) {

function ondata(chunk) {
cleanup();
trace('INITIALIZATION REPLY', chunk);
if (!chunk || chunk.length < InitializationReply.LENGTH) {
return cb(invalidInitializationReply());
}
Expand All @@ -233,6 +234,7 @@ Connection.prototype.open = function open(options, cb) {
self.port = options['port'];
timeoutObject = setTimeout(onerror, self.initializationTimeout, initializationTimeoutError());
socket.write(initializationRequestBuffer);
trace('INITIALIZATION REQUEST', initializationRequestBuffer);
});
socket.once('error', onerror);
socket.on('data', ondata);
Expand All @@ -254,6 +256,8 @@ Connection.prototype._addListeners = function _addListeners(socket) {
function ondata(chunk) {
packet.push(chunk);
if (packet.isReady()) {
trace('REPLY', packet.getTraceData());

if (self._state.sessionId !== packet.header.sessionId) {
self._state.sessionId = packet.header.sessionId;
self._state.packetCount = -1;
Expand Down Expand Up @@ -321,9 +325,6 @@ Connection.prototype.send = function send(message, receive) {
message.add(PartKind.CLIENT_INFO, this._clientInfo.getUpdatedProperties());
}

debug('send', message);
trace('REQUEST', message);

var size = this.packetSizeLimit - PACKET_HEADER_LENGTH;
var buffer = message.toBuffer(size);
if(buffer.length > size) {
Expand Down Expand Up @@ -354,6 +355,9 @@ Connection.prototype.send = function send(message, receive) {
if (this._socket) {
this._socket.write(packet);
}

debug('send', message);
trace('REQUEST', packet, this.connectOptions.connectionId);
};


Expand Down Expand Up @@ -389,7 +393,6 @@ Connection.prototype.setTransactionFlags = function setTransactionFlags(flags) {

Connection.prototype._parseReplySegment = function _parseReplySegment(buffer) {
var segment = ReplySegment.create(buffer, 0);
trace(segment.kind === SegmentKind.ERROR ? 'ERROR' : 'REPLY', segment);
return segment.getReply();
};

Expand Down
15 changes: 15 additions & 0 deletions lib/protocol/MessageBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function MessageBuffer() {
this.length = 0;
this.header = undefined;
this.data = undefined;
this.headerBuffer = undefined;
}

MessageBuffer.prototype.isReady = function () {
Expand Down Expand Up @@ -61,12 +62,26 @@ MessageBuffer.prototype.readHeader = function readHeader() {
packetCount: buffer.readUInt32LE(8),
length: buffer.readUInt32LE(12)
};
if (util.isTraceOn()) {
this.headerBuffer = buffer.slice(0, PACKET_HEADER_LENGTH);
}
this.data = buffer.slice(PACKET_HEADER_LENGTH);
this.length -= PACKET_HEADER_LENGTH;
};

MessageBuffer.prototype.getTraceData = function getTraceData() {
if (util.isTraceOn()) {
if (Array.isArray(this.data)) {
return Buffer.concat([this.headerBuffer].concat(this.data), this.length + PACKET_HEADER_LENGTH);
}
return Buffer.concat([this.headerBuffer, this.data], this.length + PACKET_HEADER_LENGTH);
}
return undefined;
}

MessageBuffer.prototype.clear = function clear() {
this.length = 0;
this.header = undefined;
this.data = undefined;
this.headerBuffer = undefined;
};
22 changes: 15 additions & 7 deletions lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var stream = require('stream');
var path = require('path');
var os = require('os');
var fs = require('fs');
var packetTracer = require('./trace/PacketTracer');

Object.defineProperties(exports, {
setImmediate: {
Expand Down Expand Up @@ -53,23 +54,30 @@ var debug = exports.debuglog('hdb_util');
exports.tracefile = function tracefile() {
var timestamp = Math.floor(Date.now() / 1000);
var filename = 'hdb.trace.' + timestamp + '.log';
return path.join(os.tmpdir(), filename);
return filename;
};

exports._appendFileSync = fs.appendFileSync;
exports.isTraceOn = function isTraceOn() {
return !!process.env.HDB_TRACE;
}
exports.tracelog = function tracelog() {
if (!process.env.HDB_TRACE) {
return function dummyTracelog() {};
}

var filename = exports.tracefile();
debug('Trace to file', filename);
return function tracelog(kind, segment) {
exports._appendFileSync(filename,
kind + '\n' +
util.inspect(segment, {
depth: 9
}));
return function tracelog(kind, buffer, connectionId) {
if (kind === 'INITIALIZATION REQUEST') {
exports._appendFileSync(filename, packetTracer.parseInitializationRequest(buffer));
} else if (kind === 'INITIALIZATION REPLY') {
exports._appendFileSync(filename, packetTracer.parseInitializationReply(buffer));
} else if (kind === 'REQUEST') {
exports._appendFileSync(filename, packetTracer.parseRequest(buffer, connectionId));
} else if (kind === 'ERROR' || kind === 'REPLY') {
exports._appendFileSync(filename, packetTracer.parseReply(buffer));
}
};
};

Expand Down
Loading