mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
implement mtxchat post()
This commit is contained in:
parent
3392b30794
commit
b095462f29
3 changed files with 56 additions and 19 deletions
|
|
@ -12,6 +12,8 @@ use std::fmt::Write as _;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Error, ErrorKind, Read, Write as StdWrite};
|
use std::io::{Error, ErrorKind, Read, Write as StdWrite};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
use trng::*;
|
||||||
use xous_ipc::Buffer;
|
use xous_ipc::Buffer;
|
||||||
|
|
||||||
/// PDDB Dict for mtxchat keys
|
/// PDDB Dict for mtxchat keys
|
||||||
|
|
@ -61,6 +63,7 @@ pub const HOSTED_MODE: bool = false;
|
||||||
//#[derive(Debug)]
|
//#[derive(Debug)]
|
||||||
pub struct MtxChat<'a> {
|
pub struct MtxChat<'a> {
|
||||||
chat: &'a Chat,
|
chat: &'a Chat,
|
||||||
|
trng: Trng,
|
||||||
netmgr: net::NetManager,
|
netmgr: net::NetManager,
|
||||||
user_id: String,
|
user_id: String,
|
||||||
user_name: String,
|
user_name: String,
|
||||||
|
|
@ -80,8 +83,10 @@ impl<'a> MtxChat<'a> {
|
||||||
pub fn new(chat: &Chat) -> MtxChat {
|
pub fn new(chat: &Chat) -> MtxChat {
|
||||||
let xns = xous_names::XousNames::new().unwrap();
|
let xns = xous_names::XousNames::new().unwrap();
|
||||||
let modals = Modals::new(&xns).expect("can't connect to Modals server");
|
let modals = Modals::new(&xns).expect("can't connect to Modals server");
|
||||||
|
let trng = Trng::new(&xns).unwrap();
|
||||||
let common = MtxChat {
|
let common = MtxChat {
|
||||||
chat: chat,
|
chat: chat,
|
||||||
|
trng: trng,
|
||||||
netmgr: net::NetManager::new(),
|
netmgr: net::NetManager::new(),
|
||||||
user_id: EMPTY.to_string(),
|
user_id: EMPTY.to_string(),
|
||||||
user_name: EMPTY.to_string(),
|
user_name: EMPTY.to_string(),
|
||||||
|
|
@ -535,6 +540,37 @@ impl<'a> MtxChat<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn gen_txn_id(&mut self) -> String {
|
||||||
|
let mut txn_id = self.trng.get_u32().expect("unable to generate random u32");
|
||||||
|
log::info!("trng.get_u32() = {}", txn_id);
|
||||||
|
txn_id += SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.expect("Time went backwards")
|
||||||
|
.subsec_nanos();
|
||||||
|
txn_id.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn post(&mut self, text: &str) {
|
||||||
|
if !self.logged_in {
|
||||||
|
if !self.login() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if self.room_id.len() == 0 {
|
||||||
|
if self.get_room_id().is_none() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let txn_id = self.gen_txn_id();
|
||||||
|
log::info!("txn_id = {}", txn_id);
|
||||||
|
let mut server = String::new();
|
||||||
|
write!(server, "{}{}", HTTPS, &self.user_domain).expect("failed to write server");
|
||||||
|
if web::send_message(&server, &self.room_id, &text, &txn_id, &self.token) {
|
||||||
|
log::info!("SENT: {}", text);
|
||||||
|
} else {
|
||||||
|
log::info!("FAILED TO SEND");
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn wifi_connected(&self) -> bool {
|
pub fn wifi_connected(&self) -> bool {
|
||||||
match self.netmgr.get_ipv4_config() {
|
match self.netmgr.get_ipv4_config() {
|
||||||
Some(conf) => conf.dhcp == com_rs::DhcpState::Bound,
|
Some(conf) => conf.dhcp == com_rs::DhcpState::Bound,
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,12 @@
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
use api::*;
|
use api::*;
|
||||||
use chat::{Chat, ChatOp, Event};
|
use chat::{Chat, Event, POST_TEXT_MAX};
|
||||||
use locales::t;
|
use locales::t;
|
||||||
use modals::Modals;
|
use modals::Modals;
|
||||||
use mtxchat::MtxChat;
|
use mtxchat::MtxChat;
|
||||||
use num_traits::*;
|
use num_traits::*;
|
||||||
|
use xous_ipc::Buffer;
|
||||||
|
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
let stack_size = 1024 * 1024;
|
let stack_size = 1024 * 1024;
|
||||||
|
|
@ -62,6 +63,7 @@ fn wrapped_main() -> ! {
|
||||||
|
|
||||||
let modals = Modals::new(&xns).expect("can't connect to Modals server");
|
let modals = Modals::new(&xns).expect("can't connect to Modals server");
|
||||||
let mut first_focus = true;
|
let mut first_focus = true;
|
||||||
|
let mut user_post: Option<String> = None;
|
||||||
loop {
|
loop {
|
||||||
let msg = xous::receive_message(sid).unwrap();
|
let msg = xous::receive_message(sid).unwrap();
|
||||||
log::debug!("got message {:?}", msg);
|
log::debug!("got message {:?}", msg);
|
||||||
|
|
@ -83,7 +85,15 @@ fn wrapped_main() -> ! {
|
||||||
}
|
}
|
||||||
Some(MtxchatOp::Menu) => {
|
Some(MtxchatOp::Menu) => {
|
||||||
Some(MtxchatOp::Post) => {
|
Some(MtxchatOp::Post) => {
|
||||||
log::info!("TODO Post to Matrix server");
|
let buffer =
|
||||||
|
unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
||||||
|
let s = buffer
|
||||||
|
.to_original::<xous_ipc::String<{ POST_TEXT_MAX }>, _>()
|
||||||
|
.unwrap();
|
||||||
|
if s.len() > 0 {
|
||||||
|
// capture input instead of calling here, so message can drop and calling server is released
|
||||||
|
user_post = Some(s.to_string());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Some(MtxchatOp::Rawkeys) => {}
|
Some(MtxchatOp::Rawkeys) => {}
|
||||||
Some(MtxchatOp::Quit) => {
|
Some(MtxchatOp::Quit) => {
|
||||||
|
|
@ -93,6 +103,10 @@ fn wrapped_main() -> ! {
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
if let Some(post) = user_post {
|
||||||
|
mtxchat.post(&post);
|
||||||
|
user_post = None;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// clean up our program
|
// clean up our program
|
||||||
log::error!("main loop exit, destroying servers");
|
log::error!("main loop exit, destroying servers");
|
||||||
|
|
|
||||||
|
|
@ -193,20 +193,11 @@ pub fn server(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Some(ChatOp::GamLine) => {
|
Some(ChatOp::GamLine) => {
|
||||||
let buffer =
|
log::info!("got ChatOp::GamLine");
|
||||||
unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
|
||||||
let s = buffer
|
|
||||||
.to_original::<xous_ipc::String<{ POST_TEXT_MAX }>, _>()
|
|
||||||
.unwrap();
|
|
||||||
//log::trace!("ui got input line: {}", s.as_str());
|
|
||||||
ui.input = Some(s);
|
|
||||||
user_post = true; // set a flag, instead of calling here, so message can drop and calling server is released
|
|
||||||
}
|
|
||||||
Some(ChatOp::GamRawkeys) => {
|
|
||||||
if let Some(cid) = app_cid {
|
if let Some(cid) = app_cid {
|
||||||
if let Some(opcode) = opcode_rawkeys {
|
if let Some(opcode) = opcode_post {
|
||||||
msg.forward(cid, opcode)
|
log::info!("Forwarding msg to Chat App: {:?}", msg);
|
||||||
.unwrap_or(log::warn!("failed to forward RawKeys msg"));
|
msg.forward(cid, opcode).expect("failed to fwd msg");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -266,10 +257,6 @@ pub fn server(
|
||||||
}
|
}
|
||||||
_ => log::warn!("got unknown message"),
|
_ => log::warn!("got unknown message"),
|
||||||
}
|
}
|
||||||
if user_post {
|
|
||||||
log::warn!("TODO relay new user post to Chat App");
|
|
||||||
user_post = false;
|
|
||||||
}
|
|
||||||
log::trace!("reached bottom of main loop");
|
log::trace!("reached bottom of main loop");
|
||||||
}
|
}
|
||||||
// clean up our program
|
// clean up our program
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue