예제 #1
0
    def setUp(self) -> None:
        self._protos = python_protos.Library.from_strings(_PROTO)

        self._info = ClientInfo(
            'the_client', object(),
            pw_rpc.Client.from_modules(callback_client.Impl(), [
                pw_rpc.Channel(1, lambda _: None),
                pw_rpc.Channel(2, lambda _: None),
            ], self._protos.modules()))
예제 #2
0
    def __init__(self,
                 device: Any,
                 proto_paths_or_modules: Iterable[python_protos.PathOrModule],
                 output: Callable[[bytes], Any] = write_to_file,
                 channels: Iterable[pw_rpc.Channel] = None,
                 client_impl: pw_rpc.client.ClientImpl = None):
        """Creates an RPC client configured to communicate using HDLC.

        Args:
          device: serial.Serial (or any class that implements read and
          write) for reading/writing data proto_paths_or_modules: paths
          to .proto files or proto modules output: where to write
          "stdout" output from the device
        """
        self.device = device
        self.protos = python_protos.Library.from_paths(proto_paths_or_modules)

        if channels is None:
            channels = [pw_rpc.Channel(1, channel_output(device.write))]

        if client_impl is None:
            client_impl = callback_client.Impl()

        self.client = pw_rpc.Client.from_modules(client_impl, channels,
                                                 self.protos.modules())
        frame_handlers: _FrameHandlers = {
            DEFAULT_ADDRESS: self._handle_rpc_packet,
            STDOUT_ADDRESS: lambda frame: output(frame.data),
        }

        # Start background thread that reads and processes RPC packets.
        threading.Thread(target=read_and_process_data,
                         daemon=True,
                         args=(device, frame_handlers)).start()
예제 #3
0
    def test_set_target_switches_between_clients(self) -> None:
        client_1_channel = self._info.rpc_client.channel(1).channel

        client_2_channel = pw_rpc.Channel(99, lambda _: None)
        info_2 = ClientInfo(
            'other_client', object(),
            pw_rpc.Client.from_modules(callback_client.Impl(),
                                       [client_2_channel],
                                       self._protos.modules()))

        variables = multi_client_terminal_variables(
            clients=[self._info, info_2],
            default_client=self._info.client,
            protos=self._protos)

        # Make sure the RPC service switches from one client to the other.
        self.assertIs(variables['the'].pkg.Service.Unary.channel,
                      client_1_channel)

        variables['set_target'](info_2.client)

        self.assertIs(variables['the'].pkg.Service.Unary.channel,
                      client_2_channel)
예제 #4
0
파일: rpc.py 프로젝트: jbampton/pigweed
def default_channels(write: Callable[[bytes], Any]) -> List[pw_rpc.Channel]:
    return [pw_rpc.Channel(1, channel_output(write))]