From 0f29df40b3c2ea641b4dff1f17e9a15a1a45d211 Mon Sep 17 00:00:00 2001 From: Max Jonas Werner Date: Tue, 16 Sep 2014 01:05:25 +0200 Subject: [PATCH] refs #4 Add Chord implementation * Fixed message routing * Added error message type * Removed unused 'to' parameter from Chord messages --- js/chord/chord.js | 6 ++++-- js/chord/node.js | 26 +++++++++++++++----------- js/chord/server.js | 4 ++-- js/chord/test.js | 9 ++++++++- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/js/chord/chord.js b/js/chord/chord.js index 957d01a..5bceada 100644 --- a/js/chord/chord.js +++ b/js/chord/chord.js @@ -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); }); } }; diff --git a/js/chord/node.js b/js/chord/node.js index b7cdb67..40ab753 100644 --- a/js/chord/node.js +++ b/js/chord/node.js @@ -39,7 +39,9 @@ ChordNode.prototype = { UPDATE_SUCCESSOR: "UPDATE_SUCCESSOR", ACK: "ACK", PUT: "PUT", - GET: "GET" + GET: "GET", + ROUTE: "ROUTE", + ERROR: "ERROR" }, toString: function() { @@ -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); }); }, @@ -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); }); }, @@ -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)); }, @@ -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) { diff --git a/js/chord/server.js b/js/chord/server.js index 2cd2e3d..549edaa 100644 --- a/js/chord/server.js +++ b/js/chord/server.js @@ -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 ]); @@ -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 ]); diff --git a/js/chord/test.js b/js/chord/test.js index 2d21fb9..c72b412 100644 --- a/js/chord/test.js +++ b/js/chord/test.js @@ -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"); + } }); } }