pub struct Client<T> {
pub id: usize,
pub num_nodes: usize,
pub address: SocketAddr,
/* private fields */
}Expand description
Represents a Client node in a distributed system that is generic for
type T, where T is the types of messages that can be sent between
Clients. Allows directed communication to any other node that shares the
Client’s client_type, which enables increased concurrency due to
decreased lock contention.
Fields§
§id: usizeThe id of this Client, assigned by the Server on startup
and is monotonically increasing based on the order of connections
num_nodes: usizeThe number of Clients in the network
address: SocketAddrThe address of this Client
Implementations§
Source§impl<RT: Send + Sync + DeserializeOwned + Serialize + Clone + 'static> Client<RT>
impl<RT: Send + Sync + DeserializeOwned + Serialize + Clone + 'static> Client<RT>
Sourcepub async fn new(
server_addr: String,
my_ip: String,
my_port: Option<String>,
num_nodes: usize,
network_name: String,
) -> Result<(Arc<Mutex<Self>>, SelectAll<FramedRead<ReadHalf<TcpStream>, MessageCodec<RT>>>, Arc<Notify>), LiquidError>
pub async fn new( server_addr: String, my_ip: String, my_port: Option<String>, num_nodes: usize, network_name: String, ) -> Result<(Arc<Mutex<Self>>, SelectAll<FramedRead<ReadHalf<TcpStream>, MessageCodec<RT>>>, Arc<Notify>), LiquidError>
Create a new Client and connect to all other nodes in the network
with the given network_name. If you wish to create multiple networks
and preserve the node_ids assigned by the Server, you should
check out the [register_network] method
§Parameters
server_addr: The address of theServerinIP:Portformatmy_ip: TheIPof thisClientmy_port: An optional port for thisClientto listen for new connections. If itsNone, uses the OS to randomly assign a port.num_nodes: The number of nodes in the network.network_name: The name of the network to connect with, will only connect with otherClients with the samenetwork_name
§Returned Values
This function returns a tuple of three things, the first element is the
Client, which can then be used to send messages to any other node
with the send_msg method. The second element is a struct that
implements StreamExt and combines the streams of all messages from
all other nodes (unordered), which you can use to easily process
messages like this:
while let Some(Ok(msg)) = streams.next().await {
// ... process the message here according to your use case
}The third element is a notifier that will only be notified when the
Server sends a ControlMsg::Kill message to the Client.
Sourcepub async fn register_network<T: Send + Sync + DeserializeOwned + Serialize + Clone + 'static>(
parent: Arc<Mutex<Self>>,
network_name: String,
) -> Result<(Arc<Mutex<Client<T>>>, SelectAll<FramedRead<ReadHalf<TcpStream>, MessageCodec<T>>>, Arc<Notify>), LiquidError>
pub async fn register_network<T: Send + Sync + DeserializeOwned + Serialize + Clone + 'static>( parent: Arc<Mutex<Self>>, network_name: String, ) -> Result<(Arc<Mutex<Client<T>>>, SelectAll<FramedRead<ReadHalf<TcpStream>, MessageCodec<T>>>, Arc<Notify>), LiquidError>
Given an already connected Client of any type, register a new network
with the given network_name that will create a new network of
Clients that preserve node_ids across all nodes by connecting in
the same order as the parent. The new Client can only talk to
Clients with the same network_name as the new network is
independent of the parent.
The tuple returned is the same as in the Client::new function.
Sourcepub async fn send_msg(
&mut self,
target_id: usize,
message: RT,
) -> Result<(), LiquidError>
pub async fn send_msg( &mut self, target_id: usize, message: RT, ) -> Result<(), LiquidError>
Send the given message to a Client with the given target_id.
Id’s are automatically assigned by a Server during the registration
period based on the order of connections.
Sourcepub async fn broadcast(&mut self, message: RT) -> Result<(), LiquidError>
pub async fn broadcast(&mut self, message: RT) -> Result<(), LiquidError>
Broadcast the given message to all currently connected clients
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Client<T>
impl<T> !RefUnwindSafe for Client<T>
impl<T> Send for Client<T>where
T: Send,
impl<T> Sync for Client<T>where
T: Sync,
impl<T> Unpin for Client<T>
impl<T> !UnwindSafe for Client<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more