From a538bd7f45db4f6594bdbddde66c123d79c97bf6 Mon Sep 17 00:00:00 2001 From: Joel Bender Date: Thu, 11 May 2017 23:45:21 -0400 Subject: [PATCH] add an option to send a 'hello' message to a client as soon as it connects --- samples/TCPServer.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/samples/TCPServer.py b/samples/TCPServer.py index 4164d0ad..fe517704 100755 --- a/samples/TCPServer.py +++ b/samples/TCPServer.py @@ -25,6 +25,9 @@ SERVER_PORT = int(os.getenv('SERVER_PORT', 9000)) IDLE_TIMEOUT = int(os.getenv('IDLE_TIMEOUT', 0)) or None +# globals +args = None + # # EchoMaster # @@ -51,9 +54,15 @@ class MiddleManASE(ApplicationServiceElement): and socket errors. """ def indication(self, add_actor=None, del_actor=None, actor_error=None, error=None): + global args + if add_actor: if _debug: MiddleManASE._debug("indication add_actor=%r", add_actor) + # it's connected, maybe say hello + if args.hello: + self.elementService.indication(PDU(b'Hello, world!\n', destination=add_actor.peer)) + if del_actor: if _debug: MiddleManASE._debug("indication del_actor=%r", del_actor) @@ -67,6 +76,8 @@ def indication(self, add_actor=None, del_actor=None, actor_error=None, error=Non # def main(): + global args + # parse the command line arguments parser = ArgumentParser(description=__doc__) parser.add_argument( @@ -84,6 +95,11 @@ def main(): help="idle connection timeout", default=IDLE_TIMEOUT, ) + parser.add_argument( + "--hello", action="store_true", + default=False, + help="send a hello message to a client when it connects", + ) args = parser.parse_args() if _debug: _log.debug("initialization")