Debian Codegraph @

python/examples/server_direct.py

raw
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 uuid
22 from proton import Message
23 from proton.handlers import MessagingHandler
24 from proton.reactor import Container
25
26
27 class Server(MessagingHandler):
28 def __init__(self, url):
29 super(Server, self).__init__()
30 self.url = url
31 self.senders = {}
32
33 def on_start(self, event):
34 print("Listening on", self.url)
35 self.container = event.container
36 self.acceptor = event.container.listen(self.url)
37
38 def on_link_opening(self, event):
39 if event.link.is_sender:
40 if event.link.remote_source and event.link.remote_source.dynamic:
41 event.link.source.address = str(uuid.uuid4())
42 self.senders[event.link.source.address] = event.link
43 elif event.link.remote_target and event.link.remote_target.address:
44 event.link.target.address = event.link.remote_target.address
45 self.senders[event.link.remote_target.address] = event.link
46 elif event.link.remote_source:
47 event.link.source.address = event.link.remote_source.address
48 elif event.link.remote_target:
49 event.link.target.address = event.link.remote_target.address
50
51 def on_message(self, event):
52 print("Received", event.message)
53 sender = self.senders.get(event.message.reply_to)
54 if not sender:
55 print("No link for reply")
56 return
57 sender.send(Message(address=event.message.reply_to, body=event.message.body.upper(),
58 correlation_id=event.message.correlation_id))
59
60
61 try:
62 Container(Server("0.0.0.0:8888")).run()
63 except KeyboardInterrupt:
64 pass