From 0c1e411318ef01d02f0d1c5352a7b034a5433057 Mon Sep 17 00:00:00 2001 From: azw Date: Sat, 11 Nov 2023 04:23:44 +0000 Subject: [PATCH] 1 --- f9/src/app.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 85 insertions(+), 4 deletions(-) diff --git a/f9/src/app.rs b/f9/src/app.rs index 2554f5d..d42b734 100644 --- a/f9/src/app.rs +++ b/f9/src/app.rs @@ -3,13 +3,26 @@ use std::cell::RefCell; use std::thread::sleep; use std::time::Duration; use tokio::runtime::Runtime; -use actix_web::{dev::ServerHandle, rt, web, HttpRequest, HttpServer}; +use actix_web::{dev::ServerHandle, + rt, + web, + get, + web::Data, + HttpRequest, + HttpResponse, + HttpServer, + Responder}; use actix_web::App as WebApp; use std::{sync::mpsc, thread}; +use std::sync::{Arc, Mutex, Condvar}; +use std::collections::HashMap; +use std::any::Any; use r9_macro::SharedFromSelf; use r9_macro_derive::SharedFromSelf; +pub type HttpRequestCb = Rc::; + pub trait UserApp { fn get_pkg_name(&self) -> String; fn init(&mut self); @@ -26,17 +39,46 @@ pub struct App { instance_id: i32, tokio_rt: Runtime, user_app: Option>>, + im_msgs: Rc::>>, + im_work_msgs: Rc::>>, + im_mutex: Mutex, + http_handlers: Arc>>, +} + +struct AppState { + app_name: String +} + +#[derive(Default)] +struct IMMsgNode { + msg_id: u16, + args: Option>>, + cb: Option>>)>>, + entry: Rc::>>, +} + +async fn index(data: Data) -> impl Responder { + println!("http.thread.id {:?}", thread::current().id()); + HttpResponse::Ok().body("Hello world!") } async fn run_app(port: u16, tx: mpsc::Sender) -> std::io::Result<()> { let server = HttpServer::new(|| { WebApp::new() - .service(web::resource("/webapp/index.php?c=Ops&a=selfChecking").to( + .data(AppState { + app_name:String::from("Actix-web"), + }) + .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) @@ -64,6 +106,10 @@ 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), _self_wp: Default::default(), } ))); @@ -76,9 +122,10 @@ impl App { } pub fn init(&mut self, user_app: Rc::>) { - //self.tokio_rt.enter(); - self.init_http_server(); + println!("main.thread.id {:?}", thread::current().id()); self.user_app = Some(user_app); + self.tokio_rt.enter(); + self.init_http_server(); crate::Timer::instance().borrow_mut().init(); self.user_app.as_ref().unwrap().borrow_mut().init(); } @@ -131,6 +178,40 @@ impl App { return 0; } + pub fn add_immsg(&mut self, msg_id: u16, args: Option>>, + cb: Box::>>)>) { + let node = IMMsgNode{ + msg_id: msg_id, + args: args, + cb: Some(cb), + entry: Default::default(), + }; + self.im_mutex.lock(); + r9::ListHead::add_tail(&self.im_msgs, &node.entry); + } + + fn dispatch_immsg(&mut self) { + { + self.im_mutex.lock(); + if !self.im_msgs.borrow().empty() { + r9::ListHead::replace_init(&self.im_msgs, &self.im_work_msgs); + } + } + while !self.im_work_msgs.borrow().empty() { + { + let node = &self.im_work_msgs.borrow().first_entry(); + match &mut node.upgrade().unwrap().borrow_mut().cb { + Some(v) => { + (*v)(&node.upgrade().unwrap().borrow_mut().args); + } + None => { + } + } + node.upgrade().unwrap().borrow_mut().entry.borrow_mut().del_init(); + } + } + } + fn init_http_server(&mut self) { let (tx, _) = mpsc::channel();