Skip to content

Commit

Permalink
refactor<coms> remove unused functions and logger init
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahelsamahy committed Sep 19, 2024
1 parent ce0a205 commit 98f7fd8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 66 deletions.
10 changes: 5 additions & 5 deletions coms/coms/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import signal
import threading
from byodr.utils.ssh import Router
from .communication import CommunicationHandler
from .communication import SegmentsCooperationCommunicationHandler
from .common_utils import *
from .robot_comm import *

Expand Down Expand Up @@ -33,12 +33,12 @@ def main():
args = parser.parse_args()

application = ComsApplication(event=quit_event, config_dir=args.config)
tel_chatter = TeleopChatter(application.get_robot_config_file(), application.get_user_config_file())
socket_manager = SocketManager(tel_chatter, quit_event=quit_event)
communication_handler = CommunicationHandler(quit_event)
tel_chatter = TeleopChatter(application.get_robot_config_file())
socket_manager = ServicesSocketManager(tel_chatter, quit_event=quit_event)
communication_handler = SegmentsCooperationCommunicationHandler(quit_event, application.get_robot_config_file())

# Starting the functions that will allow the client and server of each segment to start sending and receiving data
communication_thread = threading.Thread(target=communication_handler.start_communication, args=(socket_manager, application.get_robot_config_file()))
communication_thread = threading.Thread(target=communication_handler.start_communication, args=(socket_manager))

application.setup()
socket_manager.start_threads()
Expand Down
2 changes: 0 additions & 2 deletions coms/coms/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

# Declaring the logger
logger = logging.getLogger(__name__)
log_format = "%(levelname)s: %(filename)s %(funcName)s %(message)s"


class SegmentClient:
"""Encapsulate the client functionalities of the segment.
Expand Down
14 changes: 4 additions & 10 deletions coms/coms/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def setup(self):
self._config_hash = _hash


class SocketManager:
class ServicesSocketManager:
def __init__(self, teleop_chatter, quit_event):
self.quit_event = quit_event
self.tel_chatter_actions = teleop_chatter
Expand Down Expand Up @@ -114,10 +114,8 @@ 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
self.robot_actions = RobotActions(self.robot_config_dir)
def __init__(self, _robot_config_dir):
self.robot_actions = RobotActions(_robot_config_dir)

def filter_robot_config(self, tel_data):
"""Get new robot_config from TEL chatter socket
Expand All @@ -128,9 +126,5 @@ def filter_robot_config(self, tel_data):
# Check if tel_data is not None and then check for existence of 'robot_config'
if tel_data and "robot_config" in tel_data.get("command", {}):
new_robot_config = tel_data["command"]["robot_config"]
logger.info(new_robot_config)
# logger.info(new_robot_config)
self.robot_actions.driver(new_robot_config)

def filter_watch_dog(self):
"""place holder for watchdog function"""
pass
72 changes: 27 additions & 45 deletions coms/coms/communication.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,31 @@
import configparser
import copy
import logging
import threading
from collections import deque
import socket
import copy
import threading
import time
import multiprocessing
import signal
import configparser
from byodr.utils.ssh import Nano
from collections import deque

from byodr.utils import timestamp
from .server import SegmentServer
from byodr.utils.ssh import Nano

from .client import SegmentClient
from .command_processor import process


# This flag starts as false
quit_event = multiprocessing.Event()
quit_event.clear()

signal.signal(signal.SIGINT, lambda sig, frame: _interrupt())
signal.signal(signal.SIGTERM, lambda sig, frame: _interrupt())


# Set the flag as true when we receive interrupt signals
def _interrupt():
logger.info("Received interrupt, quitting.")
quit_event.set()

from .server import SegmentServer

logger = logging.getLogger(__name__)


class CommunicationHandler:
def __init__(self, quit_event):
class SegmentsCooperationCommunicationHandler:
def __init__(self, quit_event, robot_config_dir):
self.global_watchdog_status_list = []
self.robot_config_parser = configparser.ConfigParser()
self.quit_event = quit_event
self.local_ip = Nano.get_ip_address()
self.nano_port = 1111
self.head_ip, self.follower_ip = self.read_config_file(robot_config_dir)

def read_config_file(self, config_file_dir, local_ip):
def read_config_file(self, config_file_dir):
follower_ip = None
head_ip = None

Expand All @@ -47,7 +35,7 @@ def read_config_file(self, config_file_dir, local_ip):

# Getting our local position
for entry in sections_list[1:]:
if self.robot_config_parser.get(entry, "ip.number") == local_ip:
if self.robot_config_parser.get(entry, "ip.number") == self.local_ip:
local_position = self.robot_config_parser.get(entry, "position")
break

