Skip to content
This repository was archived by the owner on Mar 31, 2021. It is now read-only.

Commit 9b793c6

Browse files
committed
Add actual responding to the client
1 parent 9a63e7f commit 9b793c6

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

app.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,21 @@ start = function() {
7676
"/store",
7777
{
7878
value: 500
79-
}
79+
},
80+
function(err, response, data) {
81+
console.log(new Date(), data);
82+
}
8083
);
8184

8285
process.nextTick(function() {
8386
senders[startPort + 2](
8487
"/store",
8588
{
8689
value: 600
87-
}
90+
},
91+
function(err, response, data) {
92+
console.log(new Date(), data);
93+
}
8894
);
8995
});
9096
});

server.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports.create = function(port) {
2020
var prefix = "[server:" + port + "]";
2121
var index = 0;
2222
var log = function() {
23-
var date = "[" + (new Date()).valueOf() + ":" + pad((++index), 6) + "]";
23+
var date = "[" + (new Date()).toISOString() + ":" + pad((++index), 6) + "]";
2424
var args = _.toArray(arguments);
2525
args.unshift(prefix + date + "[" + port + "]");
2626
console.log.apply(console, args);
@@ -340,6 +340,7 @@ module.exports.create = function(port) {
340340
// then we start over
341341
if (numAccepted >= (numPeers / 2 + 1)) {
342342
log("majority accepted", accepted);
343+
finalResponse(null, {accepted: true, instance: instance});
343344
}
344345
else {
345346
log("majority rejected", accepted);
@@ -435,15 +436,20 @@ module.exports.create = function(port) {
435436
// We now send the ACCEPT requests to each acceptor.
436437
log("Proposal accepted by majority");
437438

438-
// Initiate the ACCEPT phase
439-
initiateAccept(instance, proposal, value, originalValue, finalResponse);
440-
441439
if (value !== originalValue) {
442440
// If we changed values, we still need to try and store
443441
// the original value the user sent us,
444442
// so we simply go again
445443
initiateProposal(currentInstance++, originalValue, finalResponse);
444+
445+
// We reset the final response in the case where the value changed,
446+
// so that we only respond for the original request coming in from the
447+
// client, not for this intermediate value we are storing
448+
finalResponse = function() {};
446449
}
450+
451+
// Initiate the ACCEPT phase, but if we changed
452+
initiateAccept(instance, proposal, value, originalValue, finalResponse);
447453
}
448454
else {
449455
// We failed to update because somebody beat us in terms
@@ -490,9 +496,14 @@ module.exports.create = function(port) {
490496

491497
// Get the next proposal number and build the proposal
492498
var instance = currentInstance++;
493-
initiateProposal(instance, value, res);
494-
495-
res.send();
499+
initiateProposal(instance, value, function(err, result) {
500+
if (err) {
501+
res.send(err);
502+
}
503+
else {
504+
res.send(result);
505+
}
506+
});
496507
});
497508

498509
app.post('/fetch', function(req, res) {

0 commit comments

Comments
 (0)