Skip to content

Instantly share code, notes, and snippets.

@guymcswain
Created February 12, 2020 20:41
Show Gist options
  • Save guymcswain/e1c710b7719ccde518c0fc2bab7bc935 to your computer and use it in GitHub Desktop.
Save guymcswain/e1c710b7719ccde518c0fc2bab7bc935 to your computer and use it in GitHub Desktop.
pigpiod proxy
// Proxy for pigpiod connection testing.
'use strict';
console.log('\x1b[36m%s\x1b[0m', 'I am cyan'); //cyan
function debug (out) {
console.log('\x1b[36m%s\x1b[0m', out);
}
const net = require('net');
var i = 0; // the ith client connection
var cx = []; // connections
const proxy = net.createServer(req => {
let connection = {
id: i++,
req: req,
res: net.createConnection(8888, '192.168.100.6')
}
cx.push(connection);
connect(connection);
});
function connect (c) {
c.res.on('connect', () => {
c.req.on('data', data => {
if (!c.res.write(data)) debug('Ground control to Major Tom?');
debug(`req-${c.id} data=${data.toJSON().data}`);
});
c.res.on('data', data => {
if (!c.req.write(data)) debug('Houston, we have a problem!');
else debug(`res-${c.id} data=${data.toJSON().data}`);
});
c.req.on('end', data => {
c.res.end(data);
debug(`req-${c.id} ended`);
});
c.res.on('end', data => {
c.req.end(data);
debug(`res-${c.id} ended`);
});
c.req.on('close', hadError => {
debug(`req-${c.id} closed ${hadError? 'with error': ''}`);
// the following appears to be unnecessary
c.req.removeAllListeners();
c.req.destroy();
});
c.res.on('close', hadError => {
debug(`res-${c.id} closed ${hadError? 'with error': ''}`);
// the following appears to be unnecessary
c.res.removeAllListeners();
c.res.destroy();
});
c.req.on('error', e => {
debug(`req-${c.id} ERROR! ${e}`);
});
c.res.on('error', e => {
debug(`res-${c.id} ERROR! ${e}`);
});
debug(`connection-${c.id} established`);
});
}
proxy.listen(8887, () => {
debug(`proxy bound to ${JSON.stringify(proxy.address())}`);
});
// fork the application running pigpio-client
let forkOptions = {
cwd: "/mnt/d/Users/guy/Projects/pigpio-client/test",
env: {DEBUG: 'pigpio', PIGPIO: 1},
silent: true
};
const cp = require('child_process');
var app = cp.fork('./connect.js', forkOptions);
app.stdout.on('data', data => {
process.stdout.write(data.toString());
// After 'disconnected' event, kill then start a new fork.
//if (data.includes('Event=disconnected,')) {
// setTimeout( () => {
// app.kill();
// app = cp.fork('./connect.js', forkOptions);
// app.stdout.on('data', data => {
// process.stdout.write(data.toString());
// });
// }, 15000);
//}
});
// TEST PIGPIOD DISCONNECT
setTimeout( () => {
/* generates 'end' then 'close' events */
//cx[0].res.end();
//cx[1].res.end();
/* generates 'close' event only*/
//cx[0].req.destroy();
//cx[1].req.destroy();
/* generate a pigpio keep-alive timeout */
cx[0].res.pause();
cx[1].res.pause();
}, 15000);
// kill app after SIGINT received
process.on('SIGINT', () => {
app.kill();
proxy.unref();
process.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment