Skip to content

Commit

Permalink
chore(mempool_infra): add documentation to remote component server
Browse files Browse the repository at this point in the history
  • Loading branch information
uriel-starkware committed Aug 18, 2024
1 parent c35d005 commit 2bf830c
Showing 1 changed file with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,89 @@ use crate::component_definitions::{
APPLICATION_OCTET_STREAM,
};

/// The `RemoteComponentServer` struct is a generic server that handles requests and responses for a
/// specified component. It receives requests, processes them using the provided component, and
/// sends back responses. The server needs to be started using the `start` function, which runs
/// indefinitely.
///
/// # Type Parameters
///
/// - `Component`: The type of the component that will handle the requests. This type must implement
/// the `ComponentRequestHandler` trait, which defines how the component processes requests and
/// generates responses.
/// - `Request`: The type of requests that the component will handle. This type must implement the
/// `serde::de::DeserializeOwned` (e.g. by using #[derive(Deserialize)]) trait.
/// - `Response`: The type of responses that the component will generate. This type must implement
/// the `Serialize` trait.
///
/// # Fields
///
/// - `component`: The component responsible for handling the requests and generating responses.
/// - `socket`: A socket address for the server to listen on.
///
/// # Example
/// ```rust
/// // Example usage of the RemoteComponentServer
/// use async_trait::async_trait;
/// use serde::{Deserialize, Serialize};
/// use starknet_mempool_infra::component_runner::{ComponentStartError, ComponentStarter};
/// use tokio::task;
///
/// use crate::starknet_mempool_infra::component_definitions::ComponentRequestHandler;
/// use crate::starknet_mempool_infra::component_server::{
/// ComponentServerStarter,
/// RemoteComponentServer,
/// };
///
/// // Define your component
/// struct MyComponent {}
///
/// #[async_trait]
/// impl ComponentStarter for MyComponent {
/// async fn start(&mut self) -> Result<(), ComponentStartError> {
/// Ok(())
/// }
/// }
///
/// // Define your request and response types
/// #[derive(Deserialize)]
/// struct MyRequest {
/// pub content: String,
/// }
///
/// #[derive(Serialize)]
/// struct MyResponse {
/// pub content: String,
/// }
///
/// // Define your request processing logic
/// #[async_trait]
/// impl ComponentRequestHandler<MyRequest, MyResponse> for MyComponent {
/// async fn handle_request(&mut self, request: MyRequest) -> MyResponse {
/// MyResponse { content: request.content.clone() + " processed" }
/// }
/// }
///
/// #[tokio::main]
/// async fn main() {
/// // Instantiate the component.
/// let component = MyComponent {};
///
/// // Set the ip address and port of the server's socket.
/// let ip_address = std::net::IpAddr::V6(std::net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
/// let port: u16 = 8080;
///
/// // Instantiate the server.
/// let mut server = RemoteComponentServer::<MyComponent, MyRequest, MyResponse>::new(
/// component, ip_address, port,
/// );
///
/// // Start the server in a new task.
/// task::spawn(async move {
/// server.start().await;
/// });
/// }
/// ```
pub struct RemoteComponentServer<Component, Request, Response>
where
Component: ComponentRequestHandler<Request, Response> + Send + 'static,
Expand Down

0 comments on commit 2bf830c

Please sign in to comment.