From d5ffcdd037a41a4f43d28b1e9064988a845e59e1 Mon Sep 17 00:00:00 2001 From: Adam Lock Date: Sun, 7 Mar 2021 14:15:58 +0000 Subject: [PATCH] #94 Allow session name to be set from the client config and through builder --- client/src/builder.rs | 11 +++++++++++ client/src/client.rs | 2 ++ client/src/config.rs | 3 +++ client/src/session.rs | 15 ++++++++++++--- samples/client.conf | 3 ++- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/client/src/builder.rs b/client/src/builder.rs index d4de6cda2..c9a999bda 100644 --- a/client/src/builder.rs +++ b/client/src/builder.rs @@ -252,6 +252,15 @@ impl ClientBuilder { self.config.single_threaded_executor = true; self } + + /// Session name - the default name to use for a new session + pub fn session_name(mut self, session_name: T) -> Self + where + T: Into, + { + self.config.session_name = session_name.into(); + self + } } #[test] @@ -275,6 +284,7 @@ fn client_builder() { .session_retry_limit(999) .session_timeout(777) .single_threaded_executor() + .session_name("SessionName") // TODO user tokens, endpoints ; @@ -298,4 +308,5 @@ fn client_builder() { assert_eq!(c.session_retry_limit, 999); assert_eq!(c.session_timeout, 777); assert_eq!(c.single_threaded_executor, true); + assert_eq!(c.session_name, "SessionName"); } diff --git a/client/src/client.rs b/client/src/client.rs index 02a883954..b88d05b1a 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -387,6 +387,7 @@ impl Client { } else { let session = Arc::new(RwLock::new(Session::new( self.application_description(), + self.config.session_name.clone(), self.certificate_store.clone(), session_info, self.session_retry_policy.clone(), @@ -461,6 +462,7 @@ impl Client { }; let mut session = Session::new( self.application_description(), + self.config.session_name.clone(), self.certificate_store.clone(), session_info, self.session_retry_policy.clone(), diff --git a/client/src/config.rs b/client/src/config.rs index 1fb590253..3391ecf39 100644 --- a/client/src/config.rs +++ b/client/src/config.rs @@ -169,6 +169,8 @@ pub struct ClientConfig { /// Use a single-threaded executor. The default executor uses a thread pool with a worker /// thread for each CPU core available on the system. pub single_threaded_executor: bool, + /// Session name + pub session_name: String, } impl Config for ClientConfig { @@ -298,6 +300,7 @@ impl ClientConfig { session_retry_interval: SessionRetryPolicy::DEFAULT_RETRY_INTERVAL_MS, session_timeout: 0, single_threaded_executor: false, + session_name: "Rust OPC UA Client".into(), } } } diff --git a/client/src/session.rs b/client/src/session.rs index 981230dcd..235516b0d 100644 --- a/client/src/session.rs +++ b/client/src/session.rs @@ -121,6 +121,8 @@ pub enum SessionCommand { pub struct Session { /// The client application's name. application_description: ApplicationDescription, + /// A name for the session, supplied during create + session_name: UAString, /// The session connection info. session_info: SessionInfo, /// Runtime state of the session, reset if disconnected. @@ -164,16 +166,22 @@ impl Session { /// /// * `Session` - the interface that shall be used to communicate between the client and the server. /// - pub(crate) fn new( + pub(crate) fn new( application_description: ApplicationDescription, + session_name: T, certificate_store: Arc>, session_info: SessionInfo, session_retry_policy: SessionRetryPolicy, single_threaded_executor: bool, - ) -> Session { + ) -> Session + where + T: Into, + { // TODO take these from the client config let decoding_limits = DecodingLimits::default(); + let session_name = session_name.into(); + let secure_channel = Arc::new(RwLock::new(SecureChannel::new( certificate_store.clone(), Role::Client, @@ -198,6 +206,7 @@ impl Session { ); Session { application_description, + session_name, session_info, session_state, certificate_store, @@ -940,7 +949,7 @@ impl Session { }; let server_uri = UAString::null(); - let session_name = UAString::from("Rust OPCUA Client"); + let session_name = self.session_name.clone(); let (client_certificate, _) = { let certificate_store = trace_write_lock_unwrap!(self.certificate_store); diff --git a/samples/client.conf b/samples/client.conf index 5eee131e0..9215816d8 100644 --- a/samples/client.conf +++ b/samples/client.conf @@ -41,4 +41,5 @@ endpoints: session_retry_limit: 10 session_retry_interval: 10000 session_timeout: 0 -single_threaded_executor: false \ No newline at end of file +single_threaded_executor: false +session_name: Rust OPC UA Client \ No newline at end of file