Skip to content

Commit

Permalink
Split async_io.py and simplify server start/stop. (#2528)
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen authored Dec 25, 2024
1 parent 46879e0 commit 4eccc75
Show file tree
Hide file tree
Showing 11 changed files with 741 additions and 714 deletions.
2 changes: 1 addition & 1 deletion examples/contrib/serial_forwarder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pymodbus.client import ModbusSerialClient
from pymodbus.datastore import ModbusServerContext
from pymodbus.datastore.remote import RemoteSlaveContext
from pymodbus.server.async_io import ModbusTcpServer
from pymodbus.server import ModbusTcpServer


_logger = logging.getLogger(__file__)
Expand Down
17 changes: 6 additions & 11 deletions examples/server_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,15 @@ def setup_server(description=None, context=None, cmdline=None):
return args


async def run_async_server(args):
async def run_async_server(args) -> None:
"""Run server."""
txt = f"### start ASYNC server, listening on {args.port} - {args.comm}"
_logger.info(txt)
server = None
if args.comm == "tcp":
address = (args.host if args.host else "", args.port if args.port else None)
server = await StartAsyncTcpServer(
await StartAsyncTcpServer(
context=args.context, # Data storage
identity=args.identity, # server identify
# TBD host=
# TBD port=
address=address, # listen address
# custom_functions=[], # allow custom handling
framer=args.framer, # The framer strategy to use
Expand All @@ -160,7 +157,7 @@ async def run_async_server(args):
args.host if args.host else "127.0.0.1",
args.port if args.port else None,
)
server = await StartAsyncUdpServer(
await StartAsyncUdpServer(
context=args.context, # Data storage
identity=args.identity, # server identify
address=address, # listen address
Expand All @@ -173,7 +170,7 @@ async def run_async_server(args):
elif args.comm == "serial":
# socat -d -d PTY,link=/tmp/ptyp0,raw,echo=0,ispeed=9600
# PTY,link=/tmp/ttyp0,raw,echo=0,ospeed=9600
server = await StartAsyncSerialServer(
await StartAsyncSerialServer(
context=args.context, # Data storage
identity=args.identity, # server identify
# timeout=1, # waiting time for request to complete
Expand All @@ -190,9 +187,8 @@ async def run_async_server(args):
)
elif args.comm == "tls":
address = (args.host if args.host else "", args.port if args.port else None)
server = await StartAsyncTlsServer(
await StartAsyncTlsServer(
context=args.context, # Data storage
host="localhost", # define tcp address where to connect to.
# port=port, # on which port
identity=args.identity, # server identify
# custom_functions=[], # allow custom handling
Expand All @@ -210,10 +206,9 @@ async def run_async_server(args):
# broadcast_enable=False, # treat slave 0 as broadcast address,
# timeout=1, # waiting time for request to complete
)
return server


async def async_helper():
async def async_helper() -> None:
"""Combine setup and run."""
_logger.info("Starting...")
run_args = setup_server(description="Run asynchronous server.")
Expand Down
21 changes: 8 additions & 13 deletions examples/server_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,15 @@
_logger.setLevel("DEBUG")


def run_sync_server(args):
def run_sync_server(args) -> None:
"""Run server."""
txt = f"### start SYNC server, listening on {args.port} - {args.comm}"
_logger.info(txt)
server = None
if args.comm == "tcp":
address = ("", args.port) if args.port else None
server = StartTcpServer(
StartTcpServer(
context=args.context, # Data storage
identity=args.identity, # server identify
# TBD host=
# TBD port=
address=address, # listen address
# custom_functions=[], # allow custom handling
framer=args.framer, # The framer strategy to use
Expand All @@ -83,7 +80,7 @@ def run_sync_server(args):
)
elif args.comm == "udp":
address = ("127.0.0.1", args.port) if args.port else None
server = StartUdpServer(
StartUdpServer(
context=args.context, # Data storage
identity=args.identity, # server identify
address=address, # listen address
Expand All @@ -96,7 +93,7 @@ def run_sync_server(args):
elif args.comm == "serial":
# socat -d -d PTY,link=/tmp/ptyp0,raw,echo=0,ispeed=9600
# PTY,link=/tmp/ttyp0,raw,echo=0,ospeed=9600
server = StartSerialServer(
StartSerialServer(
context=args.context, # Data storage
identity=args.identity, # server identify
# timeout=1, # waiting time for request to complete
Expand All @@ -113,9 +110,8 @@ def run_sync_server(args):
)
elif args.comm == "tls":
address = ("", args.port) if args.port else None
server = StartTlsServer(
StartTlsServer(
context=args.context, # Data storage
host="localhost", # define tcp address where to connect to.
# port=port, # on which port
identity=args.identity, # server identify
# custom_functions=[], # allow custom handling
Expand All @@ -133,14 +129,13 @@ def run_sync_server(args):
# broadcast_enable=False, # treat slave 0 as broadcast address,
# timeout=1, # waiting time for request to complete
)
return server


def sync_helper():
def sync_helper() -> None:
"""Combine setup and run."""
run_args = server_async.setup_server(description="Run synchronous server.")
server = run_sync_server(run_args)
server.shutdown()
run_sync_server(run_args)
# server.shutdown()


if __name__ == "__main__":
Expand Down
8 changes: 5 additions & 3 deletions pymodbus/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
"get_simulator_commandline",
]

from pymodbus.server.async_io import (
from pymodbus.server.server import (
ModbusSerialServer,
ModbusTcpServer,
ModbusTlsServer,
ModbusUdpServer,
)
from pymodbus.server.simulator.http_server import ModbusSimulatorServer
from pymodbus.server.simulator.main import get_commandline as get_simulator_commandline
from pymodbus.server.startstop import (
ServerAsyncStop,
ServerStop,
StartAsyncSerialServer,
Expand All @@ -38,5 +42,3 @@
StartTlsServer,
StartUdpServer,
)
from pymodbus.server.simulator.http_server import ModbusSimulatorServer
from pymodbus.server.simulator.main import get_commandline as get_simulator_commandline
Loading

0 comments on commit 4eccc75

Please sign in to comment.