use crate::client::prelude::ClientBuilder;
use crate::server::state::ServerState;
fn windows_lds_pki_dir() -> String {
const WINDOWS_LDS_PKI_DIR: &str = r#"C:\ProgramData\OPC Foundation\UA\pki"#;
if cfg!(windows) {
if let Ok(mut pki_dir) = std::env::var("ALLUSERSPROFILE") {
pki_dir.push_str(r#"\OPC Foundation\UA\pki"#);
pki_dir
} else {
WINDOWS_LDS_PKI_DIR.to_string()
}
} else {
WINDOWS_LDS_PKI_DIR.to_string()
}
}
fn linux_lds_pki_dir() -> String {
const LINUX_LDS_PKI_DIR: &str = "/opt/opcfoundation/ualds/pki";
LINUX_LDS_PKI_DIR.to_string()
}
pub fn register_with_discovery_server(discovery_server_url: &str, server_state: &ServerState) {
debug!(
"register_with_discovery_server, for {}",
discovery_server_url
);
let server_config = trace_read_lock!(server_state.config);
let client = ClientBuilder::new()
.application_name("DiscoveryClient")
.application_uri("urn:DiscoveryClient")
.pki_dir(server_config.pki_dir.clone())
.session_retry_limit(1)
.client();
if let Some(mut client) = client {
match client.find_servers(discovery_server_url) {
Ok(servers) => {
debug!("Servers on the discovery endpoint - {:?}", servers);
let registered_server = server_state.registered_server();
match client.register_server(discovery_server_url, registered_server) {
Ok(_) => {}
Err(err) => {
error!(
r#"Cannot register server with discovery server \"{}\".
The errors immediately preceding this message may be caused by this issue.
Check if the error "{}" indicates the reason why that the registration could not happen.
Check that your server can connect to the discovery server and that your server's cert is trusted by
the discovery server and vice versa. The discovery server's PKI directory is (Windows)
{} or (Linux) {}."#,
discovery_server_url,
err,
windows_lds_pki_dir(),
linux_lds_pki_dir()
);
}
}
}
Err(err) => {
error!(
"Cannot find servers on discovery url {}, error = {:?}",
discovery_server_url, err
);
}
}
} else {
error!("Cannot create a discovery server client config");
}
debug!("register_with_discovery_server, finished");
}