Server Util

Helper functions and classes for creating and running a gRPC service.

class bosdyn.client.server_util.ResponseContext(response, request, rpc_logger=None, channel_prefix=None, exc_callback=None)[source]

Bases: object

Helper to log gRPC request and response message to the data buffer for a service.

It should be called using a “with” statement each time an RPC is received such that the request and response proto messages can be passed in. It will automatically log the request and response to the data buffer, and mutates the headers to add additional information before logging.

Parameters:
  • response (protobuf) – any gRPC response message with a bosdyn.api.ResponseHeader proto.

  • request (protobuf) – any gRPC request message with a bosdyn.api.RequestHeader proto.

  • rpc_logger (DataBufferClient) – Optional data buffer client to log the messages; if not provided, only the headers will be mutated and nothing will be logged.

  • channel_prefix (string) – the prefix you want this req / resp pair logged under.

  • exc_callback (function) – called with exception type, value, and traceback info if an exception is raised in the body of the “with” statement.

class bosdyn.client.server_util.GrpcServiceRunner(service_servicer, add_servicer_to_server_fn, port=0, max_workers=4, max_send_message_length=None, max_receive_message_length=None, timeout_secs=3, force_sigint_capture=True, logger=None)[source]

Bases: object

A runner to start a gRPC server on a background thread and allow easy cleanup.

Parameters:
  • service_servicer (custom servicer class derived from ServiceServicer) – Servicer that defines server behavior.

  • add_servicer_to_server_fn (function) – Function generated by gRPC compilation that attaches the servicer to the gRPC server.

  • port (int) – The port number the service can be accessed through on the host system. Defaults to 0, which will assign an ephemeral port.

  • max_send_message_length (int) – Max message length (bytes) allowed for messages sent.

  • max_receive_message_length (int) – Max message length (bytes) allowed for messages received.

  • timeout_secs (int) – Number of seconds to wait for a clean server shutdown.

  • force_sigint_capture (bool) – Re-assign the termination signal handlers to default in order to prevent other scripts from blocking a clean exit. Defaults to True.

  • logger (logging.Logger) – Logger to log with.

stop()[source]

Blocks until the gRPC server shuts down.

run_until_interrupt()[source]

Spin the thread until a SIGINT, SIGTERM, or SIGQUIT is received and then shut down cleanly.

bosdyn.client.server_util.populate_response_header(response, request, error_code=1, error_msg=None)[source]

Sets the ResponseHeader header in the response.

Parameters:
  • response (bosdyn.api Response message) – The GRPC response message to be populated.

  • request (bosdyn.api Request message) – The header from the request is added to the response.

  • error_code (header_pb2.CommonError) – The status for the RPC response.

  • error_msg (str) – An optional error message describing a bad header status failure.

Returns:

Mutates the response message’s header to be fully populated.

bosdyn.client.server_util.strip_large_bytes_fields(proto_message)[source]

Removes any large bytes fields from a protobuf message depending on the proto type.

bosdyn.client.server_util.get_bytes_field_allowlist()[source]

Creates set of protos which will have bytes fields removed.

bosdyn.client.server_util.strip_image_response(proto_message)[source]

Removes bytes from the image_pb2.ImageResponse proto.

bosdyn.client.server_util.strip_get_image_response(proto_message)[source]

Removes bytes from the image_pb2.GetImageResponse proto.

bosdyn.client.server_util.strip_local_grid_responses(proto_message)[source]

Removes bytes from the local_grid_pb2.GetLocalGridsResponse proto.

bosdyn.client.server_util.strip_store_image_request(proto_message)[source]

Removes bytes from the data_acquisition_store_pb2.StoreImageRequest proto.

bosdyn.client.server_util.strip_store_data_request(proto_message)[source]

Removes bytes from the data_acquisition_store_pb2.StoreDataRequest proto.

bosdyn.client.server_util.strip_record_signal_tick(proto_message)[source]

Removes bytes from the data_buffer_pb2.RecordSignalTicksRequest proto.

bosdyn.client.server_util.strip_record_data_blob(proto_message)[source]

Removes bytes from the data_buffer_pb2.RecordDataBlobsRequest proto.