1
This commit is contained in:
parent
88640ac26f
commit
0c1e411318
@ -3,13 +3,26 @@ use std::cell::RefCell;
|
|||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::runtime::Runtime;
|
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 actix_web::App as WebApp;
|
||||||
use std::{sync::mpsc, thread};
|
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::SharedFromSelf;
|
||||||
use r9_macro_derive::SharedFromSelf;
|
use r9_macro_derive::SharedFromSelf;
|
||||||
|
|
||||||
|
pub type HttpRequestCb = Rc::<dyn FnMut (HttpRequest)>;
|
||||||
|
|
||||||
pub trait UserApp {
|
pub trait UserApp {
|
||||||
fn get_pkg_name(&self) -> String;
|
fn get_pkg_name(&self) -> String;
|
||||||
fn init(&mut self);
|
fn init(&mut self);
|
||||||
@ -26,17 +39,46 @@ pub struct App {
|
|||||||
instance_id: i32,
|
instance_id: i32,
|
||||||
tokio_rt: Runtime,
|
tokio_rt: Runtime,
|
||||||
user_app: Option<Rc::<RefCell::<dyn UserApp>>>,
|
user_app: Option<Rc::<RefCell::<dyn UserApp>>>,
|
||||||
|
im_msgs: Rc::<RefCell::<r9::ListHead::<IMMsgNode>>>,
|
||||||
|
im_work_msgs: Rc::<RefCell::<r9::ListHead::<IMMsgNode>>>,
|
||||||
|
im_mutex: Mutex<i32>,
|
||||||
|
http_handlers: Arc<Mutex<HashMap<String, HttpRequestCb>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AppState {
|
||||||
|
app_name: String
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct IMMsgNode {
|
||||||
|
msg_id: u16,
|
||||||
|
args: Option<Vec<Rc::<dyn Any>>>,
|
||||||
|
cb: Option<Box::<dyn FnMut (&Option<Vec<Rc::<dyn Any>>>)>>,
|
||||||
|
entry: Rc::<RefCell::<r9::ListHead::<IMMsgNode>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn index(data: Data<AppState>) -> impl Responder {
|
||||||
|
println!("http.thread.id {:?}", thread::current().id());
|
||||||
|
HttpResponse::Ok().body("Hello world!")
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run_app(port: u16, tx: mpsc::Sender<ServerHandle>) -> std::io::Result<()> {
|
async fn run_app(port: u16, tx: mpsc::Sender<ServerHandle>) -> std::io::Result<()> {
|
||||||
let server = HttpServer::new(|| {
|
let server = HttpServer::new(|| {
|
||||||
WebApp::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
|
|| async
|
||||||
{
|
{
|
||||||
"{}"
|
"{}"
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
.service(hello)
|
||||||
|
*/
|
||||||
})
|
})
|
||||||
.bind(("0.0.0.0", port))?
|
.bind(("0.0.0.0", port))?
|
||||||
.workers(1)
|
.workers(1)
|
||||||
@ -64,6 +106,10 @@ impl App {
|
|||||||
instance_id: 0,
|
instance_id: 0,
|
||||||
tokio_rt: Runtime::new().unwrap(),
|
tokio_rt: Runtime::new().unwrap(),
|
||||||
user_app: None,
|
user_app: None,
|
||||||
|
http_handlers: Default::default(),
|
||||||
|
im_msgs: r9::ListHead::<IMMsgNode>::new_head(),
|
||||||
|
im_work_msgs: r9::ListHead::<IMMsgNode>::new_head(),
|
||||||
|
im_mutex: Mutex::new(1),
|
||||||
_self_wp: Default::default(),
|
_self_wp: Default::default(),
|
||||||
}
|
}
|
||||||
)));
|
)));
|
||||||
@ -76,9 +122,10 @@ impl App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(&mut self, user_app: Rc::<RefCell::<dyn UserApp>>) {
|
pub fn init(&mut self, user_app: Rc::<RefCell::<dyn UserApp>>) {
|
||||||
//self.tokio_rt.enter();
|
println!("main.thread.id {:?}", thread::current().id());
|
||||||
self.init_http_server();
|
|
||||||
self.user_app = Some(user_app);
|
self.user_app = Some(user_app);
|
||||||
|
self.tokio_rt.enter();
|
||||||
|
self.init_http_server();
|
||||||
crate::Timer::instance().borrow_mut().init();
|
crate::Timer::instance().borrow_mut().init();
|
||||||
self.user_app.as_ref().unwrap().borrow_mut().init();
|
self.user_app.as_ref().unwrap().borrow_mut().init();
|
||||||
}
|
}
|
||||||
@ -131,6 +178,40 @@ impl App {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_immsg(&mut self, msg_id: u16, args: Option<Vec<Rc::<dyn Any>>>,
|
||||||
|
cb: Box::<dyn FnMut (&Option<Vec<Rc::<dyn Any>>>)>) {
|
||||||
|
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) {
|
fn init_http_server(&mut self) {
|
||||||
let (tx, _) = mpsc::channel();
|
let (tx, _) = mpsc::channel();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user