|
|
|
@ -3,11 +3,12 @@ use crate::message::{DegMessage, ServiceMsg};
|
|
|
|
|
use core::default::Default; |
|
|
|
|
use futures::Stream; |
|
|
|
|
use iced::{ |
|
|
|
|
button, Align, Application, Button, Column, Element, HorizontalAlignment, Length, Row, |
|
|
|
|
Text, TextInput, VerticalAlignment, |
|
|
|
|
button, Align, Application, Button, Column, Element, HorizontalAlignment, Length, Row, Text, |
|
|
|
|
TextInput, VerticalAlignment, |
|
|
|
|
}; |
|
|
|
|
use ironforce::res::{IFError, IFResult}; |
|
|
|
|
use ironforce::{IronForce, Keys, Message, MessageType, PublicKey}; |
|
|
|
|
use std::hash::Hash; |
|
|
|
|
use std::pin::Pin; |
|
|
|
|
use std::sync::{Arc, Mutex}; |
|
|
|
|
use std::task::{Context, Poll}; |
|
|
|
@ -223,7 +224,10 @@ impl Degeon {
|
|
|
|
|
|
|
|
|
|
pub fn process_message(&self, msg: ironforce::Message) -> IFResult<Option<GuiEvent>> { |
|
|
|
|
let deg_msg: DegMessage = |
|
|
|
|
serde_json::from_slice(msg.get_decrypted(&self.keys)?.as_slice())?; |
|
|
|
|
match serde_json::from_slice(msg.get_decrypted(&self.keys)?.as_slice()) { |
|
|
|
|
Ok(r) => r, |
|
|
|
|
Err(_) => return Ok(None) |
|
|
|
|
}; |
|
|
|
|
let sender = msg.get_sender(&self.keys).unwrap(); |
|
|
|
|
Ok(match °_msg { |
|
|
|
|
DegMessage::Text(_) | DegMessage::File(_) => { |
|
|
|
@ -240,9 +244,8 @@ impl Degeon {
|
|
|
|
|
Some(GuiEvent::SetName(sender, name.to_string())) |
|
|
|
|
} |
|
|
|
|
ServiceMsg::Ping => self |
|
|
|
|
.send_message(DegMessage::Service(ServiceMsg::HiThere), &sender) |
|
|
|
|
.send_message(DegMessage::Service(ServiceMsg::NameStatement(self.my_name.clone())), &sender) |
|
|
|
|
.map(|_| None)?, |
|
|
|
|
ServiceMsg::HiThere => Some(GuiEvent::NewChat(sender)), |
|
|
|
|
}, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
@ -258,21 +261,21 @@ impl Degeon {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn send_message(&self, msg: DegMessage, target: &PublicKey) -> IFResult<()> { |
|
|
|
|
if self.ironforce.lock().unwrap().get_tunnel(target).is_none() { |
|
|
|
|
println!("Creating a tunnel"); |
|
|
|
|
self.ironforce |
|
|
|
|
.lock() |
|
|
|
|
.unwrap() |
|
|
|
|
.initialize_tunnel_creation(target)?; |
|
|
|
|
let mut counter = 0; |
|
|
|
|
while self.ironforce.lock().unwrap().get_tunnel(target).is_none() { |
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(350)); |
|
|
|
|
counter += 1; |
|
|
|
|
if counter > 100 { |
|
|
|
|
return Err(IFError::TunnelNotFound); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// if self.ironforce.lock().unwrap().get_tunnel(target).is_none() {
|
|
|
|
|
// println!("Creating a tunnel");
|
|
|
|
|
// self.ironforce
|
|
|
|
|
// .lock()
|
|
|
|
|
// .unwrap()
|
|
|
|
|
// .initialize_tunnel_creation(target)?;
|
|
|
|
|
// let mut counter = 0;
|
|
|
|
|
// while self.ironforce.lock().unwrap().get_tunnel(target).is_none() {
|
|
|
|
|
// std::thread::sleep(std::time::Duration::from_millis(350));
|
|
|
|
|
// counter += 1;
|
|
|
|
|
// if counter > 100 {
|
|
|
|
|
// return Err(IFError::TunnelNotFound);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
self.ironforce.lock().unwrap().send_to_all( |
|
|
|
|
Message::build() |
|
|
|
|
.message_type(MessageType::Broadcast) |
|
|
|
@ -290,9 +293,12 @@ impl Stream for Degeon {
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { |
|
|
|
|
println!("Degeon worker is being polled"); |
|
|
|
|
let msg = self.ironforce.lock().unwrap().read_message(); |
|
|
|
|
println!("Msg: {:?}", msg); |
|
|
|
|
match msg.map(|msg| self.process_message(msg).unwrap()) { |
|
|
|
|
None | Some(None) => Poll::Pending, |
|
|
|
|
Some(Some(msg)) => Poll::Ready(Some(msg)), |
|
|
|
|
Some(Some(msg)) => { |
|
|
|
|
Poll::Ready(Some(msg)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -308,6 +314,13 @@ where
|
|
|
|
|
|
|
|
|
|
std::any::TypeId::of::<Self>().hash(state); |
|
|
|
|
self.ironforce.lock().unwrap().hash(state); |
|
|
|
|
|
|
|
|
|
// std::time::SystemTime::now().hash(state);
|
|
|
|
|
std::time::UNIX_EPOCH |
|
|
|
|
.elapsed() |
|
|
|
|
.unwrap() |
|
|
|
|
.as_secs() |
|
|
|
|
.hash(state); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn stream( |
|
|
|
@ -414,11 +427,7 @@ impl Application for State {
|
|
|
|
|
.push((true, new_msg.clone())); |
|
|
|
|
let data_cloned = self.data.clone(); |
|
|
|
|
let target = self.data.chats[self.selected_chat].pkey.clone(); |
|
|
|
|
std::thread::spawn(move || { |
|
|
|
|
data_cloned |
|
|
|
|
.send_message(new_msg, &target) |
|
|
|
|
.unwrap() |
|
|
|
|
}); |
|
|
|
|
std::thread::spawn(move || data_cloned.send_message(new_msg, &target).unwrap()); |
|
|
|
|
} |
|
|
|
|
GuiEvent::NewChat(pkey) => { |
|
|
|
|
if self.data.chat_with(&pkey).is_none() { |
|
|
|
|