|
|
|
@ -42,7 +42,7 @@ fn get_initialized_ironforce() -> (Arc<Mutex<IronForce>>, Keys) {
|
|
|
|
|
let ironforce = IronForce::from_file("".to_string()).unwrap(); |
|
|
|
|
let keys = ironforce.keys.clone(); |
|
|
|
|
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) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -213,27 +213,44 @@ impl Degeon {
|
|
|
|
|
|
|
|
|
|
#[pymethods] |
|
|
|
|
impl Degeon { |
|
|
|
|
/// Create a new text message and send it
|
|
|
|
|
pub fn send_text_message(&self, text: String, chat_i: usize) -> PyResult<()> { |
|
|
|
|
self.send_message( |
|
|
|
|
ProtocolMsg::NewMessage(DegMessage::new_text(text, &self.keys.get_public())), |
|
|
|
|
&self.chats[chat_i].pkey, |
|
|
|
|
) |
|
|
|
|
.map_err(|e| { |
|
|
|
|
pyo3::exceptions::PyTypeError::new_err(format!("There was an error in Rust: {:?}", e)) |
|
|
|
|
}) |
|
|
|
|
/// Create a new text message and send it (in thread)
|
|
|
|
|
///
|
|
|
|
|
/// `text` is the content of the message,
|
|
|
|
|
/// `chat_i` is the index of the chat in the list
|
|
|
|
|
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| println!("There was an error in Rust: {:?}", e)) |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Handle one message
|
|
|
|
|
pub fn handling_loop_iteration(&mut self) { |
|
|
|
|
let event = self.read_message_and_create_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 { |
|
|
|
|
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 { |
|
|
|
|
/// 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> { |
|
|
|
|
let msg_raw = self.ironforce.lock().unwrap().read_message(); |
|
|
|
|
let msg = msg_raw |
|
|
|
@ -317,7 +335,7 @@ impl Degeon {
|
|
|
|
|
} |
|
|
|
|
match msg { |
|
|
|
|
Some(Some(event)) => Some(event), |
|
|
|
|
_ => None |
|
|
|
|
_ => None, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -326,10 +344,19 @@ impl Stream for Degeon {
|
|
|
|
|
type Item = GuiEvent; |
|
|
|
|
|
|
|
|
|
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 msg = self.read_message_and_create_event(); |
|
|
|
|
if timestamp_0.elapsed() < 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))) |
|
|
|
|
} |
|
|
|
|