Commit ba6ba173 by Szeberényi Imre

Reconnect fix

parent 145e2a6d
......@@ -42,22 +42,27 @@ logger.setLevel(level)
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):
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')
if self.running:
self.send_command(
command='agent_started',
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)
args={'version': Context.get_agent_version(), 'system': system})
def connectionLost(self, reason):
reactor.stop()
logger.debug("connectionLost")
self.tickId.stop()
def connectionLost2(self, reason):
self.send_command(command='agent_stopped',
......@@ -70,10 +75,13 @@ class SerialLineReceiver(SerialLineReceiverBase):
except Exception:
logger.exception("Twisted hide exception")
def __init__(self):
super(SerialLineReceiver, self).__init__()
self.lc = LoopingCall(self.tick)
self.lc.start(5, now=False)
def shutdown(self):
self.running = False
logger.debug("shutdown")
self.connectionLost2('shutdown')
d = defer.Deferred()
reactor.callLater(0.3, d.callback, "1")
return d
def send_status(self):
import psutil
......@@ -113,6 +121,7 @@ class SerialLineReceiver(SerialLineReceiverBase):
self._pretty_fun(func), ", ".join(missing_kwargs)))
def _get_command(self, command, args):
logger.debug("_get_command %s %s" % (command, args))
if not isinstance(command, basestring) or command.startswith('_'):
raise AttributeError(u'Invalid command: %s' % command)
try:
......@@ -142,6 +151,7 @@ class SerialLineReceiver(SerialLineReceiverBase):
return "<%s>" % type(fun).__name__
def handle_command(self, command, args):
logger.debug("handle_command %s %s" % (command, args))
func = self._get_command(command, args)
retval = func(**args)
self.send_response(
......@@ -151,7 +161,6 @@ class SerialLineReceiver(SerialLineReceiverBase):
def handle_response(self, response, args):
pass
def main():
# Get proper serial class and port name
(serial, port) = get_serial()
......
......@@ -34,6 +34,7 @@ class SerialPort(abstract.FileDescriptor):
self.port, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
self.reactor = reactor
self.protocol = protocol
self.connected = 1
self.protocol.makeConnection(self)
self.startReading()
......@@ -60,9 +61,12 @@ class SerialPort(abstract.FileDescriptor):
serial data.
"""
abstract.FileDescriptor.connectionLost(self, reason)
self.protocol.connectionLost(reason)
os.close(self._serial)
sleep(2)
logger.debug("Reconecting after 5s")
sleep(5)
self._serial = os.open(
self.port, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
self.connected = 1
self.protocol.makeConnection(self)
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