Skip to content

Commit

Permalink
#149 SerialServer could be stopped when running on a thread
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoomakethu committed Aug 4, 2017
1 parent 8167445 commit 7d9a717
Showing 1 changed file with 23 additions and 36 deletions.
59 changes: 23 additions & 36 deletions pymodbus/server/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.transaction import *
from pymodbus.exceptions import NotImplementedException, NoSuchSlaveException
from pymodbus.exceptions import ModbusIOException
from pymodbus.exceptions import InvalidMessageRecievedException
from pymodbus.pdu import ModbusExceptions as merror
from pymodbus.compat import socketserver, byte2int

Expand All @@ -30,6 +28,7 @@
#---------------------------------------------------------------------------#
# Protocol Handlers
#---------------------------------------------------------------------------#

class ModbusBaseRequestHandler(socketserver.BaseRequestHandler):
''' Implements the modbus server protocol
Expand Down Expand Up @@ -87,34 +86,6 @@ def send(self, message):
raise NotImplementedException("Method not implemented by derived class")


# class ModbusSingleRequestHandler(ModbusBaseRequestHandler):
# ''' Implements the modbus server protocol
#
# This uses the socketserver.BaseRequestHandler to implement
# the client handler for a single client(serial clients)
# '''
#
# def handle(self):
# ''' Callback when we receive any data
# '''
# while self.running:
# try:
# data = self.request.recv(1024)
# if data:
# if _logger.isEnabledFor(logging.DEBUG):
# _logger.debug(' '.join([hex(byte2int(x)) for x in data]))
# self.framer.processIncomingPacket(data, self.execute)
# except Exception as msg:
# # since we only have a single socket, we cannot exit
# _logger.error("Socket error occurred %s" % msg)
#
# def send(self, message):
# ''' Send a request (string) to the network
#
# :param message: The unencoded modbus response
# '''


class ModbusSingleRequestHandler(ModbusBaseRequestHandler):
''' Implements the modbus server protocol
Expand All @@ -138,7 +109,6 @@ def handle(self):
self.framer.resetFrame()
_logger.error("Socket error occurred %s" % msg)


def send(self, message):
''' Send a request (string) to the network
Expand All @@ -152,6 +122,16 @@ def send(self, message):
return self.request.send(pdu)


class CustomSingleRequestHandler(ModbusSingleRequestHandler):

def __init__(self, request, client_address, server):
self.request = request
self.client_address = client_address
self.server = server
self.running = True
self.setup()


class ModbusConnectedRequestHandler(ModbusBaseRequestHandler):
''' Implements the modbus server protocol
Expand Down Expand Up @@ -389,6 +369,8 @@ class ModbusSerialServer(object):
server context instance.
'''

handler = None

def __init__(self, context, framer=None, identity=None, **kwargs):
''' Overloaded initializer for the socket server
Expand Down Expand Up @@ -425,6 +407,7 @@ def __init__(self, context, framer=None, identity=None, **kwargs):
self.socket = None
self._connect()
self.is_running = True
self._build_handler()

def _connect(self):
''' Connect to the serial server
Expand All @@ -449,9 +432,9 @@ def _build_handler(self):
request = self.socket
request.send = request.write
request.recv = request.read
handler = ModbusSingleRequestHandler(request,
(self.device, self.device), self)
return handler
self.handler = CustomSingleRequestHandler(request,
(self.device, self.device),
self)

def serve_forever(self):
''' Callback for connecting a new client thread
Expand All @@ -460,15 +443,19 @@ def serve_forever(self):
:param client: The address of the client
'''
_logger.debug("Started thread to serve client")
handler = self._build_handler()
if not self.handler:
self._build_handler()
while self.is_running:
handler.handle()
self.handler.handle()

def server_close(self):
''' Callback for stopping the running server
'''
_logger.debug("Modbus server stopped")
self.is_running = False
self.handler.finish()
self.handler.running = False
self.handler = None
self.socket.close()


Expand Down

0 comments on commit 7d9a717

Please sign in to comment.