Skip to content

Commit

Permalink
locka99#94 Allow session name to be set from the client config and th…
Browse files Browse the repository at this point in the history
…rough builder
  • Loading branch information
locka99 committed Mar 7, 2021
1 parent 4839f81 commit d5ffcdd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
11 changes: 11 additions & 0 deletions client/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(mut self, session_name: T) -> Self
where
T: Into<String>,
{
self.config.session_name = session_name.into();
self
}
}

#[test]
Expand All @@ -275,6 +284,7 @@ fn client_builder() {
.session_retry_limit(999)
.session_timeout(777)
.single_threaded_executor()
.session_name("SessionName")
// TODO user tokens, endpoints
;

Expand All @@ -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");
}
2 changes: 2 additions & 0 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
}
}
}
15 changes: 12 additions & 3 deletions client/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<T>(
application_description: ApplicationDescription,
session_name: T,
certificate_store: Arc<RwLock<CertificateStore>>,
session_info: SessionInfo,
session_retry_policy: SessionRetryPolicy,
single_threaded_executor: bool,
) -> Session {
) -> Session
where
T: Into<UAString>,
{
// 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,
Expand All @@ -198,6 +206,7 @@ impl Session {
);
Session {
application_description,
session_name,
session_info,
session_state,
certificate_store,
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion samples/client.conf
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ endpoints:
session_retry_limit: 10
session_retry_interval: 10000
session_timeout: 0
single_threaded_executor: false
single_threaded_executor: false
session_name: Rust OPC UA Client

0 comments on commit d5ffcdd

Please sign in to comment.