diff options
Diffstat (limited to 'mpm/python/usrp_mpm')
-rw-r--r-- | mpm/python/usrp_mpm/discovery.py | 27 | ||||
-rw-r--r-- | mpm/python/usrp_mpm/mpmtypes.py | 20 | ||||
-rw-r--r-- | mpm/python/usrp_mpm/rpc_server.py | 38 |
3 files changed, 48 insertions, 37 deletions
diff --git a/mpm/python/usrp_mpm/discovery.py b/mpm/python/usrp_mpm/discovery.py index 7a133af15..8c58fa682 100644 --- a/mpm/python/usrp_mpm/discovery.py +++ b/mpm/python/usrp_mpm/discovery.py @@ -11,13 +11,13 @@ from __future__ import print_function from multiprocessing import Process import socket from builtins import bytes -from six import iteritems from usrp_mpm.mpmtypes import MPM_DISCOVERY_PORT from usrp_mpm.mpmlog import get_main_logger +from usrp_mpm.mpmutils import to_binary_str -RESPONSE_PREAMBLE = "USRP-MPM" -RESPONSE_SEP = ";" -RESPONSE_CLAIMED_KEY = "claimed" +RESPONSE_PREAMBLE = b"USRP-MPM" +RESPONSE_SEP = b";" +RESPONSE_CLAIMED_KEY = b"claimed" # "Max MTU" is not a redundant name. We don't know the total path MTU, but we # can say for sure that it won't exceed a certain value, and that's the max MTU MAX_MTU = 8000 @@ -25,7 +25,7 @@ MAX_MTU = 8000 IP_MTU_DISCOVER = 10 IP_PMTUDISC_DO = 2 -def spawn_discovery_process(device_info, shared_state, discovery_addr): +def spawn_discovery_process(shared_state, discovery_addr): """ Returns a process that contains the device discovery. @@ -38,25 +38,27 @@ def spawn_discovery_process(device_info, shared_state, discovery_addr): """ proc = Process( target=_discovery_process, - args=(device_info, shared_state, discovery_addr) + args=(shared_state, discovery_addr) ) proc.start() return proc -def _discovery_process(device_info, state, discovery_addr): +def _discovery_process(state, discovery_addr): """ The actual process for device discovery. Is spawned by spawn_discovery_process(). """ - def create_response_string(): + log = get_main_logger().getChild('discovery') + def create_response_string(state): " Generate the string that gets sent back to the requester. " return RESPONSE_SEP.join( [RESPONSE_PREAMBLE] + \ - ["{k}={v}".format(k=k, v=v) for k, v in iteritems(device_info)] + \ - ["{k}={v}".format(k=RESPONSE_CLAIMED_KEY, v=state.claim_status.value)] + [b"type="+state.dev_type.value] + \ + [b"product="+state.dev_product.value] + \ + [b"serial="+state.dev_serial.value] + \ + [RESPONSE_CLAIMED_KEY+to_binary_str("={}".format(state.claim_status.value))] ) - log = get_main_logger().getChild('discovery') sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # FIXME really, we should only bind to the subnet but I haven't gotten that @@ -80,7 +82,8 @@ def _discovery_process(device_info, state, discovery_addr): if data.strip(b"\0") == b"MPM-DISC": log.info("Sending discovery response to %s port: %d", sender[0], sender[1]) - send_data = bytes(create_response_string(), 'ascii') + resp_str = create_response_string(state) + send_data = resp_str log.info(send_data) send_sock.sendto(send_data, sender) elif data.strip(b"\0").startswith(b"MPM-ECHO"): diff --git a/mpm/python/usrp_mpm/mpmtypes.py b/mpm/python/usrp_mpm/mpmtypes.py index 878149faf..07e058a62 100644 --- a/mpm/python/usrp_mpm/mpmtypes.py +++ b/mpm/python/usrp_mpm/mpmtypes.py @@ -15,25 +15,20 @@ from builtins import object MPM_RPC_PORT = 49601 MPM_DISCOVERY_PORT = 49600 - MPM_DISCOVERY_MESSAGE = "MPM-DISC" - class SharedState(object): """ - Holds information which should be shared between processes - Usage should be kept to a minimum + Holds information which should be shared between processes. """ - def __init__(self): self.lock = RLock() - self.claim_status = Value( - ctypes.c_bool, - False, lock=self.lock) # lock - self.claim_token = Array( - ctypes.c_char, 256, - lock=self.lock) # String with max length of 256 - + self.claim_status = Value(ctypes.c_bool, False, lock=self.lock) + # String with max length of 256 + self.claim_token = Array(ctypes.c_char, 256, lock=self.lock) + self.dev_type = Array(ctypes.c_char, 16, lock=self.lock) + self.dev_serial = Array(ctypes.c_char, 8, lock=self.lock) + self.dev_product = Array(ctypes.c_char, 16, lock=self.lock) class SID(object): """ @@ -56,7 +51,6 @@ class SID(object): self.dst_addr, self.dst_ep = \ [int(x, 10) for x in dst.split('.', 2)] else: - print(sid) self.src_addr = sid >> 24 self.src_ep = (sid >> 16) & 0xFF self.dst_addr = (sid >> 8) & 0xFF diff --git a/mpm/python/usrp_mpm/rpc_server.py b/mpm/python/usrp_mpm/rpc_server.py index 076103aa9..2310dd814 100644 --- a/mpm/python/usrp_mpm/rpc_server.py +++ b/mpm/python/usrp_mpm/rpc_server.py @@ -49,24 +49,37 @@ class MPMServer(RPCServer): # Compatibility number for MPM MPM_COMPAT_NUM = (1, 1) - def __init__(self, state, mgr, mgr_generator=None, *args, **kwargs): + def __init__(self, state, default_args): self.log = get_main_logger().getChild('RPCServer') self._state = state self._timer = Greenlet() self.session_id = None - self.periph_manager = mgr - self._mgr_generator = mgr_generator + # Create the periph_manager for this device + # This call will be forwarded to the device specific implementation + # e.g. in periph_manager/n310.py + # Which implementation is called will be determined during + # configuration with cmake (-DMPM_DEVICE). + # mgr is thus derived from PeriphManagerBase + # (see periph_manager/base.py) + from usrp_mpm.periph_manager import periph_manager + self._mgr_generator = lambda: periph_manager(default_args) + self.periph_manager = self._mgr_generator() + device_info = self.periph_manager.get_device_info() + self._state.dev_type.value = \ + to_binary_str(device_info.get("type", "n/a")) + self._state.dev_product.value = \ + to_binary_str(device_info.get("product", "n/a")) + self._state.dev_serial.value = \ + to_binary_str(device_info.get("serial", "n/a")) self._db_methods = [] self._mb_methods = [] self.claimed_methods = copy.copy(self.default_claimed_methods) self._last_error = "" - self._init_rpc_calls(mgr) + self._init_rpc_calls(self.periph_manager) # We call the server __init__ function here, and not earlier, because # first the commands need to be registered super(MPMServer, self).__init__( - *args, pack_params={'use_bin_type': True}, - **kwargs ) def _init_rpc_calls(self, mgr): @@ -434,14 +447,14 @@ class MPMServer(RPCServer): ] -def _rpc_server_process(shared_state, port, mgr, mgr_generator): +def _rpc_server_process(shared_state, port, default_args): """ This is the actual process that's running the RPC server. """ connections = Pool(1000) server = StreamServer( ('0.0.0.0', port), - handle=MPMServer(shared_state, mgr, mgr_generator), + handle=MPMServer(shared_state, default_args), spawn=connections) # catch signals and stop the stream server signal(signal.SIGTERM, lambda *args: server.stop()) @@ -449,12 +462,13 @@ def _rpc_server_process(shared_state, port, mgr, mgr_generator): server.serve_forever() -def spawn_rpc_process(state, udp_port, mgr, mgr_generator): +def spawn_rpc_process(state, udp_port, default_args): """ Returns a process that contains the RPC server """ - - proc_args = [udp_port, state, mgr, mgr_generator] - proc = Process(target=_rpc_server_process, args=proc_args) + proc = Process( + target=_rpc_server_process, + args=[udp_port, state, default_args], + ) proc.start() return proc |