mirror of https://github.com/Trivernis/vented.git
Add vented client implementation
Signed-off-by: trivernis <trivernis@protonmail.com>pull/1/head
parent
bcc3024fc3
commit
15221b6f59
@ -0,0 +1,9 @@
|
|||||||
|
use crate::event::Event;
|
||||||
|
use crate::result::VentedResult;
|
||||||
|
|
||||||
|
pub mod tcp;
|
||||||
|
|
||||||
|
pub trait VentedClient: Sized {
|
||||||
|
fn connect(address: &str) -> VentedResult<Self>;
|
||||||
|
fn emit(&mut self, event: Event) -> VentedResult<Event>;
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
use crate::client::VentedClient;
|
||||||
|
use crate::event::Event;
|
||||||
|
use crate::result::VentedResult;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::net::TcpStream;
|
||||||
|
|
||||||
|
pub struct VentedTcpClient {
|
||||||
|
connection: TcpStream,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VentedClient for VentedTcpClient {
|
||||||
|
fn connect(address: &str) -> VentedResult<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
connection: TcpStream::connect(address)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emit(&mut self, mut event: Event) -> VentedResult<Event> {
|
||||||
|
self.connection.write(&event.as_bytes())?;
|
||||||
|
|
||||||
|
Event::from_bytes(&mut self.connection)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::thread;
|
||||||
|
use std::time::Duration;
|
||||||
|
use vented::client::tcp::VentedTcpClient;
|
||||||
|
use vented::client::VentedClient;
|
||||||
|
use vented::event::Event;
|
||||||
|
use vented::server::tcp::VentedTcpServer;
|
||||||
|
use vented::server::VentedServer;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_pong_event() {
|
||||||
|
static ADDRESS: &str = "localhost:22222";
|
||||||
|
static PING: &str = "ping";
|
||||||
|
static PONG: &str = "pong";
|
||||||
|
let ping_count = Arc::new(AtomicUsize::new(0));
|
||||||
|
let server_ready = Arc::new(AtomicBool::new(false));
|
||||||
|
|
||||||
|
let mut server = VentedTcpServer::new(1);
|
||||||
|
{
|
||||||
|
let ping_received = Arc::clone(&ping_count);
|
||||||
|
server.on(PING, move |_event| {
|
||||||
|
ping_received.fetch_add(1, Ordering::Relaxed);
|
||||||
|
|
||||||
|
Some(Event::new(PONG.to_string()))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
thread::spawn({
|
||||||
|
let server_ready = Arc::clone(&server_ready);
|
||||||
|
move || {
|
||||||
|
server_ready.store(true, Ordering::Relaxed);
|
||||||
|
server.listen(ADDRESS).unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
while !server_ready.load(Ordering::Relaxed) {
|
||||||
|
thread::sleep(Duration::from_millis(1));
|
||||||
|
}
|
||||||
|
let mut client = VentedTcpClient::connect(ADDRESS).unwrap();
|
||||||
|
client.emit(Event::new(PING.to_string())).unwrap();
|
||||||
|
let response = client.emit(Event::new(PING.to_string())).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(ping_count.load(Ordering::Relaxed), 2);
|
||||||
|
assert_eq!(response.name, PONG);
|
||||||
|
}
|
Loading…
Reference in New Issue