From e3aa61bb9a20ba930f02a33c4a68af0103286ba1 Mon Sep 17 00:00:00 2001 From: _ <> Date: Tue, 27 Oct 2020 02:49:12 +0000 Subject: [PATCH] Decided to go straight for the reverse HTTP server idea --- Cargo.toml | 1 + src/bin/relay.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/bin/server.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/watcher.rs | 2 +- 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/bin/server.rs diff --git a/Cargo.toml b/Cargo.toml index d9c404d..c38bf38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,5 @@ edition = "2018" [dependencies] futures = "0.3.7" hyper = "0.13.8" +reqwest = "0.10.8" tokio = { version = "0.2", features = ["full"] } diff --git a/src/bin/relay.rs b/src/bin/relay.rs index 66d58f9..519a679 100644 --- a/src/bin/relay.rs +++ b/src/bin/relay.rs @@ -26,6 +26,7 @@ use ptth::watcher::Watchers; #[derive (Clone)] enum Message { Meow, + HttpRequest (String), } #[derive (Default)] @@ -61,6 +62,32 @@ async fn handle_wake (state: Arc , watcher_code: String) } } +async fn handle_http_listen (state: Arc , watcher_code: String) +-> Response +{ + match Watchers::long_poll (state.watchers.clone (), watcher_code).await { + Some (Message::HttpRequest (uri)) => status_reply (StatusCode::OK, uri), + _ => status_reply (StatusCode::GATEWAY_TIMEOUT, "no\n"), + } +} + +async fn handle_http_request ( + state: Arc , + watcher_code: String, + uri: String +) + -> Response +{ + let mut watchers = state.watchers.lock ().await; + + if watchers.wake_one (Message::HttpRequest (uri), &watcher_code) { + status_reply (StatusCode::OK, "ok\n") + } + else { + status_reply (StatusCode::BAD_REQUEST, "no\n") + } +} + async fn handle_udp_send () -> Response { use tokio::net::UdpSocket; @@ -109,6 +136,19 @@ async fn handle_all (req: Request , state: Arc ) else if let Some (watch_code) = prefix_match (path, "/wake/") { Ok (handle_wake (state, watch_code.into ()).await) } + else if let Some (listen_code) = prefix_match (path, "/http_listen/") { + Ok (handle_http_listen (state, listen_code.into ()).await) + } + else if let Some (rest) = prefix_match (path, "/http_request/") { + if let Some (idx) = rest.find ('/') { + let listen_code = &rest [0..idx]; + let path = &rest [idx + 1..]; + Ok (handle_http_request (state, listen_code.into (), path.into ()).await) + } + else { + Ok (status_reply (StatusCode::BAD_REQUEST, "Bad URI format")) + } + } /* else if let Some (name) = prefix_match (path, "/udp_send/") { Ok (handle_udp_send ().await) diff --git a/src/bin/server.rs b/src/bin/server.rs new file mode 100644 index 0000000..6308ca0 --- /dev/null +++ b/src/bin/server.rs @@ -0,0 +1,43 @@ +use std::error::Error; + +use hyper::{ + StatusCode, + Uri, +}; +use reqwest::Client; +use tokio::fs::File; + +#[tokio::main] +async fn main () -> Result <(), Box > { + let client = Client::new (); + + let path = "/home/user/pictures/bzqcChY.jpg"; + + loop { + let _uri = Uri::builder () + .scheme ("http") + .authority ("127.0.0.1:4000") + .path_and_query ("/listen/alien_wildlands") + .build ().unwrap (); + + let req = client.get ("http://127.0.0.1:4000/http_listen/alien_wildlands"); + let response = match req.send ().await { + Err (e) => { + println! ("Err: {:?}", e); + continue; + }, + Ok (r) => r, + }; + + if response.status () != StatusCode::OK { + continue; + } + + let body = response.bytes ().await?; + let body = std::str::from_utf8 (&body)?; + + println! ("Client requested {}", body); + } + + Ok (()) +} diff --git a/src/watcher.rs b/src/watcher.rs index 34e374c..95a3957 100644 --- a/src/watcher.rs +++ b/src/watcher.rs @@ -64,7 +64,7 @@ impl Watchers { let (s, r) = oneshot::channel (); - let timeout = Duration::from_secs (10); + let timeout = Duration::from_secs (5); let id_2 = id.clone (); {