| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- mode: python -*- |
| 3 |
# -*- coding: utf-8 -*- |
| 4 |
|
| 5 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 6 |
# or more contributor license agreements. See the NOTICE file |
| 7 |
# distributed with this work for additional information |
| 8 |
# regarding copyright ownership. The ASF licenses this file |
| 9 |
# to you under the Apache License, Version 2.0 (the |
| 10 |
# "License"); you may not use this file except in compliance |
| 11 |
# with the License. You may obtain a copy of the License at |
| 12 |
# |
| 13 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 14 |
# |
| 15 |
# Unless required by applicable law or agreed to in writing, software |
| 16 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 17 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 |
# See the License for the specific language governing permissions and |
| 19 |
# limitations under the License. |
| 20 |
from twisted.web import server |
| 21 |
from twisted.internet import reactor |
| 22 |
|
| 23 |
from avro import ipc |
| 24 |
from avro import protocol |
| 25 |
from avro import txipc |
| 26 |
|
| 27 |
MAIL_PROTOCOL_JSON = """\ |
| 28 |
{"namespace": "example.proto", |
| 29 |
"protocol": "Mail", |
| 30 |
|
| 31 |
"types": [ |
| 32 |
{"name": "Message", "type": "record", |
| 33 |
"fields": [ |
| 34 |
{"name": "to", "type": "string"}, |
| 35 |
{"name": "from", "type": "string"}, |
| 36 |
{"name": "body", "type": "string"} |
| 37 |
] |
| 38 |
} |
| 39 |
], |
| 40 |
|
| 41 |
"messages": { |
| 42 |
"send": { |
| 43 |
"request": [{"name": "message", "type": "Message"}], |
| 44 |
"response": "string" |
| 45 |
}, |
| 46 |
"replay": { |
| 47 |
"request": [], |
| 48 |
"response": "string" |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
""" |
| 53 |
MAIL_PROTOCOL = protocol.Parse(MAIL_PROTOCOL_JSON) |
| 54 |
SERVER_ADDRESS = ('localhost', 9090) |
| 55 |
|
| 56 |
class MailResponder(ipc.Responder): |
| 57 |
def __init__(self): |
| 58 |
ipc.Responder.__init__(self, MAIL_PROTOCOL) |
| 59 |
|
| 60 |
def invoke(self, message, request): |
| 61 |
if message.name == 'send': |
| 62 |
request_content = request['message'] |
| 63 |
response = "Sent message to %(to)s from %(from)s with body %(body)s" % \ |
| 64 |
request_content |
| 65 |
return response |
| 66 |
elif message.name == 'replay': |
| 67 |
return 'replay' |
| 68 |
|
| 69 |
if __name__ == '__main__': |
| 70 |
root = server.Site(txipc.AvroResponderResource(MailResponder())) |
| 71 |
reactor.listenTCP(9090, root) |
| 72 |
reactor.run() |