forked from fzaninotto/uptime
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var util = require('util'), | ||
dgram = require('dgram'), | ||
BasePoller = require('./base'); | ||
/** | ||
* starting UDP server | ||
*/ | ||
var udpServer = dgram.createSocket('udp4'); | ||
/** | ||
* binding required for getting responses | ||
* */ | ||
udpServer.bind(); | ||
udpServer.on('error', function () {}); | ||
/** | ||
* Poller constructor | ||
* | ||
* @param {Mixed} Poller Target (e.g. URL) | ||
* @param {Number} Poller timeout in milliseconds. Without response before this duration, the poller stops and executes the error callback. | ||
* @param {Function} Error/success callback | ||
* @api public | ||
*/ | ||
function UdpPoller(target, timeout, callback) { | ||
UdpPoller.super_.call(this, target, timeout, callback); | ||
} | ||
|
||
util.inherits(UdpPoller, BasePoller); | ||
|
||
UdpPoller.prototype.initialize = function() { | ||
this.udpServer = udpServer; | ||
var reg = new RegExp('udp:\/\/(.*):(\\d{1,5})'); | ||
if(!reg.test(this.target)) { | ||
console.log(this.target + ' does not seems to be valid udp url'); | ||
} | ||
var host = reg.exec(this.target); | ||
this.target = { | ||
'address': host[1], | ||
'port': host[2] | ||
}; | ||
} | ||
|
||
/** | ||
* Launch the actual polling | ||
* | ||
* @api public | ||
*/ | ||
UdpPoller.prototype.poll = function() { | ||
UdpPoller.super_.prototype.poll.call(this); | ||
var ping = new Buffer(JSON.stringify({'command': 'ping'})); | ||
this.udpServer.send(ping, 0, ping.length, this.target.port, this.target.address); | ||
this.udpServer.on("message", this.onResponseCallback.bind(this)); | ||
} | ||
|
||
/** | ||
* Response callback | ||
* @api private | ||
*/ | ||
UdpPoller.prototype.onResponseCallback = function(message, sender) { | ||
this.debug(this.getTime() + 'ms - got answer from ' + sender.address + ':' + sender.port); | ||
var cmd; | ||
try { | ||
cmd = JSON.parse(message); | ||
} catch (e) { | ||
return this.onErrorCallback({ name: "Unparsable answer", message: "server return answer " + message.toString()}); | ||
} | ||
if (cmd.command === 'pong') { | ||
this.timer.stop(); | ||
this.debug(this.getTime() + 'ms - Got response'); | ||
this.callback(null, this.getTime(), cmd); | ||
} | ||
} | ||
|
||
/** | ||
* Timeout callback | ||
* | ||
* @api private | ||
*/ | ||
UdpPoller.prototype.timeoutReached = function() { | ||
UdpPoller.super_.prototype.timeoutReached.call(this); | ||
this.udpServer.removeAllListeners(); | ||
} | ||
|
||
module.exports = UdpPoller; |