<NME> send_async.py
<BEF> ADDFILE
<MSG> added async examples

git-svn-id: 33ed6c3feaacb64944efc691d1ae8e09b17f2bf9@1551951 13f79535-47bb-0310-9956-ffa450edef68

<DFF> @@ -0,0 +1,64 @@
+#!/usr/bin/python
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+import sys, optparse
+from async import *
+
+parser = optparse.OptionParser(usage="usage: %prog [options] <msg_1> ... <msg_n>",
+                               description="simple message sender")
+parser.add_option("-a", "--address", default="amqp://0.0.0.0",
+                  help="address: //<domain>[/<name>] (default %default)")
+parser.add_option("-r", "--reply_to", help="reply_to: //<domain>[/<name>]")
+
+opts, args = parser.parse_args()
+if not args:
+  args = ["Hello World!"]
+
+class App(CallbackAdapter):
+
+    def on_start(self):
+        print "Started"
+        self.message.clear()
+        self.message.address = opts.address
+        self.message.reply_to = opts.reply_to
+        for a in args:
+            print "Sending:", a
+            self.message.body = a
+            self.send(self.message, self.on_status)
+
+        if opts.reply_to:
+            self.messenger.recv()
+
+    def on_status(self, status):
+        print "Status:", status
+        if not opts.reply_to or opts.reply_to[0] != "~":
+            args.pop(0)
+            if not args: self.stop()
+
+    def on_recv(self, msg):
+        print "Received:", msg
+        if opts.reply_to and opts.reply_to[0] == "~":
+            args.pop(0)
+            if not args: self.stop()
+
+    def on_stop(self):
+        print "Stopped"
+
+a = App(Messenger())
+a.run()

