Client

Struct Client 

Source
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: usize

The id of this Client, assigned by the Server on startup and is monotonically increasing based on the order of connections

§num_nodes: usize

The number of Clients in the network

§address: SocketAddr

The address of this Client

Implementations§

Source§

impl<RT: Send + Sync + DeserializeOwned + Serialize + Clone + 'static> Client<RT>

Source

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 the Server in IP:Port format
  • my_ip: The IP of this Client
  • my_port: An optional port for this Client to listen for new connections. If its None, 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 other Clients with the same network_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.

Source

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.

Source

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.

Source

pub async fn broadcast(&mut self, message: RT) -> Result<(), LiquidError>

Broadcast the given message to all currently connected clients

Trait Implementations§

Source§

impl<T: Debug> Debug for Client<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V