diff --git a/cli/src/commands/initialize.js b/cli/src/commands/initialize.js new file mode 100644 index 0000000000000000000000000000000000000000..35a5a94e44522bba83f0159692a8ded8511c6235 --- /dev/null +++ b/cli/src/commands/initialize.js @@ -0,0 +1,45 @@ +// @flow +const path = require("path"); +const chalk = require("chalk"); +const dgram = require("dgram"); +const process = require("process"); + +const handleErrors = require("../utils/handleErrors"); + +const PORT = 5000; +const HOST = "172.22.158.17"; +const socket = dgram.createSocket({ type: "udp4", reuseAddr: true }); + +const machineToIps = { + "0": "172.16.138.158", + "1": "172.22.156.15", + "2": "172.22.158.15", + "3": "172.22.154.16", + "4": "172.22.156.16", + "5": "172.22.158.16", + "6": "172.22.154.17", + "8": "172.22.158.17", + "9": "172.22.154.18", + "10": "172.22.156.18" +}; + +module.exports.command = "initialize [number]"; +module.exports.describe = "test commands"; +module.exports.builder = (yargs: any) => yargs; + +socket.bind(PORT); + +function sendMessage(host, number) { + const message = Buffer.from(`initialize`); + console.log(host); + socket.send(message, 0, message.length, 20000, host, function() {}); +} + +module.exports.handler = handleErrors(async (argv: {}) => { + console.log("Sending message to introducer"); + let number = argv.number; + if (number != undefined) { + await sendMessage(machineToIps[number], number); + } + process.exit(0); +}); diff --git a/cli/src/commands/list.js b/cli/src/commands/list.js index 18b123f8c2e5b0a1273fe825c35c60c77d06b558..ea7a757479f57b36aeadc41980fbb9ca65224c3a 100644 --- a/cli/src/commands/list.js +++ b/cli/src/commands/list.js @@ -47,12 +47,17 @@ socket.on("listening", function() { }); socket.on("message", function(message, rinfo) { -const parsedMessage = JSON.parse(message); - Object.keys(JSON.parse(message)).map(key => console.log(key + " " + parsedMessage[key]["status"])) + console.log("GOT MESSAGE") + console.info(`Message from: ${rinfo.address}:${rinfo.port} - ${message}`); + // console.log(JSON.parse(message)) + const parsedMessage = JSON.parse(message); + Object.keys(JSON.parse(message)).map(key => + console.log(key + " " + parsedMessage[key]) + ); }); function sendMessage(host, number) { - const message = Buffer.from(`list`); + const message = Buffer.from(`list ${number}`); console.log(host); socket.send(message, 0, message.length, 20000, host, function() {}); } diff --git a/machine/index.js b/machine/index.js index 7954671b2ea3bbc053fdce7472983d89ca88a04d..d24af6e250f511520218a3f205123cef9f1b0391 100644 --- a/machine/index.js +++ b/machine/index.js @@ -14,7 +14,7 @@ socket.bind(PORT); let connections = {}; -let machineNumber = 1; +let timeAllowed = 60 * 2 * 1000; const machineToIps = { "0": "172.16.138.158", @@ -29,82 +29,124 @@ const machineToIps = { "10": "172.22.156.18" }; +const IpsToMachine = { + "0.0.0.0": "0", + "172.16.138.158": "0", + "172.22.156.15": "1", + "172.22.158.15": "2", + "172.22.154.16": "3", + "172.22.156.16": "4", + "172.22.158.16": "5", + "172.22.154.17": "6", + "172.22.158.17": "8", + "172.22.154.18": "9", + "172.22.156.18": "10" +}; + +let machineNumber = -1; + socket.on("listening", function() { socket.addMembership(MULTICAST_ADDR); socket.setBroadcast(true); - // setInterval(sendMessage, 2500); const address = socket.address(); console.log( `UDP socket listening on ${address.address}:${address.port} pid: ${ process.pid }` ); - resetConnections() - // console.log(Date.now()) + + machineNumber = IpsToMachine[address.address]; + setInterval(checkAll, 1000 * 2); + setInterval(checkConnections, timeAllowed); }); -function resetConnections() { - connections = { - "1": { - status: "alive", - timestamp: 0 - }, - "2": { +function resetConnections(machineIds) { + connections = {}; + console.log(machineIds); + machineIds.forEach(element => { + connections[element] = { status: "alive", timestamp: 0 - }, - "3": { - status: "alive", - timestamp: 0 - }, - "4": { - status: "alive", - timestamp: 0 - }, - "5": { - status: "alive", - timestamp: 0 - } - }; + }; + }); + // ( m => { + // connections[id] = { + // status: "alive", + // timestamp: 0 + // } + // }); + // console.log(id) + // connections[id] = { + // status: "alive", + // timestamp: 0 + // } + // } + console.log(connections); } -function check() { - console.log(machineToIps["1"]) +function checkAll() { + Object.keys(connections).map(m => check(m)); +} + +function check(machineNumberBeingChecked) { + // console.log(machineNumberBeingChecked) + // console.log(machineToIps[machineNumberBeingChecked]) + // console.log("my machien number" + machineNumber) // const message = Buffer.from(`check ${machineToIps[machineNumber]}`); - const message = Buffer.from(`check ${machineToIps[0]}`); - socket.send(message, 0, message.length, PORT, machineToIps["1"], function() {}); + const message = Buffer.from(`check ${machineToIps[machineNumber]}`); + socket.send( + message, + 0, + message.length, + PORT, + machineToIps[machineNumberBeingChecked], + function() {} + ); } -function sendMessage() { +function sendListResponse() { const message = JSON.stringify(connections); socket.send(message, 0, message.length, CLI_PORT, CLI_HOST, function() {}); } function sendAck(synIP) { - const message = Buffer.from(`ack ${machineNumber}`) - console.log(machineToIps[machineNumber]) - console.log(PORT) - console.log(synIP) + const message = Buffer.from(`ack ${machineNumber}`); socket.send(message, 0, message.length, PORT, synIP, function() {}); } +//checks all the connections to make sure the latest ack was in the last two minutes +function checkConnections() { + const machines = Object.keys(connections); + const currentTime = Date.now(); + for (let machine in machines) { + if (connections[machine]) { + // console.log((currentTime - connections[machine].timestamp)) + if (timeAllowed >= currentTime - connections[machine].timestamp) { + connections[machine].status = "alive"; + } else { + connections[machine].status = "dead"; + } + } + } + console.log(connections); +} + socket.on("message", function(message, rinfo) { const text = message.toString().split(" "); if (text[0] === "list") { - sendMessage(); - check(); + checkConnections(); + sendListResponse(); } if (text[0] === "check") { - console.log("Sending Ack") - sendAck(text[1]) - // set - // sendMessage(); + // console.log("Sending Ack") + sendAck(text[1]); } if (text[0] === "ack") { - // console.log("GOT ACK") - console.log(text[1]) - connections[text[1]]["timestamp"] = Date.now() - console.log(connections[text[1]]) + connections[text[1]]["timestamp"] = Date.now(); + // console.log(connections[text[1]]) + } + if (text[0] == "initialize") { + console.log("initialize"); + resetConnections([1, 2, 3, 4, 5]); } - });