Skip to content

Commit

Permalink
add is_connected method, release v1.0.0 stable
Browse files Browse the repository at this point in the history
  • Loading branch information
selimserbes committed Dec 13, 2024
1 parent bba68f7 commit 499fd61
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 3 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rs_openshowvar"
version = "0.1.8"
version = "1.0.0"
authors = ["Selim Serbes <[email protected]>"]
edition = "2021"
description = "A Rust library for connecting to Kuka robots and performing data read/write operations using the OpenShowVar protocol."
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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";
Expand Down
7 changes: 7 additions & 0 deletions examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
23 changes: 23 additions & 0 deletions src/rs_openshowvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 499fd61

Please sign in to comment.