Skip to content

Commit

Permalink
Simple server macro
Browse files Browse the repository at this point in the history
  • Loading branch information
vi committed Mar 19, 2022
1 parent 3d92f57 commit 91d095b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/websocat-allnodes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn all_node_classes() -> websocat_api::ClassRegistrar {
reg.register::<websocat_ioless::foreachrequest::Spawner>();
reg.register::<websocat_ioless::reuse::ReuseBroadcast>();
reg.register_macro::<websocat_ioless::simple::SimpleClientSession>();
reg.register_macro::<websocat_ioless::simple::SimpleServerSession>();
reg.register::<websocat_readline::Readline>();
reg.register::<websocat_session::SessionClass>();
reg.register::<websocat_syncnodes::net::TcpConnect>();
Expand Down
48 changes: 48 additions & 0 deletions crates/websocat-ioless/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,51 @@ impl websocat_api::Macro for SimpleClientSession {
)
}
}


#[derive(Default, WebsocatMacro)]
#[auto_populate_macro_in_allclasslist]
pub struct SimpleServerSession;
impl websocat_api::Macro for SimpleServerSession {
fn official_name(&self) -> String {
"server".to_owned()
}
fn injected_cli_opts(&self) -> Vec<(String, websocat_api::CliOptionDescription)> {
vec![]
}

fn run(
&self,
mut input: StrNode,
_opts: &websocat_api::CliOpts,
) -> websocat_api::Result<StrNode> {
use StringOrSubnode::{Str, Subnode};

if !input.properties.is_empty() {
bail!("No properties expected");
}

if input.array.len() != 1 {
bail!("Input array should have length 1");
}

let mut host_and_or_port = match input.array.drain(..).next().unwrap() {
Str(x) => x,
Subnode(_) => bail!("Array element must be string, not a subnode"),
};

if ! host_and_or_port.contains(&b':') {
let mut tmp = websocat_api::bytes::BytesMut::with_capacity(10+host_and_or_port.len());
tmp.extend_from_slice(b"127.0.0.1:");
tmp.extend(host_and_or_port);
host_and_or_port = tmp.freeze();
}

eprintln!("Listening {}", String::from_utf8_lossy(&host_and_or_port.to_vec()));

StrNode::quasiquote(
b"[session left=[wsl + @ tcp-listen ,h +] right=[reuse-broadcast + @ datagrams @ stdio]]",
&|_| Ok(UnquoteResult::Bytes(host_and_or_port.clone())),
)
}
}

0 comments on commit 91d095b

Please sign in to comment.