mirror of
https://github.com/jwinterm/grim.git
synced 2026-10-21 15:38:32 +02:00
config: optimize access to node config members, fix check for port availability
This commit is contained in:
@@ -337,15 +337,5 @@ impl Network {
|
||||
ui.add_space(6.0);
|
||||
});
|
||||
}
|
||||
|
||||
/// Check whether a port is available on the provided host.
|
||||
pub fn is_port_available(host: &String, port: &String) -> bool {
|
||||
if let Ok(p) = port.parse::<u16>() {
|
||||
let ip_addr = Ipv4Addr::from_str(host.as_str()).unwrap();
|
||||
let ipv4 = SocketAddrV4::new(ip_addr, p);
|
||||
return TcpListener::bind(ipv4).is_ok();
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -113,13 +113,9 @@ impl NetworkTab for NetworkMining {
|
||||
});
|
||||
columns[1].vertical_centered(|ui| {
|
||||
//TODO: Stratum mining wallet listening address. Replace with local wallet name.
|
||||
let wallet_address = Settings::node_config_to_read()
|
||||
.members.clone()
|
||||
.server.stratum_mining_config.unwrap()
|
||||
.wallet_listener_url
|
||||
.replace("http://", "");
|
||||
let wallet_addr = NodeConfig::get_stratum_wallet_addr().replace("http://", "");
|
||||
View::rounded_box(ui,
|
||||
wallet_address,
|
||||
wallet_addr,
|
||||
t!("network_mining.rewards_wallet"),
|
||||
[false, true, false, true]);
|
||||
});
|
||||
|
||||
@@ -44,7 +44,7 @@ pub struct NodeSetup {
|
||||
impl Default for NodeSetup {
|
||||
fn default() -> Self {
|
||||
let (api_ip, api_port) = NodeConfig::get_api_address_port();
|
||||
let is_api_port_available = is_api_port_available(&api_ip, &api_port);
|
||||
let is_api_port_available = NodeConfig::is_api_port_available(&api_ip, &api_port);
|
||||
Self {
|
||||
api_port_edit: api_port,
|
||||
api_port_available_edit: is_api_port_available,
|
||||
@@ -127,8 +127,13 @@ impl NodeSetup {
|
||||
// Show API IP addresses to select.
|
||||
let (api_ip, api_port) = NodeConfig::get_api_address_port();
|
||||
Network::ip_list_ui(ui, &api_ip, &addrs, |selected_ip| {
|
||||
self.is_api_port_available = is_api_port_available(selected_ip, &api_port);
|
||||
println!("12345 selected_ip {}", selected_ip);
|
||||
let api_available = NodeConfig::is_api_port_available(selected_ip, &api_port);
|
||||
println!("12345 selected_ip is_api_port_available {}", api_available);
|
||||
self.is_api_port_available = api_available;
|
||||
println!("12345 before save");
|
||||
NodeConfig::save_api_address_port(selected_ip, &api_port);
|
||||
println!("12345 after save");
|
||||
});
|
||||
|
||||
ui.label(RichText::new(t!("network_settings.api_port"))
|
||||
@@ -161,14 +166,21 @@ impl NodeSetup {
|
||||
Navigator::show_modal(port_modal);
|
||||
cb.show_keyboard();
|
||||
});
|
||||
ui.add_space(14.0);
|
||||
ui.add_space(6.0);
|
||||
|
||||
// Show error when API server port is unavailable.
|
||||
if !self.is_api_port_available {
|
||||
// Show error when API server port is unavailable.
|
||||
ui.label(RichText::new(t!("network_settings.port_unavailable"))
|
||||
.size(16.0)
|
||||
.color(Colors::RED));
|
||||
ui.add_space(12.0);
|
||||
ui.add_space(6.0);
|
||||
} else if Node::is_running() {
|
||||
// Show reminder to restart node if settings are changed.
|
||||
ui.label(RichText::new(t!("network_settings.restart_node_required"))
|
||||
.size(16.0)
|
||||
.color(Colors::INACTIVE_TEXT)
|
||||
);
|
||||
ui.add_space(6.0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,7 +233,10 @@ impl NodeSetup {
|
||||
View::button(ui, t!("modal.save"), Colors::WHITE, || {
|
||||
// Check if port is available.
|
||||
let (ip, _) = NodeConfig::get_api_address_port();
|
||||
let available = is_api_port_available(&ip, &self.api_port_edit);
|
||||
let available = NodeConfig::is_api_port_available(
|
||||
&ip,
|
||||
&self.api_port_edit
|
||||
);
|
||||
self.api_port_available_edit = available;
|
||||
|
||||
if self.api_port_available_edit {
|
||||
@@ -242,23 +257,4 @@ impl NodeSetup {
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn is_api_port_available(ip: &String, port: &String) -> bool {
|
||||
let same_address_as_running = {
|
||||
let (current_ip, current_port) = NodeConfig::get_api_address_port();
|
||||
Node::is_running() && ¤t_ip == ip && ¤t_port == port
|
||||
};
|
||||
|
||||
if same_address_as_running || (!Node::is_running() && Network::is_port_available(&ip, &port)) {
|
||||
if &NodeConfig::get_p2p_port().to_string() != port {
|
||||
let (stratum_ip, stratum_port) = NodeConfig::get_stratum_address_port();
|
||||
return if &stratum_ip == ip {
|
||||
&stratum_port != port
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
@@ -38,7 +38,7 @@ pub struct StratumServerSetup {
|
||||
impl Default for StratumServerSetup {
|
||||
fn default() -> Self {
|
||||
let (ip, port) = NodeConfig::get_stratum_address_port();
|
||||
let is_port_available = is_stratum_port_available(&ip, &port);
|
||||
let is_port_available = NodeConfig::is_stratum_port_available(&ip, &port);
|
||||
Self {
|
||||
stratum_port_edit: port,
|
||||
stratum_port_available_edit: is_port_available,
|
||||
@@ -72,11 +72,8 @@ impl StratumServerSetup {
|
||||
// Show stratum IP addresses to select.
|
||||
let (ip, port) = NodeConfig::get_stratum_address_port();
|
||||
Network::ip_list_ui(ui, &ip, &all_ips, |selected_ip| {
|
||||
self.is_port_available = is_stratum_port_available(selected_ip, &port);
|
||||
self.is_port_available = NodeConfig::is_stratum_port_available(selected_ip, &port);
|
||||
NodeConfig::save_stratum_address_port(selected_ip, &port);
|
||||
if !self.is_port_available {
|
||||
NodeConfig::disable_stratum_autorun();
|
||||
}
|
||||
});
|
||||
|
||||
ui.label(RichText::new(t!("network_settings.port"))
|
||||
@@ -169,7 +166,10 @@ impl StratumServerSetup {
|
||||
View::button(ui, t!("modal.save"), Colors::WHITE, || {
|
||||
// Check if port is available.
|
||||
let (ip, _) = NodeConfig::get_api_address_port();
|
||||
let available = is_stratum_port_available(&ip, &self.stratum_port_edit);
|
||||
let available = NodeConfig::is_stratum_port_available(
|
||||
&ip,
|
||||
&self.stratum_port_edit
|
||||
);
|
||||
self.stratum_port_available_edit = available;
|
||||
|
||||
// Save port at config if it's available.
|
||||
@@ -187,18 +187,4 @@ impl StratumServerSetup {
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn is_stratum_port_available(ip: &String, port: &String) -> bool {
|
||||
if Network::is_port_available(&ip, &port) {
|
||||
if &NodeConfig::get_p2p_port().to_string() != port {
|
||||
let (api_ip, api_port) = NodeConfig::get_api_address_port();
|
||||
return if &api_ip == ip {
|
||||
&api_port != port
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
@@ -70,7 +70,7 @@ impl View {
|
||||
ui.input_mut().pointer = PointerState::default();
|
||||
}
|
||||
if drag_resp.drag_released() || drag_resp.clicked() {
|
||||
return true
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user