From 43409f30c5f8808b101d4d39245ff8ba37b6412d Mon Sep 17 00:00:00 2001 From: Joel Bender Date: Wed, 26 Oct 2016 00:15:04 -0400 Subject: [PATCH] defer the stop call so it can clean up, add an option to immediately send a 'hello' message --- samples/TCPClient.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/samples/TCPClient.py b/samples/TCPClient.py index 7a524f0..6b565ab 100644 --- a/samples/TCPClient.py +++ b/samples/TCPClient.py @@ -10,7 +10,7 @@ import os from bacpypes.debugging import bacpypes_debugging, ModuleLogger -from bacpypes.core import run, stop +from bacpypes.core import run, stop, deferred from bacpypes.task import TaskManager from bacpypes.comm import PDU, Client, Server, bind, ApplicationServiceElement @@ -57,6 +57,11 @@ class MiddleMan(Client, Server): def confirmation(self, pdu): if _debug: MiddleMan._debug("confirmation %r", pdu) + # check for errors + if isinstance(pdu, Exception): + if _debug: MiddleMan._debug(" - exception: %s", pdu) + return + # pass it along self.response(pdu) @@ -82,10 +87,10 @@ class MiddleManASE(ApplicationServiceElement): if delPeer: if _debug: MiddleManASE._debug(" - delete peer %s", delPeer) - # if there are no clients, quit + # if there are no clients (TCPClientActor instances), quit if not self.elementService.clients: if _debug: MiddleManASE._debug(" - quitting") - stop() + deferred(stop) def main(): @@ -106,6 +111,11 @@ def main(): help="server port (default {!r})".format(SERVER_PORT), default=SERVER_PORT, ) + parser.add_argument( + "--hello", action="store_true", + default=False, + help="send a hello message", + ) args = parser.parse_args() if _debug: _log.debug("initialization") @@ -135,12 +145,17 @@ def main(): if _debug: _log.debug(" - task_manager: %r", task_manager) # don't wait to connect - this_director.connect(server_address) + deferred(this_director.connect, server_address) + + # send hello maybe + if args.hello: + deferred(this_middle_man.indication, PDU("hello\n")) if _debug: _log.debug("running") run() + if _debug: _log.debug("fini") if __name__ == "__main__": main()