From 53936404d5c8ed7a6433e8e9f71c235d34b477b9 Mon Sep 17 00:00:00 2001 From: Gubolin Date: Sun, 8 Mar 2015 09:28:56 +0100 Subject: rewrite peering flow; add peer id blocks --- threads.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'threads.js') diff --git a/threads.js b/threads.js index 50af8dd..bc8e429 100644 --- a/threads.js +++ b/threads.js @@ -1295,6 +1295,47 @@ Process.prototype.doRemoveTemporaries = function () { } }; +// Peer to peer primitives + +Process.prototype.sendPeerMessage = function (message, peer) { + var stage = this.homeContext.receiver.parentThatIsA(StageMorph), + ide = this.homeContext.receiver.parentThatIsA(IDE_Morph); + var connection = stage.peer.connect(peer, {reliable: true}); + connection.on('open', function () { + var data; + if (typeof message == "string") { + data = message; + } else if (typeof message == "function") { + data = message.toString(); + } else { + data = ide.serializer.serialize(message); + } + connection.send(data); + }); +}; + +Process.prototype.reportPeerList = function () { + var myself = this; + + if (!this.context.wait) { + var stage = this.homeContext.receiver.parentThatIsA(StageMorph); + stage.peer.listAllPeers(function (peers) { + myself.context.result = new List(peers); + }); + this.context.wait = true; + } else if (this.context.result) { + return this.context.result; + } + + this.pushContext('doYield'); + this.pushContext(); +}; + +Process.prototype.reportPeerId = function () { + var stage = this.homeContext.receiver.parentThatIsA(StageMorph); + return stage.peerId; +}; + // Process lists primitives Process.prototype.reportNewList = function (elements) { -- cgit v1.3.1 From da6c1a37a3f760ec787299f5a7f2bbf1a149acf5 Mon Sep 17 00:00:00 2001 From: Gubolin Date: Sun, 8 Mar 2015 09:44:41 +0100 Subject: fix localization bug; accept lists as peer message receiver --- objects.js | 8 ++++---- threads.js | 9 +++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'threads.js') diff --git a/objects.js b/objects.js index 9399269..282d01f 100644 --- a/objects.js +++ b/objects.js @@ -4497,14 +4497,14 @@ StageMorph.prototype.newPeerMessage = function (data, peer) { hats.forEach(function (block) { var process = myself.threads.startProcess(block, myself.isThreadSafe); - process.context.outerContext.variables.addVar('message'); + process.context.outerContext.variables.addVar(localize('message')); process.context.outerContext.variables.setVar( - 'message', + localize('message'), message ); - process.context.outerContext.variables.addVar('peer'); + process.context.outerContext.variables.addVar(localize('peer')); process.context.outerContext.variables.setVar( - 'peer', + localize('peer'), peer ); }); diff --git a/threads.js b/threads.js index bc8e429..2d1ce77 100644 --- a/threads.js +++ b/threads.js @@ -1298,6 +1298,15 @@ Process.prototype.doRemoveTemporaries = function () { // Peer to peer primitives Process.prototype.sendPeerMessage = function (message, peer) { + var myself = this; + + if (peer instanceof List) { + peer.asArray().forEach(function (singlePeer) { + myself.sendPeerMessage(message, singlePeer); + }); + return; + } + var stage = this.homeContext.receiver.parentThatIsA(StageMorph), ide = this.homeContext.receiver.parentThatIsA(IDE_Morph); var connection = stage.peer.connect(peer, {reliable: true}); -- cgit v1.3.1