Fix channel history

pull/1/head
Yann Flory 9 years ago
parent c536ecbc1c
commit b7885eb539

@ -36,6 +36,7 @@ var run = module.exports.run = function(storage, server) {
socket.master = master socket.master = master
master.joiningClients.push(socket) master.joiningClients.push(socket)
let id = master.joiningClients.length - 1 let id = master.joiningClients.length - 1
console.log(id);
master.send(JSON.stringify({id, data: msg.data})) master.send(JSON.stringify({id, data: msg.data}))
return return
} }
@ -54,10 +55,30 @@ var run = module.exports.run = function(storage, server) {
}) })
socket.on('close', (event) => { socket.on('close', (event) => {
if (socket.hasOwnProperty('joiningClients')) { console.log('someone has closed');
for (let client of socket.joiningClients) { // If not master
client.close(POLICY_VIOLATION, 'The peer is no longer available') 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
} }
}) })
}) })

@ -226,8 +226,35 @@ return /******/ (function(modules) { // webpackBootstrap
}, { }, {
key: 'send', key: 'send',
value: function send(data) { value: function send(data) {
var protocol = _ServiceProvider2.default.get(this.settings.protocol); var channel = this;
this.topologyService.broadcast(this, protocol.message(cs.USER_DATA, { id: this.myID, data: data })); 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', key: 'openForJoining',
@ -305,6 +332,7 @@ return /******/ (function(modules) { // webpackBootstrap
}); });
// API user's message // API user's message
var USER_DATA = exports.USER_DATA = 0; var USER_DATA = exports.USER_DATA = 0;
var GET_HISTORY = exports.GET_HISTORY = 6;
// Internal messages // Internal messages
var JOIN_START = exports.JOIN_START = 2; var JOIN_START = exports.JOIN_START = 2;
@ -379,7 +407,7 @@ return /******/ (function(modules) { // webpackBootstrap
value: function get(code) { value: function get(code) {
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var service = undefined; var service = void 0;
switch (code) { switch (code) {
case cs.WEBRTC_SERVICE: case cs.WEBRTC_SERVICE:
service = new _WebRTCService2.default(options); service = new _WebRTCService2.default(options);
@ -441,6 +469,8 @@ return /******/ (function(modules) { // webpackBootstrap
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
_classCallCheck(this, FullyConnectedService); _classCallCheck(this, FullyConnectedService);
console.log('SERVICE FULLY CONNECTED CONSTRUCTED');
} }
_createClass(FullyConnectedService, [{ _createClass(FullyConnectedService, [{
@ -500,62 +530,70 @@ return /******/ (function(modules) { // webpackBootstrap
}, { }, {
key: 'broadcast', key: 'broadcast',
value: function broadcast(webChannel, data) { value: function broadcast(webChannel, data) {
console.log(data); return new Promise(function (resolve, reject) {
var _iteratorNormalCompletion = true; var _iteratorNormalCompletion = true;
var _didIteratorError = false; var _didIteratorError = false;
var _iteratorError = undefined; var _iteratorError = undefined;
try {
for (var _iterator = webChannel.channels[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var c = _step.value;
console.log(c);
c.send(data);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try { try {
if (!_iteratorNormalCompletion && _iterator.return) { for (var _iterator = webChannel.channels[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
_iterator.return(); var c = _step.value;
c.send(data);
} }
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally { } finally {
if (_didIteratorError) { try {
throw _iteratorError; if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
} }
} }
}
resolve();
});
} }
}, { }, {
key: 'sendTo', key: 'sendTo',
value: function sendTo(id, webChannel, data) { value: function sendTo(id, webChannel, data) {
var _iteratorNormalCompletion2 = true; console.log('sending to ' + id);
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try { return new Promise(function (resolve, reject) {
for (var _iterator2 = webChannel.channels[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { var _iteratorNormalCompletion2 = true;
var c = _step2.value; var _didIteratorError2 = false;
var _iteratorError2 = undefined;
if (c.peerID == id) {
c.send(data);
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try { try {
if (!_iteratorNormalCompletion2 && _iterator2.return) { for (var _iterator2 = webChannel.channels[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
_iterator2.return(); var c = _step2.value;
if (c.peerID == id) {
c.send(data);
}
} }
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally { } finally {
if (_didIteratorError2) { try {
throw _iteratorError2; if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
} }
} }
}
resolve();
});
} }
}, { }, {
key: 'leave', key: 'leave',
@ -805,7 +843,7 @@ return /******/ (function(modules) { // webpackBootstrap
var settings = Object.assign({}, this.settings, options); var settings = Object.assign({}, this.settings, options);
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var connection = undefined; var connection = void 0;
var socket = new window.WebSocket(settings.signaling); var socket = new window.WebSocket(settings.signaling);
console.log('Socket created'); console.log('Socket created');
socket.onopen = function () { socket.onopen = function () {
@ -928,8 +966,8 @@ return /******/ (function(modules) { // webpackBootstrap
}, function () {}); }, function () {});
})(); })();
} else if (msg.sdp.type === 'answer') { } else if (msg.sdp.type === 'answer') {
var sd = Object.assign(new this.RTCSessionDescription(), msg.sdp); var _sd = Object.assign(new this.RTCSessionDescription(), msg.sdp);
webChannel.connections.get(msg.senderPeerID).setRemoteDescription(sd, function () {}, function () {}); webChannel.connections.get(msg.senderPeerID).setRemoteDescription(_sd, function () {}, function () {});
} }
} else if (Reflect.has(msg, 'candidate')) { } else if (Reflect.has(msg, 'candidate')) {
webChannel.connections.get(msg.senderPeerID).addIceCandidate(new this.RTCIceCandidate(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); var settings = Object.assign({}, this.settings, options);
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var connection = undefined; var connection = void 0;
var socket = new window.WebSocket(settings.signaling); var socket = new window.WebSocket(settings.signaling);
socket.seq = 1; socket.seq = 1;
socket.facade = options.facade || null; socket.facade = options.facade || null;
@ -1108,6 +1146,10 @@ return /******/ (function(modules) { // webpackBootstrap
case cs.USER_DATA: case cs.USER_DATA:
webChannel.onmessage(msg.id, msg.data); webChannel.onmessage(msg.id, msg.data);
break; break;
case cs.GET_HISTORY:
console.log("SOMEONE WANTS HISTORY");
webChannel.onPeerMessage(msg.id, msg.code);
break;
case cs.SERVICE_DATA: case cs.SERVICE_DATA:
var service = _ServiceProvider2.default.get(msg.service); var service = _ServiceProvider2.default.get(msg.service);
service.onmessage(channel, msg.data); service.onmessage(channel, msg.data);
@ -1139,6 +1181,9 @@ return /******/ (function(modules) { // webpackBootstrap
msg.id = data.id; msg.id = data.id;
msg.data = data.data; msg.data = data.data;
break; break;
case cs.GET_HISTORY:
msg.id = data.id;
break;
case cs.SERVICE_DATA: case cs.SERVICE_DATA:
msg.service = data.service; msg.service = data.service;
msg.data = Object.assign({}, data.data); msg.data = Object.assign({}, data.data);
@ -1268,7 +1313,7 @@ return /******/ (function(modules) { // webpackBootstrap
}, { }, {
key: 'message', key: 'message',
value: function message(code, data) { value: function message(code, data) {
var type = undefined; var type = void 0;
switch (code) { switch (code) {
case cs.USER_DATA: case cs.USER_DATA:
type = 'MSG'; type = 'MSG';

@ -102,15 +102,7 @@ define([
var bump = function () {}; var bump = function () {};
var onPeerMessage = function (peer, msg) { var messagesHistory = [];
if(peer === '_HISTORY_KEEPER_') {
var msgHistory = JSON.parse(msg[4]);
onMessage(msgHistory[1], msgHistory[4]);
}
else {
warn('Illegal direct message');
}
};
var options = { var options = {
signaling: websocketUrl, signaling: websocketUrl,
@ -125,7 +117,7 @@ define([
var realtime; var realtime;
// Add the Facade's peer messages handler // Add the Facade's peer messages handler
Netflux._onPeerMessage = onPeerMessage; // Netflux._onPeerMessage = onPeerMessage;
function getParameterByName(name, url) { function getParameterByName(name, url) {
if (!url) url = window.location.href; if (!url) url = window.location.href;
@ -171,6 +163,9 @@ define([
wc.onmessage = onMessage; // On receiving message wc.onmessage = onMessage; // On receiving message
wc.onJoining = onJoining; // On user joining the session wc.onJoining = onJoining; // On user joining the session
wc.onLeaving = onLeaving; // On user leaving 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 // Open a Chainpad session
realtime = createRealtime(); realtime = createRealtime();
@ -191,12 +186,21 @@ define([
if (parsed.content[0] !== 0) { if (parsed.content[0] !== 0) {
console.log('ENVOI '+message); console.log('ENVOI '+message);
message = Crypto.encrypt(message, cryptKey); message = Crypto.encrypt(message, cryptKey);
wc.send(message); wc.send(message).then(function() {
onMessage('', message); onMessage('', message);
});
} }
// END-TODO // 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 // Get the channel history
// var hc; // var hc;
// wc.peers.forEach(function (p) { if (!hc || p.linkQuality > hc.linkQuality) { hc = p; } }); // wc.peers.forEach(function (p) { if (!hc || p.linkQuality > hc.linkQuality) { hc = p; } });
@ -228,15 +232,27 @@ define([
return '\\' +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) { var onMessage = function(peer, msg) {
// TODO : put in ChainpadAdapter // TODO : put in ChainpadAdapter
// remove the password // remove the password
messagesHistory.push(msg);
var passLen = msg.substring(0,msg.indexOf(':')); var passLen = msg.substring(0,msg.indexOf(':'));
var message = msg.substring(passLen.length+1 + Number(passLen)); var message = msg.substring(passLen.length+1 + Number(passLen));
message = Crypto.decrypt(message, cryptKey); message = Crypto.decrypt(message, cryptKey);
console.log('RECOIS '+message); console.log('RECOIS '+message);
// END-TODO ChainpadAdapter // END-TODO ChainpadAdapter

Loading…
Cancel
Save