Skip to content
Snippets Groups Projects
Commit 11efc522 authored by Matthias's avatar Matthias
Browse files

clusters ne

parent c6ae49f3
Branches
No related merge requests found
This diff is collapsed.
......@@ -4,8 +4,8 @@ const app = require('express')();
const http = require('http').Server(app);
const socket = require('socket.io')(http);
const p2p = require('socket.io-p2p-server').Server;
socket.use(p2p); // use webrtc
// const p2p = require('socket.io-p2p-server').Server;
// socket.use(p2p); // use webrtc
const fs = require('fs');
const fifoPath = '/tmp/nodeFIFO';
......@@ -21,10 +21,11 @@ function getSummary(s) {
return s.substring(0, 50) + "" + s.substring(s.length - 50, s.length);
}
// unecessary webserver
app.get('/', (req, res) => {
res.send('<h1>okcool but you gotta go for port 3000 though</h1>');
res.send('<h1>okcool but you should visit the visualization site</h1>');
});
http.listen(3000, () => {
......@@ -34,20 +35,14 @@ http.listen(3000, () => {
// named pipe anzapfen
let fifo;
try {
fifo = fs.createReadStream(fifoPath);
} catch (e) {
console.error("fifo not readable")
throw e;
}
const fifo = fs.createReadStream(fifoPath);
fifo.setEncoding("utf8");
console.log("started listener...");
// stream providing complete data packets, not the bits and pieces returned from
// the fifo
// create stream providing complete data packets
// not the bits and pieces returned from the fifo
const packetStream = new Stream.Readable({
read() {}
});
......@@ -58,22 +53,20 @@ let buffer = ""; // buffers incoming data
let frameNo = 0; // debug
const packetDelimiter = "EOT"; // string that separates packets
// buffer the FIFO stream, collect pieces of data, and turn into packet stream
fifo.on("data", msg => {
// console.log("---")
// console.log("msg", msg)
// console.log("---")
// console.log("summary msg `" + getSummary(msg) + "`")
buffer += msg;
// console.log("summary buffer `" + getSummary(buffer) + "`")
// there could potentially be multiple packets in the buffer
// so loop until we pushed all of them to the packet stream
for (let delIndex = buffer.indexOf(packetDelimiter);
delIndex !== -1;
delIndex = buffer.indexOf(packetDelimiter)) {
const packet = buffer
.substring(0, delIndex)
.replace(/\0/g, ''); // dis shit null terminated boi
.replace(/\0/g, ''); // dis shit too null terminated boi
buffer = buffer.substring(delIndex + packetDelimiter.length, buffer.length);
......@@ -83,18 +76,20 @@ fifo.on("data", msg => {
}
});
let peers = 0;
socket.on("connection", async socket => {
peers++;
function logAction(action, socket) {
const ip = socket.request.connection.remoteAddress;
const userAgent = socket.request.headers['user-agent'];
console.log(
"\nconnected", ip, "(" + peers + " connections)",
"\n"+action, ip, "(" + peers + " connections)",
"\n\t User Agent:", userAgent, "\n\t Socket ID:", socket.id
);
}
socket.on("connection", async socket => {
peers++;
logAction("connected", socket);
const myStream = new Stream.PassThrough();
myStream.setEncoding('utf8');
......@@ -118,21 +113,15 @@ socket.on("connection", async socket => {
console.error("rawData was", rawData);
}
console.error(e)
console.log(rawData);
throw e;
return;
}
if (!data) throw new Error("data was " + data);
// console.debug("sending data to", socket.id, "("+frameNo+")");
// console.log("data type:", Object.keys(data))
// const frame = new Uint8Array(12976);
// data.frame.forEach((el, frameNo) => frame[frameNo] = el);
// console.log("sent")
// send the packet to the visualization through socket.io
socket.emit("data", {
frame: data.frame,
clusters: data.clusters,
id: frameNo, // debug
timestamps: {
sent: +new Date(),
......@@ -143,10 +132,7 @@ socket.on("connection", async socket => {
socket.on('disconnect', () => {
peers--;
const ip = socket.request.connection.remoteAddress;
const userAgent = socket.request.headers['user-agent'];
console.log("\ndisconnected", ip, "("+peers+" connections)", "\n\t", ip,"\n\t",userAgent,"\n\t", socket.id)
// myStream.off('readable', listener);
logAction("disconnected", socket);
});
});
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment