Client

Struct Client 

Source
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.

  1. 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.

  2. 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

Source

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) {
        // ..
    }
}
Source

pub fn application_description(&self) -> ApplicationDescription

Returns a filled OPC UA ApplicationDescription using information from the config

Source

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.

Source

pub fn connect_to_endpoint<T>( &mut self, endpoint: T, user_identity_token: IdentityToken, ) -> Result<Arc<RwLock<Session>>, StatusCode>

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.

Source

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.

Source

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

Source

pub fn new_session_from_id<T>( &mut self, endpoint_id: T, endpoints: &[EndpointDescription], ) -> Result<Arc<RwLock<Session>>, String>
where T: Into<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

Source

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.

Source

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.

Source

pub fn get_server_endpoints_from_url<T>( &self, server_url: T, ) -> Result<Vec<EndpointDescription>, StatusCode>
where T: Into<String>,

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) {
          //...
        }
    }
}
Source

pub fn find_servers<T>( &mut self, discovery_endpoint_url: T, ) -> Result<Vec<ApplicationDescription>, StatusCode>
where T: Into<String>,

Connects to a discovery server and asks the server for a list of available server ApplicationDescription.

Source

pub fn register_server<T>( &mut self, discovery_endpoint_url: T, server: RegisteredServer, ) -> Result<(), StatusCode>
where T: Into<String>,

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.

Source

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

Trait Implementations§

Source§

impl Drop for Client

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl From<ClientConfig> for Client

Source§

fn from(config: ClientConfig) -> Client

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for Client

§

impl RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.