From 8de7f9ce3f50e3576407368c7b5b48778b29a657 Mon Sep 17 00:00:00 2001 From: Max Jonas Werner Date: Tue, 16 Sep 2014 12:05:05 +0200 Subject: [PATCH] Refs #4 Add Chord implementation * Implemented 'registerDeliveryCallback' and protocol type handling --- js/application.js | 4 +++- js/chord/chord.js | 15 ++++++++++++--- js/chord/node.js | 8 +++++--- js/chord/test.js | 3 +++ js/connectionmanager.js | 4 +++- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/js/application.js b/js/application.js index 99f7500..7a38969 100644 --- a/js/application.js +++ b/js/application.js @@ -94,7 +94,9 @@ BOPlishClient.prototype = { * @param payload {Object} The payload to send. */ send: function(to, protocol, payload) { - this._router.route(to, protocol, payload); + this._router.route(to, protocol, payload, function(err) { + console.log("RESULT: " + err); + }); }, /** * Installs a handler for the given protocol name. diff --git a/js/chord/chord.js b/js/chord/chord.js index 5bceada..d7ef917 100644 --- a/js/chord/chord.js +++ b/js/chord/chord.js @@ -11,6 +11,9 @@ var Range = require("./range"); * @constructor * @class This is a Chord DHT implementation using WebRTC data channels. * + * @param id {BigInteger} the ID of this Chord instance + * @param fallbackSignaling {WebSocket} The fallback channel to the bootstrap + * server * @param connectionManager {ConnectionManager} The connection manager instance * to be used for requesting data channel connections. */ @@ -21,7 +24,6 @@ var Chord = function(id, fallbackSignaling, connectionManager) { Helper.defineProperties(this); - // TODO(max): externalize this so Node can reuse it upon (de-)serializing if (!id) { id = Chord.randomId(); } @@ -31,6 +33,7 @@ var Chord = function(id, fallbackSignaling, connectionManager) { this._localNode._predecessor = id; this._remotes = {}; this._connectionManager = connectionManager; + this._messageCallbacks = {}; this._fingerTable = {}; this._m = 8; this._joined = false; // are we joined to a Chord ring, yet? @@ -229,7 +232,13 @@ Chord.prototype.route = function(to, type, payload, callback) { if (to === "*") { this._localNode.route(to, type, payload, callback); } else if (this.id().equals(to)) { - callback(null); + try { + this._messageCallbacks[type](payload, payload.from); + callback(null); + } catch (e) { + this.log('Unable to handle message of type ' + type + ' from ' + payload.from + ' because no callback is registered: ' + e); + callback("No application for protocol '" + type + "'"); + } } else if (Range.inOpenInterval(to, this._localNode.predecessor_id(), this.id())) { callback("No peer found with with ID " + to.toString()); } else { @@ -240,7 +249,7 @@ Chord.prototype.route = function(to, type, payload, callback) { }; Chord.prototype.registerDeliveryCallback = function(protocol, callback) { - // TODO + this._messageCallbacks[protocol] = callback; }; Chord.randomId = function() { diff --git a/js/chord/node.js b/js/chord/node.js index 40ab753..19a2beb 100644 --- a/js/chord/node.js +++ b/js/chord/node.js @@ -158,8 +158,10 @@ ChordNode.prototype = { this._send_request({ type: this.message_types.ROUTE, to: to.toString(), - proto_type: type, - payload: payload + payload: { + type: type, + payload: payload + } }, function(err, msg) { callback(err); }); @@ -260,7 +262,7 @@ ChordNode.prototype = { _route: function(msg) { var self = this; - this._chord.route(new BigInteger(msg.to), msg.proto_type, msg.payload, function(err) { + this._chord.route(new BigInteger(msg.to), msg.payload.type, msg.payload.payload, function(err) { var resp = { type: self.message_types.ACK, seqnr: msg.seqnr diff --git a/js/chord/test.js b/js/chord/test.js index c72b412..c1d730e 100644 --- a/js/chord/test.js +++ b/js/chord/test.js @@ -63,6 +63,9 @@ function debug(chord, id, bootstrapId, err, res) { }); }); var to = chords[3].id(); + chords[3].registerDeliveryCallback("test", function(msg, from) { + console.log(msg, from); + }); chords[9].route(to, "test", { a: 1, b: 2 diff --git a/js/connectionmanager.js b/js/connectionmanager.js index c480c8e..f7b7e0d 100644 --- a/js/connectionmanager.js +++ b/js/connectionmanager.js @@ -118,7 +118,9 @@ ConnectionManager.prototype = { // spec specifies that a null candidate means that the ice gathering is complete pc.onicecandidate = function() {}; pc.createOffer(function(offer) { - this._router.route(to, 'signaling-protocol', offer); + this._router.route(to, 'signaling-protocol', offer, function(err) { + console.log(err); + }); }.bind(this), this._onCreateOfferError.bind(this, errorCallback)); } }.bind(this);