summaryrefslogtreecommitdiff
path: root/objects.js
diff options
context:
space:
mode:
authorCode WvS <code-wvs@quantentunnel.de>2015-03-08 10:01:11 +0100
committerCode WvS <code-wvs@quantentunnel.de>2015-03-08 10:01:11 +0100
commit0b9b31b84c1070e51d4d3e257db5ccb1e63db683 (patch)
tree5d15dda7804c76f28ceacb285241163b83dba284 /objects.js
parent6769c91b58751b15ca45528ef0afbbc23a7525d2 (diff)
parentda6c1a37a3f760ec787299f5a7f2bbf1a149acf5 (diff)
downloadsnap-yow-0b9b31b84c1070e51d4d3e257db5ccb1e63db683.tar.gz
snap-yow-0b9b31b84c1070e51d4d3e257db5ccb1e63db683.zip
Merge remote branch "p2p"
Diffstat (limited to 'objects.js')
-rw-r--r--objects.js128
1 files changed, 128 insertions, 0 deletions
diff --git a/objects.js b/objects.js
index 13f78dc..b351cdd 100644
--- a/objects.js
+++ b/objects.js
@@ -1210,6 +1210,29 @@ SpriteMorph.prototype.initBlocks = function () {
defaults: [localize('each item')]
},
+ // peer to peer communication
+ receivePeerMessage: {
+ type: 'hat',
+ category: 'other',
+ spec: 'when I receive %upvar from %upvar',
+ defaults: [localize('message'), localize('peer')]
+ },
+ sendPeerMessage: {
+ type: 'command',
+ 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
type: 'command',
@@ -2186,6 +2209,13 @@ SpriteMorph.prototype.blockTemplates = function (category) {
blocks.push('=');
+ blocks.push(block('receivePeerMessage'));
+ blocks.push(block('sendPeerMessage'));
+ blocks.push(block('reportPeerId'));
+ blocks.push(block('reportPeerList'));
+
+ blocks.push('=');
+
if (StageMorph.prototype.enableCodeMapping) {
blocks.push(block('doMapCodeOrHeader'));
blocks.push(block('doMapStringCode'));
@@ -3645,6 +3675,9 @@ SpriteMorph.prototype.allHatBlocksFor = function (message) {
if (morph.selector === 'receiveOnClone') {
return message === '__clone__init__';
}
+ if (morph.selector === 'receivePeerMessage') {
+ return message === '__peer__message__';
+ }
}
return false;
});
@@ -4514,6 +4547,95 @@ StageMorph.prototype.init = function (globals) {
this.acceptsDrops = false;
this.setColor(new Color(255, 255, 255));
this.fps = this.frameRate;
+
+ 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: 443,
+ secure: true,
+ path: '/'
+ });
+
+ this.peer.on('open', function (id) {
+ myself.peerId = id;
+ });
+ 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) {
+ myself.newPeerMessage(data, connection.peer);
+ });
+ });
+ });
+
+ window.peers.push(this.peer);
+};
+
+StageMorph.prototype.newPeerMessage = function (data, peer) {
+ var ide = this.parentThatIsA(IDE_Morph);
+ var myself = this;
+ 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
+ this.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(localize('message'));
+ process.context.outerContext.variables.setVar(
+ localize('message'),
+ message
+ );
+ process.context.outerContext.variables.addVar(localize('peer'));
+ process.context.outerContext.variables.setVar(
+ localize('peer'),
+ peer
+ );
+ });
};
// StageMorph scaling
@@ -5383,6 +5505,12 @@ StageMorph.prototype.blockTemplates = function (category) {
blocks.push('=');
+ blocks.push(block('receivePeerMessage'));
+ blocks.push(block('sendPeerMessage'));
+ blocks.push(block('reportPeerId'));
+ blocks.push(block('reportPeerList'));
+ blocks.push('=');
+
if (StageMorph.prototype.enableCodeMapping) {
blocks.push(block('doMapCodeOrHeader'));
blocks.push(block('doMapStringCode'));