use anyhow::Result;
use std::net::SocketAddr;
use std::collections::{BTreeMap, HashMap};
use std::sync::{Arc, Mutex};
use tokio::net::{TcpListener, TcpStream};
use serde::{Deserialize, Serialize};
use std::sync::RwLock;
use std::net::TcpListener as StdTcpListener;
pub struct DebugServer {
addr: SocketAddr,
listener: Option<TcpListener>,
running: Arc<RwLock<bool>>,
}
impl DebugServer {
pub async fn new(addr: SocketAddr) -> Result<Self> {
Ok(Self {
addr,
listener: None,
running: Arc::new(Mutex::new(false)))
})
}
pub async fn start(&mut self) -> Result<()> {
let listener: _ = TcpListener::bind(self.addr).await?;
self.listener = Some(listener);
*self.running.write().await = true;
Ok(())
}
pub async fn stop(&mut self) -> Result<()> {
*self.running.write().await = false;
self.listener = None;
Ok(())
}
pub async fn is_running(&self) -> bool {
*self.running.read().await
}
}
pub struct SessionManager {
sessions: std::collections::HashMap<String, Session>,
}
#[derive(Debug, Clone)]
struct Session {
id: String,
client_name: String,
created_at: String,
}
impl SessionManager {
pub fn new() -> Self {
Self {
sessions: std::collections::HashMap::new(),
}
}
pub async fn create_session(&mut self, client_name: String) -> Result<String> {
let session_id: _ = format!("session_{}", self.sessions.len());
let session: _ = Session {
id: session_id.clone(),
client_name,
created_at: "now".to_string(),
};
self.sessions.insert(session_id.clone(), session);
Ok(session_id)
}
pub async fn get_session(&self, session_id: &str) -> Option<&Session> {
self.sessions.get(session_id)
}
pub async fn close_session(&mut self, session_id: &str) -> Result<()> {
self.sessions.remove(session_id);
Ok(())
}
}
pub struct WebSocketHandler {
}
impl WebSocketHandler {
pub fn new() -> Self {
Self {}
}
pub async fn serialize_message(&self, message: &DebugProtocol) -> Result<String> {
let json: _ = serde_json::to_string(message)?;
Ok(json)
}
pub async fn deserialize_message(&self, data: &str) -> Result<DebugProtocol> {
let message: DebugProtocol = serde_json::from_str(data)?;
Ok(message)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DebugProtocol {
SetBreakpoint {
file: String,
line: u32,
condition: Option<String>,
},
RemoveBreakpoint {
id: u32,
},
Continue,
StepOver,
StepInto,
StepOut,
Evaluate {
expression: String,
},
}
pub type SessionId = String;