From b7885eb5396831f686a1f157c29da365f94fed26 Mon Sep 17 00:00:00 2001 From: Yann Flory Date: Wed, 9 Mar 2016 18:35:39 +0100 Subject: [PATCH] Fix channel history --- WebRTCSrv.js | 27 ++++++- www/common/netflux.js | 139 +++++++++++++++++++++++------------ www/common/realtime-input.js | 42 +++++++---- 3 files changed, 145 insertions(+), 63 deletions(-) diff --git a/WebRTCSrv.js b/WebRTCSrv.js index e3e0a3c42..8b0c78321 100644 --- a/WebRTCSrv.js +++ b/WebRTCSrv.js @@ -36,6 +36,7 @@ var run = module.exports.run = function(storage, server) { socket.master = master master.joiningClients.push(socket) let id = master.joiningClients.length - 1 + console.log(id); master.send(JSON.stringify({id, data: msg.data})) return } @@ -54,10 +55,30 @@ var run = module.exports.run = function(storage, server) { }) socket.on('close', (event) => { - if (socket.hasOwnProperty('joiningClients')) { - for (let client of socket.joiningClients) { - client.close(POLICY_VIOLATION, 'The peer is no longer available') + console.log('someone has closed'); + // If not master + if (socket.hasOwnProperty('master')) { + let masterClients = socket.master.joiningClients + for (let client of masterClients) { + if(client.id === socket.id) { + console.log('close client '+client.key) + client.close(POLICY_VIOLATION, 'The peer is no longer available') + //masterClients.splice(masterClients.indexOf(client),1); + } + } + } + else if (socket.hasOwnProperty('joiningClients')) { + let firstClient + let masterClients = socket.joiningClients + for (let client of masterClients) { + firstClient = client + break; } + firstClient.close(POLICY_VIOLATION, 'The master is no longer available') + //masterClients.splice(masterClients.indexOf(firstClient),1); + firstClient.joiningClients = masterClients + console.log('change master from '+socket.key+' to '+firstClient.key) + socket = firstClient } }) }) diff --git a/www/common/netflux.js b/www/common/netflux.js index d63470bba..1c434e46c 100644 --- a/www/common/netflux.js +++ b/www/common/netflux.js @@ -226,8 +226,35 @@ return /******/ (function(modules) { // webpackBootstrap }, { key: 'send', value: function send(data) { - var protocol = _ServiceProvider2.default.get(this.settings.protocol); - this.topologyService.broadcast(this, protocol.message(cs.USER_DATA, { id: this.myID, data: data })); + var channel = this; + return new Promise(function (resolve, reject) { + if (channel.channels.size === 0) { + console.log('sizenull');resolve(); + } + var protocol = _ServiceProvider2.default.get(channel.settings.protocol); + channel.topologyService.broadcast(channel, protocol.message(cs.USER_DATA, { id: channel.myID, data: data })).then(resolve, reject); + }); + } + }, { + key: 'getHistory', + value: function getHistory(historyKeeperID) { + var channel = this; + return new Promise(function (resolve, reject) { + console.log(channel); + console.log('Je veux history ' + channel.myID); + var protocol = _ServiceProvider2.default.get(channel.settings.protocol); + channel.topologyService.sendTo(historyKeeperID, channel, protocol.message(cs.GET_HISTORY, { id: channel.myID, data: '' })).then(resolve, reject); + }); + } + }, { + key: 'sendTo', + value: function sendTo(id, msg) { + var channel = this; + return new Promise(function (resolve, reject) { + var protocol = _ServiceProvider2.default.get(channel.settings.protocol); + console.log('WCsendTo ' + id); + channel.topologyService.sendTo(id, channel, protocol.message(cs.USER_DATA, { id: channel.myID, data: msg })).then(resolve, reject); + }); } }, { key: 'openForJoining', @@ -305,6 +332,7 @@ return /******/ (function(modules) { // webpackBootstrap }); // API user's message var USER_DATA = exports.USER_DATA = 0; + var GET_HISTORY = exports.GET_HISTORY = 6; // Internal messages var JOIN_START = exports.JOIN_START = 2; @@ -379,7 +407,7 @@ return /******/ (function(modules) { // webpackBootstrap value: function get(code) { var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; - var service = undefined; + var service = void 0; switch (code) { case cs.WEBRTC_SERVICE: service = new _WebRTCService2.default(options); @@ -441,6 +469,8 @@ return /******/ (function(modules) { // webpackBootstrap var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; _classCallCheck(this, FullyConnectedService); + + console.log('SERVICE FULLY CONNECTED CONSTRUCTED'); } _createClass(FullyConnectedService, [{ @@ -500,62 +530,70 @@ return /******/ (function(modules) { // webpackBootstrap }, { key: 'broadcast', value: function broadcast(webChannel, data) { - console.log(data); - var _iteratorNormalCompletion = true; - var _didIteratorError = false; - var _iteratorError = undefined; - - try { - for (var _iterator = webChannel.channels[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { - var c = _step.value; + return new Promise(function (resolve, reject) { + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; - console.log(c); - c.send(data); - } - } catch (err) { - _didIteratorError = true; - _iteratorError = err; - } finally { try { - if (!_iteratorNormalCompletion && _iterator.return) { - _iterator.return(); + for (var _iterator = webChannel.channels[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var c = _step.value; + + c.send(data); } + } catch (err) { + _didIteratorError = true; + _iteratorError = err; } finally { - if (_didIteratorError) { - throw _iteratorError; + try { + if (!_iteratorNormalCompletion && _iterator.return) { + _iterator.return(); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } } } - } + + resolve(); + }); } }, { key: 'sendTo', value: function sendTo(id, webChannel, data) { - var _iteratorNormalCompletion2 = true; - var _didIteratorError2 = false; - var _iteratorError2 = undefined; + console.log('sending to ' + id); - try { - for (var _iterator2 = webChannel.channels[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { - var c = _step2.value; + return new Promise(function (resolve, reject) { + var _iteratorNormalCompletion2 = true; + var _didIteratorError2 = false; + var _iteratorError2 = undefined; - if (c.peerID == id) { - c.send(data); - } - } - } catch (err) { - _didIteratorError2 = true; - _iteratorError2 = err; - } finally { try { - if (!_iteratorNormalCompletion2 && _iterator2.return) { - _iterator2.return(); + for (var _iterator2 = webChannel.channels[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { + var c = _step2.value; + + if (c.peerID == id) { + c.send(data); + } } + } catch (err) { + _didIteratorError2 = true; + _iteratorError2 = err; } finally { - if (_didIteratorError2) { - throw _iteratorError2; + try { + if (!_iteratorNormalCompletion2 && _iterator2.return) { + _iterator2.return(); + } + } finally { + if (_didIteratorError2) { + throw _iteratorError2; + } } } - } + + resolve(); + }); } }, { key: 'leave', @@ -805,7 +843,7 @@ return /******/ (function(modules) { // webpackBootstrap var settings = Object.assign({}, this.settings, options); return new Promise(function (resolve, reject) { - var connection = undefined; + var connection = void 0; var socket = new window.WebSocket(settings.signaling); console.log('Socket created'); socket.onopen = function () { @@ -928,8 +966,8 @@ return /******/ (function(modules) { // webpackBootstrap }, function () {}); })(); } else if (msg.sdp.type === 'answer') { - var sd = Object.assign(new this.RTCSessionDescription(), msg.sdp); - webChannel.connections.get(msg.senderPeerID).setRemoteDescription(sd, function () {}, function () {}); + var _sd = Object.assign(new this.RTCSessionDescription(), msg.sdp); + webChannel.connections.get(msg.senderPeerID).setRemoteDescription(_sd, function () {}, function () {}); } } else if (Reflect.has(msg, 'candidate')) { webChannel.connections.get(msg.senderPeerID).addIceCandidate(new this.RTCIceCandidate(msg.candidate)); @@ -1042,7 +1080,7 @@ return /******/ (function(modules) { // webpackBootstrap var settings = Object.assign({}, this.settings, options); return new Promise(function (resolve, reject) { - var connection = undefined; + var connection = void 0; var socket = new window.WebSocket(settings.signaling); socket.seq = 1; socket.facade = options.facade || null; @@ -1108,6 +1146,10 @@ return /******/ (function(modules) { // webpackBootstrap case cs.USER_DATA: webChannel.onmessage(msg.id, msg.data); break; + case cs.GET_HISTORY: + console.log("SOMEONE WANTS HISTORY"); + webChannel.onPeerMessage(msg.id, msg.code); + break; case cs.SERVICE_DATA: var service = _ServiceProvider2.default.get(msg.service); service.onmessage(channel, msg.data); @@ -1139,6 +1181,9 @@ return /******/ (function(modules) { // webpackBootstrap msg.id = data.id; msg.data = data.data; break; + case cs.GET_HISTORY: + msg.id = data.id; + break; case cs.SERVICE_DATA: msg.service = data.service; msg.data = Object.assign({}, data.data); @@ -1268,7 +1313,7 @@ return /******/ (function(modules) { // webpackBootstrap }, { key: 'message', value: function message(code, data) { - var type = undefined; + var type = void 0; switch (code) { case cs.USER_DATA: type = 'MSG'; diff --git a/www/common/realtime-input.js b/www/common/realtime-input.js index ebddde624..6ba825f53 100644 --- a/www/common/realtime-input.js +++ b/www/common/realtime-input.js @@ -102,16 +102,8 @@ define([ var bump = function () {}; - var onPeerMessage = function (peer, msg) { - if(peer === '_HISTORY_KEEPER_') { - var msgHistory = JSON.parse(msg[4]); - onMessage(msgHistory[1], msgHistory[4]); - } - else { - warn('Illegal direct message'); - } - }; - + var messagesHistory = []; + var options = { signaling: websocketUrl, // signaling: 'ws://localhost:8000', @@ -125,7 +117,7 @@ define([ var realtime; // Add the Facade's peer messages handler - Netflux._onPeerMessage = onPeerMessage; + // Netflux._onPeerMessage = onPeerMessage; function getParameterByName(name, url) { if (!url) url = window.location.href; @@ -171,6 +163,9 @@ define([ wc.onmessage = onMessage; // On receiving message wc.onJoining = onJoining; // On user joining the session wc.onLeaving = onLeaving; // On user leaving the session + wc.onPeerMessage = function(peerId, type) { + onPeerMessage(peerId, wc); // On user leaving the session + } // Open a Chainpad session realtime = createRealtime(); @@ -191,12 +186,21 @@ define([ if (parsed.content[0] !== 0) { console.log('ENVOI '+message); message = Crypto.encrypt(message, cryptKey); - wc.send(message); - onMessage('', message); + wc.send(message).then(function() { + onMessage('', message); + }); } // END-TODO }); + var hc; + for (let c of wc.channels) { hc = c; break; } + if(hc) { + console.log('history keeper :'); + console.log(hc); + console.log('onPeer '+hc.peerID) + wc.getHistory(hc.peerID); + } // Get the channel history // var hc; // wc.peers.forEach(function (p) { if (!hc || p.linkQuality > hc.linkQuality) { hc = p; } }); @@ -227,16 +231,28 @@ define([ var whoami = new RegExp(userName.replace(/[\/\+]/g, function (c) { return '\\' +c; })); + + var onPeerMessage = function(peerID, wc) { + console.log(messagesHistory); + console.log('RTsendTo '+peerID); + messagesHistory.forEach(function(msg) { + console.log(msg); + //var message = Crypto.encrypt('1:y'+msg, cryptKey); + wc.sendTo(peerID, msg); + }); + }; var onMessage = function(peer, msg) { // TODO : put in ChainpadAdapter // remove the password + messagesHistory.push(msg); var passLen = msg.substring(0,msg.indexOf(':')); var message = msg.substring(passLen.length+1 + Number(passLen)); message = Crypto.decrypt(message, cryptKey); + console.log('RECOIS '+message); // END-TODO ChainpadAdapter