refactor: Consolidate network sync operations into LatticeServer methods
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//! CLI command handlers
|
||||
|
||||
use lattice_core::{Node, StoreHandle};
|
||||
use lattice_net::LatticeEndpoint;
|
||||
use lattice_net::LatticeServer;
|
||||
|
||||
/// Result of a command that may switch stores or exit
|
||||
pub enum CommandResult {
|
||||
@@ -18,7 +18,7 @@ pub fn block_async<F: std::future::Future>(f: F) -> F::Output {
|
||||
tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(f))
|
||||
}
|
||||
|
||||
pub type Handler = fn(&Node, Option<&StoreHandle>, Option<&LatticeEndpoint>, &[String]) -> CommandResult;
|
||||
pub type Handler = fn(&Node, Option<&StoreHandle>, Option<&LatticeServer>, &[String]) -> CommandResult;
|
||||
|
||||
pub struct Command {
|
||||
pub name: &'static str,
|
||||
@@ -53,7 +53,7 @@ pub fn commands() -> Vec<Command> {
|
||||
cmds
|
||||
}
|
||||
|
||||
fn cmd_help(_node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, _args: &[String]) -> CommandResult {
|
||||
fn cmd_help(_node: &Node, _store: Option<&StoreHandle>, _server: Option<&LatticeServer>, _args: &[String]) -> CommandResult {
|
||||
let cmds = commands();
|
||||
let mut last_group = "";
|
||||
for cmd in &cmds {
|
||||
@@ -73,7 +73,7 @@ fn cmd_help(_node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<&Latti
|
||||
CommandResult::Ok
|
||||
}
|
||||
|
||||
fn cmd_quit(_node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, _args: &[String]) -> CommandResult {
|
||||
fn cmd_quit(_node: &Node, _store: Option<&StoreHandle>, _server: Option<&LatticeServer>, _args: &[String]) -> CommandResult {
|
||||
println!("Goodbye!");
|
||||
CommandResult::Quit
|
||||
}
|
||||
|
||||
+7
-12
@@ -4,7 +4,7 @@ mod commands;
|
||||
mod node_commands;
|
||||
mod store_commands;
|
||||
|
||||
use lattice_net::spawn_accept_loop;
|
||||
use lattice_net::LatticeServer;
|
||||
use commands::CommandResult;
|
||||
use lattice_core::{NodeBuilder, StoreHandle};
|
||||
use rustyline::error::ReadlineError;
|
||||
@@ -24,11 +24,11 @@ async fn main() {
|
||||
}
|
||||
};
|
||||
|
||||
// Start Iroh endpoint using same Ed25519 identity
|
||||
let endpoint = match lattice_net::LatticeEndpoint::new(node.secret_key_bytes()).await {
|
||||
Ok(ep) => {
|
||||
println!("Iroh: {} (listening)", ep.public_key().fmt_short());
|
||||
Some(ep)
|
||||
// Create LatticeServer (creates endpoint and spawns accept loop internally)
|
||||
let server = match LatticeServer::new_from_node(node.clone()).await {
|
||||
Ok(s) => {
|
||||
println!("Iroh: {} (listening)", s.endpoint().public_key().fmt_short());
|
||||
Some(s)
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Warning: Iroh failed to start: {}", e);
|
||||
@@ -36,11 +36,6 @@ async fn main() {
|
||||
}
|
||||
};
|
||||
|
||||
// Spawn accept loop for incoming connections
|
||||
if let Some(ref ep) = endpoint {
|
||||
spawn_accept_loop(node.clone(), ep.endpoint().clone());
|
||||
}
|
||||
|
||||
let info = node.info();
|
||||
println!("Node ID: {}", info.node_id);
|
||||
println!("Data: {}", info.data_path);
|
||||
@@ -100,7 +95,7 @@ async fn main() {
|
||||
if cmd_args.len() < cmd.min_args || cmd_args.len() > cmd.max_args {
|
||||
println!("Usage: {} {}", cmd.name, cmd.args);
|
||||
} else {
|
||||
match (cmd.handler)(&node, current_store.as_ref(), endpoint.as_ref(), cmd_args) {
|
||||
match (cmd.handler)(&node, current_store.as_ref(), server.as_ref(), cmd_args) {
|
||||
CommandResult::Ok => {}
|
||||
CommandResult::SwitchTo(h) => {
|
||||
current_store = Some(h);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use crate::commands::{block_async, Command, CommandResult, Handler};
|
||||
use lattice_core::{Node, StoreHandle, PeerStatus, Uuid};
|
||||
use lattice_net::LatticeEndpoint;
|
||||
use lattice_net::LatticeServer;
|
||||
use chrono::DateTime;
|
||||
use std::time::Instant;
|
||||
|
||||
@@ -26,7 +26,7 @@ pub fn node_commands() -> Vec<Command> {
|
||||
|
||||
// --- Store management ---
|
||||
|
||||
fn cmd_init(node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, _args: &[String]) -> CommandResult {
|
||||
fn cmd_init(node: &Node, _store: Option<&StoreHandle>, _server: Option<&LatticeServer>, _args: &[String]) -> CommandResult {
|
||||
match block_async(node.init()) {
|
||||
Ok(store_id) => {
|
||||
println!("Initialized with root store: {}", store_id);
|
||||
@@ -43,7 +43,7 @@ fn cmd_init(node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<&Lattic
|
||||
}
|
||||
}
|
||||
|
||||
fn cmd_create_store(node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, _args: &[String]) -> CommandResult {
|
||||
fn cmd_create_store(node: &Node, _store: Option<&StoreHandle>, _server: Option<&LatticeServer>, _args: &[String]) -> CommandResult {
|
||||
match node.create_store() {
|
||||
Ok(store_id) => {
|
||||
println!("Created store: {}", store_id);
|
||||
@@ -65,7 +65,7 @@ fn cmd_create_store(node: &Node, _store: Option<&StoreHandle>, _endpoint: Option
|
||||
}
|
||||
}
|
||||
|
||||
fn cmd_use_store(node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, args: &[String]) -> CommandResult {
|
||||
fn cmd_use_store(node: &Node, _store: Option<&StoreHandle>, _server: Option<&LatticeServer>, args: &[String]) -> CommandResult {
|
||||
let store_id = match Uuid::parse_str(&args[0]) {
|
||||
Ok(id) => id,
|
||||
Err(_) => {
|
||||
@@ -91,7 +91,7 @@ fn cmd_use_store(node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<&L
|
||||
}
|
||||
}
|
||||
|
||||
fn cmd_list_stores(node: &Node, store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, _args: &[String]) -> CommandResult {
|
||||
fn cmd_list_stores(node: &Node, store: Option<&StoreHandle>, _server: Option<&LatticeServer>, _args: &[String]) -> CommandResult {
|
||||
let stores = match node.list_stores() {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
@@ -114,7 +114,7 @@ fn cmd_list_stores(node: &Node, store: Option<&StoreHandle>, _endpoint: Option<&
|
||||
|
||||
// --- Info ---
|
||||
|
||||
fn cmd_node_status(node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, _args: &[String]) -> CommandResult {
|
||||
fn cmd_node_status(node: &Node, _store: Option<&StoreHandle>, _server: Option<&LatticeServer>, _args: &[String]) -> CommandResult {
|
||||
println!("Node ID: {}", hex::encode(node.node_id()));
|
||||
if let Some(name) = node.name() {
|
||||
println!("Name: {}", name);
|
||||
@@ -138,7 +138,7 @@ fn cmd_node_status(node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<
|
||||
|
||||
// --- Peer management ---
|
||||
|
||||
fn cmd_invite(node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, args: &[String]) -> CommandResult {
|
||||
fn cmd_invite(node: &Node, _store: Option<&StoreHandle>, _server: Option<&LatticeServer>, args: &[String]) -> CommandResult {
|
||||
let pubkey_hex = &args[0];
|
||||
let pubkey: [u8; 32] = match hex::decode(pubkey_hex) {
|
||||
Ok(bytes) if bytes.len() == 32 => bytes.try_into().unwrap(),
|
||||
@@ -158,7 +158,7 @@ fn cmd_invite(node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<&Latt
|
||||
CommandResult::Ok
|
||||
}
|
||||
|
||||
fn cmd_peers(node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, _args: &[String]) -> CommandResult {
|
||||
fn cmd_peers(node: &Node, _store: Option<&StoreHandle>, _server: Option<&LatticeServer>, _args: &[String]) -> CommandResult {
|
||||
let peers = match block_async(node.list_peers()) {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
@@ -204,7 +204,7 @@ fn cmd_peers(node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<&Latti
|
||||
CommandResult::Ok
|
||||
}
|
||||
|
||||
fn cmd_remove(node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, args: &[String]) -> CommandResult {
|
||||
fn cmd_remove(node: &Node, _store: Option<&StoreHandle>, _server: Option<&LatticeServer>, args: &[String]) -> CommandResult {
|
||||
let pubkey_hex = &args[0];
|
||||
let pubkey: [u8; 32] = match hex::decode(pubkey_hex) {
|
||||
Ok(bytes) if bytes.len() == 32 => bytes.try_into().unwrap(),
|
||||
@@ -223,9 +223,9 @@ fn cmd_remove(node: &Node, _store: Option<&StoreHandle>, _endpoint: Option<&Latt
|
||||
|
||||
// --- Networking ---
|
||||
|
||||
fn cmd_join(node: &Node, store: Option<&StoreHandle>, endpoint: Option<&LatticeEndpoint>, args: &[String]) -> CommandResult {
|
||||
let endpoint = match endpoint {
|
||||
Some(ep) => ep,
|
||||
fn cmd_join(_node: &Node, store: Option<&StoreHandle>, server: Option<&LatticeServer>, args: &[String]) -> CommandResult {
|
||||
let server = match server {
|
||||
Some(s) => s,
|
||||
None => {
|
||||
eprintln!("Iroh endpoint not started.");
|
||||
return CommandResult::Ok;
|
||||
@@ -247,7 +247,7 @@ fn cmd_join(node: &Node, store: Option<&StoreHandle>, endpoint: Option<&LatticeE
|
||||
|
||||
println!("Joining mesh via {}...", peer_id.fmt_short());
|
||||
|
||||
match block_async(lattice_net::join_mesh(node, endpoint, peer_id)) {
|
||||
match block_async(server.join_mesh(peer_id)) {
|
||||
Ok(handle) => {
|
||||
println!("Joined mesh! Use 'sync' command to sync entries.");
|
||||
CommandResult::SwitchTo(handle)
|
||||
@@ -259,9 +259,9 @@ fn cmd_join(node: &Node, store: Option<&StoreHandle>, endpoint: Option<&LatticeE
|
||||
}
|
||||
}
|
||||
|
||||
fn cmd_sync(node: &Node, store: Option<&StoreHandle>, endpoint: Option<&LatticeEndpoint>, args: &[String]) -> CommandResult {
|
||||
let endpoint = match endpoint {
|
||||
Some(ep) => ep,
|
||||
fn cmd_sync(_node: &Node, store: Option<&StoreHandle>, server: Option<&LatticeServer>, args: &[String]) -> CommandResult {
|
||||
let server = match server {
|
||||
Some(s) => s,
|
||||
None => {
|
||||
eprintln!("Iroh endpoint not started.");
|
||||
return CommandResult::Ok;
|
||||
@@ -278,7 +278,7 @@ fn cmd_sync(node: &Node, store: Option<&StoreHandle>, endpoint: Option<&LatticeE
|
||||
|
||||
if args.is_empty() {
|
||||
// Sync with all active peers
|
||||
match block_async(lattice_net::sync_all(node, endpoint, store)) {
|
||||
match block_async(server.sync_all(store)) {
|
||||
Ok(results) => {
|
||||
if results.is_empty() {
|
||||
println!("No peers to sync with.");
|
||||
@@ -300,7 +300,7 @@ fn cmd_sync(node: &Node, store: Option<&StoreHandle>, endpoint: Option<&LatticeE
|
||||
};
|
||||
|
||||
println!("Syncing with {}...", peer_id.fmt_short());
|
||||
match block_async(lattice_net::sync_with_peer(endpoint, store, peer_id)) {
|
||||
match block_async(server.sync_with_peer(store, peer_id)) {
|
||||
Ok(result) => {
|
||||
println!("Sync complete! Applied {} entries (peer sent {})",
|
||||
result.entries_applied, result.entries_sent_by_peer);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use crate::commands::{block_async, Command, CommandResult, Handler};
|
||||
use lattice_core::{Node, StoreHandle};
|
||||
use lattice_net::LatticeEndpoint;
|
||||
use lattice_net::LatticeServer;
|
||||
use std::time::Instant;
|
||||
|
||||
pub fn store_commands() -> Vec<Command> {
|
||||
@@ -16,7 +16,7 @@ pub fn store_commands() -> Vec<Command> {
|
||||
]
|
||||
}
|
||||
|
||||
fn cmd_store_status(_node: &Node, store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, _args: &[String]) -> CommandResult {
|
||||
fn cmd_store_status(_node: &Node, store: Option<&StoreHandle>, _server: Option<&LatticeServer>, _args: &[String]) -> CommandResult {
|
||||
let Some(h) = store else {
|
||||
println!("No store selected. Use 'init' or 'use <uuid>'");
|
||||
return CommandResult::Ok;
|
||||
@@ -38,7 +38,7 @@ fn cmd_store_status(_node: &Node, store: Option<&StoreHandle>, _endpoint: Option
|
||||
CommandResult::Ok
|
||||
}
|
||||
|
||||
fn cmd_put(_node: &Node, store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, args: &[String]) -> CommandResult {
|
||||
fn cmd_put(_node: &Node, store: Option<&StoreHandle>, _server: Option<&LatticeServer>, args: &[String]) -> CommandResult {
|
||||
let Some(h) = store else {
|
||||
println!("No store selected. Use 'init' or 'use <uuid>'");
|
||||
return CommandResult::Ok;
|
||||
@@ -51,7 +51,7 @@ fn cmd_put(_node: &Node, store: Option<&StoreHandle>, _endpoint: Option<&Lattice
|
||||
CommandResult::Ok
|
||||
}
|
||||
|
||||
fn cmd_get(_node: &Node, store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, args: &[String]) -> CommandResult {
|
||||
fn cmd_get(_node: &Node, store: Option<&StoreHandle>, _server: Option<&LatticeServer>, args: &[String]) -> CommandResult {
|
||||
let Some(h) = store else {
|
||||
println!("No store selected. Use 'init' or 'use <uuid>'");
|
||||
return CommandResult::Ok;
|
||||
@@ -102,7 +102,7 @@ fn cmd_get(_node: &Node, store: Option<&StoreHandle>, _endpoint: Option<&Lattice
|
||||
CommandResult::Ok
|
||||
}
|
||||
|
||||
fn cmd_delete(_node: &Node, store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, args: &[String]) -> CommandResult {
|
||||
fn cmd_delete(_node: &Node, store: Option<&StoreHandle>, _server: Option<&LatticeServer>, args: &[String]) -> CommandResult {
|
||||
let Some(h) = store else {
|
||||
println!("No store selected. Use 'init' or 'use <uuid>'");
|
||||
return CommandResult::Ok;
|
||||
@@ -115,7 +115,7 @@ fn cmd_delete(_node: &Node, store: Option<&StoreHandle>, _endpoint: Option<&Latt
|
||||
CommandResult::Ok
|
||||
}
|
||||
|
||||
fn cmd_list(_node: &Node, store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, args: &[String]) -> CommandResult {
|
||||
fn cmd_list(_node: &Node, store: Option<&StoreHandle>, _server: Option<&LatticeServer>, args: &[String]) -> CommandResult {
|
||||
let Some(h) = store else {
|
||||
println!("No store selected. Use 'init' or 'use <uuid>'");
|
||||
return CommandResult::Ok;
|
||||
@@ -173,7 +173,7 @@ fn cmd_list(_node: &Node, store: Option<&StoreHandle>, _endpoint: Option<&Lattic
|
||||
CommandResult::Ok
|
||||
}
|
||||
|
||||
fn cmd_author_state(node: &Node, store: Option<&StoreHandle>, _endpoint: Option<&LatticeEndpoint>, args: &[String]) -> CommandResult {
|
||||
fn cmd_author_state(node: &Node, store: Option<&StoreHandle>, _server: Option<&LatticeServer>, args: &[String]) -> CommandResult {
|
||||
let store = match store {
|
||||
Some(s) => s,
|
||||
None => {
|
||||
|
||||
Reference in New Issue
Block a user