|
|
@ -42,7 +42,7 @@ fn get_initialized_ironforce() -> (Arc<Mutex<IronForce>>, Keys) { |
|
|
|
let ironforce = IronForce::from_file("".to_string()).unwrap(); |
|
|
|
let ironforce = IronForce::from_file("".to_string()).unwrap(); |
|
|
|
let keys = ironforce.keys.clone(); |
|
|
|
let keys = ironforce.keys.clone(); |
|
|
|
println!("ID: {}", keys.get_public().get_short_id()); |
|
|
|
println!("ID: {}", keys.get_public().get_short_id()); |
|
|
|
let (_thread, ironforce) = ironforce.launch_main_loop(1000); |
|
|
|
let (_thread, ironforce) = ironforce.launch_main_loop(270); |
|
|
|
(ironforce, keys) |
|
|
|
(ironforce, keys) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -213,27 +213,44 @@ impl Degeon { |
|
|
|
|
|
|
|
|
|
|
|
#[pymethods] |
|
|
|
#[pymethods] |
|
|
|
impl Degeon { |
|
|
|
impl Degeon { |
|
|
|
/// Create a new text message and send it
|
|
|
|
/// Create a new text message and send it (in thread)
|
|
|
|
pub fn send_text_message(&self, text: String, chat_i: usize) -> PyResult<()> { |
|
|
|
///
|
|
|
|
self.send_message( |
|
|
|
/// `text` is the content of the message,
|
|
|
|
ProtocolMsg::NewMessage(DegMessage::new_text(text, &self.keys.get_public())), |
|
|
|
/// `chat_i` is the index of the chat in the list
|
|
|
|
&self.chats[chat_i].pkey, |
|
|
|
pub fn send_text_message(&mut self, text: String, chat_i: usize) { |
|
|
|
|
|
|
|
let msg = DegMessage::new_text(text, &self.keys.get_public()); |
|
|
|
|
|
|
|
self.chats[chat_i].messages.push(msg.clone()); |
|
|
|
|
|
|
|
let cloned_self = self.clone(); |
|
|
|
|
|
|
|
std::thread::spawn(move || { |
|
|
|
|
|
|
|
cloned_self |
|
|
|
|
|
|
|
.send_message( |
|
|
|
|
|
|
|
ProtocolMsg::NewMessage(msg), |
|
|
|
|
|
|
|
&cloned_self.chats[chat_i].pkey, |
|
|
|
) |
|
|
|
) |
|
|
|
.map_err(|e| { |
|
|
|
.map_err(|e| println!("There was an error in Rust: {:?}", e)) |
|
|
|
pyo3::exceptions::PyTypeError::new_err(format!("There was an error in Rust: {:?}", e)) |
|
|
|
}); |
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Handle one message
|
|
|
|
/// Handle one message
|
|
|
|
pub fn handling_loop_iteration(&mut self) { |
|
|
|
pub fn handling_loop_iteration(&mut self) { |
|
|
|
let event = self.read_message_and_create_event(); |
|
|
|
let event = self.read_message_and_create_event(); |
|
|
|
if let Some(event) = event { |
|
|
|
if let Some(event) = event { |
|
|
|
self.process_event(&event, true).unwrap_or_else(|e| println!("Error: {:?}", e)); |
|
|
|
self.process_event(&event, true) |
|
|
|
|
|
|
|
.unwrap_or_else(|e| println!("Error: {:?}", e)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Get length of the IF's message queue
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// If IF worker is locked, returns 0
|
|
|
|
pub fn message_queue_len(&self) -> usize { |
|
|
|
pub fn message_queue_len(&self) -> usize { |
|
|
|
self.ironforce.lock().unwrap().messages.len() |
|
|
|
self.ironforce.try_lock().map(|r| r.messages.len()).unwrap_or(0) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Check if the message was written by the current user (since `PublicKey` isn't a python type).
|
|
|
|
|
|
|
|
/// Returns True if the author of the message is the current user
|
|
|
|
|
|
|
|
pub fn check_message_ownership(&self, message: &DegMessage) -> bool { |
|
|
|
|
|
|
|
message.sender == self.keys.get_public() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -293,6 +310,7 @@ impl Degeon { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Degeon { |
|
|
|
impl Degeon { |
|
|
|
|
|
|
|
/// Get one message from the IF message queue and process it, resulting in an event
|
|
|
|
pub fn read_message_and_create_event(&self) -> Option<GuiEvent> { |
|
|
|
pub fn read_message_and_create_event(&self) -> Option<GuiEvent> { |
|
|
|
let msg_raw = self.ironforce.lock().unwrap().read_message(); |
|
|
|
let msg_raw = self.ironforce.lock().unwrap().read_message(); |
|
|
|
let msg = msg_raw |
|
|
|
let msg = msg_raw |
|
|
@ -317,7 +335,7 @@ impl Degeon { |
|
|
|
} |
|
|
|
} |
|
|
|
match msg { |
|
|
|
match msg { |
|
|
|
Some(Some(event)) => Some(event), |
|
|
|
Some(Some(event)) => Some(event), |
|
|
|
_ => None |
|
|
|
_ => None, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -326,10 +344,19 @@ impl Stream for Degeon { |
|
|
|
type Item = GuiEvent; |
|
|
|
type Item = GuiEvent; |
|
|
|
|
|
|
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { |
|
|
|
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { |
|
|
|
|
|
|
|
if self.ironforce.try_lock().is_err() { |
|
|
|
|
|
|
|
return Poll::Ready(Some(GuiEvent::None)); |
|
|
|
|
|
|
|
} |
|
|
|
let timestamp_0 = std::time::Instant::now(); |
|
|
|
let timestamp_0 = std::time::Instant::now(); |
|
|
|
let msg = self.read_message_and_create_event(); |
|
|
|
let msg = self.read_message_and_create_event(); |
|
|
|
if timestamp_0.elapsed() < std::time::Duration::from_millis(5) { |
|
|
|
if timestamp_0.elapsed() < std::time::Duration::from_millis(5) { |
|
|
|
std::thread::sleep(std::time::Duration::from_millis(5)); |
|
|
|
std::thread::sleep(std::time::Duration::from_millis(5)); |
|
|
|
|
|
|
|
if rand::random::<bool>() { |
|
|
|
|
|
|
|
return Poll::Pending; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if timestamp_0.elapsed() > std::time::Duration::from_millis(800) { |
|
|
|
|
|
|
|
println!("Poll_next took {:?}", timestamp_0.elapsed()); |
|
|
|
} |
|
|
|
} |
|
|
|
Poll::Ready(Some(msg.unwrap_or(GuiEvent::None))) |
|
|
|
Poll::Ready(Some(msg.unwrap_or(GuiEvent::None))) |
|
|
|
} |
|
|
|
} |
|
|
|