Skip to content

Commit

Permalink
Refactor main function to integrate RpcClient and initialize Veilid f…
Browse files Browse the repository at this point in the history
…or group operations
  • Loading branch information
tripledoublev committed Nov 27, 2024
1 parent 466c6b4 commit b41d3fa
Showing 1 changed file with 62 additions and 50 deletions.
112 changes: 62 additions & 50 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::backend::Backend;
use crate::rpc::RpcService;
use crate::rpc::{RpcService, RpcClient};
use crate::rpc::{JoinGroupRequest, RemoveGroupRequest};
use crate::common::{CommonKeypair, DHTEntity};
use crate::common::{CommonKeypair, DHTEntity, init_veilid};
use crate::constants::{UNABLE_TO_GET_GROUP_NAME, UNABLE_TO_SET_GROUP_NAME};
use crate::group::Group;
use crate::repo::Repo;
use anyhow::{anyhow, Result};
use clap::{Arg, Command, ArgAction, Subcommand};
use tokio::fs;
use tokio::task;
use tokio::sync::Mutex;
use std::sync::Arc;
use xdg::BaseDirectories;
Expand Down Expand Up @@ -62,20 +61,20 @@ async fn main() -> anyhow::Result<()> {
Command::new("join")
.about("Join a group")
.arg(
Arg::new("group_url")
.long("group-url")
.help("URL of the group to join")
.required(true),
Arg::new("group_url")
.long("group-url")
.help("URL of the group to join")
.required(true),
),
)
.subcommand(
Command::new("remove")
.about("Remove a group")
.arg(
Arg::new("group_id")
.long("group-id")
.help("ID of the group to remove")
.required(true),
Arg::new("group_id")
.long("group-id")
.help("ID of the group to remove")
.required(true),
),
)
.subcommand(Command::new("list").about("List known groups"))
Expand Down Expand Up @@ -109,47 +108,60 @@ async fn main() -> anyhow::Result<()> {

// Start the update listener
rpc_service.start_update_listener().await?;
}

match matches.subcommand() {
Some(("join", sub_matches)) => {
let group_url = sub_matches.get_one::<String>("group_url").unwrap();
backend.start().await?;
let rpc_service = RpcService::from_backend(&backend).await?;
rpc_service.join_group(JoinGroupRequest { group_url: group_url.clone() }).await?;
println!("Joined group with URL: {}", group_url);
}
Some(("remove", sub_matches)) => {
let group_id = sub_matches.get_one::<String>("group_id").unwrap();
backend.start().await?;
let rpc_service = RpcService::from_backend(&backend).await?;
rpc_service.remove_group(RemoveGroupRequest { group_id: group_id.clone() }).await?;
println!("Removed group with ID: {}", group_id);
}
Some(("list", _)) => {
backend.start().await?;
let rpc_service = RpcService::from_backend(&backend).await?;
let response = rpc_service.list_groups().await?;
for group_id in response.group_ids {
println!("Group ID: {}", group_id);
} else {
match matches.subcommand() {
Some(("join", sub_matches)) => {
let (veilid_api, _update_rx) =
init_veilid(&base_dir, "save-dweb-backend".to_string()).await?;

let group_url = sub_matches.get_one::<String>("group_url").unwrap();
println!("Joining group: {}", group_url);

// Pass initialized Veilid to the RPC client
let rpc_client = RpcClient::from_veilid(veilid_api.clone(), backend_url).await?;
rpc_client.join_group(group_url.to_string()).await?;
println!("Successfully joined group.");
}
Some(("list", _)) => {
let (veilid_api, _update_rx) =
init_veilid(&base_dir, "save-dweb-backend".to_string()).await?;

println!("Listing all groups...");
let rpc_client = RpcClient::from_veilid(veilid_api.clone(), backend_url).await?;
let response = rpc_client.list_groups().await?;
for group_id in response.group_ids {
println!("Group ID: {}", group_id);
}
}
Some(("remove", sub_matches)) => {
let (veilid_api, _update_rx) =
init_veilid(&base_dir, "save-dweb-backend".to_string()).await?;

let group_id = sub_matches.get_one::<String>("group_id").unwrap();
println!("Removing group: {}", group_id);

// Pass initialized Veilid to the RPC client
let rpc_client = RpcClient::from_veilid(veilid_api.clone(), backend_url).await?;
rpc_client.remove_group(group_id.to_string()).await?;
println!("Successfully removed group.");
}
Some(("start", _)) => {
let (veilid_api, _update_rx) =
init_veilid(&base_dir, "save-dweb-backend".to_string()).await?;

backend.start().await?;
let rpc_service = RpcService::from_backend(&backend).await?;
println!("RPC service started at URL: {}", rpc_service.get_descriptor_url());
rpc_service.start_update_listener().await?;
}
_ => {
// Otherwise, start the normal backend and group operations
backend.start().await?;
tokio::signal::ctrl_c().await?;
backend.stop().await?;
}
}
Some(("start", _)) => {
backend.start().await?;
let rpc_service = RpcService::from_backend(&backend).await?;
let rpc_url = rpc_service.get_descriptor_url();
println!("RPC service started at URL: {}", rpc_url);
rpc_service.start_update_listener().await?;
}
_ => {
// Otherwise, start the normal backend and group operations
backend.start().await?;

// Await for ctrl-c and then stop the backend
tokio::signal::ctrl_c().await?;
backend.stop().await?;
}
}

Ok(())
}
}

0 comments on commit b41d3fa

Please sign in to comment.