Skip to content
Snippets Groups Projects
Commit e886d298 authored by arshiam2's avatar arshiam2
Browse files

cleaned up repo

parent 8c7c299d
No related branches found
No related tags found
1 merge request!2WIP: Old 4
*
!index.js
!package.json
!yarn.lock
\ No newline at end of file
node_modules
\ No newline at end of file
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
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);
}
});
{
"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="
}
}
}
{
"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"
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment