Client

Struct Client 

Source
pub struct Client {
    pub client_type: PubSubClient,
    /* private fields */
}
Expand description

Simple pub sub Client

Fields§

§client_type: PubSubClient

Implementations§

Source§

impl Client

Source

pub fn new(client_type: PubSubClient) -> Self

Creates a new instance of Client

use simple_pub_sub::client::{self, PubSubClient, Client};
let client_type = simple_pub_sub::client::PubSubTcpClient {
       server: "localhost".to_string(),
       port: 6480,
       cert: None,
       cert_password: None,
};

// initialize the client.
let mut pub_sub_client = simple_pub_sub::client::Client::new(
    simple_pub_sub::client::PubSubClient::Tcp(client_type)
);
Examples found in repository?
examples/publish_unix.rs (lines 7-9)
2async fn main() -> Result<(), anyhow::Error> {
3    let client_type = simple_pub_sub::client::PubSubUnixClient {
4        path: "/tmp/simple.sock".to_string(),
5    };
6    // initialize the client.
7    let mut client = simple_pub_sub::client::Client::new(
8        simple_pub_sub::client::PubSubClient::Unix(client_type),
9    );
10
11    client.connect().await?;
12    // publish to the given topic.
13    client
14        .publish(
15            "abc".to_string(),
16            "test message".to_string().into_bytes().to_vec(),
17        )
18        .await
19}
More examples
Hide additional examples
examples/publish_tcp.rs (line 11)
2async fn main() -> Result<(), anyhow::Error> {
3    let client_type = simple_pub_sub::client::PubSubTcpClient {
4        server: "localhost".to_string(),
5        port: 6480,
6        cert: None,
7        cert_password: None,
8    };
9    // initialize the client.
10    let mut client =
11        simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
12
13    client.connect().await?;
14    // publish to the given topic.
15    client
16        .publish(
17            "abc".to_string(),
18            "test message".to_string().into_bytes().to_vec(),
19        )
20        .await
21}
examples/subscribe_unix.rs (lines 9-11)
4async fn main() -> Result<(), anyhow::Error> {
5    let client_type = simple_pub_sub::client::PubSubUnixClient {
6        path: "/tmp/simple.sock".to_string(),
7    };
8    // initialize the client.
9    let mut client = simple_pub_sub::client::Client::new(
10        simple_pub_sub::client::PubSubClient::Unix(client_type),
11    );
12
13    // connect the client.
14    client.connect().await?;
15    // subscribe to the given topic.
16    client.subscribe("abc".to_string()).await?;
17
18    loop {
19        select! {
20            msg = client.read_message()=>{
21                match  msg{
22                    Ok(msg)=>{
23                        println!("{}:{:?}", msg.topic, msg.message);
24                    }
25                    Err(e)=>{
26                        println!("Error: {:?}", e);
27                    }
28                }
29            }
30        }
31    }
32}
examples/subscribe_tcp.rs (line 14)
4async fn main() -> Result<(), anyhow::Error> {
5    let client_type = simple_pub_sub::client::PubSubTcpClient {
6        server: "localhost".to_string(),
7        port: 6480,
8        cert: None,
9        cert_password: None,
10    };
11
12    // initialize the client.
13    let mut client =
14        simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
15
16    client.connect().await?;
17    // subscribe to the given topic.
18    client.subscribe("abc".to_string()).await?;
19
20    loop {
21        select! {
22            msg = client.read_message()=>{
23                match  msg{
24                    Ok(msg)=>{
25                        println!("{}:{:?}", msg.topic, msg.message);
26                    }
27                    Err(e)=>{
28                        println!("Error: {:?}", e);
29                    }
30                }
31            }
32        }
33    }
34}
Source

pub async fn connect(&mut self) -> Result<()>

Connects to the server

 use simple_pub_sub::client::{self, PubSubClient, Client};
 let client_type = simple_pub_sub::client::PubSubTcpClient {
        server: "localhost".to_string(),
        port: 6480,
        cert: None,
        cert_password: None,
 };

 // initialize the client.
 let mut pub_sub_client = simple_pub_sub::client::Client::new(
     simple_pub_sub::client::PubSubClient::Tcp(client_type),
 );
 pub_sub_client.connect();
Examples found in repository?
examples/publish_unix.rs (line 11)
2async fn main() -> Result<(), anyhow::Error> {
3    let client_type = simple_pub_sub::client::PubSubUnixClient {
4        path: "/tmp/simple.sock".to_string(),
5    };
6    // initialize the client.
7    let mut client = simple_pub_sub::client::Client::new(
8        simple_pub_sub::client::PubSubClient::Unix(client_type),
9    );
10
11    client.connect().await?;
12    // publish to the given topic.
13    client
14        .publish(
15            "abc".to_string(),
16            "test message".to_string().into_bytes().to_vec(),
17        )
18        .await
19}
More examples
Hide additional examples
examples/publish_tcp.rs (line 13)
2async fn main() -> Result<(), anyhow::Error> {
3    let client_type = simple_pub_sub::client::PubSubTcpClient {
4        server: "localhost".to_string(),
5        port: 6480,
6        cert: None,
7        cert_password: None,
8    };
9    // initialize the client.
10    let mut client =
11        simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
12
13    client.connect().await?;
14    // publish to the given topic.
15    client
16        .publish(
17            "abc".to_string(),
18            "test message".to_string().into_bytes().to_vec(),
19        )
20        .await
21}
examples/subscribe_unix.rs (line 14)
4async fn main() -> Result<(), anyhow::Error> {
5    let client_type = simple_pub_sub::client::PubSubUnixClient {
6        path: "/tmp/simple.sock".to_string(),
7    };
8    // initialize the client.
9    let mut client = simple_pub_sub::client::Client::new(
10        simple_pub_sub::client::PubSubClient::Unix(client_type),
11    );
12
13    // connect the client.
14    client.connect().await?;
15    // subscribe to the given topic.
16    client.subscribe("abc".to_string()).await?;
17
18    loop {
19        select! {
20            msg = client.read_message()=>{
21                match  msg{
22                    Ok(msg)=>{
23                        println!("{}:{:?}", msg.topic, msg.message);
24                    }
25                    Err(e)=>{
26                        println!("Error: {:?}", e);
27                    }
28                }
29            }
30        }
31    }
32}
examples/subscribe_tcp.rs (line 16)
4async fn main() -> Result<(), anyhow::Error> {
5    let client_type = simple_pub_sub::client::PubSubTcpClient {
6        server: "localhost".to_string(),
7        port: 6480,
8        cert: None,
9        cert_password: None,
10    };
11
12    // initialize the client.
13    let mut client =
14        simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
15
16    client.connect().await?;
17    // subscribe to the given topic.
18    client.subscribe("abc".to_string()).await?;
19
20    loop {
21        select! {
22            msg = client.read_message()=>{
23                match  msg{
24                    Ok(msg)=>{
25                        println!("{}:{:?}", msg.topic, msg.message);
26                    }
27                    Err(e)=>{
28                        println!("Error: {:?}", e);
29                    }
30                }
31            }
32        }
33    }
34}
Source

pub async fn post(&mut self, msg: Msg) -> Result<Vec<u8>>

Sends the message to the given server and returns the ack the server could be either a tcp or unix server

 use simple_pub_sub::client::{PubSubClient, Client};
 use simple_pub_sub::message::Msg;
 use simple_pub_sub::PktType;
 async fn publish_msg(){
   let client_type = simple_pub_sub::client::PubSubTcpClient {
          server: "localhost".to_string(),
          port: 6480,
          cert: None,
          cert_password: None,
   };

 // initialize the client.
 let mut pub_sub_client = simple_pub_sub::client::Client::new(
     simple_pub_sub::client::PubSubClient::Tcp(client_type),
 );
 pub_sub_client.connect().await.unwrap();
 let msg = Msg::new(PktType::PUBLISH, "Test".to_string(), Some(b"The message".to_vec()));
   pub_sub_client.post(msg).await.unwrap();
 }
Source

pub async fn publish(&mut self, topic: String, message: Vec<u8>) -> Result<()>

Publishes the message to the given topic

use simple_pub_sub::client::{PubSubClient, Client};
async fn publish_msg(){
  let client_type = simple_pub_sub::client::PubSubTcpClient {
         server: "localhost".to_string(),
         port: 6480,
         cert: None,
         cert_password: None,
  };

// initialize the client.
let mut pub_sub_client = simple_pub_sub::client::Client::new(
    simple_pub_sub::client::PubSubClient::Tcp(client_type),
);
pub_sub_client.connect().await.unwrap();
// subscribe to the given topic.
pub_sub_client
  .publish(
    "Abc".to_string(),
    "Test message".to_string().into_bytes().to_vec(),
  ).await.unwrap();
}
Examples found in repository?
examples/publish_unix.rs (lines 14-17)
2async fn main() -> Result<(), anyhow::Error> {
3    let client_type = simple_pub_sub::client::PubSubUnixClient {
4        path: "/tmp/simple.sock".to_string(),
5    };
6    // initialize the client.
7    let mut client = simple_pub_sub::client::Client::new(
8        simple_pub_sub::client::PubSubClient::Unix(client_type),
9    );
10
11    client.connect().await?;
12    // publish to the given topic.
13    client
14        .publish(
15            "abc".to_string(),
16            "test message".to_string().into_bytes().to_vec(),
17        )
18        .await
19}
More examples
Hide additional examples
examples/publish_tcp.rs (lines 16-19)
2async fn main() -> Result<(), anyhow::Error> {
3    let client_type = simple_pub_sub::client::PubSubTcpClient {
4        server: "localhost".to_string(),
5        port: 6480,
6        cert: None,
7        cert_password: None,
8    };
9    // initialize the client.
10    let mut client =
11        simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
12
13    client.connect().await?;
14    // publish to the given topic.
15    client
16        .publish(
17            "abc".to_string(),
18            "test message".to_string().into_bytes().to_vec(),
19        )
20        .await
21}
Source

pub async fn query(&mut self, topic: String) -> Result<String>

Sends the query message to the server

use simple_pub_sub::client::{self, PubSubClient, Client};
async fn query(){
  let client_type = simple_pub_sub::client::PubSubTcpClient {
         server: "localhost".to_string(),
         port: 6480,
         cert: None,
         cert_password: None,
  };

// initialize the client.
let mut pub_sub_client = simple_pub_sub::client::Client::new(
    simple_pub_sub::client::PubSubClient::Tcp(client_type),
);
pub_sub_client.connect().await.unwrap();
pub_sub_client.query("Test".to_string());
}
Source

pub async fn subscribe(&mut self, topic: String) -> Result<()>

subscribes to the given topic

 use simple_pub_sub::client::{self, PubSubClient, Client};
 let client_type = simple_pub_sub::client::PubSubTcpClient {
        server: "localhost".to_string(),
        port: 6480,
        cert: None,
        cert_password: None,
 };
 // initialize the client.
 let mut pub_sub_client = simple_pub_sub::client::Client::new(
     simple_pub_sub::client::PubSubClient::Tcp(client_type));
 pub_sub_client.subscribe("Test".to_string());
Examples found in repository?
examples/subscribe_unix.rs (line 16)
4async fn main() -> Result<(), anyhow::Error> {
5    let client_type = simple_pub_sub::client::PubSubUnixClient {
6        path: "/tmp/simple.sock".to_string(),
7    };
8    // initialize the client.
9    let mut client = simple_pub_sub::client::Client::new(
10        simple_pub_sub::client::PubSubClient::Unix(client_type),
11    );
12
13    // connect the client.
14    client.connect().await?;
15    // subscribe to the given topic.
16    client.subscribe("abc".to_string()).await?;
17
18    loop {
19        select! {
20            msg = client.read_message()=>{
21                match  msg{
22                    Ok(msg)=>{
23                        println!("{}:{:?}", msg.topic, msg.message);
24                    }
25                    Err(e)=>{
26                        println!("Error: {:?}", e);
27                    }
28                }
29            }
30        }
31    }
32}
More examples
Hide additional examples
examples/subscribe_tcp.rs (line 18)
4async fn main() -> Result<(), anyhow::Error> {
5    let client_type = simple_pub_sub::client::PubSubTcpClient {
6        server: "localhost".to_string(),
7        port: 6480,
8        cert: None,
9        cert_password: None,
10    };
11
12    // initialize the client.
13    let mut client =
14        simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
15
16    client.connect().await?;
17    // subscribe to the given topic.
18    client.subscribe("abc".to_string()).await?;
19
20    loop {
21        select! {
22            msg = client.read_message()=>{
23                match  msg{
24                    Ok(msg)=>{
25                        println!("{}:{:?}", msg.topic, msg.message);
26                    }
27                    Err(e)=>{
28                        println!("Error: {:?}", e);
29                    }
30                }
31            }
32        }
33    }
34}
Source

pub async fn read_message(&mut self) -> Result<Msg>

reads the incoming message from the server useful when you need to read the messages in loop

use simple_pub_sub::client::{self, PubSubClient, Client};

async fn read_messages(){
  let client_type = simple_pub_sub::client::PubSubTcpClient {
         server: "localhost".to_string(),
         port: 6480,
         cert: None,
         cert_password: None,
  };
  // initialize the client.
  let mut pub_sub_client = simple_pub_sub::client::Client::new(
      simple_pub_sub::client::PubSubClient::Tcp(client_type));
  pub_sub_client.connect().await.unwrap();
  pub_sub_client.subscribe("Test".to_string()).await.unwrap();

  loop {
      match pub_sub_client.read_message().await{
          Ok(msg)=>{
              println!("{}: {:?}", msg.topic, msg.message);
          }
          Err(e)=>{
              println!("error: {:?}", e);
              break
          }
      }
  }
}
Examples found in repository?
examples/subscribe_unix.rs (line 20)
4async fn main() -> Result<(), anyhow::Error> {
5    let client_type = simple_pub_sub::client::PubSubUnixClient {
6        path: "/tmp/simple.sock".to_string(),
7    };
8    // initialize the client.
9    let mut client = simple_pub_sub::client::Client::new(
10        simple_pub_sub::client::PubSubClient::Unix(client_type),
11    );
12
13    // connect the client.
14    client.connect().await?;
15    // subscribe to the given topic.
16    client.subscribe("abc".to_string()).await?;
17
18    loop {
19        select! {
20            msg = client.read_message()=>{
21                match  msg{
22                    Ok(msg)=>{
23                        println!("{}:{:?}", msg.topic, msg.message);
24                    }
25                    Err(e)=>{
26                        println!("Error: {:?}", e);
27                    }
28                }
29            }
30        }
31    }
32}
More examples
Hide additional examples
examples/subscribe_tcp.rs (line 22)
4async fn main() -> Result<(), anyhow::Error> {
5    let client_type = simple_pub_sub::client::PubSubTcpClient {
6        server: "localhost".to_string(),
7        port: 6480,
8        cert: None,
9        cert_password: None,
10    };
11
12    // initialize the client.
13    let mut client =
14        simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
15
16    client.connect().await?;
17    // subscribe to the given topic.
18    client.subscribe("abc".to_string()).await?;
19
20    loop {
21        select! {
22            msg = client.read_message()=>{
23                match  msg{
24                    Ok(msg)=>{
25                        println!("{}:{:?}", msg.topic, msg.message);
26                    }
27                    Err(e)=>{
28                        println!("Error: {:?}", e);
29                    }
30                }
31            }
32        }
33    }
34}

Trait Implementations§

Source§

impl Debug for Client

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Client

§

impl RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl UnwindSafe for Client

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, 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.