Decided to go straight for the reverse HTTP server idea

main
_ 2020-10-27 02:49:12 +00:00
parent 8b3f952091
commit e3aa61bb9a
4 changed files with 85 additions and 1 deletions

View File

@ -9,4 +9,5 @@ edition = "2018"
[dependencies] [dependencies]
futures = "0.3.7" futures = "0.3.7"
hyper = "0.13.8" hyper = "0.13.8"
reqwest = "0.10.8"
tokio = { version = "0.2", features = ["full"] } tokio = { version = "0.2", features = ["full"] }

View File

@ -26,6 +26,7 @@ use ptth::watcher::Watchers;
#[derive (Clone)] #[derive (Clone)]
enum Message { enum Message {
Meow, Meow,
HttpRequest (String),
} }
#[derive (Default)] #[derive (Default)]
@ -61,6 +62,32 @@ async fn handle_wake (state: Arc <ServerState>, watcher_code: String)
} }
} }
async fn handle_http_listen (state: Arc <ServerState>, watcher_code: String)
-> Response <Body>
{
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 <ServerState>,
watcher_code: String,
uri: String
)
-> Response <Body>
{
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 <Body> async fn handle_udp_send () -> Response <Body>
{ {
use tokio::net::UdpSocket; use tokio::net::UdpSocket;
@ -109,6 +136,19 @@ async fn handle_all (req: Request <Body>, state: Arc <ServerState>)
else if let Some (watch_code) = prefix_match (path, "/wake/") { else if let Some (watch_code) = prefix_match (path, "/wake/") {
Ok (handle_wake (state, watch_code.into ()).await) 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/") { else if let Some (name) = prefix_match (path, "/udp_send/") {
Ok (handle_udp_send ().await) Ok (handle_udp_send ().await)

43
src/bin/server.rs Normal file
View File

@ -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 <dyn Error>> {
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 (())
}

View File

@ -64,7 +64,7 @@ impl <T: 'static + Clone + Send + Sync> Watchers <T> {
let (s, r) = oneshot::channel (); let (s, r) = oneshot::channel ();
let timeout = Duration::from_secs (10); let timeout = Duration::from_secs (5);
let id_2 = id.clone (); let id_2 = id.clone ();
{ {