/
py_exchange_boot.py
112 lines (91 loc) · 3 KB
/
py_exchange_boot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
## The contents of this file are subject to the Mozilla Public License
## Version 1.1 (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.mozilla.org/MPL/
##
## Software distributed under the License is distributed on an "AS IS"
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
## the License for the specific language governing rights and limitations
## under the License.
##
## The Original Code is script-exchange.
##
## The Initial Developers of the Original Code are Rabbit Technologies
## Ltd and Tony Garnock-Jones.
##
## Portions created by Rabbit Technologies Ltd or by Tony Garnock-Jones
## are Copyright (C) 2010 Rabbit Technologies Ltd and Tony Garnock-Jones.
##
## All Rights Reserved.
##
## Contributor(s): ______________________________________.
import simplejson
import sys
modules = {}
def send_obj(id, op, payload):
print simplejson.dumps({'id': id, 'op': op, 'payload': payload})
sys.stdout.flush()
def dbg(v):
send_obj(None, "cast", ["io", "format", ["~p~n", [v]]])
def info(v):
send_obj(None, "cast", ["error_logger", "info_report", [v]])
def log_error(report):
send_obj(None, "cast", ["error_logger", "error_report", [report]])
def do_cast(payload):
(m, f, a) = payload
m = modules[m]
f = getattr(m, f)
return f(*a)
def mainloop1():
try:
line = raw_input()
except EOFError:
return False
req = simplejson.loads(line)
return dispatch_request(req)
def dispatch_request(req):
op = req['op']
if op == "call":
send_obj(req['id'], "reply", do_cast(req['payload']))
elif op == "cast":
do_cast(req['payload'])
else:
log_error({"message": "Invalid request sent to js instance", "req": req})
return True
def mainloop():
try:
while mainloop1():
pass
except:
import traceback
log_error({"message": "Exception in mainloop", "e": traceback.format_exc()})
###########################################################################
class Message(object):
def __init__(self, rk, props, body):
self.routing_key = rk
self.properties = props
self.body = body
self.routing_actions = []
def fanout(self):
self.routing_actions.append("fanout")
def direct(self, rk):
self.routing_actions.append(["direct", rk])
def topic(self, rk):
self.routing_actions.append(["topic", rk])
class Exchange:
def __init__(self):
self.handlers = {}
def validate(self, xname, definition):
return True
def create(self, xname, definition):
vars = {}
exec definition in globals(), vars
self.handlers[','.join(xname)] = vars['handler']
def publish(self, xname, rk, props, body):
handler = self.handlers[','.join(xname)]
msg = Message(rk, props, body)
handler(msg)
return [msg.properties, msg.body, msg.routing_actions]
modules['Exchange'] = Exchange()
info("Python instance started OK")
mainloop()