Skip to content

Commit

Permalink
refacto(examples): return Exception from Service imp
Browse files Browse the repository at this point in the history
  • Loading branch information
creberust committed Jan 16, 2024
1 parent 26e447c commit eee13e1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 67 deletions.
26 changes: 6 additions & 20 deletions examples/rtu-over-tcp-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ struct ExampleService {

impl tokio_modbus::server::Service for ExampleService {
type Request = SlaveRequest<'static>;
type Response = Response;
type Error = std::io::Error;
type Future = future::Ready<Result<Self::Response, Self::Error>>;
type Future = future::Ready<Result<Response, Exception>>;

fn call(&self, req: Self::Request) -> Self::Future {
println!("{}", req.slave);
Expand Down Expand Up @@ -68,11 +66,7 @@ impl tokio_modbus::server::Service for ExampleService {
}
_ => {
println!("SERVER: Exception::IllegalFunction - Unimplemented function code in request: {req:?}");
// TODO: We want to return a Modbus Exception response `IllegalFunction`. https://github.com/slowtec/tokio-modbus/issues/165
future::ready(Err(std::io::Error::new(
std::io::ErrorKind::AddrNotAvailable,
"Unimplemented function code in request".to_string(),
)))
future::ready(Err(Exception::IllegalFunction))
}
}
}
Expand Down Expand Up @@ -101,19 +95,15 @@ fn register_read(
registers: &HashMap<u16, u16>,
addr: u16,
cnt: u16,
) -> Result<Vec<u16>, std::io::Error> {
) -> Result<Vec<u16>, Exception> {
let mut response_values = vec![0; cnt.into()];
for i in 0..cnt {
let reg_addr = addr + i;
if let Some(r) = registers.get(&reg_addr) {
response_values[i as usize] = *r;
} else {
// TODO: Return a Modbus Exception response `IllegalDataAddress` https://github.com/slowtec/tokio-modbus/issues/165
println!("SERVER: Exception::IllegalDataAddress");
return Err(std::io::Error::new(
std::io::ErrorKind::AddrNotAvailable,
format!("no register at address {reg_addr}"),
));
return Err(Exception::IllegalDataAddress);
}
}

Expand All @@ -126,18 +116,14 @@ fn register_write(
registers: &mut HashMap<u16, u16>,
addr: u16,
values: &[u16],
) -> Result<(), std::io::Error> {
) -> Result<(), Exception> {
for (i, value) in values.iter().enumerate() {
let reg_addr = addr + i as u16;
if let Some(r) = registers.get_mut(&reg_addr) {
*r = *value;
} else {
// TODO: Return a Modbus Exception response `IllegalDataAddress` https://github.com/slowtec/tokio-modbus/issues/165
println!("SERVER: Exception::IllegalDataAddress");
return Err(std::io::Error::new(
std::io::ErrorKind::AddrNotAvailable,
format!("no register at address {reg_addr}"),
));
return Err(Exception::IllegalDataAddress);
}
}

Expand Down
10 changes: 4 additions & 6 deletions examples/rtu-server-address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@ struct Service {

impl tokio_modbus::server::Service for Service {
type Request = SlaveRequest<'static>;
type Response = Option<Response>;
type Error = std::io::Error;
type Future = future::Ready<Result<Self::Response, Self::Error>>;
type Future = future::Ready<Result<Response, Exception>>;

fn call(&self, req: Self::Request) -> Self::Future {
if req.slave != self.slave.into() {
return future::ready(Ok(None));
return future::ready(Err(Exception::IllegalFunction));
}
match req.request {
Request::ReadInputRegisters(_addr, cnt) => {
let mut registers = vec![0; cnt.into()];
registers[2] = 0x77;
future::ready(Ok(Some(Response::ReadInputRegisters(registers))))
future::ready(Ok(Response::ReadInputRegisters(registers)))
}
_ => unimplemented!(),
_ => future::ready(Err(Exception::IllegalFunction)),
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions examples/rtu-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ struct Service;

impl tokio_modbus::server::Service for Service {
type Request = SlaveRequest<'static>;
type Response = Response;
type Error = std::io::Error;
type Future = future::Ready<Result<Self::Response, Self::Error>>;
type Future = future::Ready<Result<Response, Exception>>;

fn call(&self, req: Self::Request) -> Self::Future {
match req.request {
Expand Down
24 changes: 6 additions & 18 deletions examples/tcp-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ struct ExampleService {

impl tokio_modbus::server::Service for ExampleService {
type Request = Request<'static>;
type Response = Response;
type Error = std::io::Error;
type Future = future::Ready<Result<Self::Response, Self::Error>>;
type Future = future::Ready<Result<Response, Exception>>;

fn call(&self, req: Self::Request) -> Self::Future {
match req {
Expand Down Expand Up @@ -67,11 +65,7 @@ impl tokio_modbus::server::Service for ExampleService {
}
_ => {
println!("SERVER: Exception::IllegalFunction - Unimplemented function code in request: {req:?}");
// TODO: We want to return a Modbus Exception response `IllegalFunction`. https://github.com/slowtec/tokio-modbus/issues/165
future::ready(Err(std::io::Error::new(
std::io::ErrorKind::AddrNotAvailable,
"Unimplemented function code in request".to_string(),
)))
future::ready(Err(Exception::IllegalFunction))
}
}
}
Expand Down Expand Up @@ -100,7 +94,7 @@ fn register_read(
registers: &HashMap<u16, u16>,
addr: u16,
cnt: u16,
) -> Result<Vec<u16>, std::io::Error> {
) -> Result<Vec<u16>, Exception> {
let mut response_values = vec![0; cnt.into()];
for i in 0..cnt {
let reg_addr = addr + i;
Expand All @@ -109,10 +103,7 @@ fn register_read(
} else {
// TODO: Return a Modbus Exception response `IllegalDataAddress` https://github.com/slowtec/tokio-modbus/issues/165
println!("SERVER: Exception::IllegalDataAddress");
return Err(std::io::Error::new(
std::io::ErrorKind::AddrNotAvailable,
format!("no register at address {reg_addr}"),
));
return Err(Exception::IllegalDataAddress);
}
}

Expand All @@ -125,18 +116,15 @@ fn register_write(
registers: &mut HashMap<u16, u16>,
addr: u16,
values: &[u16],
) -> Result<(), std::io::Error> {
) -> Result<(), Exception> {
for (i, value) in values.iter().enumerate() {
let reg_addr = addr + i as u16;
if let Some(r) = registers.get_mut(&reg_addr) {
*r = *value;
} else {
// TODO: Return a Modbus Exception response `IllegalDataAddress` https://github.com/slowtec/tokio-modbus/issues/165
println!("SERVER: Exception::IllegalDataAddress");
return Err(std::io::Error::new(
std::io::ErrorKind::AddrNotAvailable,
format!("no register at address {reg_addr}"),
));
return Err(Exception::IllegalDataAddress);
}
}

Expand Down
26 changes: 6 additions & 20 deletions examples/tls-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ struct ExampleService {

impl tokio_modbus::server::Service for ExampleService {
type Request = Request<'static>;
type Response = Response;
type Error = std::io::Error;
type Future = future::Ready<Result<Self::Response, Self::Error>>;
type Future = future::Ready<Result<Response, Exception>>;

fn call(&self, req: Self::Request) -> Self::Future {
match req {
Expand Down Expand Up @@ -117,11 +115,7 @@ impl tokio_modbus::server::Service for ExampleService {
}
_ => {
println!("SERVER: Exception::IllegalFunction - Unimplemented function code in request: {req:?}");
// TODO: We want to return a Modbus Exception response `IllegalFunction`. https://github.com/slowtec/tokio-modbus/issues/165
future::ready(Err(std::io::Error::new(
std::io::ErrorKind::AddrNotAvailable,
"Unimplemented function code in request".to_string(),
)))
future::ready(Err(Exception::IllegalFunction))
}
}
}
Expand Down Expand Up @@ -150,19 +144,15 @@ fn register_read(
registers: &HashMap<u16, u16>,
addr: u16,
cnt: u16,
) -> Result<Vec<u16>, std::io::Error> {
) -> Result<Vec<u16>, Exception> {
let mut response_values = vec![0; cnt.into()];
for i in 0..cnt {
let reg_addr = addr + i;
if let Some(r) = registers.get(&reg_addr) {
response_values[i as usize] = *r;
} else {
// TODO: Return a Modbus Exception response `IllegalDataAddress` https://github.com/slowtec/tokio-modbus/issues/165
println!("SERVER: Exception::IllegalDataAddress");
return Err(std::io::Error::new(
std::io::ErrorKind::AddrNotAvailable,
format!("no register at address {reg_addr}"),
));
return Err(Exception::IllegalDataAddress);
}
}

Expand All @@ -175,18 +165,14 @@ fn register_write(
registers: &mut HashMap<u16, u16>,
addr: u16,
values: &[u16],
) -> Result<(), std::io::Error> {
) -> Result<(), Exception> {
for (i, value) in values.iter().enumerate() {
let reg_addr = addr + i as u16;
if let Some(r) = registers.get_mut(&reg_addr) {
*r = *value;
} else {
// TODO: Return a Modbus Exception response `IllegalDataAddress` https://github.com/slowtec/tokio-modbus/issues/165
println!("SERVER: Exception::IllegalDataAddress");
return Err(std::io::Error::new(
std::io::ErrorKind::AddrNotAvailable,
format!("no register at address {reg_addr}"),
));
return Err(Exception::IllegalDataAddress);
}
}

Expand Down

0 comments on commit eee13e1

Please sign in to comment.