| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
# or more contributor license agreements. See the NOTICE file |
| 5 |
# distributed with this work for additional information |
| 6 |
# regarding copyright ownership. The ASF licenses this file |
| 7 |
# to you under the Apache License, Version 2.0 (the |
| 8 |
# "License"); you may not use this file except in compliance |
| 9 |
# with the License. You may obtain a copy of the License at |
| 10 |
# |
| 11 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
# |
| 13 |
# Unless required by applicable law or agreed to in writing, |
| 14 |
# software distributed under the License is distributed on an |
| 15 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
# KIND, either express or implied. See the License for the |
| 17 |
# specific language governing permissions and limitations |
| 18 |
# under the License. |
| 19 |
# |
| 20 |
|
| 21 |
import tornado.ioloop |
| 22 |
import tornado.web |
| 23 |
from tornado.gen import coroutine |
| 24 |
from tornado.concurrent import Future |
| 25 |
from proton import Message |
| 26 |
from proton.handlers import MessagingHandler |
| 27 |
from proton_tornado import Container |
| 28 |
|
| 29 |
|
| 30 |
class Client(MessagingHandler): |
| 31 |
def __init__(self, host, address): |
| 32 |
super(Client, self).__init__() |
| 33 |
self.host = host |
| 34 |
self.address = address |
| 35 |
self.sent = [] |
| 36 |
self.pending = [] |
| 37 |
self.reply_address = None |
| 38 |
self.sender = None |
| 39 |
self.receiver = None |
| 40 |
|
| 41 |
def on_start(self, event): |
| 42 |
conn = event.container.connect(self.host) |
| 43 |
self.sender = event.container.create_sender(conn, self.address) |
| 44 |
self.receiver = event.container.create_receiver(conn, None, dynamic=True) |
| 45 |
|
| 46 |
def on_link_opened(self, event): |
| 47 |
if event.receiver == self.receiver: |
| 48 |
self.reply_address = event.link.remote_source.address |
| 49 |
self.do_request() |
| 50 |
|
| 51 |
def on_sendable(self, event): |
| 52 |
self.do_request() |
| 53 |
|
| 54 |
def on_message(self, event): |
| 55 |
if self.sent: |
| 56 |
request, future = self.sent.pop(0) |
| 57 |
print("%s => %s" % (request, event.message.body)) |
| 58 |
future.set_result(event.message.body) |
| 59 |
self.do_request() |
| 60 |
|
| 61 |
def do_request(self): |
| 62 |
if self.pending and self.reply_address and self.sender.credit: |
| 63 |
request, future = self.pending.pop(0) |
| 64 |
self.sent.append((request, future)) |
| 65 |
req = Message(reply_to=self.reply_address, body=request) |
| 66 |
self.sender.send(req) |
| 67 |
|
| 68 |
def request(self, body): |
| 69 |
future = Future() |
| 70 |
self.pending.append((body, future)) |
| 71 |
self.do_request() |
| 72 |
self.container.touch() |
| 73 |
return future |
| 74 |
|
| 75 |
|
| 76 |
class ExampleHandler(tornado.web.RequestHandler): |
| 77 |
def initialize(self, client): |
| 78 |
self.client = client |
| 79 |
|
| 80 |
def get(self): |
| 81 |
self._write_open() |
| 82 |
self._write_form() |
| 83 |
self._write_close() |
| 84 |
|
| 85 |
@coroutine |
| 86 |
def post(self): |
| 87 |
response = yield self.client.request(self.get_body_argument("message")) |
| 88 |
self.set_header("Content-Type", "text/html") |
| 89 |
self._write_open() |
| 90 |
self._write_form() |
| 91 |
self.write("Response: " + response) |
| 92 |
self._write_close() |
| 93 |
|
| 94 |
def _write_open(self): |
| 95 |
self.write('<html><body>') |
| 96 |
|
| 97 |
def _write_close(self): |
| 98 |
self.write('</body></html>') |
| 99 |
|
| 100 |
def _write_form(self): |
| 101 |
self.write('<form action="/client" method="POST">' |
| 102 |
'Request: <input type="text" name="message">' |
| 103 |
'<input type="submit" value="Submit">' |
| 104 |
'</form>') |
| 105 |
|
| 106 |
|
| 107 |
loop = tornado.ioloop.IOLoop.instance() |
| 108 |
client = Client("localhost:5672", "examples") |
| 109 |
client.container = Container(client, loop=loop) |
| 110 |
client.container.initialise() |
| 111 |
app = tornado.web.Application([tornado.web.url(r"/client", ExampleHandler, dict(client=client))]) |
| 112 |
app.listen(8888) |
| 113 |
try: |
| 114 |
loop.start() |
| 115 |
except KeyboardInterrupt: |
| 116 |
loop.stop() |