[][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 global_secret_b = SecretKey::generate(&mut thread_rng());
let nodes = vec![
Node {
       id: "B".to_string(),
       address: None,
       trusted: true,
       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::generate(&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>
});
assert!(server.emit("B".to_string(), Event::new("ping".to_string())).get_value().is_err()) // this won't work without a known node B

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.

pub fn node_id(&self) -> String[src]

Returns the nodeId of the server

pub fn nodes(&self) -> Vec<Node>[src]

Returns the nodes known to the server

pub fn nodes_ref(&self) -> Arc<Mutex<HashMap<String, Node>>>[src]

Returns the actual reference to the inner node list

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

Emits an event to the specified Node The actual writing is done in a separate thread from the thread pool. With the returned wait group 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 sender. Multiple handlers can be registered for an event.

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