diff --git a/introducer/.dockerignore b/introducer/.dockerignore new file mode 100644 index 0000000000000000000000000000000000000000..c0ff3acb4f3345d048dd9155c2f501c7b3dd9b68 --- /dev/null +++ b/introducer/.dockerignore @@ -0,0 +1,4 @@ +* +!index.js +!package.json +!yarn.lock \ No newline at end of file diff --git a/introducer/.gitignore b/introducer/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..b512c09d476623ff4bf8d0d63c29b784925dbdf8 --- /dev/null +++ b/introducer/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/introducer/Dockerfile b/introducer/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..093a0852d47aebab3e15ba3f0a8cd94d29cc7012 --- /dev/null +++ b/introducer/Dockerfile @@ -0,0 +1,13 @@ +FROM mhart/alpine-node:10 as base +WORKDIR /usr/src +COPY package.json yarn.lock ./ +RUN yarn --production + +FROM mhart/alpine-node:base-10 +WORKDIR /usr/src +COPY --from=base /usr/src . +COPY . . +CMD node index.js + +EXPOSE 20000:20000/udp +EXPOSE 20000 diff --git a/introducer/index.js b/introducer/index.js new file mode 100644 index 0000000000000000000000000000000000000000..751d847095125d5e33099d30d84f470da2b0afc0 --- /dev/null +++ b/introducer/index.js @@ -0,0 +1,157 @@ +const PORT = 20000; +const MULTICAST_ADDR = "233.255.255.255"; + +const dgram = require("dgram"); +const process = require("process"); +var ip = require("ip"); + +const socket = dgram.createSocket({ type: "udp4", reuseAddr: true }); + +const CLI_HOST = "172.16.138.158"; + +const CLI_PORT = "5000"; + +socket.bind(PORT); + +let connections = {}; + +let machines = ["2" + Date.now(), "3" + Date.now()]; + +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" +}; + +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 = IpsToMachine[ip.address()]; + +socket.on("listening", function() { + socket.addMembership(MULTICAST_ADDR); + socket.setBroadcast(true); + const address = socket.address(); + console.log( + `UDP socket listening on ${address.address}:${address.port} pid: ${ + process.pid + } machine ${machineNumber}` + ); +}); + +function syn(machineNumberBeingChecked) { + console.log(machineNumberBeingChecked); + console.log("my machien number" + machineNumber); + const message = Buffer.from(`check ${machineToIps[machineNumber]}`); + socket.send( + message, + 0, + message.length, + PORT, + machineToIps[machineNumberBeingChecked], + function() {} + ); +} + +function initialize(machineNumber, connectedTo) { + const message = Buffer.from(`initialize [${connectedTo}]`); + socket.send( + message, + 0, + message.length, + PORT, + machineToIps[machineNumber], + function() {} + ); +} + +function getPairings(machines) { + for (let i = 0; i < machines.length; i++) { + surroundingNodes = []; + + if (i + 1 > machines.length - 1) { + surroundingNodes.push(machines[i + 1 - machines.length]); + } else { + surroundingNodes.push(machines[i + 1]); + } + + if (i + 2 > machines.length - 1) { + surroundingNodes.push(machines[i + 2 - machines.length]); + } else { + surroundingNodes.push(machines[i + 2]); + } + + if (i - 1 < 0) { + surroundingNodes.push(machines[i - 1 + machines.length]); + } else { + surroundingNodes.push(machines[i - 1]); + } + + if (i - 2 < 0) { + surroundingNodes.push(machines[i - 2 + machines.length]); + } else { + surroundingNodes.push(machines[i - 2]); + } + + // console.log( + // "Node :" + + // machines[i] + + // " is connect to " + + // Array.from(new Set(surroundingNodes)) + // ); + initialize(machines[i][0], Array.from(new Set(surroundingNodes))); + } +} + +socket.on("message", function(message, rinfo) { + const text = message.toString().split(" "); + if (text[0] === "init") { + // console.log("GOT INIR"); + getPairings(machines); + } + if (text[0] === "join") { + if ( + machines + .map(m => m.toString()[0]) + .filter(f => f === text[1].toString()[0]).length == 0 + ) { + machines.push(text[1]); + console.log(machines); + getPairings(machines); + } else { + ("This machine has already joined"); + } + } + + if (text[0] === "leave") { + machines = machines.filter(m => m.toString()[0] !== text[1]); + getPairings(machines); + } + + if (text[0] === "dead") { + console.log("Killed " + text[1].toString()[0]) + const ondLength = machines.length + machines = machines.filter(m => m.toString()[0] !== text[1].toString()[0]); + console.log(machines) + getPairings(machines); + + } +}); diff --git a/introducer/package-lock.json b/introducer/package-lock.json new file mode 100644 index 0000000000000000000000000000000000000000..fd52091bca5c3a378da61238b5df3858b2997952 --- /dev/null +++ b/introducer/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "introducer", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "dgram": { + "version": "1.0.1", + "resolved": "http://registry.npmjs.org/dgram/-/dgram-1.0.1.tgz", + "integrity": "sha1-N/OyAPgDOl/3WTAwicgc42G2UcM=" + } + } +} diff --git a/introducer/package.json b/introducer/package.json new file mode 100644 index 0000000000000000000000000000000000000000..35a4303c97a7b0d0d9bef959c7d15bf8ef8e8fdf --- /dev/null +++ b/introducer/package.json @@ -0,0 +1,16 @@ +{ + "name": "introducer", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "node index.js", + "test": "node test.js" + }, + "author": "Aria <arshiam2828@gmail.com>", + "license": "MIT", + "dependencies": { + "dgram": "^1.0.1", + "ip": "^1.1.5" + } +}