Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
osie
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
osie
Commits
17ea7806
Commit
17ea7806
authored
Jun 06, 2023
by
Ivan Tyagov
Browse files
Options
Browse Files
Download
Plain Diff
Keep alive upd multicast
See merge request
nexedi/osie!31
parents
433d13c1
42025c70
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
19 deletions
+51
-19
multicast-keep-alive/receiver.py
multicast-keep-alive/receiver.py
+24
-9
multicast-keep-alive/sender.py
multicast-keep-alive/sender.py
+27
-10
No files found.
multicast-keep-alive/receiver.py
View file @
17ea7806
import
socket
import
socket
import
struct
import
struct
import
sys
import
sys
from
threading
import
*
import
time
samples_count
=
int
(
sys
.
argv
[
1
])
host
=
"192.168.2.1"
port
=
8000
one_microsecond
=
1
/
1000000.
multicast_group
=
'224.3.29.71'
multicast_group
=
'224.3.29.71'
server_address
=
(
''
,
10000
)
server_address
=
(
''
,
10000
)
timeout
=
int
(
sys
.
argv
[
1
])
samples_count
=
int
(
sys
.
argv
[
2
])
# Create the socket
# send to edge server unicast keep-alive messages
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
def
send_msg
():
while
True
:
msg
=
str
(
int
(
time
.
time
()
*
1000000
))
s
.
send
(
msg
.
encode
(
"utf-8"
))
print
"
\
t
-> %s (unicast)"
%
msg
time
.
sleep
(
timeout
*
one_microsecond
)
# Bind to the server address
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
sock
.
bind
(
server_address
)
s
.
connect
((
host
,
port
))
send_thread
=
Thread
(
target
=
send_msg
)
send_thread
.
daemon
=
True
send_thread
.
start
()
#s.close()
# Subscribe to UDP datagrams published from edge server
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
sock
.
bind
(
server_address
)
# Tell the operating system to add the socket to the multicast group
# Tell the operating system to add the socket to the multicast group
# on all interfaces.
# on all interfaces.
group
=
socket
.
inet_aton
(
multicast_group
)
group
=
socket
.
inet_aton
(
multicast_group
)
...
@@ -19,18 +37,15 @@ mreq = struct.pack('4sL', group, socket.INADDR_ANY)
...
@@ -19,18 +37,15 @@ mreq = struct.pack('4sL', group, socket.INADDR_ANY)
sock
.
setsockopt
(
socket
.
IPPROTO_IP
,
socket
.
IP_ADD_MEMBERSHIP
,
mreq
)
sock
.
setsockopt
(
socket
.
IPPROTO_IP
,
socket
.
IP_ADD_MEMBERSHIP
,
mreq
)
last_micro_second
=
0
last_micro_second
=
0
# Receive/respond loop
i
=
0
i
=
0
l
=
[]
l
=
[]
while
i
<
samples_count
:
while
i
<
samples_count
:
data
,
address
=
sock
.
recvfrom
(
1024
)
data
,
address
=
sock
.
recvfrom
(
1024
)
current_micro_second
=
int
(
data
)
current_micro_second
=
int
(
data
)
diff
=
current_micro_second
-
last_micro_second
diff
=
current_micro_second
-
last_micro_second
#if diff > 200:
# print >>sys.stderr, diff
last_micro_second
=
current_micro_second
last_micro_second
=
current_micro_second
if
i
>
0
:
if
i
>
0
:
# om
o
t first cycle as we care for diff between cycles
# om
i
t first cycle as we care for diff between cycles
l
.
append
(
str
(
diff
))
l
.
append
(
str
(
diff
))
i
+=
1
i
+=
1
...
...
multicast-keep-alive/sender.py
View file @
17ea7806
import
socket
import
socket
import
struct
import
struct
import
sys
,
time
import
sys
,
time
from
threading
import
*
one_microsecond
=
1
/
1000000.0
host
=
"192.168.2.1"
port
=
8000
multicast_group
=
(
'224.3.29.71'
,
10000
)
multicast_group
=
(
'224.3.29.71'
,
10000
)
timeout
=
int
(
sys
.
argv
[
1
])
# Create the datagram socket
# open socket unicast port for connection from couplers
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
serversocket
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
serversocket
.
bind
((
host
,
port
))
serversocket
.
listen
(
5
)
one_microsecond
=
1
/
1000000.0
def
receive_msg
():
conn
,
addr
=
serversocket
.
accept
()
while
True
:
try
:
msg
=
conn
.
recv
(
1024
).
decode
(
"utf8"
)
print
"
\
t
<- %s (unicast)"
%
msg
except
OSError
as
error
:
return
error
receive_thread
=
Thread
(
target
=
receive_msg
)
receive_thread
.
daemon
=
True
receive_thread
.
start
()
print
"Started unicast keep-alive server"
print
"Start sending UDP keep-alive datagrams"
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
# Set the time-to-live for messages to 1 so they do not go past the
# Set the time-to-live for messages to 1 so they do not go past the
# local network segment.
# local network segment.
ttl
=
struct
.
pack
(
'b'
,
1
)
ttl
=
struct
.
pack
(
'b'
,
1
)
sock
.
setsockopt
(
socket
.
IPPROTO_IP
,
socket
.
IP_MULTICAST_TTL
,
ttl
)
sock
.
setsockopt
(
socket
.
IPPROTO_IP
,
socket
.
IP_MULTICAST_TTL
,
ttl
)
last_micro_seconds
=
int
(
time
.
time
()
*
1000000
)
last_micro_seconds
=
int
(
time
.
time
()
*
1000000
)
# XXX: make CLI arguments
# send over UDP keep-alive messages
timeout
=
int
(
sys
.
argv
[
1
])
timeout_tolerance
=
int
(
sys
.
argv
[
2
])
warning_limit
=
timeout
+
timeout_tolerance
while
True
:
while
True
:
micro_seconds
=
int
(
time
.
time
()
*
1000000
)
micro_seconds
=
int
(
time
.
time
()
*
1000000
)
diff
=
micro_seconds
-
last_micro_seconds
diff
=
micro_seconds
-
last_micro_seconds
last_micro_seconds
=
micro_seconds
last_micro_seconds
=
micro_seconds
message
=
str
(
micro_seconds
)
message
=
str
(
micro_seconds
)
if
diff
/
1000000
>
warning_limit
:
print
"-> %s (udp)"
%
message
print
>>
sys
.
stderr
,
'%s'
%
diff
sent
=
sock
.
sendto
(
message
,
multicast_group
)
sent
=
sock
.
sendto
(
message
,
multicast_group
)
time
.
sleep
(
timeout
*
one_microsecond
)
# wait interval
time
.
sleep
(
timeout
*
one_microsecond
)
# wait interval
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment