ya_runtime_api/
server.rs

1pub mod proto {
2    #![allow(clippy::derive_partial_eq_without_eq)]
3
4    include!(concat!(env!("OUT_DIR"), "/ya_runtime_api.rs"));
5
6    impl response::Error {
7        pub fn msg<D: std::fmt::Display>(msg: D) -> Self {
8            let mut e = Self::default();
9            e.set_code(response::ErrorCode::Internal);
10            e.message = msg.to_string();
11            e
12        }
13    }
14}
15mod codec;
16
17#[cfg(feature = "codec")]
18pub use codec::Codec;
19pub use proto::request::{CreateNetwork, KillProcess, RunProcess};
20pub use proto::response::create_network::Endpoint as NetworkEndpoint;
21pub use proto::response::runtime_status::Counter as RuntimeCounter;
22pub use proto::response::runtime_status::Kind as RuntimeStatusKind;
23pub use proto::response::runtime_status::State as RuntimeState;
24pub use proto::response::CreateNetwork as CreateNetworkResp;
25pub use proto::response::Error as ErrorResponse;
26pub use proto::response::RunProcess as RunProcessResp;
27pub use proto::response::{ErrorCode, ProcessStatus, RuntimeStatus};
28pub use proto::{Network, NetworkInterface};
29
30use futures::future::{BoxFuture, LocalBoxFuture};
31use futures::prelude::*;
32use std::process::{ExitStatus, Stdio};
33use std::sync::Arc;
34use tokio::process;
35
36pub type AsyncResponse<'a, T> = LocalBoxFuture<'a, Result<T, ErrorResponse>>;
37
38/// Service interface
39pub trait RuntimeService {
40    /// Perform version handshake
41    fn hello(&self, version: &str) -> AsyncResponse<'_, String>;
42    /// Spawn a process
43    fn run_process(&self, run: RunProcess) -> AsyncResponse<'_, RunProcessResp>;
44    /// Kill a spawned process
45    fn kill_process(&self, kill: KillProcess) -> AsyncResponse<'_, ()>;
46    /// Setup a virtual private network
47    fn create_network(&self, network: CreateNetwork) -> AsyncResponse<'_, CreateNetworkResp>;
48    /// Perform service shutdown
49    fn shutdown(&self) -> AsyncResponse<'_, ()>;
50}
51
52/// Process and internal event handler
53pub trait RuntimeHandler {
54    /// Process event handler
55    fn on_process_status<'a>(&self, status: ProcessStatus) -> BoxFuture<'a, ()>;
56    /// Runtime event handler
57    fn on_runtime_status<'a>(&self, status: RuntimeStatus) -> BoxFuture<'a, ()>;
58}
59
60/// Runtime control interface
61pub trait RuntimeControl {
62    /// Return runtime process id
63    fn id(&self) -> u32;
64    /// Stop the runtime
65    fn stop(&self);
66    /// Return a future, resolved when the runtime is stopped
67    fn stopped(&self) -> BoxFuture<'_, i32>;
68}
69
70mod client;
71mod service;
72
73pub use client::spawn;
74pub use service::{run, run_async};