diff --git a/nodes/io/30-socketin.html b/nodes/deprecated/30-socketin.html similarity index 100% rename from nodes/io/30-socketin.html rename to nodes/deprecated/30-socketin.html diff --git a/nodes/deprecated/30-socketin.js b/nodes/deprecated/30-socketin.js new file mode 100644 index 000000000..4e10bf878 --- /dev/null +++ b/nodes/deprecated/30-socketin.js @@ -0,0 +1,152 @@ +/** + * 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. + **/ + +var RED = require(process.env.NODE_RED_HOME+"/red/red"); + +function SocketIn(n) { + RED.nodes.createNode(this,n); + this.warn("node type deprecated - will be moved to node-red-nodes project at next point update"); + this.port = n.port; + this.topic = n.topic; + this.trans = (n.transport||n.trans||"").toLowerCase(); + var node = this; + if (this.trans == "http") { + var http = require('http'); + var server = http.createServer(function (req, res) { + //node.log("http "+req.url); + var msg = {topic:node.topic,payload:req.url.slice(1)}; + node.send(msg); + res.writeHead(304, {'Content-Type': 'text/plain'}); + res.end('\n'); + }).listen(node.port); + server.on('error', function (e) { + if (e.code == 'EADDRINUSE') { + setTimeout(node.error('TCP port is already in use - please reconfigure socket.'),250); + } + else { console.log(e); } + server = null; + }); + node.log('http listener at http://127.0.0.1:'+node.port+'/'); + + this._close = function() { + if (server) server.close(); + node.log('http listener stopped'); + } + } + + if (this.trans == "tcp") { + var net = require('net'); + var server = net.createServer(function (socket) { + var buffer = null; + socket.on('data', function (chunk) { + if (buffer == null) { + buffer = chunk; + } else { + buffer = Buffer.concat([buffer,chunk]); + } + }); + socket.on('end', function() { + var msg = {topic:node.topic, payload:buffer, fromip:socket.remoteAddress+':'+socket.remotePort}; + node.send(msg); + }); + }); + server.on('error', function (e) { + if (e.code == 'EADDRINUSE') { + setTimeout(node.error('TCP port is already in use - please reconfigure socket.'),250); + } + else { console.log(e); } + server = null; + }); + server.listen(node.port); + node.log('tcp listener on port :'+node.port); + + this._close = function() { + if (server) server.close(); + node.log('tcp listener stopped'); + } + } + + if (this.trans == "tcpc") { + var net = require('net'); + var client; + var to; + function setupTcpClient() { + node.log('tcpc connecting to port :'+node.port); + client = net.connect({port: node.port}, function() { + node.log("tcpc connected"); + }); + + client.on('data', function (data) { + var msg = {topic:node.topic, payload:data}; + node.send(msg); + }); + + client.on('end', function() { + node.log("tcpc socket ended"); + }); + + client.on('close', function() { + node.log('tcpc socket closed'); + to = setTimeout(setupTcpClient, 10000); //Try to reconnect + }); + + client.on('error', function() { + node.log('tcpc socket error'); + client = null; + to = setTimeout(setupTcpClient, 10000); //Try to reconnect + }); + } + setupTcpClient(); + + this._close = function() { + if (client) client.end(); + //client.destroy(); + clearTimeout(to); + node.log('tcpc stopped client'); + } + setupTcpClient(); + } + + if (this.trans == "udp") { + var dgram = require('dgram'); + var server = dgram.createSocket('udp4'); + server.on('listening', function () { + var address = server.address(); + node.log('udp listener at ' + address.address + ":" + address.port); + }); + server.on('message', function (message, remote) { + var msg = {topic:node.topic,payload:message,fromip:remote.address+':'+remote.port}; + node.send(msg); + }); + server.on('error', function (e) { + console.log(e); + server = null; + }); + server.bind(node.port); + + this._close = function() { + if (server) server.close(); + node.log('udp listener stopped'); + } + } + +} + +RED.nodes.registerType("socket in",SocketIn); + +SocketIn.prototype.close = function() { + this._close(); +} diff --git a/nodes/io/30-socketout.html b/nodes/deprecated/30-socketout.html similarity index 100% rename from nodes/io/30-socketout.html rename to nodes/deprecated/30-socketout.html diff --git a/nodes/deprecated/30-socketout.js b/nodes/deprecated/30-socketout.js new file mode 100644 index 000000000..625b0ecce --- /dev/null +++ b/nodes/deprecated/30-socketout.js @@ -0,0 +1,65 @@ +/** + * 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. + **/ + +var RED = require(process.env.NODE_RED_HOME+"/red/red"); + +function SocketOut(n) { + RED.nodes.createNode(this,n); + this.warn("node type deprecated - will be moved to node-red-nodes project at next point update"); + this.host = n.host; + this.port = n.port * 1; + this.name = n.name; + this.trans = n.transport||n.trans||""; + var node = this; + this.on("input", function(msg) { + if (msg != null) { + if (this.trans == "http") { + var http = require("http"); + http.get(msg.payload, function(res) { + node.log("http : response : " + res.statusCode); + }).on('error', function(e) { + node.error("http : error : " + e.message); + }); + } + if (this.trans == "tcp") { + var net = require('net'); + var client = new net.Socket(); + client.on('error', function (err) { + node.error('tcp : '+err); + }); + client.connect(this.port, this.host, function() { + try { client.end(msg.payload); } + catch (e) { node.error(e); } + }); + } + if (this.trans == "udp") { + var dgram = require('dgram'); + var sock = dgram.createSocket('udp4'); // only use ipv4 for now + sock.bind(this.port); // have to bind before you can enable broadcast... + sock.setBroadcast(true); // turn on broadcast + var buf = new Buffer(msg.payload); + sock.send(buf, 0, buf.length, this.port, this.host, function(err, bytes) { + if (err) node.error("udp : "+err); + //util.log('[socket out] udp :' +bytes); + sock.close(); + }); + } + } + }); + var node = this; +} + +RED.nodes.registerType("socket out",SocketOut); diff --git a/nodes/io/32-multicast.html b/nodes/deprecated/32-multicast.html similarity index 100% rename from nodes/io/32-multicast.html rename to nodes/deprecated/32-multicast.html diff --git a/nodes/deprecated/32-multicast.js b/nodes/deprecated/32-multicast.js new file mode 100644 index 000000000..38e58a37a --- /dev/null +++ b/nodes/deprecated/32-multicast.js @@ -0,0 +1,119 @@ +/** + * 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. + **/ + +var RED = require(process.env.NODE_RED_HOME+"/red/red"); +var dgram = require('dgram'); + +// The Input Node +function MCastIn(n) { + RED.nodes.createNode(this,n); + this.warn("node type deprecated - will be moved to node-red-nodes project at next point update"); + this.group = n.group; + this.port = n.port; + this.host = n.host || null; + this.base64 = n.base64; + this.iface = n.iface || null; + this.multicast = n.multicast; + var node = this; + + var server = dgram.createSocket('udp4'); + + server.on("error", function (err) { + console.log("udp listener error:\n" + err.stack); + server.close(); + }); + + server.on('message', function (message, remote) { + var msg; + if (node.base64) { msg = { payload:message.toString('base64'), fromip:remote.address+':'+remote.port }; } + else { msg = { payload:message, fromip:remote.address+':'+remote.port }; } + node.send(msg); + }); + + server.on('listening', function () { + var address = server.address(); + node.log('udp listener at ' + address.address + ":" + address.port); + if (node.multicast) { + server.setBroadcast(true) + server.setMulticastTTL(128); + server.addMembership(node.group,node.iface); + node.log("udp multicast group "+node.group); + } + }); + + //server.bind(node.port,node.host); + server.bind(node.port,node.host); + + this._close = function() { + server.close(); + node.log('udp listener stopped'); + } + +} + +MCastIn.prototype.close = function() { + this._close(); +} +RED.nodes.registerType("multicast in",MCastIn); + +// The Output Node +function MCastOut(n) { + RED.nodes.createNode(this,n); + this.warn("node type deprecated"); + this.group = n.group; + this.port = n.port; + this.host = n.host || null; + this.base64 = n.base64; + this.iface = n.iface || null; + this.multicast = n.multicast; + var node = this; + + var sock = dgram.createSocket('udp4'); // only use ipv4 for now + sock.bind(node.port); // have to bind before you can enable broadcast... + sock.setBroadcast(true); // turn on broadcast + sock.setMulticastTTL(128); + sock.addMembership(node.group,node.iface); // Add to the multicast group + node.log('udp multicaster ready on '+node.group+":"+node.port); + + node.on("input", function(msg) { + if (msg.payload != null) { + console.log("MCast:",msg.payload); + var message; + if (node.base64) { + message = new Buffer(msg.payload,'base64'); + } + else { + message = new Buffer(msg.payload); + } + sock.send(message, 0, message.length, node.port, node.group, function(err, bytes) { + if (err) node.error("udp : "+err); + //util.log('[socket out] udp :' +bytes); + }); + } + }); + + this._close = function() { + sock.close(); + node.log('udp multicaster stopped'); + } + +} + +RED.nodes.registerType("multicast out",MCastOut); + +MCastOut.prototype.close = function() { + this._close(); +} diff --git a/nodes/deprecated/35-rpi-gpio-in.html b/nodes/deprecated/35-rpi-gpio-in.html index 5c6d119ac..d98f50117 100644 --- a/nodes/deprecated/35-rpi-gpio-in.html +++ b/nodes/deprecated/35-rpi-gpio-in.html @@ -47,7 +47,7 @@