Skip to main content

Handle

Struct Handle 

Source
pub struct Handle { /* private fields */ }
Expand description

A server running in the background, from Server::spawn. Stopping it (or dropping it) stops accepting connections and ends every session.

Implementations§

Source§

impl Handle

Source

pub fn local_addr(&self) -> SocketAddr

The address the server listens on.

Source

pub fn url(&self) -> &str

The address to open in a browser, with the token.

Examples found in repository?
examples/session_probe.rs (line 98)
65fn main() -> std::io::Result<()> {
66    let mut args = std::env::args().skip(1);
67    let mut addr = "127.0.0.1:8080".to_string();
68    let mut token = None;
69    let mut startup = None;
70    let mut stdin_stop = false;
71    let mut nested = false;
72    while let Some(arg) = args.next() {
73        let mut value = || args.next().expect("a value after the option");
74        match arg.as_str() {
75            "--token" => token = Some(value()),
76            "--startup-sleep" => startup = Some(value().parse::<u64>().expect("ms")),
77            "--stdin-stop" => stdin_stop = true,
78            "--nested" => nested = true,
79            _ => addr = arg,
80        }
81    }
82    if nested {
83        // An unrelated program that serves an app: it should get the
84        // address it asked for.
85        let server = rich_web::Server::bind(addr.as_str(), app)?;
86        println!("BOUND {}", server.local_addr());
87        return Ok(());
88    }
89    if let (Some(ms), true) = (startup, std::env::var_os(rich_web::SESSION_ENV).is_some()) {
90        std::thread::sleep(Duration::from_millis(ms));
91    }
92    let mut server = rich_web::Server::bind(addr.as_str(), app)?.title("session probe");
93    if let Some(token) = token {
94        server = server.token(token);
95    }
96    if stdin_stop {
97        let handle = server.spawn()?;
98        println!("Serving session probe at {}", handle.url());
99        let mut line = String::new();
100        std::io::stdin().lock().read_line(&mut line)?;
101        let started = Instant::now();
102        handle.stop();
103        println!("STOPPED {}", started.elapsed().as_millis());
104        return Ok(());
105    }
106    server.run()
107}
Source

pub fn sessions(&self) -> usize

The sessions running now.

Source

pub fn stop(self)

Stop: no more connections, and every session ends (within one turn of its loop, at most 50 ms). A program’s session ends its program: it is hung up on, and a LocalPty’s is killed, with its process group, if it is still running a second later. This returns once every program has ended; a connection still sending its request is not served.

Examples found in repository?
examples/session_probe.rs (line 102)
65fn main() -> std::io::Result<()> {
66    let mut args = std::env::args().skip(1);
67    let mut addr = "127.0.0.1:8080".to_string();
68    let mut token = None;
69    let mut startup = None;
70    let mut stdin_stop = false;
71    let mut nested = false;
72    while let Some(arg) = args.next() {
73        let mut value = || args.next().expect("a value after the option");
74        match arg.as_str() {
75            "--token" => token = Some(value()),
76            "--startup-sleep" => startup = Some(value().parse::<u64>().expect("ms")),
77            "--stdin-stop" => stdin_stop = true,
78            "--nested" => nested = true,
79            _ => addr = arg,
80        }
81    }
82    if nested {
83        // An unrelated program that serves an app: it should get the
84        // address it asked for.
85        let server = rich_web::Server::bind(addr.as_str(), app)?;
86        println!("BOUND {}", server.local_addr());
87        return Ok(());
88    }
89    if let (Some(ms), true) = (startup, std::env::var_os(rich_web::SESSION_ENV).is_some()) {
90        std::thread::sleep(Duration::from_millis(ms));
91    }
92    let mut server = rich_web::Server::bind(addr.as_str(), app)?.title("session probe");
93    if let Some(token) = token {
94        server = server.token(token);
95    }
96    if stdin_stop {
97        let handle = server.spawn()?;
98        println!("Serving session probe at {}", handle.url());
99        let mut line = String::new();
100        std::io::stdin().lock().read_line(&mut line)?;
101        let started = Instant::now();
102        handle.stop();
103        println!("STOPPED {}", started.elapsed().as_millis());
104        return Ok(());
105    }
106    server.run()
107}

Trait Implementations§

Source§

impl Drop for Handle

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.