Commit ba6ba173 by Szeberényi Imre

Reconnect fix

parent 145e2a6d
...@@ -42,22 +42,27 @@ logger.setLevel(level) ...@@ -42,22 +42,27 @@ logger.setLevel(level)
class SerialLineReceiver(SerialLineReceiverBase): class SerialLineReceiver(SerialLineReceiverBase):
def __init__(self):
super(SerialLineReceiver, self).__init__()
self.tickId = LoopingCall(self.tick)
reactor.addSystemEventTrigger("before", "shutdown", self.shutdown)
self.running = True
def connectionMade(self): def connectionMade(self):
logger.debug("connectionMade")
self.tickId.start(5, now=False)
self.transport.dataBuffer = b""
self.transport._tempDataBuffer = [] # will be added to dataBuffer in doWrite
self.transport._tempDataLen = 0
self.transport.write('\r\n') self.transport.write('\r\n')
self.send_command( if self.running:
command='agent_started', self.send_command(
args={'version': Context.get_agent_version(), command='agent_started',
'system': system}) args={'version': Context.get_agent_version(), 'system': system})
def shutdown():
self.connectionLost2('shutdown')
d = defer.Deferred()
reactor.callLater(0.3, d.callback, "1")
return d
reactor.addSystemEventTrigger("before", "shutdown", shutdown)
def connectionLost(self, reason): def connectionLost(self, reason):
reactor.stop() logger.debug("connectionLost")
self.tickId.stop()
def connectionLost2(self, reason): def connectionLost2(self, reason):
self.send_command(command='agent_stopped', self.send_command(command='agent_stopped',
...@@ -70,10 +75,13 @@ class SerialLineReceiver(SerialLineReceiverBase): ...@@ -70,10 +75,13 @@ class SerialLineReceiver(SerialLineReceiverBase):
except Exception: except Exception:
logger.exception("Twisted hide exception") logger.exception("Twisted hide exception")
def __init__(self): def shutdown(self):
super(SerialLineReceiver, self).__init__() self.running = False
self.lc = LoopingCall(self.tick) logger.debug("shutdown")
self.lc.start(5, now=False) self.connectionLost2('shutdown')
d = defer.Deferred()
reactor.callLater(0.3, d.callback, "1")
return d
def send_status(self): def send_status(self):
import psutil import psutil
...@@ -113,6 +121,7 @@ class SerialLineReceiver(SerialLineReceiverBase): ...@@ -113,6 +121,7 @@ class SerialLineReceiver(SerialLineReceiverBase):
self._pretty_fun(func), ", ".join(missing_kwargs))) self._pretty_fun(func), ", ".join(missing_kwargs)))
def _get_command(self, command, args): def _get_command(self, command, args):
logger.debug("_get_command %s %s" % (command, args))
if not isinstance(command, basestring) or command.startswith('_'): if not isinstance(command, basestring) or command.startswith('_'):
raise AttributeError(u'Invalid command: %s' % command) raise AttributeError(u'Invalid command: %s' % command)
try: try:
...@@ -142,6 +151,7 @@ class SerialLineReceiver(SerialLineReceiverBase): ...@@ -142,6 +151,7 @@ class SerialLineReceiver(SerialLineReceiverBase):
return "<%s>" % type(fun).__name__ return "<%s>" % type(fun).__name__
def handle_command(self, command, args): def handle_command(self, command, args):
logger.debug("handle_command %s %s" % (command, args))
func = self._get_command(command, args) func = self._get_command(command, args)
retval = func(**args) retval = func(**args)
self.send_response( self.send_response(
...@@ -151,7 +161,6 @@ class SerialLineReceiver(SerialLineReceiverBase): ...@@ -151,7 +161,6 @@ class SerialLineReceiver(SerialLineReceiverBase):
def handle_response(self, response, args): def handle_response(self, response, args):
pass pass
def main(): def main():
# Get proper serial class and port name # Get proper serial class and port name
(serial, port) = get_serial() (serial, port) = get_serial()
......
...@@ -34,6 +34,7 @@ class SerialPort(abstract.FileDescriptor): ...@@ -34,6 +34,7 @@ class SerialPort(abstract.FileDescriptor):
self.port, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK) self.port, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
self.reactor = reactor self.reactor = reactor
self.protocol = protocol self.protocol = protocol
self.connected = 1
self.protocol.makeConnection(self) self.protocol.makeConnection(self)
self.startReading() self.startReading()
...@@ -60,9 +61,12 @@ class SerialPort(abstract.FileDescriptor): ...@@ -60,9 +61,12 @@ class SerialPort(abstract.FileDescriptor):
serial data. serial data.
""" """
abstract.FileDescriptor.connectionLost(self, reason) abstract.FileDescriptor.connectionLost(self, reason)
self.protocol.connectionLost(reason)
os.close(self._serial) os.close(self._serial)
sleep(2) logger.debug("Reconecting after 5s")
sleep(5)
self._serial = os.open( self._serial = os.open(
self.port, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK) self.port, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
self.connected = 1
self.protocol.makeConnection(self)
self.startReading() self.startReading()
logger.info("Reconnecting")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment