pub struct Client { /* private fields */ }Expand description
The Client defines a connection that can be used to to get end points or establish
one or more sessions with an OPC UA server. It is constructed via a ClientBuilder or
from a described configuration ClientConfig that could be deserialized from file.
You have a couple of choices when creating a client that connects to a server depending on whether you know the endpoints up front.
-
Define all the endpoints you expect to connect with via your builder / config and then use
connect_to_endpoint_id()to connect to one of them by its id. This option assumes that your client and the server it connects to are describing the same endpoints. It will not work if the server describes different endpoints than the one in your config. -
Define no endpoints and then call
connect_to_endpoint()with an ad hoc endpoint description. This is the suitable choice if your client can connect to a multitude of servers without advance description of their endpoints.
Implementations§
Source§impl Client
impl Client
Sourcepub fn new(config: ClientConfig) -> Client
pub fn new(config: ClientConfig) -> Client
Creates a new Client instance from a ClientConfig. The configuration
defines the behaviour of the new client, which endpoints it recognizes, where it stores
certificates etc.
A Client can be made directly or by using a ClientBuilder.
§Example
use opcua_client::prelude::*;
use std::path::PathBuf;
fn main() {
let mut client = Client::new(ClientConfig::load(&PathBuf::from("./myclient.conf")).unwrap());
if let Ok(session) = client.connect_to_endpoint_id(None) {
// ..
}
}Sourcepub fn application_description(&self) -> ApplicationDescription
pub fn application_description(&self) -> ApplicationDescription
Returns a filled OPC UA ApplicationDescription using information from the config
Sourcepub fn connect_to_endpoint_id(
&mut self,
endpoint_id: Option<&str>,
) -> Result<Arc<RwLock<Session>>, StatusCode>
pub fn connect_to_endpoint_id( &mut self, endpoint_id: Option<&str>, ) -> Result<Arc<RwLock<Session>>, StatusCode>
Connects to a named endpoint that you have defined in the ClientConfig
and creates / activates a Session for that endpoint. Note that GetEndpoints is first
called on the server and it is expected to support the endpoint you intend to connect to.
Returns with the session that has been established or an error.
Important Note: The Session you receive from this call is protected because it is
accessed by multiple internal threads. You must scope lock calls to this session object and not
hold the lock for more than required.
Sourcepub fn connect_to_endpoint<T>(
&mut self,
endpoint: T,
user_identity_token: IdentityToken,
) -> Result<Arc<RwLock<Session>>, StatusCode>where
T: Into<EndpointDescription>,
pub fn connect_to_endpoint<T>(
&mut self,
endpoint: T,
user_identity_token: IdentityToken,
) -> Result<Arc<RwLock<Session>>, StatusCode>where
T: Into<EndpointDescription>,
Connects to an ad-hoc server endpoint description. and creates / activates a Session for
that endpoint.
Returns with the session that has been established or an error.
Important Note: The Session you receive from this call is protected because it is
accessed by multiple internal threads. You must scope lock calls to this session object and not
hold the lock for more than required.
Sourcepub fn default_endpoint(&self) -> Result<ClientEndpoint, String>
pub fn default_endpoint(&self) -> Result<ClientEndpoint, String>
Gets the ClientEndpoint information for the default endpoint, as defined
by the configuration. If there is no default endpoint, this function will return an error.
Sourcepub fn new_session(
&mut self,
endpoints: &[EndpointDescription],
) -> Result<Arc<RwLock<Session>>, String>
pub fn new_session( &mut self, endpoints: &[EndpointDescription], ) -> Result<Arc<RwLock<Session>>, String>
Creates a new Session using the default endpoint specified in the config. If
there is no default, or the endpoint does not exist, this function will return an error
Sourcepub fn new_session_from_id<T>(
&mut self,
endpoint_id: T,
endpoints: &[EndpointDescription],
) -> Result<Arc<RwLock<Session>>, String>
pub fn new_session_from_id<T>( &mut self, endpoint_id: T, endpoints: &[EndpointDescription], ) -> Result<Arc<RwLock<Session>>, String>
Creates a new Session using the named endpoint id. If there is no
endpoint of that id in the config, this function will return an error
Sourcepub fn new_session_from_info<T>(
&mut self,
session_info: T,
) -> Result<Arc<RwLock<Session>>, String>where
T: Into<SessionInfo>,
pub fn new_session_from_info<T>(
&mut self,
session_info: T,
) -> Result<Arc<RwLock<Session>>, String>where
T: Into<SessionInfo>,
Creates an ad hoc new Session using the specified endpoint url, security policy and mode.
This function supports anything that implements Into<SessionInfo>, for example EndpointDescription.
Sourcepub fn get_server_endpoints(
&self,
) -> Result<Vec<EndpointDescription>, StatusCode>
pub fn get_server_endpoints( &self, ) -> Result<Vec<EndpointDescription>, StatusCode>
Connects to the client’s default configured endpoint asks the server for a list of
EndpointDescription that it hosts. If there is an error, the function will
return an error.
Sourcepub fn get_server_endpoints_from_url<T>(
&self,
server_url: T,
) -> Result<Vec<EndpointDescription>, StatusCode>
pub fn get_server_endpoints_from_url<T>( &self, server_url: T, ) -> Result<Vec<EndpointDescription>, StatusCode>
Connects to the specified server_url with a None/None connection and asks for a list of
EndpointDescription that it hosts.
§Example
use opcua_client::prelude::*;
use std::path::PathBuf;
fn main() {
let mut client = Client::new(ClientConfig::load(&PathBuf::from("./myclient.conf")).unwrap());
if let Ok(endpoints) = client.get_server_endpoints_from_url("opc.tcp://foo:1234") {
if let Some(endpoint) = Client::find_matching_endpoint(&endpoints, "opc.tcp://foo:1234/mypath", SecurityPolicy::None, MessageSecurityMode::None) {
//...
}
}
}Sourcepub fn find_servers<T>(
&mut self,
discovery_endpoint_url: T,
) -> Result<Vec<ApplicationDescription>, StatusCode>
pub fn find_servers<T>( &mut self, discovery_endpoint_url: T, ) -> Result<Vec<ApplicationDescription>, StatusCode>
Connects to a discovery server and asks the server for a list of
available server ApplicationDescription.
Sourcepub fn register_server<T>(
&mut self,
discovery_endpoint_url: T,
server: RegisteredServer,
) -> Result<(), StatusCode>
pub fn register_server<T>( &mut self, discovery_endpoint_url: T, server: RegisteredServer, ) -> Result<(), StatusCode>
Called by servers that wish to register themselves with a discovery server.
In this role, the server becomes the client of the discovery server, so it needs to connect as a client, query the endpoints, establish a session, register its own endpoints and then disconnect.
The implementation of this function looks for the strongest endpoint of the discovery server to register itself on. That makes it possible that the discovery server may reject the connection if it does not trust the client. In that instance, it is up to the user to do whatever is required to make the discovery server trust the registering server.
For example the standard OPC foundation discovery server will drop the server’s cert in a
rejected/ folder on the filesystem and this cert has to be moved to a trusted/certs/ folder.
Sourcepub fn find_matching_endpoint(
endpoints: &[EndpointDescription],
endpoint_url: &str,
security_policy: SecurityPolicy,
security_mode: MessageSecurityMode,
) -> Option<EndpointDescription>
pub fn find_matching_endpoint( endpoints: &[EndpointDescription], endpoint_url: &str, security_policy: SecurityPolicy, security_mode: MessageSecurityMode, ) -> Option<EndpointDescription>
Find an endpoint supplied from the list of endpoints that matches the input criteria