[][src]Struct vented::server::VentedServer

pub struct VentedServer { /* fields omitted */ }

The vented server that provides parallel handling of connections Usage:

use vented::server::VentedServer;
use vented::server::data::Node;
use vented::crypto::SecretKey;
use rand::thread_rng;
use vented::event::Event;

let nodes = vec![
Node {
       id: "B".to_string(),
       address: None,
       public_key: global_secret_b.public_key() // load it from somewhere
   },
];
// in a real world example the secret key needs to be loaded from somewhere because connections
// with unknown keys are not accepted.
let global_secret = SecretKey::new(&mut thread_rng());
let mut server = VentedServer::new("A".to_string(), global_secret, nodes.clone(), 4);


server.listen("localhost:20000".to_string());
server.on("pong", |_event| {
   println!("Pong!");
   
   None    // the return value is the response event Option<Event>
});
server.emit("B".to_string(), Event::new("ping".to_string())).unwrap();

Implementations

impl VentedServer[src]

pub fn new(
    node_id: String,
    secret_key: SecretKey,
    nodes: Vec<Node>,
    num_threads: usize
) -> Self
[src]

Creates a new vented server with a given node_id and secret key that are used to authenticate against other servers. The given nodes are used for authentication. The server runs with 2x the given amount of threads.

pub fn emit(&self, node_id: String, event: Event) -> VentedResult<WaitGroup>[src]

Emits an event to the specified Node The actual writing is done in a separate thread from the thread pool. With the returned waitgroup one can wait for the event to be written.

pub fn on<F: 'static>(&mut self, event_name: &str, handler: F) where
    F: Fn(Event) -> Option<Event> + Send + Sync
[src]

Adds a handler for the given event. The event returned by the handler is returned to the server. If there is more than one handler, the response will be piped to the next handler. The oder is by order of insertion. The first registered handler will be executed first.

pub fn listen(&mut self, address: String) -> WaitGroup[src]

Starts listening on the specified address (with port!) This will cause a new thread to start up so that the method returns immediately With the returned wait group one can wait for the server to be ready. The method can be called multiple times to start listeners on multiple ports.

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T[src]

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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