1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

revert ping node experimental changes

oops…
This commit is contained in:
Dave Conway-Jones 2016-02-13 11:15:02 +00:00
parent b0c8b1e83f
commit 73269848ba
3 changed files with 33 additions and 22 deletions

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2013,2016 IBM Corp. * Copyright 2013 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,35 +16,46 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
var dns = require("dns") var spawn = require('child_process').spawn;
var ping = require ("net-ping"); var plat = require('os').platform();
function PingNode(n) { function PingNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.host = n.host; this.host = n.host;
this.timer = n.timer * 1000; this.timer = n.timer * 1000;
this.session = ping.createSession();
var node = this; var node = this;
node.tout = setInterval(function() { node.tout = setInterval(function() {
dns.lookup(node.host, function(err, address, family) { var ex;
if (typeof address === 'undefined') { address = node.host; } if (plat == "linux") { ex = spawn('ping', ['-n', '-w', '5', '-c', '1', node.host]); }
node.session.pingHost(address, function(error, target, sent, rcvd) { else if (plat.match(/^win/)) { ex = spawn('ping', ['-n', '1', '-w', '5000', node.host]); }
var msg = { payload:false, topic:target }; else if (plat == "darwin") { ex = spawn('ping', ['-n', '-t', '5', '-c', '1', node.host]); }
if (error) { else { node.error("Sorry - your platform - "+plat+" - is not recognised."); }
msg.error = error.toString(); var res = false;
} else { var line = "";
msg.payload = rcvd - sent; //var regex = /from.*time.(.*)ms/;
} var regex = /=.*[<|=]([0-9]*).*TTL|ttl..*=([0-9\.]*)/;
node.send(msg); ex.stdout.on('data', function (data) {
node.session.close(); line += data.toString();
}); });
//ex.stderr.on('data', function (data) {
//console.log('[ping] stderr: ' + data);
//});
ex.on('close', function (code) {
var m = regex.exec(line)||"";
if (m !== '') {
if (m[1]) { res = Number(m[1]); }
if (m[2]) { res = Number(m[2]); }
}
var msg = { payload:false, topic:node.host };
if (code === 0) { msg = { payload:res, topic:node.host }; }
try { node.send(msg); }
catch(e) {}
}); });
}, node.timer); }, node.timer);
this.on("close", function() { this.on("close", function() {
if (node.tout) { clearInterval(this.tout); } if (this.tout) { clearInterval(this.tout); }
if (node.session) { node.session.close(); }
}); });
} }
RED.nodes.registerType("ping",PingNode); RED.nodes.registerType("ping",PingNode);

View File

@ -22,11 +22,11 @@ The fix is to allow it as follows
Usage Usage
----- -----
Pings a machine and returns the trip time in mS as **msg.payload**. Pings a machine and returns the trip time in mS as `msg.payload`.
Returns boolean `false` if no response received, or if the host is unresolveable. Returns boolean `false` if no response received, or if the host is unresolveable.
**msg.wrror** will contain any error message if necessary. `msg.error` will contain any error message if necessary.
**msg.topic** contains the ip address of the target host. `msg.topic` contains the ip address of the target host.
Default ping is every 20 seconds but can be configured. Default ping is every 20 seconds but can be configured.

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-ping", "name" : "node-red-node-ping",
"version" : "0.0.9", "version" : "0.0.10",
"description" : "A Node-RED node to ping a remote server, for use as a keep-alive check.", "description" : "A Node-RED node to ping a remote server, for use as a keep-alive check.",
"dependencies" : { "dependencies" : {
"net-ping":"1.1.*" "net-ping":"1.1.*"