{'commit': 'diff --git a/examples/messenger/py/async.py b/examples/messenger/py/async.py\nnew file mode 100755\nindex 00000000..14fc4c9b\n--- /dev/null\n+++ b/examples/messenger/py/async.py\n@@ -0,0 +1,81 @@\n+#!/usr/bin/python\n+#\n+# Licensed to the Apache Software Foundation (ASF) under one\n+# or more contributor license agreements.  See the NOTICE file\n+# distributed with this work for additional information\n+# regarding copyright ownership.  The ASF licenses this file\n+# to you under the Apache License, Version 2.0 (the\n+# "License"); you may not use this file except in compliance\n+# with the License.  You may obtain a copy of the License at\n+#\n+#   http://www.apache.org/licenses/LICENSE-2.0\n+#\n+# Unless required by applicable law or agreed to in writing,\n+# software distributed under the License is distributed on an\n+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n+# KIND, either express or implied.  See the License for the\n+# specific language governing permissions and limitations\n+# under the License.\n+#\n+import sys\n+from proton import *\n+\n+class CallbackAdapter:\n+\n+    def __init__(self, messenger):\n+        self.messenger = messenger\n+        self.messenger.blocking = False\n+        self.messenger.outgoing_window = 1024\n+        self.messenger.incoming_window = 1024\n+        # for application use\n+        self.message = Message()\n+        self._incoming_message = Message()\n+        self.tracked = {}\n+\n+    def run(self):\n+        self.running = True\n+        self.messenger.start()\n+        self.on_start()\n+\n+        while self.running:\n+            self.messenger.work()\n+            self._process()\n+\n+        self.messenger.stop()\n+\n+        while not self.messenger.stopped:\n+            self.messenger.work()\n+            self._process()\n+\n+        self.on_stop()\n+\n+    def stop(self):\n+        self.running = False\n+\n+    def _process(self):\n+        self._process_outgoing()\n+        self._process_incoming()\n+\n+    def _process_outgoing(self):\n+        for t, on_status in self.tracked.items():\n+            status = self.messenger.status(t)\n+            if status != PENDING:\n+                on_status(status)\n+                self.messenger.settle(t)\n+                del self.tracked[t]\n+\n+    def _process_incoming(self):\n+        while self.messenger.incoming:\n+            t = self.messenger.get(self._incoming_message)\n+            try:\n+                self.on_recv(self._incoming_message)\n+                self.messenger.accept(t)\n+            except:\n+                ex = sys.exc_info()[1]\n+                print "Exception:", ex\n+                self.messenger.reject(t)\n+\n+    def send(self, message, on_status=None):\n+        t = self.messenger.put(message)\n+        if on_status:\n+            self.tracked[t] = on_status\ndiff --git a/examples/messenger/py/recv_async.py b/examples/messenger/py/recv_async.py\nnew file mode 100755\nindex 00000000..ba4f0635\n--- /dev/null\n+++ b/examples/messenger/py/recv_async.py\n@@ -0,0 +1,55 @@\n+#!/usr/bin/python\n+#\n+# Licensed to the Apache Software Foundation (ASF) under one\n+# or more contributor license agreements.  See the NOTICE file\n+# distributed with this work for additional information\n+# regarding copyright ownership.  The ASF licenses this file\n+# to you under the Apache License, Version 2.0 (the\n+# "License"); you may not use this file except in compliance\n+# with the License.  You may obtain a copy of the License at\n+#\n+#   http://www.apache.org/licenses/LICENSE-2.0\n+#\n+# Unless required by applicable law or agreed to in writing,\n+# software distributed under the License is distributed on an\n+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n+# KIND, either express or implied.  See the License for the\n+# specific language governing permissions and limitations\n+# under the License.\n+#\n+import sys, optparse\n+from async import *\n+\n+parser = optparse.OptionParser(usage="usage: %prog [options] <addr_1> ... <addr_n>",\n+                               description="simple message receiver")\n+\n+opts, args = parser.parse_args()\n+\n+if not args:\n+  args = ["amqp://~0.0.0.0"]\n+\n+class App(CallbackAdapter):\n+\n+    def on_start(self):\n+        print "Started"\n+        for a in args:\n+            print "Subscribing to:", a\n+            self.messenger.subscribe(a)\n+        self.messenger.recv()\n+\n+    def on_recv(self, msg):\n+        print "Received:", msg\n+        if msg.body == "die":\n+            self.stop()\n+        if msg.reply_to:\n+            self.message.clear()\n+            self.message.address = msg.reply_to\n+            self.message.body = "Reply for: %s" % msg.body\n+            print "Replied:", self.message\n+            self.send(self.message)\n+\n+    def on_stop(self):\n+        print "Stopped"\n+\n+a = App(Messenger())\n+a.run()\ndiff --git a/examples/messenger/py/send_async.py b/examples/messenger/py/send_async.py\nnew file mode 100755\nindex 00000000..526fd7de\n--- /dev/null\n+++ b/examples/messenger/py/send_async.py\n@@ -0,0 +1,64 @@\n+#!/usr/bin/python\n+#\n+# Licensed to the Apache Software Foundation (ASF) under one\n+# or more contributor license agreements.  See the NOTICE file\n+# distributed with this work for additional information\n+# regarding copyright ownership.  The ASF licenses this file\n+# to you under the Apache License, Version 2.0 (the\n+# "License"); you may not use this file except in compliance\n+# with the License.  You may obtain a copy of the License at\n+#\n+#   http://www.apache.org/licenses/LICENSE-2.0\n+#\n+# Unless required by applicable law or agreed to in writing,\n+# software distributed under the License is distributed on an\n+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n+# KIND, either express or implied.  See the License for the\n+# specific language governing permissions and limitations\n+# under the License.\n+#\n+import sys, optparse\n+from async import *\n+\n+parser = optparse.OptionParser(usage="usage: %prog [options] <msg_1> ... <msg_n>",\n+                               description="simple message sender")\n+parser.add_option("-a", "--address", default="amqp://0.0.0.0",\n+                  help="address: //<domain>[/<name>] (default %default)")\n+parser.add_option("-r", "--reply_to", help="reply_to: //<domain>[/<name>]")\n+\n+opts, args = parser.parse_args()\n+if not args:\n+  args = ["Hello World!"]\n+\n+class App(CallbackAdapter):\n+\n+    def on_start(self):\n+        print "Started"\n+        self.message.clear()\n+        self.message.address = opts.address\n+        self.message.reply_to = opts.reply_to\n+        for a in args:\n+            print "Sending:", a\n+            self.message.body = a\n+            self.send(self.message, self.on_status)\n+\n+        if opts.reply_to:\n+            self.messenger.recv()\n+\n+    def on_status(self, status):\n+        print "Status:", status\n+        if not opts.reply_to or opts.reply_to[0] != "~":\n+            args.pop(0)\n+            if not args: self.stop()\n+\n+    def on_recv(self, msg):\n+        print "Received:", msg\n+        if opts.reply_to and opts.reply_to[0] == "~":\n+            args.pop(0)\n+            if not args: self.stop()\n+\n+    def on_stop(self):\n+        print "Stopped"\n+\n+a = App(Messenger())\n+a.run()\n', 'commit_subject': 'added async examples', 'repo_name': 'apache/qpid-proton', 'committer': {'name': 'Rafael H. Schloming', 'email': 'b1c66c9e7e8818f7fa26986bc8a8b76a4809d5f9@apache.org', 'time_sec': '1387376677', 'tz_offset': '0', 'date': {'seconds': '1387376677'}}, 'commit_tree': '5950ba7e5ded9b93fca8aa3fe998bd3f06718838', 'commit_message': 'added async examples\n\ngit-svn-id: 33ed6c3feaacb64944efc691d1ae8e09b17f2bf9@1551951 13f79535-47bb-0310-9956-ffa450edef68\n', 'language_name': 'C++', 'license': 'apache-2.0', 'hunks': '@@ -0,0 +1,64 @@\n+#!/usr/bin/python\n+#\n+# Licensed to the Apache Software Foundation (ASF) under one\n+# or more contributor license agreements.  See the NOTICE file\n+# distributed with this work for additional information\n+# regarding copyright ownership.  The ASF licenses this file\n+# to you under the Apache License, Version 2.0 (the\n+# "License"); you may not use this file except in compliance\n+# with the License.  You may obtain a copy of the License at\n+#\n+#   http://www.apache.org/licenses/LICENSE-2.0\n+#\n+# Unless required by applicable law or agreed to in writing,\n+# software distributed under the License is distributed on an\n+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n+# KIND, either express or implied.  See the License for the\n+# specific language governing permissions and limitations\n+# under the License.\n+#\n+import sys, optparse\n+from async import *\n+\n+parser = optparse.OptionParser(usage="usage: %prog [options] <msg_1> ... <msg_n>",\n+                               description="simple message sender")\n+parser.add_option("-a", "--address", default="amqp://0.0.0.0",\n+                  help="address: //<domain>[/<name>] (default %default)")\n+parser.add_option("-r", "--reply_to", help="reply_to: //<domain>[/<name>]")\n+\n+opts, args = parser.parse_args()\n+if not args:\n+  args = ["Hello World!"]\n+\n+class App(CallbackAdapter):\n+\n+    def on_start(self):\n+        print "Started"\n+        self.message.clear()\n+        self.message.address = opts.address\n+        self.message.reply_to = opts.reply_to\n+        for a in args:\n+            print "Sending:", a\n+            self.message.body = a\n+            self.send(self.message, self.on_status)\n+\n+        if opts.reply_to:\n+            self.messenger.recv()\n+\n+    def on_status(self, status):\n+        print "Status:", status\n+        if not opts.reply_to or opts.reply_to[0] != "~":\n+            args.pop(0)\n+            if not args: self.stop()\n+\n+    def on_recv(self, msg):\n+        print "Received:", msg\n+        if opts.reply_to and opts.reply_to[0] == "~":\n+            args.pop(0)\n+            if not args: self.stop()\n+\n+    def on_stop(self):\n+        print "Stopped"\n+\n+a = App(Messenger())\n+a.run()\n', 'addition_count': 64, 'deletion_count': 0, 'src_file': '/dev/null', 'tgt_file': 'b/examples/messenger/py/send_async.py', 'file_extension': '.py', 'before_file': 'ADDFILE', 'message': 'added async examples\n\ngit-svn-id: 33ed6c3feaacb64944efc691d1ae8e09b17f2bf9@1551951 13f79535-47bb-0310-9956-ffa450edef68\n', 'author_name': 'Rafael H. Schloming', 'lang': 'py'}