use anyhow::Result;
use std::sync::Arc;
use webrtc::peer_connection::RTCPeerConnection;
pub struct Peer {
pub peer_id: String,
pub room_id: String,
pub peer_connection: Arc<RTCPeerConnection>,
pub publishing: bool,
pub subscribing: Vec<String>, }
impl Peer {
pub fn new(peer_id: &str, room_id: &str, pc: Arc<RTCPeerConnection>) -> Self {
Self {
peer_id: peer_id.to_string(),
room_id: room_id.to_string(),
peer_connection: pc,
publishing: false,
subscribing: Vec::new(),
}
}
pub async fn notify_peer_joined(&self, new_peer_id: &str) -> Result<()> {
println!("Peer {} 通知: {} 加入房间", self.peer_id, new_peer_id);
Ok(())
}
pub async fn notify_peer_left(&self, left_peer_id: &str) -> Result<()> {
println!("Peer {} 通知: {} 离开房间", self.peer_id, left_peer_id);
Ok(())
}
pub fn set_publishing(&mut self, publishing: bool) {
self.publishing = publishing;
}
pub fn add_subscription(&mut self, publisher_id: String) {
if !self.subscribing.contains(&publisher_id) {
self.subscribing.push(publisher_id);
}
}
pub fn remove_subscription(&mut self, publisher_id: &str) {
self.subscribing.retain(|id| id != publisher_id);
}
pub async fn close(&self) -> Result<()> {
self.peer_connection.close().await?;
Ok(())
}
}