2020-10-27 01:42:10 +00:00
|
|
|
use std::{
|
|
|
|
collections::*,
|
|
|
|
error::Error,
|
2020-10-28 01:20:06 +00:00
|
|
|
convert::{
|
|
|
|
Infallible,
|
2020-10-28 01:31:38 +00:00
|
|
|
TryFrom,
|
2020-10-28 01:20:06 +00:00
|
|
|
},
|
2020-10-28 01:31:38 +00:00
|
|
|
iter::FromIterator,
|
2020-10-28 01:20:06 +00:00
|
|
|
net::SocketAddr,
|
|
|
|
sync::{
|
|
|
|
Arc
|
|
|
|
},
|
2020-10-28 01:31:38 +00:00
|
|
|
time::{Duration},
|
2020-10-27 01:42:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use futures::channel::oneshot;
|
2020-10-27 03:36:52 +00:00
|
|
|
use hyper::{
|
|
|
|
Body,
|
|
|
|
Method,
|
|
|
|
Request,
|
|
|
|
Response,
|
|
|
|
Server,
|
|
|
|
StatusCode,
|
|
|
|
};
|
2020-10-27 01:42:10 +00:00
|
|
|
use hyper::service::{make_service_fn, service_fn};
|
|
|
|
|
|
|
|
use tokio::{
|
|
|
|
sync::Mutex,
|
|
|
|
time::delay_for,
|
|
|
|
};
|
|
|
|
|
2020-10-28 01:20:06 +00:00
|
|
|
use ptth::{
|
|
|
|
http_serde::*,
|
|
|
|
watcher::Watchers,
|
|
|
|
};
|
2020-10-27 01:42:10 +00:00
|
|
|
|
|
|
|
enum Message {
|
|
|
|
Meow,
|
2020-10-28 01:20:06 +00:00
|
|
|
HttpRequestResponse (RequestParts),
|
2020-10-27 13:04:28 +00:00
|
|
|
HttpResponseResponseStream (Body),
|
2020-10-27 01:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Default)]
|
|
|
|
struct ServerState {
|
|
|
|
watchers: Arc <Mutex <Watchers <Message>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn status_reply <B: Into <Body>> (status: StatusCode, b: B)
|
|
|
|
-> Response <Body>
|
|
|
|
{
|
|
|
|
Response::builder ().status (status).body (b.into ()).unwrap ()
|
|
|
|
}
|
|
|
|
|
2020-10-27 02:20:37 +00:00
|
|
|
async fn handle_watch (state: Arc <ServerState>, watcher_code: String)
|
2020-10-27 01:42:10 +00:00
|
|
|
-> Response <Body>
|
|
|
|
{
|
2020-10-27 02:20:37 +00:00
|
|
|
match Watchers::long_poll (state.watchers.clone (), watcher_code).await {
|
2020-10-27 01:42:10 +00:00
|
|
|
None => status_reply (StatusCode::OK, "no\n"),
|
|
|
|
Some (_) => status_reply (StatusCode::OK, "actually, yes\n"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 02:20:37 +00:00
|
|
|
async fn handle_wake (state: Arc <ServerState>, watcher_code: String)
|
2020-10-27 01:42:10 +00:00
|
|
|
-> Response <Body>
|
|
|
|
{
|
|
|
|
let mut watchers = state.watchers.lock ().await;
|
|
|
|
|
2020-10-27 02:20:37 +00:00
|
|
|
if watchers.wake_one (Message::Meow, &watcher_code) {
|
|
|
|
status_reply (StatusCode::OK, "ok\n")
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
status_reply (StatusCode::BAD_REQUEST, "no\n")
|
|
|
|
}
|
2020-10-27 01:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 02:49:12 +00:00
|
|
|
async fn handle_http_listen (state: Arc <ServerState>, watcher_code: String)
|
|
|
|
-> Response <Body>
|
|
|
|
{
|
2020-10-28 01:20:06 +00:00
|
|
|
//println! ("Step 1");
|
2020-10-27 02:49:12 +00:00
|
|
|
match Watchers::long_poll (state.watchers.clone (), watcher_code).await {
|
2020-10-28 01:20:06 +00:00
|
|
|
Some (Message::HttpRequestResponse (parts)) => {
|
2020-10-27 03:27:25 +00:00
|
|
|
println! ("Step 3");
|
2020-10-28 01:20:06 +00:00
|
|
|
status_reply (StatusCode::OK, rmp_serde::to_vec (&parts).unwrap ())
|
2020-10-27 03:27:25 +00:00
|
|
|
},
|
2020-10-27 02:49:12 +00:00
|
|
|
_ => status_reply (StatusCode::GATEWAY_TIMEOUT, "no\n"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 03:27:25 +00:00
|
|
|
async fn handle_http_response (
|
2020-10-27 03:36:52 +00:00
|
|
|
req: Request <Body>,
|
2020-10-27 03:27:25 +00:00
|
|
|
state: Arc <ServerState>,
|
2020-10-27 03:36:52 +00:00
|
|
|
req_id: String,
|
2020-10-27 03:27:25 +00:00
|
|
|
)
|
|
|
|
-> Response <Body>
|
|
|
|
{
|
|
|
|
println! ("Step 6");
|
2020-10-27 13:04:28 +00:00
|
|
|
let body = req.into_body ();
|
2020-10-27 03:27:25 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut watchers = state.watchers.lock ().await;
|
|
|
|
|
|
|
|
println! ("Step 7");
|
2020-10-27 13:04:28 +00:00
|
|
|
if ! watchers.wake_one (Message::HttpResponseResponseStream (body), &req_id)
|
2020-10-27 03:27:25 +00:00
|
|
|
{
|
2020-10-28 01:20:06 +00:00
|
|
|
println! ("Step 8 (bad thing)");
|
2020-10-28 01:39:02 +00:00
|
|
|
status_reply (StatusCode::BAD_REQUEST, "A bad thing happened.\n")
|
2020-10-27 03:27:25 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
println! ("Step 8");
|
|
|
|
status_reply (StatusCode::OK, "ok\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 02:49:12 +00:00
|
|
|
async fn handle_http_request (
|
2020-10-28 01:31:38 +00:00
|
|
|
req: http::request::Parts,
|
|
|
|
uri: String,
|
2020-10-27 02:49:12 +00:00
|
|
|
state: Arc <ServerState>,
|
2020-10-28 01:20:06 +00:00
|
|
|
watcher_code: String
|
2020-10-27 02:49:12 +00:00
|
|
|
)
|
|
|
|
-> Response <Body>
|
|
|
|
{
|
2020-10-28 01:31:38 +00:00
|
|
|
let parts = {
|
|
|
|
let id = ulid::Ulid::new ().to_string ();
|
|
|
|
let method = match ptth::http_serde::Method::try_from (req.method) {
|
|
|
|
Ok (x) => x,
|
|
|
|
_ => return status_reply (StatusCode::BAD_REQUEST, "Method not supported"),
|
|
|
|
};
|
|
|
|
let headers = HashMap::from_iter (
|
|
|
|
req.headers.into_iter ()
|
|
|
|
.filter_map (|(k, v)| k.map (|k| (k, v)))
|
|
|
|
.map (|(k, v)| (String::from (k.as_str ()), v.as_bytes ().to_vec ()))
|
|
|
|
);
|
|
|
|
|
|
|
|
RequestParts {
|
|
|
|
id,
|
|
|
|
method,
|
|
|
|
uri,
|
|
|
|
headers,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-28 01:20:06 +00:00
|
|
|
println! ("Step 2 {}", parts.id);
|
2020-10-27 03:27:25 +00:00
|
|
|
|
|
|
|
let (s, r) = oneshot::channel ();
|
|
|
|
let timeout = Duration::from_secs (5);
|
|
|
|
|
2020-10-28 01:20:06 +00:00
|
|
|
let id_2 = parts.id.clone ();
|
2020-10-27 03:27:25 +00:00
|
|
|
{
|
|
|
|
let mut that = state.watchers.lock ().await;
|
|
|
|
that.add_watcher_with_id (s, id_2)
|
2020-10-27 02:49:12 +00:00
|
|
|
}
|
2020-10-27 03:27:25 +00:00
|
|
|
|
2020-10-28 01:20:06 +00:00
|
|
|
let req_id = parts.id.clone ();
|
|
|
|
|
2020-10-27 03:27:25 +00:00
|
|
|
tokio::spawn (async move {
|
|
|
|
{
|
|
|
|
let mut watchers = state.watchers.lock ().await;
|
|
|
|
|
|
|
|
println! ("Step 3");
|
2020-10-28 01:20:06 +00:00
|
|
|
if ! watchers.wake_one (Message::HttpRequestResponse (parts), &watcher_code) {
|
2020-10-27 03:27:25 +00:00
|
|
|
watchers.remove_watcher (&req_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delay_for (timeout).await;
|
|
|
|
{
|
|
|
|
let mut that = state.watchers.lock ().await;
|
|
|
|
that.remove_watcher (&req_id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
match r.await {
|
2020-10-27 13:04:28 +00:00
|
|
|
Ok (Message::HttpResponseResponseStream (body)) => {
|
2020-10-27 03:27:25 +00:00
|
|
|
println! ("Step 7");
|
2020-10-28 02:10:40 +00:00
|
|
|
|
|
|
|
Response::builder ()
|
|
|
|
.status (StatusCode::OK)
|
|
|
|
.header ("Accept-Ranges", "bytes")
|
|
|
|
.body (body)
|
|
|
|
.unwrap ()
|
2020-10-27 03:27:25 +00:00
|
|
|
},
|
|
|
|
_ => status_reply (StatusCode::GATEWAY_TIMEOUT, "server didn't reply in time or somethin'"),
|
2020-10-27 02:49:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 02:20:37 +00:00
|
|
|
fn prefix_match <'a> (hay: &'a str, needle: &str) -> Option <&'a str>
|
|
|
|
{
|
|
|
|
if hay.starts_with (needle) {
|
|
|
|
Some (&hay [needle.len ()..])
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 01:42:10 +00:00
|
|
|
async fn handle_all (req: Request <Body>, state: Arc <ServerState>)
|
|
|
|
-> Result <Response <Body>, Infallible>
|
|
|
|
{
|
2020-10-27 02:20:37 +00:00
|
|
|
let path = req.uri ().path ();
|
2020-10-28 01:20:06 +00:00
|
|
|
//println! ("{}", path);
|
2020-10-27 01:42:10 +00:00
|
|
|
|
2020-10-27 03:36:52 +00:00
|
|
|
if req.method () == Method::POST {
|
|
|
|
return Ok (if let Some (request_code) = prefix_match (path, "/http_response/") {
|
|
|
|
let request_code = request_code.into ();
|
|
|
|
handle_http_response (req, state, request_code).await
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
status_reply (StatusCode::BAD_REQUEST, "Can't POST this\n")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-27 02:20:37 +00:00
|
|
|
if let Some (watch_code) = prefix_match (path, "/watch/") {
|
|
|
|
Ok (handle_watch (state, watch_code.into ()).await)
|
2020-10-27 01:42:10 +00:00
|
|
|
}
|
2020-10-27 02:20:37 +00:00
|
|
|
else if let Some (watch_code) = prefix_match (path, "/wake/") {
|
|
|
|
Ok (handle_wake (state, watch_code.into ()).await)
|
2020-10-27 01:42:10 +00:00
|
|
|
}
|
2020-10-27 02:49:12 +00:00
|
|
|
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 ('/') {
|
2020-10-28 01:20:06 +00:00
|
|
|
let listen_code = String::from (&rest [0..idx]);
|
2020-10-28 01:31:38 +00:00
|
|
|
let path = String::from (&rest [idx..]);
|
2020-10-28 01:20:06 +00:00
|
|
|
let (parts, _) = req.into_parts ();
|
2020-10-28 01:31:38 +00:00
|
|
|
|
|
|
|
Ok (handle_http_request (parts, path, state, listen_code).await)
|
2020-10-27 02:49:12 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Ok (status_reply (StatusCode::BAD_REQUEST, "Bad URI format"))
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 01:42:10 +00:00
|
|
|
else {
|
|
|
|
Ok (status_reply (StatusCode::OK, "Hi\n"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main () -> Result <(), Box <dyn Error>> {
|
|
|
|
let addr = SocketAddr::from(([0, 0, 0, 0], 4000));
|
|
|
|
|
|
|
|
let state = Arc::new (ServerState::default ());
|
|
|
|
|
|
|
|
let make_svc = make_service_fn (|_conn| {
|
|
|
|
let state = state.clone ();
|
|
|
|
|
|
|
|
async {
|
|
|
|
Ok::<_, Infallible> (service_fn (move |req| {
|
|
|
|
let state = state.clone ();
|
|
|
|
|
|
|
|
handle_all (req, state)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let server = Server::bind (&addr).serve (make_svc);
|
|
|
|
|
|
|
|
server.await?;
|
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|