From 18521debbcc8e2b2faabfcf4770e09370a3405b7 Mon Sep 17 00:00:00 2001 From: azw Date: Sat, 11 Nov 2023 05:36:38 +0000 Subject: [PATCH] 1 --- f9/src/app.rs | 67 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/f9/src/app.rs b/f9/src/app.rs index 690c77a..78d3659 100644 --- a/f9/src/app.rs +++ b/f9/src/app.rs @@ -1,6 +1,5 @@ use std::rc::{Rc, Weak}; use std::cell::RefCell; -use std::thread::sleep; use std::time::Duration; use tokio::runtime::Runtime; use actix_web::{dev::ServerHandle, @@ -42,11 +41,11 @@ pub struct App { im_msgs: Rc::>>, im_work_msgs: Rc::>>, im_mutex: Mutex, - http_handlers: Arc>>, + webapp_state: Arc::, } struct AppState { - app_name: String + request: Arc>>>>, } #[derive(Default)] @@ -57,28 +56,43 @@ struct IMMsgNode { entry: Rc::>>, } -async fn index(data: Data) -> impl Responder { - println!("http.thread.id {:?}", thread::current().id()); - HttpResponse::Ok().body("Hello world!") +struct HttpContext { + id: u64, + req: HttpRequest, + add_tick: i64, + handled: bool, + rsp: String } -async fn run_app(port: u16, tx: mpsc::Sender) -> std::io::Result<()> { - let server = HttpServer::new(|| { +async fn index(data: Data>, req: HttpRequest) -> impl Responder { + println!("http.thread.id {:?}", thread::current().id()); + let context = Arc::new( + Mutex::new(HttpContext{ + id: 0, + req: req, + add_tick: 0, + handled: false, + rsp: "".to_string() + }) + ); + { + //data.request.lock(); + } + { + while !context.lock().unwrap().handled { + tokio::time::sleep(tokio::time::Duration::from_millis(10)).await; + } + } + let data = context.lock().unwrap().rsp.clone(); + HttpResponse::Ok().body(data) +} + +async fn run_app(port: u16, app_state: Arc::, + tx: mpsc::Sender) -> std::io::Result<()> { + let server = HttpServer::new(move || { WebApp::new() - .data(AppState { - app_name:String::from("Actix-web"), - }) + .data(app_state.clone()) .route("/webapp/index.php",web::get().to(index)) - /* - WebApp::new() - .service(web::resource("/webapp/index.php").to( - || async - { - "{}" - }) - ) - .service(hello) - */ }) .bind(("0.0.0.0", port))? .workers(1) @@ -106,10 +120,14 @@ impl App { instance_id: 0, tokio_rt: Runtime::new().unwrap(), user_app: None, - http_handlers: Default::default(), im_msgs: r9::ListHead::::new_head(), im_work_msgs: r9::ListHead::::new_head(), im_mutex: Mutex::new(1), + webapp_state: Arc::new( + AppState{ + //request: Default::default() + } + ), _self_wp: Default::default(), } ))); @@ -138,7 +156,7 @@ impl App { pub fn run(&mut self) { loop { crate::Timer::instance().borrow_mut().update(); - sleep(Duration::from_millis(1)); + std::thread::sleep(Duration::from_millis(1)); self.dispatch_immsg(); } } @@ -217,8 +235,9 @@ impl App { let (tx, _) = mpsc::channel(); let port = self.user_app.as_ref().unwrap().borrow().get_http_listen_port() as u16; + let app_state = self.webapp_state.clone(); thread::spawn(move || { - let server_future = run_app(port, tx); + let server_future = run_app(port, app_state, tx); rt::System::new().block_on(server_future) }); }