Skip to content
This repository has been archived by the owner on Sep 19, 2019. It is now read-only.

Commit

Permalink
Refs #4 Add Chord implementation
Browse files Browse the repository at this point in the history
* Implemented 'registerDeliveryCallback' and protocol type handling
  • Loading branch information
Max Jonas Werner committed Sep 16, 2014
1 parent 0f29df4 commit 8de7f9c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
4 changes: 3 additions & 1 deletion js/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 12 additions & 3 deletions js/chord/chord.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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();
}
Expand All @@ -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?
Expand Down Expand Up @@ -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 {
Expand All @@ -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() {
Expand Down
8 changes: 5 additions & 3 deletions js/chord/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions js/chord/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion js/connectionmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8de7f9c

Please sign in to comment.