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

Commit

Permalink
Add logic to start script
Browse files Browse the repository at this point in the history
  • Loading branch information
dylankb committed Mar 30, 2018
1 parent dd65868 commit 99a1564
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const kadNodePort = require('./constants').KADNODE_PORT
const publicIp = require('public-ip');
const fs = require('fs');
const fileUtils = require('./utils/file').fileSystem;
const backoff = require('backoff');

publicIp.v4().then(ip => {
const kademliaNode = new kad.KademliaNode({
Expand All @@ -30,6 +31,7 @@ publicIp.v4().then(ip => {
console.log('end')
})
serverConnection.on('data', (receivedData, error) => {
if (error) { throw error; }
receivedData = JSON.parse(receivedData)
console.log("received data: ", receivedData)

Expand Down Expand Up @@ -72,9 +74,42 @@ publicIp.v4().then(ip => {
batNode.retrieveFile(filePath);
} else if (receivedData.messageType === "CLI_AUDIT_FILE") {
let filePath = receivedData.filePath;

console.log("received path: ", filePath);
let fibonacciBackoff = backoff.exponential({
randomisationFactor: 0,
initialDelay: 20,
maxDelay: 2000
});

console.log("received path: ", filePath);
batNode.auditFile(filePath);

// post audit cleanup
serverConnection.on('close', () => {
batnode._audit.ready = false;
batnode._audit.data = null;
batnode._audit.passed = false;
});

fibonacciBackoff.failAfter(10);

fibonacciBackoff.on('backoff', function(number, delay) {
console.log(number + ' ' + delay + 'ms');
});

fibonacciBackoff.on('ready', function() {
if (!batnode._audit.ready) {
fibonacciBackoff.backoff();
} else {
serverConnection.write(JSON.stringify(batnode._audit.passed));
return;
}
});

fibonacciBackoff.on('fail', function() {
console.log('Timeout: failed to complete audit');
});

fibonacciBackoff.backoff();
}
});
}
Expand Down

0 comments on commit 99a1564

Please sign in to comment.