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