-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui.py
163 lines (126 loc) · 5.17 KB
/
ui.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
import os
import sys
sys.path.append("../magnet")
from magnet import pole
from magnet import field
from string import Template
import simplejson as json
from twisted.application import service, internet
from twisted.web import resource, server, static
from twisted.internet import reactor
from agent_roles import Say, Disk, DemoRole
HOST = "amoeba.ucsd.edu"
SPEC = "../magnet/magnet/spec/amqp0-8.xml"
VH = "/resource_agents" #vhost
X = "ui" #exchange
RP = "*" #routing_pattern
SYS = "resource_agents" #system_name
SER = "resource" #service_name
r_T = "nginx_resource_agent" #token
c_T = "controller_agent" #token
class ResourceAgentUI(resource.Resource):
def __init__(self, service):
resource.Resource.__init__(self)
self.service = service
def getChild(self, name, request):
if name == '':
return self
return resource.Resource.getChild(self, name, request)
def render_GET(self, request):
role = request.args.get("role", ["Control"])[0]
method = request.args.get("method", ["say"])[0]
payload = request.args.get("payload", ["hello world"])[0]
role_payload = {"from":self.__class__.__name__, "method":method, "payload":payload}
msgobj = {"role":role, "payload":role_payload}
#print "Sending msg => '%s'" % msgobj
self.service.consume_message(msgobj)
return "{'method':%s, 'payload':%s}" % (method, payload)
class ControllerAgentUI(resource.Resource):
def __init__(self, service):
resource.Resource.__init__(self)
self.service = service
def getChild(self, name, request):
if name == '':
return self
return resource.Resource.getChild(self, name, request)
def render_GET(self, request):
role = request.args.get("role", ["Control"])[0]
method = request.args.get("method", ["say"])[0]
payload = request.args.get("payload", ["hello world"])[0]
role_payload = {"from":self.__class__.__name__, "method":method, "payload":payload}
msgobj = {"role":role, "payload":role_payload}
#print "Sending msg => '%s'" % msgobj
self.service.consume_message(msgobj)
return "{'method':%s, 'payload':%s}" % (method, payload)
class Root(resource.Resource):
isLeaf = False
def getChild(self, name, request):
if name == '':
return self
return resource.Resource.getChild(self, name, request)
def _get_children_info(self):
"""Get listing of all children and their associated
urls and class names.
This where we can expose all Agent's entry points
from the UI (aka 'what Agents do we have, and what
are their methods?')
"""
children_info = {}
#XXX use "twisted.python.reflect" or Python's inspect model here:
for kv in self.children.iteritems():
try:
# if a child has attr 'service', we assume it has
# a corresponding Agent and url. #XXX inspect all methods as well.
if hasattr(kv[1], "service"):
#kv[1].service.amqpclient #get other data from here: exchange, rk, etc
#print dir(kv[1].service)
#print dir(kv[1].service.connector)
# children_info[url] = Agents UI class name
children_info[kv[0]] = kv[1].__class__.__name__
except AttributeError:
continue
return json.dumps(children_info)
def render_GET(self, request):
children_info = self._get_children_info()
tvals = {"agents":children_info}
html = Template(open("static/index.html").read()).substitute(tvals)
return html
# --- new agent ----
say_role = Say()
disk_role = Disk()
demo_role = DemoRole()
r_agent_control_role = pole.AgentControl()
r_agent = pole.Agent(X, "osx") #(exchange, resource)
r_agent.addRole(say_role)
r_agent.addRole(disk_role)
r_agent.addRole(demo_role)
r_agent.addAgentContact('Controller', ('agents', 'Controller'))
r_agent.addRole(r_agent_control_role)
r_manlay = field.ChannelManagementLayer()
r_manlay.addAgent(r_agent)
r_connector = field.AMQPConnector(r_manlay, host='amoeba.ucsd.edu', spec_path=SPEC)
r_connector.connect()
c_agent_control_role = pole.AgentControl()
c_agent = pole.Agent(X, "osx") #(exchange, resource)
c_agent.addRole(demo_role)
c_agent.addRole(say_role)
c_agent.addRole(disk_role)
c_agent.addAgentContact('Controller', ('agents', 'Controller'))
c_agent.addRole(c_agent_control_role)
c_manlay = field.ChannelManagementLayer()
c_manlay.addAgent(c_agent)
c_connector = field.AMQPConnector(c_manlay, host='amoeba.ucsd.edu', spec_path=SPEC)
c_connector.connect()
# === Web Interface ===
webui = Root()
webui.putChild('resource', ResourceAgentUI(r_agent))
webui.putChild('controller', ControllerAgentUI(c_agent))
webui.putChild('static', static.File(os.path.join(os.path.abspath("."), "static")))
webuisite = internet.TCPServer(8008, server.Site(webui))
#=======================
# === Twisted Application ===
application = service.Application("resource_agents")
webuisite.setServiceParent(application)
r_connector.setServiceParent(application)
c_connector.setServiceParent(application)