Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs425-mp2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
arshiam2
cs425-mp2
Commits
34fe3b2d
Commit
34fe3b2d
authored
6 years ago
by
arshiam2
Browse files
Options
Downloads
Patches
Plain Diff
updated commands
parent
4ae6f389
No related branches found
No related tags found
1 merge request
!1
updated commands
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
cli/src/commands/initialize.js
+45
-0
45 additions, 0 deletions
cli/src/commands/initialize.js
cli/src/commands/list.js
+8
-3
8 additions, 3 deletions
cli/src/commands/list.js
machine/index.js
+87
-45
87 additions, 45 deletions
machine/index.js
with
140 additions
and
48 deletions
cli/src/commands/initialize.js
0 → 100644
+
45
−
0
View file @
34fe3b2d
// @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
);
});
This diff is collapsed.
Click to expand it.
cli/src/commands/list.js
+
8
−
3
View file @
34fe3b2d
...
...
@@ -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
()
{});
}
...
...
This diff is collapsed.
Click to expand it.
machine/index.js
+
87
−
45
View file @
34fe3b2d
...
...
@@ -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
send
Messag
e
()
{
function
send
ListRespons
e
()
{
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
]);
}
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment