From 499fd61e4a2b15b75454c5cee30a8bf1f962b35f Mon Sep 17 00:00:00 2001 From: selimserbes Date: Fri, 13 Dec 2024 23:52:59 +0300 Subject: [PATCH] add is_connected method, release v1.0.0 stable --- CHANGELOG.md | 10 ++++++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 9 ++++++++- examples/example.rs | 7 +++++++ src/rs_openshowvar.rs | 23 +++++++++++++++++++++++ tests/unit/test.rs | 26 ++++++++++++++++++++++++++ 7 files changed, 76 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dcca4d7..695795e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,3 +37,13 @@ ## [0.1.8] - 2024-07-22 - Updated e2e tests and refined error handling. + +## [1.0.0] - 2024-12-13 + +### Added + +- `is_connected` method added to `OpenShowVar` for checking the connection status. + +### Changed + +- Library reached stable release with version `1.0.0`. diff --git a/Cargo.lock b/Cargo.lock index b7ba051..1cbbef5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "rs_openshowvar" -version = "0.1.8" +version = "1.0.0" diff --git a/Cargo.toml b/Cargo.toml index 388a129..0836947 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rs_openshowvar" -version = "0.1.8" +version = "1.0.0" authors = ["Selim Serbes "] edition = "2021" description = "A Rust library for connecting to Kuka robots and performing data read/write operations using the OpenShowVar protocol." diff --git a/README.md b/README.md index 7e068dd..fc285a6 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Add `rs_openshowvar` as a dependency in your `Cargo.toml`: ```toml [dependencies] -rs_openshowvar = "0.1.8" +rs_openshowvar = "1.0.0" ``` ## Usage @@ -64,6 +64,13 @@ pub fn main() { } } + // Check if the connection is active using the is_connected method + if robot.is_connected() { + println!("Connection is active."); + } else { + println!("Connection is not active."); + } + // Specify the variable name and value let variable_name = "existing_var"; let value = "new_value"; diff --git a/examples/example.rs b/examples/example.rs index 5f696c5..a0c8ff6 100644 --- a/examples/example.rs +++ b/examples/example.rs @@ -38,6 +38,13 @@ pub fn main() { } } + // Check if the connection is active using the is_connected method + if robot.is_connected() { + println!("Connection is active."); + } else { + println!("Connection is not active."); + } + // Specify the variable name and value let variable_name = "existing_var"; let value = "new_value"; diff --git a/src/rs_openshowvar.rs b/src/rs_openshowvar.rs index 3981ae3..0639461 100644 --- a/src/rs_openshowvar.rs +++ b/src/rs_openshowvar.rs @@ -62,6 +62,29 @@ impl OpenShowVar { Ok(()) } + /// Checks if the connection to the TCP server is active. + /// + /// # Returns + /// + /// Returns `true` if the connection is active, `false` otherwise. + /// + /// # Example + /// + /// ``` + /// use rs_openshowvar::OpenShowVar; + /// let mut osv = OpenShowVar::new("127.0.0.1".to_string(), 7000); + /// osv.connect().unwrap(); + /// assert!(osv.is_connected()); + /// ``` + pub fn is_connected(&self) -> bool { + // Check if the connection object exists and the peer address is accessible + if let Some(ref conn) = self.conn { + conn.peer_addr().is_ok() + } else { + false + } + } + /// Sends a variable value. /// /// # Arguments diff --git a/tests/unit/test.rs b/tests/unit/test.rs index fb4d83b..4b73c71 100644 --- a/tests/unit/test.rs +++ b/tests/unit/test.rs @@ -75,6 +75,32 @@ fn test_connect() { assert!(osv.conn.is_some()); } +// Tests the `is_connected` method of the `OpenShowVar` struct. +#[test] +fn test_is_connected() { + // Start a mock server + let (listener, _handle) = start_mock_server(); + let addr = listener.local_addr().unwrap(); + + // Create an `OpenShowVar` instance + let mut osv = OpenShowVar::new(addr.ip().to_string(), addr.port()); + + // Before connecting, `is_connected` should return false + assert!(!osv.is_connected()); + + // Connect to the mock server + osv.connect().unwrap(); + + // After connecting, `is_connected` should return true + assert!(osv.is_connected()); + + // Disconnect from the mock server + osv.disconnect(); + + // After disconnecting, `is_connected` should return false + assert!(!osv.is_connected()); +} + // Tests the `send` method of the `OpenShowVar` struct. #[test] fn test_send() {