| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
# |
| 19 |
|
| 20 |
from proton import Message |
| 21 |
from proton.reactor import Container |
| 22 |
from proton.handlers import MessagingHandler |
| 23 |
|
| 24 |
|
| 25 |
class Server(MessagingHandler): |
| 26 |
def __init__(self, host, address): |
| 27 |
super(Server, self).__init__() |
| 28 |
self.container = Container(self) |
| 29 |
self.conn = self.container.connect(host) |
| 30 |
self.receiver = self.container.create_receiver(self.conn, address) |
| 31 |
self.sender = self.container.create_sender(self.conn, None) |
| 32 |
|
| 33 |
def on_message(self, event): |
| 34 |
self.on_request(event.message.body, event.message.reply_to) |
| 35 |
|
| 36 |
def on_connection_close(self, endpoint, error): |
| 37 |
if error: |
| 38 |
print("Closed due to %s" % error) |
| 39 |
self.conn.close() |
| 40 |
|
| 41 |
def run(self): |
| 42 |
self.container.run() |
| 43 |
|
| 44 |
def send(self, response, reply_to): |
| 45 |
msg = Message(body=response) |
| 46 |
if self.sender: |
| 47 |
msg.address = reply_to |
| 48 |
self.sender.send(msg) |
| 49 |
|
| 50 |
def on_request(self, request, reply_to): |
| 51 |
pass |