Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
Robobin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
plw1g21
Robobin
Commits
939bcd44
Commit
939bcd44
authored
5 months ago
by
Paul-Winpenny
Browse files
Options
Downloads
Patches
Plain Diff
OOPified the wifi handler.
parent
7680cd6e
No related branches found
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
Connectivity/SampleServerPi.py
+164
-96
164 additions, 96 deletions
Connectivity/SampleServerPi.py
with
164 additions
and
96 deletions
Connectivity/SampleServerPi.py
+
164
−
96
View file @
939bcd44
import
queue
import
socket
import
socket
import
threading
import
threading
import
time
import
time
UDP_IP
=
"
255.255.255.255
"
class
RoboBinConnectionHandler
:
UDP_PORT
=
5005
def
__init__
(
self
,
udp_ip
=
"
255.255.255.255
"
,
udp_port
=
5005
,
listen_port
=
5006
):
LISTEN_PORT
=
5006
self
.
queue
=
queue
.
Queue
(
10
)
self
.
UDP_IP
=
udp_ip
def
broadcast_presence
():
self
.
UDP_PORT
=
udp_port
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
self
.
LISTEN_PORT
=
listen_port
sock
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_BROADCAST
,
1
)
self
.
stop_event
=
threading
.
Event
()
self
.
message_handlers
=
{
while
True
:
"
PING
"
:
self
.
handle_ping
,
message
=
b
"
ROBOBIN_PRESENT
"
"
TIME
"
:
self
.
handle_time_request
,
sock
.
sendto
(
message
,
(
UDP_IP
,
UDP_PORT
))
"
CALLOVER
"
:
self
.
handle_call_over
,
print
(
"
Broadcasting: {}
"
.
format
(
message
.
decode
()))
"
CUSTOM
"
:
self
.
handle_custom_message
,
time
.
sleep
(
5
)
}
def
handle_ping
(
client_socket
):
self
.
udp_sock
=
None
print
(
"
Received PING from client.
"
)
self
.
tcp_socket
=
None
#
response
=
b
"
PONG
"
print
(
"
Sending PONG to client.
"
)
def
broadcast_presence
(
self
):
client_socket
.
sendall
(
response
)
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
sock
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_BROADCAST
,
1
)
def
handle_time_request
(
client_socket
):
current_time
=
time
.
ctime
().
encode
()
while
not
self
.
stop_event
.
is_set
():
print
(
f
"
Sending current time:
{
current_time
.
decode
()
}
"
)
message
=
b
"
ROBOBIN_PRESENT
"
client_socket
.
sendall
(
current_time
)
try
:
sock
.
sendto
(
message
,
(
self
.
UDP_IP
,
self
.
UDP_PORT
))
def
handle_custom_message
(
client_socket
,
message
):
print
(
"
Broadcasting: {}
"
.
format
(
message
.
decode
()))
response
=
f
"
Received custom message:
{
message
}
"
.
encode
()
except
OSError
as
e
:
print
(
f
"
Custom handler response:
{
response
.
decode
()
}
"
)
print
(
f
"
Broadcast error:
{
e
}
"
)
client_socket
.
sendall
(
response
)
break
def
handle_unknown_message
(
client_socket
):
time
.
sleep
(
5
)
response
=
b
"
I don
'
t know this message.
"
print
(
"
Sending response to unknown message.
"
)
sock
.
close
()
client_socket
.
sendall
(
response
)
print
(
"
Broadcasting stopped.
"
)
message_handlers
=
{
def
handle_ping
(
self
,
client_socket
):
"
PING
"
:
handle_ping
,
print
(
"
Received PING from client.
"
)
"
TIME
"
:
handle_time_request
,
response
=
b
"
PONG
"
"
CUSTOM
"
:
handle_custom_message
,
print
(
"
Sending PONG to client.
"
)
}
client_socket
.
sendall
(
response
)
def
handle_client_connection
(
client_socket
):
def
handle_time_request
(
self
,
client_socket
):
try
:
current_time
=
time
.
ctime
().
encode
()
while
True
:
print
(
f
"
Sending current time:
{
current_time
.
decode
()
}
"
)
request
=
client_socket
.
recv
(
1024
)
client_socket
.
sendall
(
current_time
)
if
not
request
:
print
(
"
No request received, closing connection.
"
)
def
handle_custom_message
(
self
,
client_socket
,
message
):
break
response
=
f
"
Received custom message:
{
message
}
"
.
encode
()
message
=
request
.
decode
()
print
(
f
"
Custom handler response:
{
response
.
decode
()
}
"
)
print
(
"
Received from client: {}
"
.
format
(
message
))
client_socket
.
sendall
(
response
)
def
handle_call_over
(
self
,
client_socket
,
message
):
parts
=
message
.
split
(
"
"
,
1
)
response
=
f
"
User has requested node:
{
message
}
"
.
encode
()
message_type
=
parts
[
0
]
print
(
f
"
Call over handler response:
{
response
.
decode
()
}
"
)
message_data
=
parts
[
1
]
if
len
(
parts
)
>
1
else
None
client_socket
.
sendall
(
response
)
if
message_type
in
message_handlers
:
def
handle_unknown_message
(
self
,
client_socket
):
if
message_type
==
"
CUSTOM
"
and
message_data
:
response
=
b
"
I don
'
t know this message.
"
message_handlers
[
message_type
](
client_socket
,
message_data
)
print
(
"
Sending response to unknown message.
"
)
else
:
client_socket
.
sendall
(
response
)
message_handlers
[
message_type
](
client_socket
)
else
:
def
handle_client_connection
(
self
,
client_socket
):
handle_unknown_message
(
client_socket
)
except
ConnectionResetError
:
print
(
"
Client connection was forcibly closed.
"
)
except
Exception
as
e
:
print
(
f
"
An error occurred while handling the client connection:
{
e
}
"
)
finally
:
client_socket
.
close
()
print
(
"
Client disconnected.
"
)
def
listen_for_connections
():
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
sock
.
bind
((
''
,
LISTEN_PORT
))
while
True
:
try
:
try
:
data
,
addr
=
sock
.
recvfrom
(
1024
)
while
not
self
.
stop_event
.
is_set
():
if
data
.
decode
()
==
"
CONNECT
"
:
client_socket
.
settimeout
(
1
)
print
(
"
Received connection request from {}
"
.
format
(
addr
))
try
:
request
=
client_socket
.
recv
(
1024
)
except
socket
.
timeout
:
continue
except
OSError
as
e
:
print
(
f
"
Client socket error:
{
e
}
"
)
break
if
not
request
:
print
(
"
No request received, closing connection.
"
)
break
message
=
request
.
decode
()
print
(
"
Received from client: {}
"
.
format
(
message
))
parts
=
message
.
split
(
"
"
,
1
)
message_type
=
parts
[
0
]
message_data
=
parts
[
1
]
if
len
(
parts
)
>
1
else
None
if
message_type
in
self
.
message_handlers
:
if
message_data
:
self
.
message_handlers
[
message_type
](
client_socket
,
message_data
)
else
:
self
.
message_handlers
[
message_type
](
client_socket
)
else
:
self
.
handle_unknown_message
(
client_socket
)
with
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
as
tcp_socket
:
except
ConnectionResetError
:
tcp_socket
.
bind
((
''
,
5006
))
print
(
"
Client connection was forcibly closed.
"
)
tcp_socket
.
listen
(
1
)
except
Exception
as
e
:
print
(
f
"
An error occurred while handling the client connection:
{
e
}
"
)
finally
:
client_socket
.
close
()
print
(
"
Client disconnected.
"
)
def
listen_for_connections
(
self
):
self
.
udp_sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
self
.
udp_sock
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
self
.
udp_sock
.
bind
((
''
,
self
.
LISTEN_PORT
))
while
not
self
.
stop_event
.
is_set
():
try
:
self
.
udp_sock
.
settimeout
(
1
)
# Timeout for blocking recvfrom call
data
,
addr
=
self
.
udp_sock
.
recvfrom
(
1024
)
if
data
.
decode
()
==
"
CONNECT
"
:
print
(
"
Received connection request from {}
"
.
format
(
addr
))
self
.
tcp_socket
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
tcp_socket
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
self
.
tcp_socket
.
bind
((
''
,
self
.
LISTEN_PORT
))
self
.
tcp_socket
.
listen
(
1
)
print
(
"
Listening for TCP connection...
"
)
print
(
"
Listening for TCP connection...
"
)
client_socket
,
client_addr
=
tcp_socket
.
accept
()
client_socket
,
client_addr
=
self
.
tcp_socket
.
accept
()
print
(
"
Client connected from {}
"
.
format
(
client_addr
))
print
(
"
Client connected from {}
"
.
format
(
client_addr
))
threading
.
Thread
(
target
=
handle_client_connection
,
args
=
(
client_socket
,)).
start
()
threading
.
Thread
(
target
=
self
.
handle_client_connection
,
args
=
(
client_socket
,)).
start
()
except
Exception
as
e
:
except
socket
.
timeout
:
print
(
f
"
An error occurred while listening for connections:
{
e
}
"
)
continue
except
OSError
as
e
:
# Start broadcasting and listening threads
print
(
f
"
UDP socket no longer open (likely due to stop event):
"
)
broadcast_thread
=
threading
.
Thread
(
target
=
broadcast_presence
)
break
listen_thread
=
threading
.
Thread
(
target
=
listen_for_connections
)
except
Exception
as
e
:
print
(
f
"
An error occurred while listening for connections:
{
e
}
"
)
broadcast_thread
.
start
()
listen_thread
.
start
()
# Close sockets if stop event is set
if
self
.
udp_sock
:
broadcast_thread
.
join
()
self
.
udp_sock
.
close
()
listen_thread
.
join
()
if
self
.
tcp_socket
:
self
.
tcp_socket
.
close
()
print
(
"
Listening stopped.
"
)
def
start
(
self
):
self
.
broadcast_thread
=
threading
.
Thread
(
target
=
self
.
broadcast_presence
)
self
.
listen_thread
=
threading
.
Thread
(
target
=
self
.
listen_for_connections
)
self
.
broadcast_thread
.
start
()
self
.
listen_thread
.
start
()
def
stop
(
self
):
print
(
"
Stopping server...
"
)
self
.
stop_event
.
set
()
# Safely close the sockets to prevent further operations on closed sockets
if
self
.
udp_sock
:
self
.
udp_sock
.
close
()
if
self
.
tcp_socket
:
self
.
tcp_socket
.
close
()
self
.
broadcast_thread
.
join
()
self
.
listen_thread
.
join
()
print
(
"
Server stopped.
"
)
# Instantiate and start the handler
if
__name__
==
"
__main__
"
:
robobin_handler
=
RoboBinConnectionHandler
()
robobin_handler
.
start
()
print
(
"
Server started. Type
'
stop
'
to shut down.
"
)
# Main loop to accept CLI input
while
True
:
command
=
input
(
"
Enter command:
"
)
if
command
.
strip
().
lower
()
==
"
stop
"
:
robobin_handler
.
stop
()
elif
command
.
strip
().
lower
()
==
"
status
"
:
print
(
"
Server is running.
"
)
#printstatus() # Funciton will be added later
break
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