Пример #1
0
 def __init__(self,
              server_address=DEFAULT_SERVER_ADDRESS,
              server_port=DEFAULT_SERVER_PORT,
              rpc_timeout_sec=None):
     channel = grpc.insecure_channel(f'{server_address}:{server_port}')
     self._stub = DeviceReportStub(channel)
     self._active_requests = []
     self._rpc_timeout_sec = (rpc_timeout_sec if rpc_timeout_sec is not None
                              else DEFAULT_RPC_TIMEOUT_SEC)
Пример #2
0
class DeviceReportClient:
    """gRPC client to send device results"""
    def __init__(self,
                 server_address=DEFAULT_SERVER_ADDRESS,
                 server_port=DEFAULT_SERVER_PORT,
                 rpc_timeout_sec=None):
        channel = grpc.insecure_channel(f'{server_address}:{server_port}')
        self._stub = DeviceReportStub(channel)
        self._active_requests = []
        self._rpc_timeout_sec = (rpc_timeout_sec if rpc_timeout_sec is not None
                                 else DEFAULT_RPC_TIMEOUT_SEC)

    def start(self):
        """Start the client"""

    def send_device_result(self, mac, device_result):
        """Send device result of a device to server"""
        devices_state = {
            'device_mac_behaviors': {
                mac: {
                    'port_behavior': device_result
                }
            }
        }
        self._stub.ReportDevicesState(dict_proto(devices_state, DevicesState),
                                      timeout=self._rpc_timeout_sec)

    def _port_event_handler(self, callback, result_generator, cancel_request):
        for port_event in result_generator:
            callback(port_event)
        self._active_requests.remove(cancel_request)

    def connect(self, mac, callback):
        """Connect to remote and start streaming port events back"""
        device = {"mac": mac}
        result_generator = self._stub.GetPortState(dict_proto(device, Device))

        def cancel_request():
            result_generator.cancel()

        self._active_requests.append(cancel_request)
        threading.Thread(target=self._port_event_handler,
                         args=(callback, result_generator,
                               cancel_request)).start()

    def stop(self):
        """Terminates all onging grpc calls"""
        for handler in self._active_requests:
            handler()

    def send_device_heartbeats(self):
        """Null function for sending device heartbeats"""
Пример #3
0
class DeviceReportClient:
    """gRPC client to send device result"""
    def __init__(self,
                 server_address=_SERVER_ADDRESS_DEFAULT,
                 server_port=_SERVER_PORT_DEFAULT,
                 rpc_timeout_sec=_DEFAULT_RPC_TIMEOUT_SEC):
        self._initialize_stub(server_address, server_port)
        self._active_requests = []
        self._rpc_timeout_sec = rpc_timeout_sec

    def _initialize_stub(self, sever_address, server_port):
        channel = grpc.insecure_channel(f'{sever_address}:{server_port}')
        self._stub = DeviceReportStub(channel)

    def send_device_result(self, mac, device_result):
        """Send device result of a device to server"""
        devices_state = {
            'device_mac_behaviors': {
                mac: {
                    'port_behavior': device_result
                }
            }
        }
        self._stub.ReportDevicesState(dict_proto(devices_state, DevicesState),
                                      timeout=self._rpc_timeout_sec)

    def _port_event_handler(self, mac, callback):
        device = {"mac": mac}
        result_generator = self._stub.GetPortState(dict_proto(device, Device))

        def cancel_request():
            result_generator.cancel()

        self._active_requests.append(cancel_request)
        for port_event in result_generator:
            callback(port_event)
        self._active_requests.remove(cancel_request)

    def get_port_events(self, mac, callback):
        """Gets remote port events"""
        threading.Thread(target=self._port_event_handler,
                         args=(mac, callback)).start()

    def terminate(self):
        """Terminates all onging grpc calls"""
        for handler in self._active_requests:
            handler()
Пример #4
0
    def setUp(self):
        """setup fixture for each test method"""
        channel = grpc.insecure_channel(f'{self.SERVER_ADDRESS}:{self.SERVER_PORT}')
        self._client = DeviceReportStub(channel)

        self._server = DeviceReportServer(
            self._process_devices_state, self.SERVER_ADDRESS, self.SERVER_PORT)
        self._server.start()
Пример #5
0
 def _initialize_stub(self, sever_address, server_port):
     channel = grpc.insecure_channel(f'{sever_address}:{server_port}')
     self._stub = DeviceReportStub(channel)