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

Feature server address #12

Merged
merged 2 commits into from
Jul 24, 2012
Merged
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
20 changes: 12 additions & 8 deletions pymodbus/server/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,34 +187,38 @@ def _send(self, message, addr):
#---------------------------------------------------------------------------#
# Starting Factories
#---------------------------------------------------------------------------#
def StartTcpServer(context, identity=None):
def StartTcpServer(context, identity=None, server_address=None):
''' Helper method to start the Modbus Async TCP server

:param context: The server data context
:param identify: The server identity to use (default empty)
:param server_address: An optional (interface,port) to bind to.
'''
from twisted.internet import reactor

_logger.info("Starting Modbus TCP Server on %s" % Defaults.Port)
if not server_address:
server_address = ("", Defaults.Port)
_logger.info("Starting Modbus TCP Server on %s:%s" % server_address)
framer = ModbusSocketFramer
factory = ModbusServerFactory(context, framer, identity)
InstallManagementConsole({'factory': factory})
reactor.listenTCP(Defaults.Port, factory)
reactor.listenTCP(server_address[1], factory, interface=server_address[0])
reactor.run()


def StartUdpServer(context, identity=None):
def StartUdpServer(context, identity=None, server_address=None):
''' Helper method to start the Modbus Async Udp server

:param context: The server data context
:param identify: The server identity to use (default empty)
:param server_address: An optional (interface,port) to bind to.
'''
from twisted.internet import reactor

_logger.info("Starting Modbus UDP Server on %s" % Defaults.Port)
if not server_address:
server_address = ("", Defaults.Port)
_logger.info("Starting Modbus UDP Server on %s:%s" % server_address)
framer = ModbusSocketFramer
server = ModbusUdpProtocol(context, framer, identity)
reactor.listenUDP(Defaults.Port, server)
reactor.listenUDP(server_address[1], server, interface=server_address[0])
reactor.run()


Expand Down
28 changes: 17 additions & 11 deletions pymodbus/server/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ class ModbusTcpServer(SocketServer.ThreadingTCPServer):
server context instance.
'''

def __init__(self, context, framer=None, identity=None):
def __init__(self, context, framer=None, identity=None,
server_address=None):
''' Overloaded initializer for the socket server

If the identify structure is not passed in, the ModbusControlBlock
Expand All @@ -210,7 +211,7 @@ def __init__(self, context, framer=None, identity=None):
:param context: The ModbusServerContext datastore
:param framer: The framer strategy to use
:param identity: An optional identify structure

:param server_address: An optional (interface,port) to bind to.
'''
self.threads = []
self.decoder = ServerDecoder()
Expand All @@ -222,7 +223,8 @@ def __init__(self, context, framer=None, identity=None):
self.control.Identity.update(identity)

SocketServer.ThreadingTCPServer.__init__(self,
("", Defaults.Port), ModbusConnectedRequestHandler)
server_address or ("", Defaults.Port),
ModbusConnectedRequestHandler)

def process_request(self, request, client):
''' Callback for connecting a new client thread
Expand Down Expand Up @@ -251,7 +253,8 @@ class ModbusUdpServer(SocketServer.ThreadingUDPServer):
server context instance.
'''

def __init__(self, context, framer=None, identity=None):
def __init__(self, context, framer=None, identity=None,
server_address=None):
''' Overloaded initializer for the socket server

If the identify structure is not passed in, the ModbusControlBlock
Expand All @@ -260,7 +263,7 @@ def __init__(self, context, framer=None, identity=None):
:param context: The ModbusServerContext datastore
:param framer: The framer strategy to use
:param identity: An optional identify structure

:param server_address: An optional (interface,port) to bind to.
'''
self.threads = []
self.decoder = ServerDecoder()
Expand All @@ -271,8 +274,9 @@ def __init__(self, context, framer=None, identity=None):
if isinstance(identity, ModbusDeviceIdentification):
self.control.Identity.update(identity)

SocketServer.ThreadingUDPServer.__init__(self,
("", Defaults.Port), ModbusDisconnectedRequestHandler)
SocketServer.ThreadingUDPServer.__init__(
self, server_address or ("", Defaults.Port),
ModbusDisconnectedRequestHandler)

def process_request(self, request, client):
''' Callback for connecting a new client thread
Expand Down Expand Up @@ -387,25 +391,27 @@ def server_close(self):
#---------------------------------------------------------------------------#
# Creation Factories
#---------------------------------------------------------------------------#
def StartTcpServer(context=None, identity=None):
def StartTcpServer(context=None, identity=None, server_address=None):
''' A factory to start and run a tcp modbus server

:param context: The ModbusServerContext datastore
:param identity: An optional identify structure
:param server_address: An optional (interface,port) to bind to.
'''
framer = ModbusSocketFramer
server = ModbusTcpServer(context, framer, identity)
server = ModbusTcpServer(context, framer, identity, server_address)
server.serve_forever()


def StartUdpServer(context=None, identity=None):
def StartUdpServer(context=None, identity=None, server_address=None):
''' A factory to start and run a udp modbus server

:param context: The ModbusServerContext datastore
:param identity: An optional identify structure
:param server_address: An optional (interface,port) to bind to.
'''
framer = ModbusSocketFramer
server = ModbusUdpServer(context, framer, identity)
server = ModbusUdpServer(context, framer, identity, server_address)
server.serve_forever()


Expand Down