Expand All @@ -66,7 +54,7 @@ def read_config_file(self, config_file_dir, local_ip):

return head_ip, follower_ip

def start_communication(self, socket_manager, robot_config_dir):
def start_communication(self, socket_manager):
"""Use:
The main function that starts all other communication functionalities.
Starts 3 threads:\n
Expand All @@ -81,17 +69,11 @@ def start_communication(self, socket_manager, robot_config_dir):
# A deque that will store messages received by the server of the segment
msg_from_server_queue = deque(maxlen=1)

# Getting the IP of the local machine
local_ip = Nano.get_ip_address()
nano_port = 1111

# Reading the config files to receive information about this and neighboring segments
head_ip, follower_ip = self.read_config_file(robot_config_dir, local_ip)
segment_client = SegmentClient(self.follower_ip, self.nano_port, 0.10) # The client that will connect to a follower
segment_server = SegmentServer(self.local_ip, self.nano_port, 0.10) # The server that will wait for the lead to connect

segment_client = SegmentClient(follower_ip, nano_port, 0.10) # The client that will connect to a follower
segment_server = SegmentServer(local_ip, nano_port, 0.10) # The server that will wait for the lead to connect

command_receiver_thread = threading.Thread(target=self.command_receiver, args=(socket_manager, segment_client, local_ip, head_ip, msg_from_server_queue))
# I need to implement some sort of HZ here on these functions
command_receiver_thread = threading.Thread(target=self.command_receiver, args=(socket_manager, segment_client, self.local_ip, self.head_ip, msg_from_server_queue))
client_interface_thread = threading.Thread(target=self.client_code, args=(socket_manager, segment_client))
server_interface_thread = threading.Thread(target=self.server_code, args=(segment_server, msg_from_server_queue))

Expand All @@ -117,6 +99,7 @@ def command_receiver(self, socket_manager, segment_client, local_ip, head_ip, ms
-If any other segment calls this function, it will receive commands from the deque.
This deque is being filled up by the server thread, with commands received from its LD segment,
"""
# that is a blank command to start with
previous_command = {"steering": 0.0, "throttle": 0, "time": 0, "navigator": {"route": None}, "button_b": 1, "velocity": 0.0}

# The head segment forwards commands from teleop to pilot
Expand All @@ -131,10 +114,10 @@ def command_receiver(self, socket_manager, segment_client, local_ip, head_ip, ms
while not self.quit_event.is_set():

# Getting the local watchdog status
status_dictionary = socket_manager.get_watchdog_status()
status_dictionary = socket_manager.get_watchdog_status() # I think this line i useless
self.global_watchdog_status_list[0] = status_dictionary.get("status")

# Receiving the commands that we will forward to our FL, from Teleop
# Receiving the commands that we will forward to our FL, from our Teleop
segment_client.msg_to_server = socket_manager.get_teleop_input()
if segment_client.msg_to_server is not None:
segment_client.msg_to_server["time"] = timestamp()
Expand Down Expand Up @@ -247,12 +230,11 @@ def client_code(self, socket_manager, segment_client):

def server_code(self, segment_server, msg_from_server_queue):
"""Use:
The main functionality of the server of the segment. It will setup a socket server and will wait for a client to connect.
Once a client connects, the server will wait for data, and when it is received, will send back a reply.
The data received will be appended to a deque. The command receiver thread consumes items from the deque by forwarding commands to the local pilot.
The main functionality of the server of the segment.
Returns:
Nothing
It will setup a socket server and will wait for a client to connect.
Once a client connects, the server will wait for data, and when it is received, will send back a reply.
The data received will be appended to a deque. The command_receiver thread consumes items from the deque by forwarding commands to the local pilot.
"""

counter_server = 0
Expand Down
3 changes: 1 addition & 2 deletions coms/coms/robot_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ def __init__(self, listening_ip, event, robot_config_dir, sub_port=5454, req_por
self._sub_port = sub_port
self._req_port = req_port
self._quit_event = event
self.robot_config_dir = robot_config_dir
self._robot_actions = RobotActions(self.robot_config_dir)
self._robot_actions = RobotActions(robot_config_dir)

self.context = zmq.Context()
self.req_socket = self.context.socket(zmq.REQ)
Expand Down
2 changes: 0 additions & 2 deletions coms/coms/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

# Declaring the logger
logger = logging.getLogger(__name__)
log_format = "%(levelname)s: %(filename)s %(funcName)s %(message)s"


class SegmentServer:
"""Encapsulate the server functionalities of the segment.
Expand Down

0 comments on commit 98f7fd8

Please sign in to comment.