Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify Server::Service:call signature to include request's unit ID #97

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/rtu-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
type Error = std::io::Error;
type Future = future::Ready<Result<Self::Response, Self::Error>>;

fn call(&self, req: Self::Request) -> Self::Future {
fn call(&self, _: Slave, req: Self::Request) -> Self::Future {
match req {
Request::ReadInputRegisters(_addr, cnt) => {
let mut registers = vec![0; cnt as usize];
Expand Down
2 changes: 1 addition & 1 deletion examples/tcp-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
type Error = std::io::Error;
type Future = future::Ready<Result<Self::Response, Self::Error>>;

fn call(&self, req: Self::Request) -> Self::Future {
fn call(&self, _: Slave, req: Self::Request) -> Self::Future {
match req {
Request::ReadInputRegisters(_addr, cnt) => {
let mut registers = vec![0; cnt as usize];
Expand Down
5 changes: 4 additions & 1 deletion src/server/rtu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ where
}?;

let hdr = request.hdr;
let response = service.call(request.pdu.0).await.map_err(Into::into)?;
let response = service
.call(request.hdr.slave_id.into(), request.pdu.0)
.await
.map_err(Into::into)?;
framed
.send(rtu::ResponseAdu {
hdr,
Expand Down
15 changes: 8 additions & 7 deletions src/server/service.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::slave::Slave;
use std::{future::Future, io, rc::Rc, sync::Arc};

/// A Modbus server service.
Expand All @@ -15,7 +16,7 @@ pub trait Service {
type Future: Future<Output = Result<Self::Response, Self::Error>> + Send + Sync + Unpin;

/// Process the request and return the response asynchronously.
fn call(&self, req: Self::Request) -> Self::Future;
fn call(&self, slave: Slave, req: Self::Request) -> Self::Future;
}

/// Creates new `Service` values.
Expand Down Expand Up @@ -79,8 +80,8 @@ impl<S: Service + ?Sized + 'static> Service for Box<S> {
type Error = S::Error;
type Future = S::Future;

fn call(&self, request: S::Request) -> Self::Future {
(**self).call(request)
fn call(&self, slave: Slave, request: S::Request) -> Self::Future {
(**self).call(slave, request)
}
}

Expand All @@ -90,8 +91,8 @@ impl<S: Service + ?Sized + 'static> Service for Rc<S> {
type Error = S::Error;
type Future = S::Future;

fn call(&self, request: S::Request) -> Self::Future {
(**self).call(request)
fn call(&self, slave: Slave, request: S::Request) -> Self::Future {
(**self).call(slave, request)
}
}

Expand All @@ -101,7 +102,7 @@ impl<S: Service + ?Sized + 'static> Service for Arc<S> {
type Error = S::Error;
type Future = S::Future;

fn call(&self, request: S::Request) -> Self::Future {
(**self).call(request)
fn call(&self, slave: Slave, request: S::Request) -> Self::Future {
(**self).call(slave, request)
}
}
11 changes: 8 additions & 3 deletions src/server/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ where

let request = request.unwrap()?;
let hdr = request.hdr;
let response = service.call(request.pdu.0).await.map_err(Into::into)?;
let response = service
.call(request.hdr.unit_id.into(), request.pdu.0)
.await
.map_err(Into::into)?;

framed
.send(tcp::ResponseAdu {
Expand Down Expand Up @@ -187,6 +190,7 @@ fn configure_tcp(_workers: usize, _tcp: &Socket) -> io::Result<()> {
mod tests {
use super::*;
use crate::server::Service;
use crate::slave::Slave;

use futures::future;

Expand All @@ -203,7 +207,7 @@ mod tests {
type Error = Error;
type Future = future::Ready<Result<Self::Response, Self::Error>>;

fn call(&self, _: Self::Request) -> Self::Future {
fn call(&self, _: Slave, _: Self::Request) -> Self::Future {
future::ready(Ok(self.response.clone()))
}
}
Expand All @@ -212,8 +216,9 @@ mod tests {
response: Response::ReadInputRegisters(vec![0x33]),
};

let slave = Slave(1);
let pdu = Request::ReadInputRegisters(0, 1);
let rsp_adu = service.call(pdu).await.unwrap();
let rsp_adu = service.call(slave, pdu).await.unwrap();

assert_eq!(rsp_adu, service.response);
}
Expand Down