Skip to content

Commit

Permalink
refactor<coms> indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahelsamahy committed Sep 19, 2024
1 parent d8c35a7 commit be6c9f6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions coms/coms/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def join_threads(self):

class TeleopChatter:
"""Resolve the data incoming from Teleop chatter socket"""

def __init__(self, _robot_config_dir, _segment_config_dir):
self.robot_config_dir = _robot_config_dir
self.seg_config_dir = _segment_config_dir
Expand Down
40 changes: 18 additions & 22 deletions coms/coms/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import json
from byodr.utils.ssh import Nano

# import signal
# signal.signal(signal.SIGPIPE,signal.SIG_DFL)

Expand All @@ -13,30 +14,29 @@
log_format = "%(levelname)s: %(filename)s %(funcName)s %(message)s"



class Segment_server():
class Segment_server:
"""Class that encapsulates the server functionalities of the segment.
Methods:
- start_server(): The server starts listening for clients
- send_to_LD(): The server sends a reply to its LD
- recv_from_LD(): The server receives data from its LD
Methods:
- start_server(): The server starts listening for clients
- send_to_LD(): The server sends a reply to its LD
- recv_from_LD(): The server receives data from its LD
Args:
arg_server_ip (Str): IP of the server -> '192.168.1.100'
arg_server_port (Int): Port that the server uses -> '1111'
arg_timeout (Float): Server socket timeout -> '0.1' (In seconds)
"""
Args:
arg_server_ip (Str): IP of the server -> '192.168.1.100'
arg_server_port (Int): Port that the server uses -> '1111'
arg_timeout (Float): Server socket timeout -> '0.1' (In seconds)
"""

# Method that is called after the class is being initiated, to give it its values
def __init__(self, arg_server_ip, arg_server_port, arg_timeout):
# Giving the class the values from the class call
self.server_ip = arg_server_ip
self.server_port = arg_server_port
self.timeout = arg_timeout # Maybe 100ms
self.msg_to_client = {'rep': '-'}
self.msg_from_client = {'msg': '-'}
self.timeout = arg_timeout # Maybe 100ms
self.msg_to_client = {"rep": "-"}
self.msg_from_client = {"msg": "-"}
self.prev_msg = None
self.processed_command = {'cmd': '-'}
self.processed_command = {"cmd": "-"}

# The server socket that will wait for clients to connect
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Expand All @@ -47,16 +47,15 @@ def __init__(self, arg_server_ip, arg_server_port, arg_timeout):
# Variables that will store the data of the client socket when/if the client connects
self.client_socket = None
self.client_address = None


# Starting the server
def start_server(self):
while True:
try:
logger.info(f"[Server] Server is listening on {(self.server_ip, self.server_port)}")
self.client_socket, self.client_address = self.server_socket.accept() # Waiting for clients to connect. Blocking function
self.client_socket, self.client_address = self.server_socket.accept() # Waiting for clients to connect. Blocking function
logger.info(f"[Server] New client {self.client_address} connected.")
self.client_socket.settimeout(self.timeout) # We set the timeout that the server will wait for data from the client
self.client_socket.settimeout(self.timeout) # We set the timeout that the server will wait for data from the client

# Starting actions when a client connects.
# We break from this loop so that the code can move on to different function calls
Expand All @@ -67,13 +66,11 @@ def start_server(self):
logger.exception("[Server] Exception details:")
pass


# Sending to the client
def send_to_LD(self):
message_to_send = json.dumps(self.msg_to_client).encode('utf-8')
message_to_send = json.dumps(self.msg_to_client).encode("utf-8")
self.client_socket.send(message_to_send)


# Receiving from the client
def recv_from_LD(self):
recv_message = self.client_socket.recv(512)
Expand All @@ -84,4 +81,3 @@ def recv_from_LD(self):
except json.JSONDecodeError as e:
logger.error(f"[Server] Error while decoding JSON from client: {e}")
self.msg_from_client = self.prev_msg

0 comments on commit be6c9f6

Please sign in to comment.