http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/messenger/server.py ---------------------------------------------------------------------- diff --git a/examples/python/messenger/server.py b/examples/python/messenger/server.py deleted file mode 100755 index 8c25879..0000000 --- a/examples/python/messenger/server.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/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. -# -from __future__ import print_function -import sys, optparse -from proton import * - -parser = optparse.OptionParser(usage="usage: %prog <addr_1> ... <addr_n>", - description="simple message server") - -opts, args = parser.parse_args() - -if not args: - args = ["amqp://~0.0.0.0"] - -mng = Messenger() -mng.start() - -for a in args: - mng.subscribe(a) - -def dispatch(request, response): - if request.subject: - response.subject = "Re: %s" % request.subject - response.properties = request.properties - print("Dispatched %s %s" % (request.subject, request.properties)) - -msg = Message() -reply = Message() - -while True: - if mng.incoming < 10: - mng.recv(10) - - if mng.incoming > 0: - mng.get(msg) - if msg.reply_to: - print(msg.reply_to) - reply.address = msg.reply_to - reply.correlation_id = msg.correlation_id - reply.body = msg.body - dispatch(msg, reply) - mng.put(reply) - mng.send() - -mng.stop()
http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/proton-server.conf ---------------------------------------------------------------------- diff --git a/examples/python/proton-server.conf b/examples/python/proton-server.conf deleted file mode 100644 index 6d236fe..0000000 --- a/examples/python/proton-server.conf +++ /dev/null @@ -1 +0,0 @@ -mech_list: EXTERNAL ANONYMOUS http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/proton_server.py ---------------------------------------------------------------------- diff --git a/examples/python/proton_server.py b/examples/python/proton_server.py deleted file mode 100755 index 9351f55..0000000 --- a/examples/python/proton_server.py +++ /dev/null @@ -1,51 +0,0 @@ -from __future__ import print_function -# -# 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. -# - -from proton import Message -from proton.reactor import Container -from proton.handlers import MessagingHandler - -class Server(MessagingHandler): - def __init__(self, host, address): - super(Server, self).__init__() - self.container = Container(self) - self.conn = self.container.connect(host) - self.receiver = self.container.create_receiver(self.conn, address) - self.sender = self.container.create_sender(self.conn, None) - - def on_message(self, event): - self.on_request(event.message.body, event.message.reply_to) - - def on_connection_close(self, endpoint, error): - if error: print("Closed due to %s" % error) - self.conn.close() - - def run(self): - self.container.run() - - def send(self, response, reply_to): - msg = Message(body=response) - if self.sender: - msg.address = reply_to - self.sender.send(msg) - - def on_request(self, request, reply_to): - pass - http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/proton_tornado.py ---------------------------------------------------------------------- diff --git a/examples/python/proton_tornado.py b/examples/python/proton_tornado.py deleted file mode 100755 index 7c787d5..0000000 --- a/examples/python/proton_tornado.py +++ /dev/null @@ -1,114 +0,0 @@ -#!/usr/bin/env 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 tornado.ioloop -from proton.reactor import Container as BaseContainer -from proton.handlers import IOHandler - -class TornadoLoopHandler: - - def __init__(self, loop=None, handler_base=None): - self.loop = loop or tornado.ioloop.IOLoop.instance() - self.io = handler_base or IOHandler() - self.count = 0 - - def on_reactor_init(self, event): - self.reactor = event.reactor - - def on_reactor_quiesced(self, event): - event.reactor.yield_() - - def on_unhandled(self, name, event): - event.dispatch(self.io) - - def _events(self, sel): - events = self.loop.ERROR - if sel.reading: - events |= self.loop.READ - if sel.writing: - events |= self.loop.WRITE - return events - - def _schedule(self, sel): - if sel.deadline: - self.loop.add_timeout(sel.deadline, lambda: self._expired(sel)) - - def _expired(self, sel): - sel.expired() - - def _process(self): - self.reactor.process() - if not self.reactor.quiesced: - self.loop.add_callback(self._process) - - def _callback(self, sel, events): - if self.loop.READ & events: - sel.readable() - if self.loop.WRITE & events: - sel.writable() - self._process() - - def on_selectable_init(self, event): - sel = event.context - if sel.fileno() >= 0: - self.loop.add_handler(sel.fileno(), lambda fd, events: self._callback(sel, events), self._events(sel)) - self._schedule(sel) - self.count += 1 - - def on_selectable_updated(self, event): - sel = event.context - if sel.fileno() > 0: - self.loop.update_handler(sel.fileno(), self._events(sel)) - self._schedule(sel) - - def on_selectable_final(self, event): - sel = event.context - if sel.fileno() > 0: - self.loop.remove_handler(sel.fileno()) - sel.release() - self.count -= 1 - if self.count == 0: - self.loop.add_callback(self._stop) - - def _stop(self): - self.reactor.stop() - self.loop.stop() - -class Container(object): - def __init__(self, *handlers, **kwargs): - self.tornado_loop = kwargs.get('loop', tornado.ioloop.IOLoop.instance()) - kwargs['global_handler'] = TornadoLoopHandler(self.tornado_loop, kwargs.get('handler_base', None)) - self.container = BaseContainer(*handlers, **kwargs) - - def initialise(self): - self.container.start() - self.container.process() - - def run(self): - self.initialise() - self.tornado_loop.start() - - def touch(self): - self._process() - - def _process(self): - self.container.process() - if not self.container.quiesced: - self.tornado_loop.add_callback(self._process) http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/queue_browser.py ---------------------------------------------------------------------- diff --git a/examples/python/queue_browser.py b/examples/python/queue_browser.py deleted file mode 100755 index 34d2377..0000000 --- a/examples/python/queue_browser.py +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env 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. -# - -from __future__ import print_function -from proton.reactor import Container, Copy -from proton.handlers import MessagingHandler - -class Recv(MessagingHandler): - def __init__(self): - super(Recv, self).__init__() - - def on_start(self, event): - conn = event.container.connect("localhost:5672") - event.container.create_receiver(conn, "examples", options=Copy()) - - def on_message(self, event): - print(event.message) - if event.receiver.queued == 0 and event.receiver.drained: - event.connection.close() - -try: - Container(Recv()).run() -except KeyboardInterrupt: pass - - - http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/README.md ---------------------------------------------------------------------- diff --git a/examples/python/reactor/README.md b/examples/python/reactor/README.md deleted file mode 100644 index b08fdbd..0000000 --- a/examples/python/reactor/README.md +++ /dev/null @@ -1,34 +0,0 @@ -The examples in this directory provide a basic introduction to the -proton reactor API and are best viewed in the order presented below. - -The examples contain comments that explain things in a tutorial-style -manner. At some point soon this content will be pulled out into a -proper tutorial that references the relevant code snippets from these -examples. Until then please bear with this clumsy style of -presentation. - -This API is present in C as well and most of these examples will -transliterate into C in a fairly straightforward way. - - - hello-world.py - - goodbye-world.py - - - scheduling.py - - counter.py - - count-randomly.py - - - unhandled.py - - reactor-logger.py - - global-logger.py - - delegates.py - - - handlers.py - - - echo.py - - cat.py - - - send.py - - recv.py - - - tornado-hello-world.py - - tornado-send.py http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/cat.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/cat.py b/examples/python/reactor/cat.py deleted file mode 100755 index 82ebd27..0000000 --- a/examples/python/reactor/cat.py +++ /dev/null @@ -1,57 +0,0 @@ -#!/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. -# - -from __future__ import print_function -import sys, os -from proton.reactor import Reactor - -class Echo: - - def __init__(self, source): - self.source = source - - def on_selectable_init(self, event): - sel = event.context # XXX: no selectable property yet - - # We can configure a selectable with any file descriptor we want. - sel.fileno(self.source.fileno()) - # Ask to be notified when the file is readable. - sel.reading = True - event.reactor.update(sel) - - def on_selectable_readable(self, event): - sel = event.context - - # The on_selectable_readable event tells us that there is data - # to be read, or the end of stream has been reached. - data = os.read(sel.fileno(), 1024) - if data: - print(data, end=' ') - else: - sel.terminate() - event.reactor.update(sel) - -class Program: - - def on_reactor_init(self, event): - event.reactor.selectable(Echo(open(sys.argv[1]))) - -r = Reactor(Program()) -r.run() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/count-randomly.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/count-randomly.py b/examples/python/reactor/count-randomly.py deleted file mode 100755 index fb3709a..0000000 --- a/examples/python/reactor/count-randomly.py +++ /dev/null @@ -1,78 +0,0 @@ -#!/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. -# - -from __future__ import print_function -import time, random -from proton.reactor import Reactor - -# Let's try to modify our counter example. In addition to counting to -# 10 in quarter second intervals, let's also print out a random number -# every half second. This is not a super easy thing to express in a -# purely sequential program, but not so difficult using events. - -class Counter: - - def __init__(self, limit): - self.limit = limit - self.count = 0 - - def on_timer_task(self, event): - self.count += 1 - print(self.count) - if not self.done(): - event.reactor.schedule(0.25, self) - - # add a public API to check for doneness - def done(self): - return self.count >= self.limit - -class Program: - - def on_reactor_init(self, event): - self.start = time.time() - print("Hello, World!") - - # Save the counter instance in an attribute so we can refer to - # it later. - self.counter = Counter(10) - event.reactor.schedule(0.25, self.counter) - - # Now schedule another event with a different handler. Note - # that the timer tasks go to separate handlers, and they don't - # interfere with each other. - event.reactor.schedule(0.5, self) - - def on_timer_task(self, event): - # keep on shouting until we are done counting - print("Yay, %s!" % random.randint(10, 100)) - if not self.counter.done(): - event.reactor.schedule(0.5, self) - - def on_reactor_final(self, event): - print("Goodbye, World! (after %s long seconds)" % (time.time() - self.start)) - -# In hello-world.py we said the reactor exits when there are no more -# events to process. While this is true, it's not actually complete. -# The reactor exits when there are no more events to process and no -# possibility of future events arising. For that reason the reactor -# will keep running until there are no more scheduled events and then -# exit. -r = Reactor(Program()) -r.run() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/counter.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/counter.py b/examples/python/reactor/counter.py deleted file mode 100755 index 7c8167a..0000000 --- a/examples/python/reactor/counter.py +++ /dev/null @@ -1,61 +0,0 @@ -#!/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. -# - -from __future__ import print_function -import time -from proton.reactor import Reactor - -class Counter: - - def __init__(self, limit): - self.limit = limit - self.count = 0 - - def on_timer_task(self, event): - self.count += 1 - print(self.count) - if self.count < self.limit: - # A recurring task can be acomplished by just scheduling - # another event. - event.reactor.schedule(0.25, self) - -class Program: - - def on_reactor_init(self, event): - self.start = time.time() - print("Hello, World!") - - # Note that unlike the previous scheduling example, we pass in - # a separate object for the handler. This means that the timer - # event we just scheduled will not be seen by Program as it is - # being handled by the Counter instance we create. - event.reactor.schedule(0.25, Counter(10)) - - def on_reactor_final(self, event): - print("Goodbye, World! (after %s long seconds)" % (time.time() - self.start)) - -# In hello-world.py we said the reactor exits when there are no more -# events to process. While this is true, it's not actually complete. -# The reactor exits when there are no more events to process and no -# possibility of future events arising. For that reason the reactor -# will keep running until there are no more scheduled events and then -# exit. -r = Reactor(Program()) -r.run() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/delegates.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/delegates.py b/examples/python/reactor/delegates.py deleted file mode 100755 index 1a8e1e9..0000000 --- a/examples/python/reactor/delegates.py +++ /dev/null @@ -1,49 +0,0 @@ -#!/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. -# - -from __future__ import print_function -import time -from proton.reactor import Reactor - -# Events know how to dispatch themselves to handlers. By combining -# this with on_unhandled, you can provide a kind of inheritance -# between handlers using delegation. - -class Hello: - - def on_reactor_init(self, event): - print("Hello, World!") - -class Goodbye: - - def on_reactor_final(self, event): - print("Goodbye, World!") - -class Program: - - def __init__(self, *delegates): - self.delegates = delegates - - def on_unhandled(self, name, event): - for d in self.delegates: - event.dispatch(d) - -r = Reactor(Program(Hello(), Goodbye())) -r.run() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/echo.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/echo.py b/examples/python/reactor/echo.py deleted file mode 100755 index 17529d9..0000000 --- a/examples/python/reactor/echo.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/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. -# - -from __future__ import print_function -import sys, os -from proton.reactor import Reactor - -class Echo: - - def __init__(self, source): - self.source = source - - def on_selectable_init(self, event): - sel = event.context # XXX: no selectable property yet - - # We can configure a selectable with any file descriptor we want. - sel.fileno(self.source.fileno()) - # Ask to be notified when the file is readable. - sel.reading = True - event.reactor.update(sel) - - def on_selectable_readable(self, event): - sel = event.context - - # The on_selectable_readable event tells us that there is data - # to be read, or the end of stream has been reached. - data = os.read(sel.fileno(), 1024) - if data: - print(data, end=' ') - else: - sel.terminate() - event.reactor.update(sel) - -class Program: - - def on_reactor_init(self, event): - # Every selectable is a possible source of future events. Our - # selectable stays alive until it reads the end of stream - # marker. This will keep the whole reactor running until we - # type Control-D. - print("Type whatever you want and then use Control-D to exit:") - event.reactor.selectable(Echo(sys.stdin)) - -r = Reactor(Program()) -r.run() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/global-logger.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/global-logger.py b/examples/python/reactor/global-logger.py deleted file mode 100755 index 3cbe11c..0000000 --- a/examples/python/reactor/global-logger.py +++ /dev/null @@ -1,59 +0,0 @@ -#!/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. -# - -from __future__ import print_function -import time -from proton.reactor import Reactor - -# Not every event goes to the reactor's event handler. If we have a -# separate handler for something like a scheduled task, then those -# events aren't logged by the logger associated with the reactor's -# handler. Sometimes this is useful if you don't want to see them, but -# sometimes you want the global picture. - -class Logger: - - def on_unhandled(self, name, event): - print("LOG:", name, event) - -class Task: - - def on_timer_task(self, event): - print("Mission accomplished!") - -class Program: - - def on_reactor_init(self, event): - print("Hello, World!") - event.reactor.schedule(0, Task()) - - def on_reactor_final(self, event): - print("Goodbye, World!") - -r = Reactor(Program()) - -# In addition to having a regular handler, the reactor also has a -# global handler that sees every event. By adding the Logger to the -# global handler instead of the regular handler, we can log every -# single event that occurs in the system regardless of whether or not -# there are specific handlers associated with the objects that are the -# target of those events. -r.global_handler.add(Logger()) -r.run() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/goodbye-world.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/goodbye-world.py b/examples/python/reactor/goodbye-world.py deleted file mode 100755 index f251c8a..0000000 --- a/examples/python/reactor/goodbye-world.py +++ /dev/null @@ -1,47 +0,0 @@ -#!/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. -# - -from __future__ import print_function -from proton.reactor import Reactor - -# So far the reactive hello-world doesn't look too different from a -# regular old non-reactive hello-world. The on_reactor_init method can -# be used roughly as a 'main' method would. A program that only uses -# that one event, however, isn't going to be very reactive. By using -# other events, we can write a fully reactive program. - -class Program: - - # As before we handle the reactor init event. - def on_reactor_init(self, event): - print("Hello, World!") - - # In addition to an initial event, the reactor also produces an - # event when it is about to exit. This may not behave much - # differently than just putting the goodbye print statement inside - # on_reactor_init, but as we grow our program, this piece of it - # will always be what happens last, and will always happen - # regardless of what other paths the main logic of our program - # might take. - def on_reactor_final(self, event): - print("Goodbye, World!") - -r = Reactor(Program()) -r.run() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/handlers.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/handlers.py b/examples/python/reactor/handlers.py deleted file mode 100755 index ee8d807..0000000 --- a/examples/python/reactor/handlers.py +++ /dev/null @@ -1,49 +0,0 @@ -#!/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. -# - -from __future__ import print_function -import time -from proton.reactor import Reactor - - -class World: - - def on_reactor_init(self, event): - print("World!") - -class Goodbye: - - def on_reactor_final(self, event): - print("Goodbye, World!") - -class Hello: - - def __init__(self): - # When an event dispatches itself to a handler, it also checks - # if that handler has a "handlers" attribute and dispatches - # the event to any children. - self.handlers = [World(), Goodbye()] - - # The parent handler always receives the event first. - def on_reactor_init(self, event): - print("Hello", end=' ') - -r = Reactor(Hello()) -r.run() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/hello-world.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/hello-world.py b/examples/python/reactor/hello-world.py deleted file mode 100755 index f1708db..0000000 --- a/examples/python/reactor/hello-world.py +++ /dev/null @@ -1,43 +0,0 @@ -#!/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. -# - -from __future__ import print_function -from proton.reactor import Reactor - -# The proton reactor provides a general purpose event processing -# library for writing reactive programs. A reactive program is defined -# by a set of event handlers. An event handler is just any class or -# object that defines the "on_<event>" methods that it cares to -# handle. - -class Program: - - # The reactor init event is produced by the reactor itself when it - # starts. - def on_reactor_init(self, event): - print("Hello, World!") - -# When you construct a reactor, you give it a handler. -r = Reactor(Program()) - -# When you call run, the reactor will process events. The reactor init -# event is what kicks off everything else. When the reactor has no -# more events to process, it exits. -r.run() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/reactor-logger.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/reactor-logger.py b/examples/python/reactor/reactor-logger.py deleted file mode 100755 index 2d3f9de..0000000 --- a/examples/python/reactor/reactor-logger.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/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. -# - -from __future__ import print_function -import time -from proton.reactor import Reactor - -class Logger: - - def on_unhandled(self, name, event): - print("LOG:", name, event) - -class Program: - - def on_reactor_init(self, event): - print("Hello, World!") - - def on_reactor_final(self, event): - print("Goodbye, World!") - -# You can pass multiple handlers to a reactor when you construct it. -# Each of these handlers will see every event the reactor sees. By -# combining this with on_unhandled, you can log each event that goes -# to the reactor. -r = Reactor(Program(), Logger()) -r.run() - -# Note that if you wanted to add the logger later, you could also -# write the above as below. All arguments to the reactor are just -# added to the default handler for the reactor. - -def logging_enabled(): - return False - -r = Reactor(Program()) -if logging_enabled(): - r.handler.add(Logger()) -r.run() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/recv.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/recv.py b/examples/python/reactor/recv.py deleted file mode 100755 index c6f07f1..0000000 --- a/examples/python/reactor/recv.py +++ /dev/null @@ -1,49 +0,0 @@ -#!/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. -# - -from __future__ import print_function -from proton import Message -from proton.reactor import Reactor -from proton.handlers import CHandshaker, CFlowController - -class Program: - - def __init__(self): - self.handlers = [CHandshaker(), CFlowController()] - self.message = Message() - - def on_reactor_init(self, event): - # Create an amqp acceptor. - event.reactor.acceptor("0.0.0.0", 5672) - # There is an optional third argument to the Reactor.acceptor - # call. Using it, we could supply a handler here that would - # become the handler for all accepted connections. If we omit - # it, the reactor simply inherets all the connection events. - - def on_delivery(self, event): - # XXX: we could make rcv.recv(self.message) work here to - # compliment the similar thing on send - rcv = event.receiver - if rcv and self.message.recv(rcv): - print(self.message) - event.delivery.settle() - -r = Reactor(Program()) -r.run() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/scheduling.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/scheduling.py b/examples/python/reactor/scheduling.py deleted file mode 100755 index 8956821..0000000 --- a/examples/python/reactor/scheduling.py +++ /dev/null @@ -1,52 +0,0 @@ -#!/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. -# - -from __future__ import print_function -import time -from proton.reactor import Reactor - -class Program: - - def on_reactor_init(self, event): - self.start = time.time() - print("Hello, World!") - - # We can schedule a task event for some point in the future. - # This will cause the reactor to stick around until it has a - # chance to process the event. - - # The first argument is the delay. The second argument is the - # handler for the event. We are just using self for now, but - # we could pass in another object if we wanted. - task = event.reactor.schedule(1.0, self) - - # We can ignore the task if we want to, but we can also use it - # to pass stuff to the handler. - task.something_to_say = "Yay" - - def on_timer_task(self, event): - task = event.context # xxx: don't have a task property on event yet - print(task.something_to_say, "my task is complete!") - - def on_reactor_final(self, event): - print("Goodbye, World! (after %s long seconds)" % (time.time() - self.start)) - -r = Reactor(Program()) -r.run() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/send.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/send.py b/examples/python/reactor/send.py deleted file mode 100755 index 4356da1..0000000 --- a/examples/python/reactor/send.py +++ /dev/null @@ -1,92 +0,0 @@ -#!/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 -from proton import Message, Url -from proton.reactor import Reactor -from proton.handlers import CHandshaker - -# This is a send in terms of low level AMQP events. There are handlers -# that can streamline this significantly if you don't want to worry -# about all the details, but it is useful to see how the AMQP engine -# classes interact with handlers and events. - -class Send: - - def __init__(self, message, target): - self.message = message - self.target = target if target is not None else "examples" - # Use the handlers property to add some default handshaking - # behaviour. - self.handlers = [CHandshaker()] - - def on_connection_init(self, event): - conn = event.connection - - # Every session or link could have their own handler(s) if we - # wanted simply by setting the "handler" slot on the - # given session or link. - ssn = conn.session() - - # If a link doesn't have an event handler, the events go to - # its parent session. If the session doesn't have a handler - # the events go to its parent connection. If the connection - # doesn't have a handler, the events go to the reactor. - snd = ssn.sender("sender") - snd.target.address = self.target - conn.open() - ssn.open() - snd.open() - - def on_transport_error(self, event): - print event.transport.condition - - def on_link_flow(self, event): - snd = event.sender - if snd.credit > 0: - dlv = snd.send(self.message) - dlv.settle() - snd.close() - snd.session.close() - snd.connection.close() - -class Program: - - def __init__(self, url, content): - self.url = url - self.content = content - - def on_reactor_init(self, event): - # You can use the connection method to create AMQP connections. - - # This connection's handler is the Send object. All the events - # for this connection will go to the Send object instead of - # going to the reactor. If you were to omit the Send object, - # all the events would go to the reactor. - event.reactor.connection_to_host(self.url.host, self.url.port, - Send(Message(self.content), - self.url.path)) - -args = sys.argv[1:] -url = Url(args.pop() if args else "localhost:5672/examples") -content = args.pop() if args else "Hello World!" - -r = Reactor(Program(url, content)) -r.run() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/tornado-hello-world.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/tornado-hello-world.py b/examples/python/reactor/tornado-hello-world.py deleted file mode 100755 index d06cd1b..0000000 --- a/examples/python/reactor/tornado-hello-world.py +++ /dev/null @@ -1,42 +0,0 @@ -#!/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. -# - -from __future__ import print_function -import tornado.ioloop -from tornado_app import TornadoApp - -# The proton reactor provides a general purpose event processing -# library for writing reactive programs. A reactive program is defined -# by a set of event handlers. An event handler is just any class or -# object that defines the "on_<event>" methods that it cares to -# handle. - -class Program: - - # The reactor init event is produced by the reactor itself when it - # starts. - def on_reactor_init(self, event): - print("Hello, World!") - -# The TornadoApp integrates a Reactor into tornado's ioloop. -TornadoApp(Program()) - -# Now the tornado main loop will behave like the reactor's main loop. -tornado.ioloop.IOLoop.instance().start() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/tornado-send.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/tornado-send.py b/examples/python/reactor/tornado-send.py deleted file mode 100755 index c69876a..0000000 --- a/examples/python/reactor/tornado-send.py +++ /dev/null @@ -1,84 +0,0 @@ -#!/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, tornado.ioloop -from tornado_app import TornadoApp -from proton import Message, Url -from proton.handlers import CHandshaker - -class Send: - - def __init__(self, message, target): - self.message = message - self.target = target if target is not None else "examples" - # Use the handlers property to add some default handshaking - # behaviour. - self.handlers = [CHandshaker()] - - def on_connection_init(self, event): - conn = event.connection - - # Every session or link could have their own handler(s) if we - # wanted simply by setting the "handler" slot on the - # given session or link. - ssn = conn.session() - - # If a link doesn't have an event handler, the events go to - # its parent session. If the session doesn't have a handler - # the events go to its parent connection. If the connection - # doesn't have a handler, the events go to the reactor. - snd = ssn.sender("sender") - snd.target.address = self.target - conn.open() - ssn.open() - snd.open() - - def on_link_flow(self, event): - snd = event.sender - if snd.credit > 0: - dlv = snd.send(self.message) - dlv.settle() - snd.close() - snd.session.close() - snd.connection.close() - -class Program: - - def __init__(self, url, content): - self.url = url - self.content = content - - def on_reactor_init(self, event): - # You can use the connection method to create AMQP connections. - - # This connection's handler is the Send object. All the events - # for this connection will go to the Send object instead of - # going to the reactor. If you were to omit the Send object, - # all the events would go to the reactor. - event.reactor.connection_to_host(self.url.host, self.url.port, - Send(Message(self.content), - self.url.path)) - -args = sys.argv[1:] -url = Url(args.pop() if args else "localhost:5672/examples") -content = args.pop() if args else "Hello World!" - -TornadoApp(Program(url, content)) -tornado.ioloop.IOLoop.instance().start() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/tornado_app.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/tornado_app.py b/examples/python/reactor/tornado_app.py deleted file mode 100644 index 966ac8b..0000000 --- a/examples/python/reactor/tornado_app.py +++ /dev/null @@ -1,93 +0,0 @@ -#!/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 tornado.ioloop -from proton.reactor import Reactor -from proton.handlers import IOHandler - -class TornadoApp: - - def __init__(self, *args): - self.reactor = Reactor(*args) - self.reactor.global_handler = self - self.io = IOHandler() - self.loop = tornado.ioloop.IOLoop.instance() - self.count = 0 - self.reactor.start() - self.reactor.process() - - def on_reactor_quiesced(self, event): - event.reactor.yield_() - - def on_unhandled(self, name, event): - event.dispatch(self.io) - - def _events(self, sel): - events = self.loop.ERROR - if sel.reading: - events |= self.loop.READ - if sel.writing: - events |= self.loop.WRITE - return events - - def _schedule(self, sel): - if sel.deadline: - self.loop.add_timeout(sel.deadline, lambda: self.expired(sel)) - - def _expired(self, sel): - sel.expired() - - def _process(self): - self.reactor.process() - if not self.reactor.quiesced: - self.loop.add_callback(self._process) - - def _callback(self, sel, events): - if self.loop.READ & events: - sel.readable() - if self.loop.WRITE & events: - sel.writable() - self._process() - - def on_selectable_init(self, event): - sel = event.context - if sel.fileno() >= 0: - self.loop.add_handler(sel.fileno(), lambda fd, events: self._callback(sel, events), self._events(sel)) - self._schedule(sel) - self.count += 1 - - def on_selectable_updated(self, event): - sel = event.context - if sel.fileno() > 0: - self.loop.update_handler(sel.fileno(), self._events(sel)) - self._schedule(sel) - - def on_selectable_final(self, event): - sel = event.context - if sel.fileno() > 0: - self.loop.remove_handler(sel.fileno()) - sel.release() - self.count -= 1 - if self.count == 0: - self.loop.add_callback(self._stop) - - def _stop(self): - self.reactor.stop() - self.loop.stop() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/reactor/unhandled.py ---------------------------------------------------------------------- diff --git a/examples/python/reactor/unhandled.py b/examples/python/reactor/unhandled.py deleted file mode 100755 index 9ab2212..0000000 --- a/examples/python/reactor/unhandled.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/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. -# - -from __future__ import print_function -import time -from proton.reactor import Reactor - -class Program: - - # If an event occurs and its handler doesn't have an on_<event> - # method, the reactor will attempt to call the on_unhandled method - # if it exists. This can be useful not only for debugging, but for - # logging and for delegating/inheritance. - def on_unhandled(self, name, event): - print(name, event) - -r = Reactor(Program()) -r.run() http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/recurring_timer.py ---------------------------------------------------------------------- diff --git a/examples/python/recurring_timer.py b/examples/python/recurring_timer.py deleted file mode 100755 index b59dbe1..0000000 --- a/examples/python/recurring_timer.py +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env 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. -# - -from __future__ import print_function -from proton.reactor import Container, Handler - -class Recurring(Handler): - def __init__(self, period): - self.period = period - - def on_reactor_init(self, event): - self.container = event.reactor - self.container.schedule(self.period, self) - - def on_timer_task(self, event): - print("Tick...") - self.container.schedule(self.period, self) - -try: - container = Container(Recurring(1.0)) - container.run() -except KeyboardInterrupt: - container.stop() - print() - - http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/recurring_timer_tornado.py ---------------------------------------------------------------------- diff --git a/examples/python/recurring_timer_tornado.py b/examples/python/recurring_timer_tornado.py deleted file mode 100755 index 07ebd26..0000000 --- a/examples/python/recurring_timer_tornado.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env 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. -# - -from __future__ import print_function -import time -from proton.reactor import Handler -from proton_tornado import TornadoLoop - -class Recurring(Handler): - def __init__(self, period): - self.period = period - - def on_start(self, event): - self.container = event.container - self.container.schedule(time.time() + self.period, subject=self) - - def on_timer(self, event): - print("Tick...") - self.container.schedule(time.time() + self.period, subject=self) - -try: - container = TornadoLoop(Recurring(1.0)) - container.run() -except KeyboardInterrupt: - container.stop() - print() - - http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/selected_recv.py ---------------------------------------------------------------------- diff --git a/examples/python/selected_recv.py b/examples/python/selected_recv.py deleted file mode 100755 index dc062d5..0000000 --- a/examples/python/selected_recv.py +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env 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. -# - -from __future__ import print_function -from proton.reactor import Container, Selector -from proton.handlers import MessagingHandler - -class Recv(MessagingHandler): - def __init__(self): - super(Recv, self).__init__() - - def on_start(self, event): - conn = event.container.connect("localhost:5672") - event.container.create_receiver(conn, "examples", options=Selector("colour = 'green'")) - - def on_message(self, event): - print(event.message.body) - -try: - Container(Recv()).run() -except KeyboardInterrupt: pass - - - http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/server.py ---------------------------------------------------------------------- diff --git a/examples/python/server.py b/examples/python/server.py deleted file mode 100755 index 8cebaad..0000000 --- a/examples/python/server.py +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/env 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. -# - -from __future__ import print_function -from proton import Message -from proton.handlers import MessagingHandler -from proton.reactor import Container - -class Server(MessagingHandler): - def __init__(self, url, address): - super(Server, self).__init__() - self.url = url - self.address = address - - def on_start(self, event): - print("Listening on", self.url) - self.container = event.container - self.conn = event.container.connect(self.url) - self.receiver = event.container.create_receiver(self.conn, self.address) - self.server = self.container.create_sender(self.conn, None) - - def on_message(self, event): - print("Received", event.message) - self.server.send(Message(address=event.message.reply_to, body=event.message.body.upper(), - correlation_id=event.message.correlation_id)) - -try: - Container(Server("0.0.0.0:5672", "examples")).run() -except KeyboardInterrupt: pass - - - http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/server_direct.py ---------------------------------------------------------------------- diff --git a/examples/python/server_direct.py b/examples/python/server_direct.py deleted file mode 100755 index a9910f1..0000000 --- a/examples/python/server_direct.py +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env 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. -# - -from __future__ import print_function -from proton import generate_uuid, Message -from proton.handlers import MessagingHandler -from proton.reactor import Container - -class Server(MessagingHandler): - def __init__(self, url): - super(Server, self).__init__() - self.url = url - self.senders = {} - - def on_start(self, event): - print("Listening on", self.url) - self.container = event.container - self.acceptor = event.container.listen(self.url) - - def on_link_opening(self, event): - if event.link.is_sender: - if event.link.remote_source and event.link.remote_source.dynamic: - event.link.source.address = str(generate_uuid()) - self.senders[event.link.source.address] = event.link - elif event.link.remote_target and event.link.remote_target.address: - event.link.target.address = event.link.remote_target.address - self.senders[event.link.remote_target.address] = event.link - elif event.link.remote_source: - event.link.source.address = event.link.remote_source.address - elif event.link.remote_target: - event.link.target.address = event.link.remote_target.address - - def on_message(self, event): - print("Received", event.message) - sender = self.senders.get(event.message.reply_to) - if not sender: - print("No link for reply") - return - sender.send(Message(address=event.message.reply_to, body=event.message.body.upper(), - correlation_id=event.message.correlation_id)) - -try: - Container(Server("0.0.0.0:8888")).run() -except KeyboardInterrupt: pass - - - http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/server_tx.py ---------------------------------------------------------------------- diff --git a/examples/python/server_tx.py b/examples/python/server_tx.py deleted file mode 100755 index 51e734c..0000000 --- a/examples/python/server_tx.py +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/env 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. -# - -from __future__ import print_function -from proton import Message -from proton.reactor import Container -from proton.handlers import MessagingHandler, TransactionHandler - -class TxRequest(TransactionHandler): - def __init__(self, response, sender, request_delivery): - super(TxRequest, self).__init__() - self.response = response - self.sender = sender - self.request_delivery = request_delivery - - def on_transaction_declared(self, event): - event.transaction.send(self.sender, self.response) - event.transaction.accept(self.request_delivery) - event.transaction.commit() - - def on_transaction_committed(self, event): - print("Request processed successfully") - - def on_transaction_aborted(self, event): - print("Request processing aborted") - - -class TxServer(MessagingHandler): - def __init__(self, host, address): - super(TxServer, self).__init__(auto_accept=False) - self.host = host - self.address = address - - def on_start(self, event): - self.container = event.container - self.conn = event.container.connect(self.host, reconnect=False) - self.receiver = event.container.create_receiver(self.conn, self.address) - self.senders = {} - self.relay = None - - def on_message(self, event): - sender = self.relay - if not sender: - sender = self.senders.get(event.message.reply_to) - if not sender: - sender = self.container.create_sender(self.conn, event.message.reply_to) - self.senders[event.message.reply_to] = sender - - response = Message(address=event.message.reply_to, body=event.message.body.upper(), - correlation_id=event.message.correlation_id) - self.container.declare_transaction(self.conn, handler=TxRequest(response, sender, event.delivery)) - - def on_connection_open(self, event): - if event.connection.remote_offered_capabilities and 'ANONYMOUS-RELAY' in event.connection.remote_offered_capabilities: - self.relay = self.container.create_sender(self.conn, None) - -try: - Container(TxServer("localhost:5672", "examples")).run() -except KeyboardInterrupt: pass - - - http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/simple_recv.py ---------------------------------------------------------------------- diff --git a/examples/python/simple_recv.py b/examples/python/simple_recv.py deleted file mode 100755 index 5322500..0000000 --- a/examples/python/simple_recv.py +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/env 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. -# - -from __future__ import print_function -import optparse -from proton.handlers import MessagingHandler -from proton.reactor import Container - -class Recv(MessagingHandler): - def __init__(self, url, count): - super(Recv, self).__init__() - self.url = url - self.expected = count - self.received = 0 - - def on_start(self, event): - event.container.create_receiver(self.url) - - def on_message(self, event): - if event.message.id and event.message.id < self.received: - # ignore duplicate message - return - if self.expected == 0 or self.received < self.expected: - print(event.message.body) - self.received += 1 - if self.received == self.expected: - event.receiver.close() - event.connection.close() - -parser = optparse.OptionParser(usage="usage: %prog [options]") -parser.add_option("-a", "--address", default="localhost:5672/examples", - help="address from which messages are received (default %default)") -parser.add_option("-m", "--messages", type="int", default=100, - help="number of messages to receive; 0 receives indefinitely (default %default)") -opts, args = parser.parse_args() - -try: - Container(Recv(opts.address, opts.messages)).run() -except KeyboardInterrupt: pass - - - http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/simple_send.py ---------------------------------------------------------------------- diff --git a/examples/python/simple_send.py b/examples/python/simple_send.py deleted file mode 100755 index 7717a16..0000000 --- a/examples/python/simple_send.py +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env 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. -# - -from __future__ import print_function, unicode_literals -import optparse -from proton import Message -from proton.handlers import MessagingHandler -from proton.reactor import Container - -class Send(MessagingHandler): - def __init__(self, url, messages): - super(Send, self).__init__() - self.url = url - self.sent = 0 - self.confirmed = 0 - self.total = messages - - def on_start(self, event): - event.container.create_sender(self.url) - - def on_sendable(self, event): - while event.sender.credit and self.sent < self.total: - msg = Message(id=(self.sent+1), body={'sequence':(self.sent+1)}) - event.sender.send(msg) - self.sent += 1 - - def on_accepted(self, event): - self.confirmed += 1 - if self.confirmed == self.total: - print("all messages confirmed") - event.connection.close() - - def on_disconnected(self, event): - self.sent = self.confirmed - -parser = optparse.OptionParser(usage="usage: %prog [options]", - description="Send messages to the supplied address.") -parser.add_option("-a", "--address", default="localhost:5672/examples", - help="address to which messages are sent (default %default)") -parser.add_option("-m", "--messages", type="int", default=100, - help="number of messages to send (default %default)") -opts, args = parser.parse_args() - -try: - Container(Send(opts.address, opts.messages)).run() -except KeyboardInterrupt: pass http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/sync_client.py ---------------------------------------------------------------------- diff --git a/examples/python/sync_client.py b/examples/python/sync_client.py deleted file mode 100755 index 0b7b045..0000000 --- a/examples/python/sync_client.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env 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. -# - -""" -Demonstrates the client side of the synchronous request-response pattern -(also known as RPC or Remote Procecure Call) using proton. - -""" -from __future__ import print_function, unicode_literals - -import optparse -from proton import Message, Url, ConnectionException, Timeout -from proton.utils import SyncRequestResponse, BlockingConnection -from proton.handlers import IncomingMessageHandler -import sys - -parser = optparse.OptionParser(usage="usage: %prog [options]", - description="Send requests to the supplied address and print responses.") -parser.add_option("-a", "--address", default="localhost:5672/examples", - help="address to which messages are sent (default %default)") -parser.add_option("-t", "--timeout", type="float", default=5, - help="Give up after this time out (default %default)") -opts, args = parser.parse_args() - -url = Url(opts.address) -client = SyncRequestResponse(BlockingConnection(url, timeout=opts.timeout), url.path) - -try: - REQUESTS= ["Twas brillig, and the slithy toves", - "Did gire and gymble in the wabe.", - "All mimsy were the borogroves,", - "And the mome raths outgrabe."] - for request in REQUESTS: - response = client.call(Message(body=request)) - print("%s => %s" % (request, response.body)) -finally: - client.connection.close() - http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/test_examples.py ---------------------------------------------------------------------- diff --git a/examples/python/test_examples.py b/examples/python/test_examples.py deleted file mode 100644 index 7ecde61..0000000 --- a/examples/python/test_examples.py +++ /dev/null @@ -1,151 +0,0 @@ -# -# 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 -import subprocess -import time -import unittest - -if sys.version_info[0] == 2: - _unicode_prefix = 'u' -else: - _unicode_prefix = '' - - -class ExamplesTest(unittest.TestCase): - def test_helloworld(self, example="helloworld.py"): - p = subprocess.Popen([example], stderr=subprocess.STDOUT, stdout=subprocess.PIPE, - universal_newlines=True) - p.wait() - output = [l.strip() for l in p.stdout] - self.assertEqual(output, ['Hello World!']) - - def test_helloworld_direct(self): - self.test_helloworld('helloworld_direct.py') - - def test_helloworld_blocking(self): - self.test_helloworld('helloworld_blocking.py') - - def test_helloworld_tornado(self): - self.test_helloworld('helloworld_tornado.py') - - def test_helloworld_direct_tornado(self): - self.test_helloworld('helloworld_direct_tornado.py') - - def test_simple_send_recv(self, recv='simple_recv.py', send='simple_send.py'): - r = subprocess.Popen([recv], stderr=subprocess.STDOUT, stdout=subprocess.PIPE, - universal_newlines=True) - s = subprocess.Popen([send], stderr=subprocess.STDOUT, stdout=subprocess.PIPE, - universal_newlines=True) - s.wait() - r.wait() - actual = [l.strip() for l in r.stdout] - expected = ["{%s'sequence': int32(%i)}" % (_unicode_prefix, (i+1)) for i in range(100)] - self.assertEqual(actual, expected) - - def test_client_server(self, client=['client.py'], server=['server.py'], sleep=0): - s = subprocess.Popen(server, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, - universal_newlines=True) - if sleep: - time.sleep(sleep) - c = subprocess.Popen(client, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, - universal_newlines=True) - c.wait() - s.terminate() - actual = [l.strip() for l in c.stdout] - inputs = ["Twas brillig, and the slithy toves", - "Did gire and gymble in the wabe.", - "All mimsy were the borogroves,", - "And the mome raths outgrabe."] - expected = ["%s => %s" % (l, l.upper()) for l in inputs] - self.assertEqual(actual, expected) - - def test_sync_client_server(self): - self.test_client_server(client=['sync_client.py']) - - def test_client_server_tx(self): - self.test_client_server(server=['server_tx.py']) - - def test_sync_client_server_tx(self): - self.test_client_server(client=['sync_client.py'], server=['server_tx.py']) - - def test_client_server_direct(self): - self.test_client_server(client=['client.py', '-a', 'localhost:8888/examples'], server=['server_direct.py'], sleep=0.5) - - def test_sync_client_server_direct(self): - self.test_client_server(client=['sync_client.py', '-a', 'localhost:8888/examples'], server=['server_direct.py'], sleep=0.5) - - def test_db_send_recv(self): - self.maxDiff = None - # setup databases - subprocess.check_call(['db_ctrl.py', 'init', './src_db']) - subprocess.check_call(['db_ctrl.py', 'init', './dst_db']) - fill = subprocess.Popen(['db_ctrl.py', 'insert', './src_db'], - stdin=subprocess.PIPE, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, - universal_newlines=True) - for i in range(100): - fill.stdin.write("Message-%i\n" % (i+1)) - fill.stdin.close() - fill.wait() - # run send and recv - r = subprocess.Popen(['db_recv.py', '-m', '100'], stderr=subprocess.STDOUT, stdout=subprocess.PIPE, - universal_newlines=True) - s = subprocess.Popen(['db_send.py', '-m', '100'], stderr=subprocess.STDOUT, stdout=subprocess.PIPE, - universal_newlines=True) - s.wait() - r.wait() - # verify output of receive - actual = [l.strip() for l in r.stdout] - expected = ["inserted message %i" % (i+1) for i in range(100)] - self.assertEqual(actual, expected) - # verify state of databases - v = subprocess.Popen(['db_ctrl.py', 'list', './dst_db'], stderr=subprocess.STDOUT, stdout=subprocess.PIPE, - universal_newlines=True) - v.wait() - expected = ["(%i, %s'Message-%i')" % ((i+1), _unicode_prefix, (i+1)) for i in range(100)] - actual = [l.strip() for l in v.stdout] - self.assertEqual(actual, expected) - - def test_tx_send_tx_recv(self): - self.test_simple_send_recv(recv='tx_recv.py', send='tx_send.py') - - def test_simple_send_direct_recv(self): - self.maxDiff = None - r = subprocess.Popen(['direct_recv.py', '-a', 'localhost:8888'], stderr=subprocess.STDOUT, stdout=subprocess.PIPE, - universal_newlines=True) - time.sleep(0.5) - s = subprocess.Popen(['simple_send.py', '-a', 'localhost:8888'], stderr=subprocess.STDOUT, stdout=subprocess.PIPE, - universal_newlines=True) - s.wait() - r.wait() - actual = [l.strip() for l in r.stdout] - expected = ["{%s'sequence': int32(%i)}" % (_unicode_prefix, (i+1)) for i in range(100)] - self.assertEqual(actual, expected) - - def test_direct_send_simple_recv(self): - s = subprocess.Popen(['direct_send.py', '-a', 'localhost:8888'], stderr=subprocess.STDOUT, stdout=subprocess.PIPE, - universal_newlines=True) - time.sleep(0.5) - r = subprocess.Popen(['simple_recv.py', '-a', 'localhost:8888'], stderr=subprocess.STDOUT, stdout=subprocess.PIPE, - universal_newlines=True) - r.wait() - s.wait() - actual = [l.strip() for l in r.stdout] - expected = ["{%s'sequence': int32(%i)}" % (_unicode_prefix, (i+1)) for i in range(100)] - self.assertEqual(actual, expected) http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/examples/python/tx_recv.py ---------------------------------------------------------------------- diff --git a/examples/python/tx_recv.py b/examples/python/tx_recv.py deleted file mode 100755 index 4baddcf..0000000 --- a/examples/python/tx_recv.py +++ /dev/null @@ -1,80 +0,0 @@ -#!/usr/bin/env 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. -# - -from __future__ import print_function -import optparse -from proton import Url -from proton.reactor import Container -from proton.handlers import MessagingHandler, TransactionHandler - -class TxRecv(MessagingHandler, TransactionHandler): - def __init__(self, url, messages, batch_size): - super(TxRecv, self).__init__(prefetch=0, auto_accept=False) - self.url = Url(url) - self.expected = messages - self.batch_size = batch_size - self.current_batch = 0 - self.committed = 0 - - def on_start(self, event): - self.container = event.container - self.conn = self.container.connect(self.url) - self.receiver = self.container.create_receiver(self.conn, self.url.path) - self.container.declare_transaction(self.conn, handler=self) - self.transaction = None - - def on_message(self, event): - print(event.message.body) - self.transaction.accept(event.delivery) - self.current_batch += 1 - if self.current_batch == self.batch_size: - self.transaction.commit() - self.transaction = None - - def on_transaction_declared(self, event): - self.receiver.flow(self.batch_size) - self.transaction = event.transaction - - def on_transaction_committed(self, event): - self.committed += self.current_batch - self.current_batch = 0 - if self.expected == 0 or self.committed < self.expected: - self.container.declare_transaction(self.conn, handler=self) - else: - event.connection.close() - - def on_disconnected(self, event): - self.current_batch = 0 - -parser = optparse.OptionParser(usage="usage: %prog [options]") -parser.add_option("-a", "--address", default="localhost:5672/examples", - help="address from which messages are received (default %default)") -parser.add_option("-m", "--messages", type="int", default=100, - help="number of messages to receive; 0 receives indefinitely (default %default)") -parser.add_option("-b", "--batch-size", type="int", default=10, - help="number of messages in each transaction (default %default)") -opts, args = parser.parse_args() - -try: - Container(TxRecv(opts.address, opts.messages, opts.batch_size)).run() -except KeyboardInterrupt: pass - - - --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org