Skip to content

Commit

Permalink
Add ports in use warning to udp node
Browse files Browse the repository at this point in the history
to close node-red#786
Thanks @Hugobox
  • Loading branch information
Dave Conway-Jones authored and Dave Conway-Jones committed Jan 17, 2016
1 parent 882b7d0 commit 3902a34
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
18 changes: 17 additions & 1 deletion nodes/core/io/32-udp.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright 2013 IBM Corp.
Copyright 2013,2016 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -52,6 +52,7 @@
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
</div>
<div class="form-tips"><span data-i18n="udp.tip.in"></span></div>
<div class="form-tips" id="udpporttip"><span data-i18n="[html]udp.tip.port"></span></div>
</script>

<script type="text/x-red" data-help-name="udp in">
Expand Down Expand Up @@ -98,6 +99,21 @@
}
});
$("#node-input-multicast").change();

var porttip = this._("udp.tip.port");
var alreadyused = this._("udp.errors.alreadyused");
var portsInUse = {};
$.getJSON('udp-ports/'+this.id,function(data) {
portsInUse = data || {};
$('#udpporttip').html(porttip + Object.keys(data||{}));
});
$("#node-input-port").change(function() {
var portnew = $("#node-input-port").val();
if (portsInUse.hasOwnProperty($("#node-input-port").val())) {
console.log($("#node-input-port").val());
RED.notify(alreadyused+" "+$("#node-input-port").val(),"warn");
}
});
}
});
</script>
Expand Down
17 changes: 15 additions & 2 deletions nodes/core/io/32-udp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2013 IBM Corp.
* Copyright 2013,2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
module.exports = function(RED) {
"use strict";
var dgram = require('dgram');
var udpInputPortsInUse = {};

// The Input Node
function UDPin(n) {
Expand All @@ -28,6 +29,12 @@ module.exports = function(RED) {
this.multicast = n.multicast;
this.ipv = n.ipv || "udp4";
var node = this;
if (!udpInputPortsInUse.hasOwnProperty(this.port)) {
udpInputPortsInUse[this.port] = n.id;
}
else {
node.warn(RED._("udp.errors.alreadyused",node.port));
}

var opts = {type:node.ipv, reuseAddr:true};
if (process.version.indexOf("v0.10") === 0) { opts = node.ipv; }
Expand Down Expand Up @@ -76,6 +83,10 @@ module.exports = function(RED) {
});

node.on("close", function() {
console.log("ID=",node.id);
if (udpInputPortsInUse[node.port] === node.id) {
delete udpInputPortsInUse[node.port];
}
try {
server.close();
node.log(RED._("udp.status.listener-stopped"));
Expand All @@ -86,9 +97,11 @@ module.exports = function(RED) {

server.bind(node.port,node.iface);
}
RED.httpAdmin.get('/udp-ports/:id', RED.auth.needsPermission('udp-in.read'), function(req,res) {
res.json(udpInputPortsInUse);
});
RED.nodes.registerType("udp in",UDPin);


// The Output Node
function UDPout(n) {
RED.nodes.createNode(this,n);
Expand Down
6 changes: 4 additions & 2 deletions nodes/core/locales/en-US/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@
},
"tip": {
"in": "Tip: Make sure your firewall will allow the data in.",
"out": "Tip: leave address and port blank if you want to set using <b>msg.ip</b> and <b>msg.port</b>."
"out": "Tip: leave address and port blank if you want to set using <b>msg.ip</b> and <b>msg.port</b>.",
"port": "Ports already in use: "
},
"status": {
"listener-at": "udp listener at __host__:__port__",
Expand All @@ -417,7 +418,8 @@
"interface": "Must be ip address of the required interface",
"ip-notset": "udp: ip address not set",
"port-notset": "udp: port not set",
"port-invalid": "udp: port number not valid"
"port-invalid": "udp: port number not valid",
"alreadyused": "udp: port already in use"
}
},
"switch": {
Expand Down

0 comments on commit 3902a34

Please sign in to comment.