mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
NLS exec node NLS function/temple/delay nodes NLS function/template/delay/trigger/comment nodes NLS io nodes (mqtt/httpin/websocket/watch/serial) NLS messages.json for tcpin NLS io nodes (tcpin & udp half) NLS io nodes (udp) NLS logic nodes (switch/change) NLS logic (range) and parsers (csv&html) nodes NLS parser nodes (json/xml) NLS test case update for logic/parsers NLS analysis and hardware nodes NLS storage nodes (file/redisout/mongodb) and test NLS storage node (tail) NLS social nodes (feedparse/email/irc) NLS socal node (twitter half change) NLS social node (twitter) and core node (unknown)
182 lines
7.1 KiB
JavaScript
182 lines
7.1 KiB
JavaScript
/**
|
|
* Copyright 2013 IBM Corp.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
**/
|
|
|
|
module.exports = function(RED) {
|
|
"use strict";
|
|
var dgram = require('dgram');
|
|
|
|
// The Input Node
|
|
function UDPin(n) {
|
|
RED.nodes.createNode(this,n);
|
|
this.group = n.group;
|
|
this.port = n.port;
|
|
this.datatype = n.datatype;
|
|
this.iface = n.iface || null;
|
|
this.multicast = n.multicast;
|
|
this.ipv = n.ipv || "udp4";
|
|
var node = this;
|
|
|
|
var server = dgram.createSocket(node.ipv); // default to ipv4
|
|
|
|
server.on("error", function (err) {
|
|
if ((err.code == "EACCES") && (node.port < 1024)) {
|
|
node.error(RED._("udp.errors.access-error"));
|
|
} else {
|
|
node.error(RED._("udp.errors.udp-error")+" : "+err.code);
|
|
}
|
|
server.close();
|
|
});
|
|
|
|
server.on('message', function (message, remote) {
|
|
var msg;
|
|
if (node.datatype =="base64") {
|
|
msg = { payload:message.toString('base64'), fromip:remote.address+':'+remote.port, ip:remote.address, port:remote.port };
|
|
} else if (node.datatype =="utf8") {
|
|
msg = { payload:message.toString('utf8'), fromip:remote.address+':'+remote.port, ip:remote.address, port:remote.port };
|
|
} else {
|
|
msg = { payload:message, fromip:remote.address+':'+remote.port, ip:remote.address, port:remote.port };
|
|
}
|
|
node.send(msg);
|
|
});
|
|
|
|
server.on('listening', function () {
|
|
var address = server.address();
|
|
node.log(RED._("udp.errors.listener-at") + ' ' + address.address + ":" + address.port);
|
|
if (node.multicast == "true") {
|
|
server.setBroadcast(true);
|
|
try {
|
|
server.setMulticastTTL(128);
|
|
server.addMembership(node.group,node.iface);
|
|
node.log(RED._("udp.errors.mc-group")+" "+node.group);
|
|
} catch (e) {
|
|
if (e.errno == "EINVAL") {
|
|
node.error(RED._("udp.errors.bad-mcaddress"));
|
|
} else if (e.errno == "ENODEV") {
|
|
node.error(RED._("udp.errors.interface"));
|
|
} else {
|
|
node.error(RED._("udp.errors.error")+" :"+e.errno);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
node.on("close", function() {
|
|
try {
|
|
server.close();
|
|
node.log(RED._("udp.errors.listener-stopped"));
|
|
} catch (err) {
|
|
node.error(err);
|
|
}
|
|
});
|
|
|
|
// Hack for when you have both in and out udp nodes sharing a port
|
|
// if udp in starts last it shares better - so give it a chance to be last
|
|
setTimeout( function() { server.bind(node.port,node.iface); }, 250);
|
|
}
|
|
RED.nodes.registerType("udp in",UDPin);
|
|
|
|
|
|
// The Output Node
|
|
function UDPout(n) {
|
|
RED.nodes.createNode(this,n);
|
|
//this.group = n.group;
|
|
this.port = n.port;
|
|
this.outport = n.outport||"";
|
|
this.base64 = n.base64;
|
|
this.addr = n.addr;
|
|
this.iface = n.iface || null;
|
|
this.multicast = n.multicast;
|
|
this.ipv = n.ipv || "udp4";
|
|
var node = this;
|
|
|
|
var sock = dgram.createSocket(node.ipv); // default to ipv4
|
|
|
|
sock.on("error", function(err) {
|
|
// Any async error will also get reported in the sock.send call.
|
|
// This handler is needed to ensure the error marked as handled to
|
|
// prevent it going to the global error handler and shutting node-red
|
|
// down.
|
|
});
|
|
if (node.multicast != "false") {
|
|
if (node.outport == "") { node.outport = node.port; }
|
|
sock.bind(node.outport, function() { // have to bind before you can enable broadcast...
|
|
sock.setBroadcast(true); // turn on broadcast
|
|
if (node.multicast == "multi") {
|
|
try {
|
|
sock.setMulticastTTL(128);
|
|
sock.addMembership(node.addr,node.iface); // Add to the multicast group
|
|
node.log(RED._("udp.errors.mc-ready")+' : '+node.outport+' -> '+node.addr+":"+node.port);
|
|
} catch (e) {
|
|
if (e.errno == "EINVAL") {
|
|
node.error(RED._("udp.errors.bad-mcaddress"));
|
|
} else if (e.errno == "ENODEV") {
|
|
node.error(RED._("udp.errors.interface"));
|
|
} else {
|
|
node.error(RED._("udp.errors.error")+" :"+e.errno);
|
|
}
|
|
}
|
|
} else {
|
|
node.log(RED._("udp.errors.bc-ready")+' : '+node.outport+' -> '+node.addr+":"+node.port);
|
|
}
|
|
});
|
|
} else if (node.outport != "") {
|
|
sock.bind(node.outport);
|
|
node.log(RED._("udp.errors.ready")+' : '+node.outport+' -> '+node.addr+":"+node.port);
|
|
} else {
|
|
node.log(RED._("udp.errors.ready")+' : '+node.addr+":"+node.port);
|
|
}
|
|
|
|
node.on("input", function(msg) {
|
|
if (msg.hasOwnProperty("payload")) {
|
|
var add = node.addr || msg.ip || "";
|
|
var por = node.port || msg.port || 0;
|
|
if (add == "") {
|
|
node.warn(RED._("udp.errors.ip-notset"));
|
|
} else if (por == 0) {
|
|
node.warn(RED._("udp.errors.port-notset"));
|
|
} else if (isNaN(por) || (por < 1) || (por > 65535)) {
|
|
node.warn(RED._("udp.errors.port-invalid"));
|
|
} else {
|
|
var message;
|
|
if (node.base64) {
|
|
message = new Buffer(msg.payload, 'base64');
|
|
} else if (msg.payload instanceof Buffer) {
|
|
message = msg.payload;
|
|
} else {
|
|
message = new Buffer(""+msg.payload);
|
|
}
|
|
sock.send(message, 0, message.length, por, add, function(err, bytes) {
|
|
if (err) {
|
|
node.error("udp : "+err,msg);
|
|
}
|
|
message = null;
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
node.on("close", function() {
|
|
try {
|
|
sock.close();
|
|
node.log(RED._("udp.errors.output-stopped"));
|
|
} catch (err) {
|
|
node.error(err);
|
|
}
|
|
});
|
|
}
|
|
RED.nodes.registerType("udp out",UDPout);
|
|
}
|