use std::{fmt::{self, Debug},sync::Arc};
use webrtc::data_channel::RTCDataChannel;
use crate::{
server::types::{RoomId, RoomUser, UserMessage},
client::{message::Message, rtc_base::WebConnection, signaling::negotiator::HandshakeState},
app::{
event::BasicEvent,
file_manager::{FileProgressReport, InputFile, SpeedReport},
},
};
#[derive(Clone, Debug)]
pub enum AppEvent {
None,
FocusNext,
FocusPrev,
Client(AppEventClient),
Server(AppEventServer),
}
impl From<AppEvent> for BasicEvent {
fn from(ev: AppEvent) -> Self {
Self::App(ev)
}
}
#[derive(Clone, Debug)]
pub enum AppEventClient {
Quit,
InitConnection(WebConnection),
ChannelOpened(DebugDataChannel),
Connected,
Disconnected,
UpdateHandshakeState(HandshakeState),
ManualSignalingInit(bool),
ManualSignalingInput(String),
ManualSignalingOutput(String),
MessageReceived(Message),
OutputFileProgress(FileProgressReport),
ReportFileSpeed(SpeedReport),
InputFileProgress(FileProgressReport),
OutputFileFinished(DebugDataChannel),
InputFileNew(InputFile),
MetaSent(DebugDataChannel),
}
impl From<AppEventClient> for AppEvent {
fn from(ev: AppEventClient) -> Self {
Self::Client(ev)
}
}
impl From<AppEventClient> for BasicEvent {
fn from(ev: AppEventClient) -> Self {
Self::App(AppEvent::from(ev))
}
}
#[derive(Clone, Debug)]
pub enum AppEventServer {
Quit,
AddRoom(RoomId),
RemoveRoom(RoomId),
AddRoomUser(RoomUser),
RemoveRoomUser(RoomUser),
AddMessage(UserMessage),
}
impl From<AppEventServer> for AppEvent {
fn from(ev: AppEventServer) -> Self {
Self::Server(ev)
}
}
impl From<AppEventServer> for BasicEvent {
fn from(ev: AppEventServer) -> Self {
Self::App(AppEvent::from(ev))
}
}
#[derive(Clone)]
pub struct DebugDataChannel {
pub dc: Arc<RTCDataChannel>,
}
impl DebugDataChannel {
pub fn new(dc: Arc<RTCDataChannel>) -> Self {
Self { dc }
}
}
impl fmt::Debug for DebugDataChannel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let channel = &self.dc;
let mut debug_struct = f.debug_struct("RTCDataChannel");
debug_struct
.field("id", &channel.id())
.field("label", &channel.label())
.field("protocol", &channel.protocol())
.field("ordered", &channel.ordered())
.field("max_retransmits", &channel.max_retransmits())
.field("max_packet_lifetime", &channel.max_packet_lifetime())
.field("ready_state", &channel.ready_state());
debug_struct.finish()
}
}