Skip to content

Commit

Permalink
added ping command sidorares#38
Browse files Browse the repository at this point in the history
  • Loading branch information
sidorares committed Aug 28, 2013
1 parent 95402cf commit cbca864
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/commands/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"client_handshake server_handshake query execute".split(' ').forEach(function(name) {
"client_handshake server_handshake query execute ping".split(' ').forEach(function(name) {
var ctor = require('./' + name);
module.exports[ctor.name] = ctor;
});
30 changes: 30 additions & 0 deletions lib/commands/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var Command = require('./command');
var util = require('util');
var CommandCode = require('../constants/commands');
var Packet = require('../packets/packet');

// TODO: time statistics?
// usefull for queue size and network latency monitoring
// store created,sent,reply timestamps

function Ping(callback)
{
Command.call(this);
this.onResult = callback;
}
util.inherits(Ping, Command);

Ping.prototype.start = function(packet, connection) {
var ping = new Packet(0, new Buffer([0, 0, 0, 0, CommandCode.PING]));
connection.writePacket(ping);
return Ping.prototype.pingResponse;
};

Ping.prototype.pingResponse = function(packet) {
// TODO: check it's OK packet. error check already done in caller
if (this.onResult)
this.onResult();
return null;
};

module.exports = Ping;
4 changes: 4 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ Connection.prototype.execute = function(sql, values, cb) {
return this.addCommand(new Commands.Execute(options.sql, options.values, cb));
};

Connection.prototype.ping = function(cb) {
return this.addCommand(new Commands.Ping(cb));
};

Connection.prototype.connect = function(cb) {
// TODO: call cb only after succesfull handshake response
// on('connect') handler?
Expand Down

0 comments on commit cbca864

Please sign in to comment.