fix clippy and globalref and android

This commit is contained in:
John Smith 2022-03-13 12:45:36 -04:00
parent f7f4d86cf1
commit 2cdfa59bb6
13 changed files with 97 additions and 107 deletions

@ -1 +1 @@
Subproject commit 085982c42704f9097c78e5f9848f345fbf888485
Subproject commit f73c27e66e43763f0f63ca9e697e77419f157a52

View File

@ -133,15 +133,14 @@ async fn main() -> Result<(), String> {
}
}
// Get client address
let server_addrs;
if let Some(address_arg) = matches.value_of("address") {
server_addrs = address_arg
let server_addrs = if let Some(address_arg) = matches.value_of("address") {
address_arg
.to_socket_addrs()
.map_err(|e| format!("Invalid server address '{}'", e))?
.collect()
} else {
server_addrs = settings.address.addrs.clone();
}
settings.address.addrs.clone()
};
let server_addr = server_addrs.first().cloned();
// Create command processor

View File

@ -1,7 +1,5 @@
use directories::*;
use serde_derive::*;
use std::ffi::OsStr;
use std::net::{SocketAddr, ToSocketAddrs};
@ -116,9 +114,7 @@ impl<'de> serde::Deserialize<'de> for NamedSocketAddrs {
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
let addr_iter = s
.to_socket_addrs()
.map_err(serde::de::Error::custom)?;
let addr_iter = s.to_socket_addrs().map_err(serde::de::Error::custom)?;
Ok(NamedSocketAddrs {
name: s,
addrs: addr_iter.collect(),
@ -206,13 +202,12 @@ pub struct Settings {
impl Settings {
pub fn get_default_config_path() -> PathBuf {
// Get default configuration file location
let mut default_config_path;
if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
default_config_path = PathBuf::from(my_proj_dirs.config_dir());
} else {
default_config_path = PathBuf::from("./");
}
let mut default_config_path =
if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
PathBuf::from(my_proj_dirs.config_dir())
} else {
PathBuf::from("./")
};
default_config_path.push("veilid-client.conf");
default_config_path
@ -220,13 +215,12 @@ impl Settings {
pub fn get_default_log_directory() -> PathBuf {
// Get default configuration file location
let mut default_log_directory;
if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
default_log_directory = PathBuf::from(my_proj_dirs.config_dir());
} else {
default_log_directory = PathBuf::from("./");
}
let mut default_log_directory =
if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
PathBuf::from(my_proj_dirs.config_dir())
} else {
PathBuf::from("./")
};
default_log_directory.push("logs/");
default_log_directory

View File

@ -557,14 +557,14 @@ impl UI {
}
ConnectionState::Retrying(addr, _) => {
status.append_styled(
format!("Reconnecting to {} ", addr.to_string()),
format!("Reconnecting to {} ", addr),
ColorStyle::highlight_inactive(),
);
status.append_styled("|", ColorStyle::highlight_inactive());
}
ConnectionState::Connected(addr, _) => {
status.append_styled(
format!("Connected to {} ", addr.to_string()),
format!("Connected to {} ", addr),
ColorStyle::highlight_inactive(),
);
status.append_styled("|", ColorStyle::highlight_inactive());

View File

@ -19,6 +19,13 @@ pub struct AndroidGlobals {
pub ctx: GlobalRef,
}
impl Drop for AndroidGlobals {
fn drop(&mut self) {
// Ensure we're attached before dropping GlobalRef
self.vm.attach_current_thread_as_daemon().unwrap();
}
}
lazy_static! {
pub static ref ANDROID_GLOBALS: Arc<Mutex<Option<AndroidGlobals>>> = Arc::new(Mutex::new(None));
}
@ -76,7 +83,7 @@ pub fn veilid_core_setup_android<'a>(
pub fn get_android_globals() -> (JavaVM, GlobalRef) {
let globals_locked = ANDROID_GLOBALS.lock();
let globals = globals_locked.as_ref().unwrap();
let env = globals.vm.attach_current_thread().unwrap();
let env = globals.vm.attach_current_thread_as_daemon().unwrap();
let vm = env.get_java_vm().unwrap();
let ctx = globals.ctx.clone();
(vm, ctx)

View File

@ -1455,9 +1455,9 @@ impl RPCProcessor {
match eventual_value.await {
ReceiptEvent::Returned => Ok(true),
ReceiptEvent::Expired => Ok(false),
ReceiptEvent::Cancelled => Err(rpc_error_internal(
"receipt was dropped before expiration".to_owned(),
)),
ReceiptEvent::Cancelled => {
Err(rpc_error_internal("receipt was dropped before expiration"))
}
}
}

View File

@ -279,13 +279,13 @@ async fn test_operations() {
assert_eq!(d1, d2);
assert!(d1 <= d2);
assert!(d1 >= d2);
assert!(!(d1 < d2));
assert!(!(d1 > d2));
assert!(d1 >= d2);
assert!(d1 <= d2);
assert_eq!(d2, d1);
assert!(d2 <= d1);
assert!(d2 >= d1);
assert!(!(d2 < d1));
assert!(!(d2 > d1));
assert!(d2 >= d1);
assert!(d2 <= d1);
// Verify nibbles
assert_eq!(d1.nibble(0), 0x9u8);

View File

@ -83,21 +83,19 @@ cfg_if! {
else {
fn get_data_dir() -> PathBuf {
let out;
cfg_if! {
if #[cfg(target_os = "android")] {
out = PathBuf::from(intf::utils::android::get_files_dir());
PathBuf::from(intf::utils::android::get_files_dir())
} else {
use directories::*;
if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "VeilidCoreTests") {
out = PathBuf::from(my_proj_dirs.data_local_dir());
PathBuf::from(my_proj_dirs.data_local_dir())
} else {
out = PathBuf::from("./");
PathBuf::from("./")
}
}
}
out
}
pub fn get_table_store_path() -> String {

View File

@ -295,8 +295,8 @@ impl Address {
}
pub fn address_string_with_port(&self, port: u16) -> String {
match self {
Address::IPV4(v4) => format!("{}:{}", v4.to_string(), port),
Address::IPV6(v6) => format!("[{}]:{}", v6.to_string(), port),
Address::IPV4(v4) => format!("{}:{}", v4, port),
Address::IPV6(v6) => format!("[{}]:{}", v6, port),
}
}
pub fn is_global(&self) -> bool {

View File

@ -262,7 +262,7 @@ impl FromStr for SplitUrl {
return Err("Invalid scheme specified".to_owned());
}
let userinfo = {
if let Some((userinfo_str, after)) = rest.split_once("@") {
if let Some((userinfo_str, after)) = rest.split_once('@') {
rest = after;
Some(url_decode(userinfo_str)?)
} else {

View File

@ -67,43 +67,40 @@ pub async fn run_veilid_server(settings: Settings, logs: VeilidLogs) -> Result<(
})
});
// Handle log messages on main thread for capnproto rpc
let client_log_receiver_jh = capi
.clone()
.map(|capi| {
logs.client_log_channel
.clone()
.map(|mut client_log_channel| {
async_std::task::spawn_local(async move {
// Batch messages to either 16384 chars at once or every second to minimize packets
let rate = Duration::from_secs(1);
let mut start = Instant::now();
let mut messages = String::new();
loop {
let timeout_dur =
rate.checked_sub(start.elapsed()).unwrap_or(Duration::ZERO);
match async_std::future::timeout(timeout_dur, client_log_channel.recv())
.await
{
Ok(Ok(message)) => {
messages += &message;
if messages.len() > 16384 {
capi.clone()
.handle_client_log(core::mem::take(&mut messages));
start = Instant::now();
}
}
Ok(Err(_)) => break,
Err(_) => {
let client_log_receiver_jh = capi.clone().and_then(|capi| {
logs.client_log_channel
.clone()
.map(|mut client_log_channel| {
async_std::task::spawn_local(async move {
// Batch messages to either 16384 chars at once or every second to minimize packets
let rate = Duration::from_secs(1);
let mut start = Instant::now();
let mut messages = String::new();
loop {
let timeout_dur =
rate.checked_sub(start.elapsed()).unwrap_or(Duration::ZERO);
match async_std::future::timeout(timeout_dur, client_log_channel.recv())
.await
{
Ok(Ok(message)) => {
messages += &message;
if messages.len() > 16384 {
capi.clone()
.handle_client_log(core::mem::take(&mut messages));
start = Instant::now();
}
}
Ok(Err(_)) => break,
Err(_) => {
capi.clone()
.handle_client_log(core::mem::take(&mut messages));
start = Instant::now();
}
}
})
}
})
})
.flatten();
})
});
// Auto-attach if desired
if auto_attach {

View File

@ -363,7 +363,7 @@ impl NamedSocketAddrs {
let portstr = &self.name[split + 1..];
let port: u16 = portstr.parse::<u16>().map_err(drop)? + offset;
self.name = format!("{}:{}", hoststr, port.to_string());
self.name = format!("{}:{}", hoststr, port);
} else {
return Err(());
}
@ -684,13 +684,12 @@ impl Settings {
pub fn get_default_config_path() -> PathBuf {
// Get default configuration file location
let mut default_config_path;
if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
default_config_path = PathBuf::from(my_proj_dirs.config_dir());
} else {
default_config_path = PathBuf::from("./");
}
let mut default_config_path =
if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
PathBuf::from(my_proj_dirs.config_dir())
} else {
PathBuf::from("./")
};
default_config_path.push("veilid-server.conf");
default_config_path
@ -698,13 +697,12 @@ impl Settings {
pub fn get_default_table_store_path() -> PathBuf {
// Get default configuration file location
let mut default_config_path;
if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
default_config_path = PathBuf::from(my_proj_dirs.data_local_dir());
} else {
default_config_path = PathBuf::from("./");
}
let mut default_config_path =
if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
PathBuf::from(my_proj_dirs.data_local_dir())
} else {
PathBuf::from("./")
};
default_config_path.push("table_store");
default_config_path
@ -712,13 +710,12 @@ impl Settings {
pub fn get_default_block_store_path() -> PathBuf {
// Get default configuration file location
let mut default_config_path;
if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
default_config_path = PathBuf::from(my_proj_dirs.data_local_dir());
} else {
default_config_path = PathBuf::from("./");
}
let mut default_config_path =
if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
PathBuf::from(my_proj_dirs.data_local_dir())
} else {
PathBuf::from("./")
};
default_config_path.push("block_store");
default_config_path
@ -726,13 +723,12 @@ impl Settings {
pub fn get_default_protected_store_insecure_fallback_directory() -> PathBuf {
// Get default configuration file location
let mut default_config_path;
if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
default_config_path = PathBuf::from(my_proj_dirs.data_local_dir());
} else {
default_config_path = PathBuf::from("./");
}
let mut default_config_path =
if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
PathBuf::from(my_proj_dirs.data_local_dir())
} else {
PathBuf::from("./")
};
default_config_path.push("protected_store");
default_config_path

View File

@ -33,21 +33,20 @@ impl VeilidLogs {
if settingsr.logging.file.enabled {
let log_path = Path::new(&settingsr.logging.file.path);
let logfile;
if settingsr.logging.file.append {
logfile = OpenOptions::new()
let logfile = if settingsr.logging.file.append {
OpenOptions::new()
.create(true)
.append(true)
.open(log_path)
.map_err(|e| format!("failed to open log file: {}", e))?
} else {
logfile = OpenOptions::new()
OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(log_path)
.map_err(|e| format!("failed to open log file: {}", e))?
}
};
logs.push(WriteLogger::new(
convert_loglevel(settingsr.logging.file.level),
cb.build(),