From 8e8ee38e40a7b8abaf32db1dd051248c46af0085 Mon Sep 17 00:00:00 2001 From: Nicolas Pope <nwpope@utu.fi> Date: Tue, 19 May 2020 19:29:14 +0300 Subject: [PATCH] Some corrections to web service, essentially working --- web-service/server/src/index.js | 51 ++++++++++++++++++++------------- web-service/server/src/peer.js | 5 +++- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/web-service/server/src/index.js b/web-service/server/src/index.js index c4664d272..339be2b68 100644 --- a/web-service/server/src/index.js +++ b/web-service/server/src/index.js @@ -42,12 +42,8 @@ let peer_data = []; * @param {number} rate Bitrate index requested * @param {string} dest URI destination */ -function RGBDClient(peer, N, rate, dest) { +function RGBDClient(peer) { this.peer = peer; - this.txmax = N*16; // 16 is for 16 blocks per frame... this will change in the near future - this.rate = rate; - this.dest = dest; - this.txcount = 0; } /** @@ -55,7 +51,7 @@ function RGBDClient(peer, N, rate, dest) { */ RGBDClient.prototype.push = function(uri, latency, spacket, packet) { this.peer.send(uri, latency, spacket, packet); - this.txcount++; + //this.txcount++; } /** @@ -82,10 +78,10 @@ function RGBDStream(uri, peer) { peer.bind(uri, (latency, spacket, packet) => { // Forward frames to all clients this.pushFrames(latency, spacket, packet); - this.rxcount++; - if (this.rxcount >= this.rxmax && this.clients.length > 0) { - this.subscribe(); - } + //this.rxcount++; + //if (this.rxcount >= this.rxmax && this.clients.length > 0) { + // this.subscribe(); + //} }); /*peer.bind(uri, (frame, ttime, chunk, rgb, depth) => { @@ -98,17 +94,18 @@ function RGBDStream(uri, peer) { });*/ } -RGBDStream.prototype.addClient = function(peer, N, rate, dest) { +RGBDStream.prototype.addClient = function(peer) { // TODO(Nick) Verify that it isn't already in list... for (let i=0; i<this.clients.length; i++) { - if (this.clients[i].peer.string_id == peer.string_id) return; + //if (this.clients[i].peer.string_id == peer.string_id) return; + if (this.clients[i].peer === peer) return; } - this.clients.push(new RGBDClient(peer, N, rate, dest)); - console.log("MINMAX", this.rxcount, this.rxmax); - if (this.rxcount >= this.rxmax) { - this.subscribe(); - } + this.clients.push(new RGBDClient(peer)); + //console.log("MINMAX", this.rxcount, this.rxmax); + //if (this.rxcount >= this.rxmax) { + // this.subscribe(); + //} } RGBDStream.prototype.subscribe = function() { @@ -133,13 +130,13 @@ RGBDStream.prototype.pushFrames = function(latency, spacket, packet) { this.clients[i].push(this.uri, latency, spacket, packet); } - let i=0; + /*let i=0; while (i < this.clients.length) { if (this.clients[i].txcount >= this.clients[i].txmax) { console.log("remove client"); this.clients.splice(i, 1); } else i++; - } + }*/ } // ---- PROTOCOL --------------------------------------------------------------- @@ -329,7 +326,21 @@ app.ws('/', (ws, req) => { p.bind("find_stream", (uri) => { const parsedURI = stringSplitter(uri) if (uri_to_peer.hasOwnProperty(parsedURI)) { - console.log("Stream found: ", uri); + console.log("Stream found: ", uri, parsedURI); + + if (!p.isBound(uri)) { + console.log("Adding local stream binding"); + p.bind(uri, (ttimeoff, spkt, pkt) => { + console.log("STREAM: ", spkt); + let speer = uri_to_peer[parsedURI]; + if (speer) { + uri_data[parsedURI].addClient(p); + speer.send(parsedURI, ttimeoff, spkt, pkt); + } else if (speer) console.log("Stream response"); + else console.error("No stream peer"); + }); + } + return [Peer.uuid]; } else { console.log("Stream not found: ", uri) diff --git a/web-service/server/src/peer.js b/web-service/server/src/peer.js index dd943a093..c31ee2426 100644 --- a/web-service/server/src/peer.js +++ b/web-service/server/src/peer.js @@ -40,7 +40,6 @@ function Peer(ws) { this.master = false; let message = (raw) => { - // console.log(raw) //Gets right data for client if(this.sock.on === undefined){ raw = raw.data; @@ -185,6 +184,10 @@ Peer.prototype.bind = function(name, f) { } } +Peer.prototype.isBound = function(name) { + return this.bindings.hasOwnProperty(name) || this.proxies.hasOwnProperty(name); +} + /** * Allow an RPC call to pass through to another machine with minimal local * processing. -- GitLab