#!/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 proton import Message from proton_events import EventLoop, ClientHandler, TransactionHandler class TxRequest(TransactionHandler): def __init__(self, response, sender, request_delivery, conn): self.response = response self.sender = sender self.request_delivery = request_delivery self.conn = conn def on_transaction_declared(self, event): self.sender.send_msg(self.response, transaction=event.transaction) self.accept(self.request_delivery, transaction=event.transaction) 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(ClientHandler): def __init__(self, host, address): self.eventloop = EventLoop() self.conn = self.eventloop.connect(host, handler=self, reconnect=False) self.receiver = self.conn.create_receiver(address, handler=self) self.senders = {} self.relay = None def auto_accept(self): return False def on_link_open(self, event): self.conn.create_transaction() def on_message(self, event): sender = self.relay if not sender: sender = self.senders.get(event.message.reply_to) if not sender: sender = self.conn.create_sender(event.message.reply_to) self.senders[event.message.reply_to] = sender response = Message(address=event.message.reply_to, body=event.message.body.upper()) self.conn.declare_transaction(handler=TxRequest(response, sender, event.delivery, self.conn)) def on_connection_open(self, event): if event.connection.remote_offered_capabilities and 'ANONYMOUS-RELAY' in event.connection.remote_offered_capabilities: self.relay = self.conn.create_sender(None) def run(self): self.eventloop.run() try: TxServer("localhost:5672", "examples").run() except KeyboardInterrupt: pass