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
* Fixed message routing
* Added error message type
* Removed unused 'to' parameter from Chord messages
  • Loading branch information
Max Jonas Werner committed Sep 15, 2014
1 parent c41159e commit 0f29df4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
6 changes: 4 additions & 2 deletions js/chord/chord.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,14 @@ Chord.prototype.remove = function(key) {

Chord.prototype.route = function(to, type, payload, callback) {
if (to === "*") {
this._localNode.route(to, type, payload);
this._localNode.route(to, type, payload, callback);
} else if (this.id().equals(to)) {
callback(null);
} else if (Range.inOpenInterval(to, this._localNode.predecessor_id(), this.id())) {
callback("No peer found with with ID " + to.toString());
} else {
this._localNode.successor(function(err, successorNode) {
successorNode.route(to, type, payload);
successorNode.route(to, type, payload, callback);
});
}
};
Expand Down
26 changes: 15 additions & 11 deletions js/chord/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ ChordNode.prototype = {
UPDATE_SUCCESSOR: "UPDATE_SUCCESSOR",
ACK: "ACK",
PUT: "PUT",
GET: "GET"
GET: "GET",
ROUTE: "ROUTE",
ERROR: "ERROR"
},

toString: function() {
Expand Down Expand Up @@ -151,15 +153,15 @@ ChordNode.prototype = {
});
},

route: function(to, type, payload) {
route: function(to, type, payload, callback) {
var self = this;
this._send_request({
type: this.message_types.ROUTE,
to: to,
to: to.toString(),
proto_type: type,
payload: payload
}, function(err, msg) {
self.log("route result", msg);
callback(err);
});
},

Expand Down Expand Up @@ -258,11 +260,16 @@ ChordNode.prototype = {

_route: function(msg) {
var self = this;
this._chord.route(msg.to, msg.proto_type, msg.payload, function(err) {
self._send({
this._chord.route(new BigInteger(msg.to), msg.proto_type, msg.payload, function(err) {
var resp = {
type: self.message_types.ACK,
seqnr: msg.seqnr
});
};
if (err) {
resp.type = self.message_types.ERROR;
resp.error = err;
}
self._send(resp);
});
},

Expand All @@ -273,9 +280,6 @@ ChordNode.prototype = {
},

_send: function(msg) {
if (!this._localNode) {
msg.to = this._peer.id.toString();
}
msg.from = this._chord.id().toString();
this._peer.dataChannel.send(JSON.stringify(msg));
},
Expand All @@ -294,7 +298,7 @@ ChordNode.prototype = {

_handle_response: function(msg, callback) {
delete this._pending[msg.seqnr];
callback(null, msg);
callback(msg.error, msg);
},

_handle_request: function(msg) {
Expand Down
4 changes: 2 additions & 2 deletions js/chord/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Server.prototype.connect = function(to, callback) {
var onmessage = local.dataChannel.onmessage;
if (typeof(onmessage) === "function") {
onmessage.apply(local.dataChannel, [{
data: JSON.stringify(msg)
data: msg
},
self.id
]);
Expand All @@ -41,7 +41,7 @@ Server.prototype.connect = function(to, callback) {
var onmessage = remote.dataChannel.onmessage;
if (typeof(onmessage) === "function") {
onmessage.apply(remote.dataChannel, [{
data: JSON.stringify(msg)
data: msg
},
to
]);
Expand Down
9 changes: 8 additions & 1 deletion js/chord/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ function debug(chord, id, bootstrapId, err, res) {
console.log("//{" + k + ": " + v + "}");
});
});
chords[9].route(chords[3].id(), "test", {
var to = chords[3].id();
chords[9].route(to, "test", {
a: 1,
b: 2
}, function(err) {
if (err) {
console.log("Error routing to", to.toString(), err);
} else {
console.log("routing successful");
}
});
}
}
Expand Down

0 comments on commit 0f29df4

Please sign in to comment.