1
This commit is contained in:
parent
eb14a702c3
commit
dea499028d
@ -50,7 +50,6 @@ pub struct App {
|
|||||||
im_work_msgs: Rc::<RefCell::<r9::ListHead::<IMMsgNode>>>,
|
im_work_msgs: Rc::<RefCell::<r9::ListHead::<IMMsgNode>>>,
|
||||||
im_mutex: Mutex<i32>,
|
im_mutex: Mutex<i32>,
|
||||||
http_handlers: HashMap<String, Box::<dyn FnMut (&mut HttpContext)>>,
|
http_handlers: HashMap<String, Box::<dyn FnMut (&mut HttpContext)>>,
|
||||||
//http_handlers: HashMap<String, Box::<dyn Fn (&HttpContext)>>,
|
|
||||||
webapp_state: Arc::<AppState>,
|
webapp_state: Arc::<AppState>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,16 +151,17 @@ impl App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn uninit() {
|
pub fn uninit() {
|
||||||
//.user_app.as_ref().unwrap().borrow_mut().uninit();
|
let user_app = App::instance().borrow_mut().user_app.clone();
|
||||||
|
user_app.as_ref().unwrap().borrow_mut().uninit();
|
||||||
crate::Timer::instance().borrow_mut().uninit();
|
crate::Timer::instance().borrow_mut().uninit();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
loop {
|
loop {
|
||||||
crate::Timer::update();
|
crate::Timer::update();
|
||||||
|
App::dispatch_immsg();
|
||||||
|
App::dispatch_httprequest();
|
||||||
std::thread::sleep(Duration::from_millis(1));
|
std::thread::sleep(Duration::from_millis(1));
|
||||||
//self.dispatch_immsg();
|
|
||||||
//self.dispatch_httprequest();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -213,16 +213,11 @@ impl App {
|
|||||||
r9::ListHead::add_tail(&self.im_msgs, &node.entry);
|
r9::ListHead::add_tail(&self.im_msgs, &node.entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dispatch_immsg(&mut self) {
|
fn dispatch_immsg() {
|
||||||
{
|
let im_work_msgs = App::instance().borrow_mut().fetch_immsg();
|
||||||
let _ = self.im_mutex.lock();
|
while !im_work_msgs.borrow().empty() {
|
||||||
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();
|
let node = &im_work_msgs.borrow().first_entry();
|
||||||
match &mut node.upgrade().unwrap().borrow_mut().cb {
|
match &mut node.upgrade().unwrap().borrow_mut().cb {
|
||||||
Some(v) => {
|
Some(v) => {
|
||||||
(*v)(&node.upgrade().unwrap().borrow_mut().args);
|
(*v)(&node.upgrade().unwrap().borrow_mut().args);
|
||||||
@ -235,35 +230,60 @@ impl App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dispatch_httprequest(&mut self) {
|
fn fetch_immsg(&mut self) -> Rc::<RefCell::<r9::ListHead::<IMMsgNode>>> {
|
||||||
|
r9::ListHead::replace_init(&self.im_msgs, &self.im_work_msgs);
|
||||||
|
return self.im_work_msgs.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dispatch_httprequest() {
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct Ca {
|
struct Ca {
|
||||||
c: String,
|
c: String,
|
||||||
a: String
|
a: String
|
||||||
}
|
}
|
||||||
{
|
let pending_list = App::instance().borrow_mut().fetch_request();
|
||||||
let pending_list = &mut self.webapp_state.request.lock().unwrap();
|
match pending_list {
|
||||||
while pending_list.len() > 0 {
|
Some(list) => {
|
||||||
match pending_list.pop() {
|
for req in list {
|
||||||
Some(v) => {
|
let ctx = &mut req.lock().unwrap();
|
||||||
let ctx = &mut v.lock().unwrap();
|
let ca = web::Query::<Ca>::from_query(&ctx.query_str).unwrap();
|
||||||
let req = web::Query::<Ca>::from_query(&ctx.query_str).unwrap();
|
let key = format!("{}${}", &ca.c, &ca.a);
|
||||||
let key = format!("{}${}", &req.c, &req.a);
|
match &mut App::instance().borrow_mut().get_http_handle(&key) {
|
||||||
match self.http_handlers.get_mut(&key) {
|
Some(handle) => {
|
||||||
Some(v1) => {
|
(handle)(ctx);
|
||||||
(v1)(ctx);
|
ctx.handled = true;
|
||||||
ctx.handled = true;
|
}
|
||||||
}
|
None => {
|
||||||
None => {
|
//ctx.handled = true;
|
||||||
//ctx.handled = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
None => {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fetch_request(&mut self) -> Option<Vec<Arc<Mutex<HttpContext>>>> {
|
||||||
|
let pending_list = &mut self.webapp_state.request.lock().unwrap();
|
||||||
|
if pending_list.len() <= 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let result = pending_list.clone();
|
||||||
|
pending_list.clear();
|
||||||
|
return Some(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_http_handle(&mut self, key: &String) ->
|
||||||
|
Option<&mut Box::<dyn FnMut (&mut HttpContext)>> {
|
||||||
|
match self.http_handlers.get_mut(key) {
|
||||||
|
Some(v) => {
|
||||||
|
return Some(v);
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,7 +298,8 @@ impl App {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_http_handler(&mut self, key: String, cb: Box::<dyn FnMut (&mut HttpContext)>) {
|
pub fn add_http_handler(&mut self, key: String,
|
||||||
|
cb: Box::<dyn FnMut (&mut HttpContext)>) {
|
||||||
self.http_handlers.insert(key, cb);
|
self.http_handlers.insert(key, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user