diff options
| author | Gubolin <gubolin@fantasymail.de> | 2015-03-08 09:28:56 +0100 |
|---|---|---|
| committer | Gubolin <gubolin@fantasymail.de> | 2015-03-08 09:28:56 +0100 |
| commit | 53936404d5c8ed7a6433e8e9f71c235d34b477b9 (patch) | |
| tree | 6038c09e012d436eb8b9a1cd2e5df0a0c23afee7 | |
| parent | b83d4d83bf521b34b878662c7f145cc9286f096a (diff) | |
| download | snap-yow-53936404d5c8ed7a6433e8e9f71c235d34b477b9.tar.gz snap-yow-53936404d5c8ed7a6433e8e9f71c235d34b477b9.zip | |
rewrite peering flow; add peer id blocks
| -rw-r--r-- | gui.js | 2 | ||||
| -rw-r--r-- | objects.js | 142 | ||||
| -rw-r--r-- | threads.js | 41 |
3 files changed, 126 insertions, 59 deletions
@@ -245,6 +245,8 @@ IDE_Morph.prototype.init = function (isAutoFill) { // override inherited properites: this.color = this.backgroundColor; + + window.peers = []; }; IDE_Morph.prototype.openIn = function (world) { @@ -1188,6 +1188,16 @@ SpriteMorph.prototype.initBlocks = function () { category: 'other', spec: 'send %s to %s' }, + reportPeerId: { + type: 'reporter', + category: 'other', + spec: 'my peer id' + }, + reportPeerList: { + type: 'reporter', + category: 'other', + spec: 'peers online' + }, // Code mapping - experimental doMapCodeOrHeader: { // experimental @@ -2118,6 +2128,8 @@ SpriteMorph.prototype.blockTemplates = function (category) { blocks.push(block('receivePeerMessage')); blocks.push(block('sendPeerMessage')); + blocks.push(block('reportPeerId')); + blocks.push(block('reportPeerList')); blocks.push('='); @@ -4408,86 +4420,96 @@ StageMorph.prototype.init = function (globals) { this.setColor(new Color(255, 255, 255)); this.fps = this.frameRate; - this.peer = new Peer({ + this.initPeering(); +}; + +StageMorph.prototype.initPeering = function (id) { + var myself = this; + + if (window.peers) { + // I don't know why, but it works. + window.peers.forEach(function (oldpeer) { + if (id != oldpeer.id) { + oldpeer.destroy() + } + }); + } + + this.peer = new Peer(id, { host: 'snapmesh.herokuapp.com', - port: 80, + port: 443, + secure: true, path: '/' }); - var myself = this; this.peer.on('open', function (id) { myself.peerId = id; - console.log('Id: ' + id); // DEBUG + }); + this.peer.on('disconnected', function () { + // peer.reconnect does not work (?) because 'id' is undefined + if (!myself.peer.destroyed) { + myself.initPeering(myself.peerId); + } + }); + this.peer.on('error', function (err) { + console.log(err); // DEBUG }); this.peer.on('connection', function (connection) { connection.on('open', function () { connection.on('data', function (data) { - var ide = myself.parentThatIsA(IDE_Morph); - var hats = [], model, message; - try { - model = ide.serializer.parse(data); - message = ide.serializer.loadValue(model); - - // TODO: If a Context is sent, a new Sprite appears. - // This below is just a workaround for one-level rings, - // objects should be cleaned recursively. - message.receiver = null; - message.outerContext = null; - } catch (err) { - console.log(err); // DEBUG - // Ok, it does not seem to be XML. It must be a string then. - message = data; - } - - // call hat blocks - myself.children.concat(myself).forEach(function (morph) { - if (morph instanceof SpriteMorph - || morph instanceof StageMorph) { - hats = hats.concat( - morph.allHatBlocksFor('__peer__message__')); - } - }); - hats.forEach(function (block) { - var process = myself.threads.startProcess(block, - myself.isThreadSafe); - process.context.outerContext.variables.addVar('message'); - process.context.outerContext.variables.setVar( - 'message', - message - ); - process.context.outerContext.variables.addVar('peer'); - process.context.outerContext.variables.setVar( - 'peer', - connection.peer - ); - }); + myself.newPeerMessage(data, connection.peer); }); }); }); + + window.peers.push(this.peer); }; -// peer to peer communication +StageMorph.prototype.newPeerMessage = function (data, peer) { + var ide = this.parentThatIsA(IDE_Morph); + var myself = this; + var hats = [], model, message; -SpriteMorph.prototype.sendPeerMessage = function (message, peer) { - var stage = this.parentThatIsA(StageMorph), - ide = this.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); + try { + model = ide.serializer.parse(data); + message = ide.serializer.loadValue(model); + + // TODO: If a Context is sent, a new Sprite appears. + // This below is just a workaround for one-level rings, + // objects should be cleaned recursively. + message.receiver = null; + message.outerContext = null; + } catch (err) { + console.log(err); // DEBUG + // Ok, it does not seem to be XML. It must be a string then. + message = data; + } + + // call hat blocks + this.children.concat(myself).forEach(function (morph) { + if (morph instanceof SpriteMorph + || morph instanceof StageMorph) { + hats = hats.concat( + morph.allHatBlocksFor('__peer__message__')); } - connection.send(data); + }); + hats.forEach(function (block) { + var process = myself.threads.startProcess(block, + myself.isThreadSafe); + process.context.outerContext.variables.addVar('message'); + process.context.outerContext.variables.setVar( + 'message', + message + ); + process.context.outerContext.variables.addVar('peer'); + process.context.outerContext.variables.setVar( + 'peer', + peer + ); }); }; -StageMorph.prototype.sendPeerMessage = SpriteMorph.prototype.sendPeerMessage; - // StageMorph scaling StageMorph.prototype.setScale = function (number) { @@ -5407,6 +5429,8 @@ StageMorph.prototype.blockTemplates = function (category) { blocks.push(block('receivePeerMessage')); blocks.push(block('sendPeerMessage')); + blocks.push(block('reportPeerId')); + blocks.push(block('reportPeerList')); blocks.push('='); if (StageMorph.prototype.enableCodeMapping) { @@ -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) { |
