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
7c2b5f9a
Commit
7c2b5f9a
authored
6 years ago
by
Aditya Khanna
Browse files
Options
Downloads
Patches
Plain Diff
init
parent
e90f064e
Branches
adi
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
index.js
+77
-21
77 additions, 21 deletions
index.js
with
77 additions
and
21 deletions
index.js
+
77
−
21
View file @
7c2b5f9a
const
PORT
=
20000
;
const
MULTICAST_ADDR
=
"
233.255.255.255
"
;
const
dgram
=
require
(
"
dgram
"
);
const
process
=
require
(
"
process
"
);
dgram
=
require
(
'
dgram
'
);
const
socket
=
dgram
.
createSocket
({
type
:
"
udp4
"
,
reuseAddr
:
true
});
var
serverPort
=
20000
;
var
clientPort
=
50000
;
acks
=
{};
socket
.
bind
(
PORT
);
// Client listens for ACKs
socket
.
on
(
"
listening
"
,
function
()
{
socket
.
addMembership
(
MULTICAST_ADDR
);
socket
.
setBroadcast
(
true
)
setInterval
(
sendMessage
,
2500
);
const
address
=
socket
.
address
();
function
clientListen
(
port
)
{
var
server
=
dgram
.
createSocket
({
type
:
"
udp4
"
,
reuseAddr
:
true
});
server
.
on
(
"
message
"
,
function
(
msg
,
rinfo
)
{
console
.
log
(
"
Client got:
"
+
msg
+
"
from
"
+
rinfo
.
address
+
"
:
"
+
rinfo
.
port
);
var
now
=
Date
.
now
()
/
1000
|
0
;
var
k
=
rinfo
.
address
+
"
:
"
+
rinfo
.
port
;
if
(
!
(
k
in
acks
))
{
acks
[
k
]
=
[];
acks
[
k
].
push
(
true
);
acks
[
k
].
push
(
now
);
}
else
{
acks
[
k
][
0
]
=
true
;
acks
[
k
][
1
]
=
now
;
}
console
.
log
(
acks
)
checkIfFailed
(
now
);
});
server
.
bind
(
port
);
}
function
checkIfFailed
(
now
)
{
for
(
var
key
in
acks
)
{
if
(
now
-
acks
[
key
][
1
]
>
2
)
{
acks
[
key
][
0
]
=
false
;
}
}
}
// Server listens for PINGs & sends back ACKs
function
listen
(
port
)
{
var
server
=
dgram
.
createSocket
({
type
:
"
udp4
"
,
reuseAddr
:
true
});
server
.
bind
(
port
);
server
.
on
(
"
listening
"
,
function
()
{
server
.
addMembership
(
MULTICAST_ADDR
);
server
.
setBroadcast
(
true
)
const
address
=
server
.
address
();
console
.
log
(
`UDP socket listening on
${
address
.
address
}
:
${
address
.
port
}
pid:
${
process
.
pid
}
`
);
});
`UDP socket listening on
${
address
.
address
}
:
${
address
.
port
}
pid:
${
process
.
pid
}
`
);
});
function
sendMessage
()
{
const
message
=
Buffer
.
from
(
`docker
${
process
.
pid
}
`
);
socket
.
send
(
message
,
0
,
message
.
length
,
PORT
,
MULTICAST_ADDR
,
function
()
{
// console.info(`Sending message "${message}"`);
server
.
on
(
"
message
"
,
function
(
msg
,
rinfo
)
{
if
(
rinfo
.
port
!=
clientPort
)
{
console
.
log
(
"
Server got
"
+
msg
+
"
from
"
+
rinfo
.
address
+
"
:
"
+
rinfo
.
port
);
var
message
=
new
Buffer
.
from
(
"
ACK
"
);
const
address
=
server
.
address
();
server
.
send
(
message
,
0
,
message
.
length
,
rinfo
.
port
,
rinfo
.
address
,
function
(
err
,
bytes
)
{
console
.
log
(
"
Sending
"
+
message
+
"
to
"
+
rinfo
.
address
+
"
:
"
+
rinfo
.
port
+
"
from
"
+
address
.
address
+
"
:
"
+
address
.
port
);
});}
});
}
socket
.
on
(
"
message
"
,
function
(
message
,
rinfo
)
{
console
.
info
(
`Message from:
${
rinfo
.
address
}
:
${
rinfo
.
port
}
-
${
message
}
`
);
});
\ No newline at end of file
listen
(
serverPort
);
// Client sends PINGs
clientListen
(
clientPort
);
var
client
=
dgram
.
createSocket
({
type
:
"
udp4
"
,
reuseAddr
:
true
});
client
.
bind
(
clientPort
);
var
message
=
new
Buffer
.
from
(
"
PING
"
);
setInterval
(
sendMessage
,
2500
);
function
sendMessage
()
{
client
.
send
(
message
,
0
,
message
.
length
,
serverPort
,
MULTICAST_ADDR
,
function
(
err
,
bytes
)
{
console
.
log
(
"
Sending
"
+
message
+
"
to
"
+
MULTICAST_ADDR
+
"
:
"
+
serverPort
);
// clientPort = client.address().port;
console
.
log
(
"
Client listening for ACKs on
"
+
clientPort
);
});
}
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