3 from nevow import rend, inevow
4 from nevow.static import File as nevow_File
5 from nevow.util import resource_filename
6 from foolscap.api import SturdyRef
7 from twisted.internet import address
10 from allmydata import get_package_versions_string
11 from allmydata.util import idlib
12 from allmydata.web.common import getxmlfile, get_arg
14 class IntroducerRoot(rend.Page):
17 docFactory = getxmlfile("introducer.xhtml")
19 child_operations = None
21 def __init__(self, introducer_node):
22 self.introducer_node = introducer_node
23 self.introducer_service = introducer_node.getServiceNamed("introducer")
24 rend.Page.__init__(self, introducer_node)
25 static_dir = resource_filename("allmydata.web", "static")
26 for filen in os.listdir(static_dir):
27 self.putChild(filen, nevow_File(os.path.join(static_dir, filen)))
29 def renderHTTP(self, ctx):
30 t = get_arg(inevow.IRequest(ctx), "t")
32 return self.render_JSON(ctx)
33 return rend.Page.renderHTTP(self, ctx)
35 def render_JSON(self, ctx):
37 clients = self.introducer_service.get_subscribers()
38 subscription_summary = dict([ (name, len(clients[name]))
39 for name in clients ])
40 res["subscription_summary"] = subscription_summary
42 announcement_summary = {}
44 for (ann,when) in self.introducer_service.get_announcements().values():
45 (furl, service_name, ri_name, nickname, ver, oldest) = ann
46 if service_name not in announcement_summary:
47 announcement_summary[service_name] = 0
48 announcement_summary[service_name] += 1
49 if service_name not in service_hosts:
50 service_hosts[service_name] = set()
51 # it's nice to know how many distinct hosts are available for
52 # each service. We define a "host" by a set of addresses
53 # (hostnames or ipv4 addresses), which we extract from the
54 # connection hints. In practice, this is usually close
55 # enough: when multiple services are run on a single host,
56 # they're usually either configured with the same addresses,
57 # or setLocationAutomatically picks up the same interfaces.
58 locations = SturdyRef(furl).getTubRef().getLocations()
59 # list of tuples, ("ipv4", host, port)
60 host = frozenset([hint[1]
62 if hint[0] == "ipv4"])
63 service_hosts[service_name].add(host)
64 res["announcement_summary"] = announcement_summary
65 distinct_hosts = dict([(name, len(hosts))
67 in service_hosts.iteritems()])
68 res["announcement_distinct_hosts"] = distinct_hosts
70 return simplejson.dumps(res, indent=1) + "\n"
72 # FIXME: This code is duplicated in root.py and introweb.py.
73 def data_version(self, ctx, data):
74 return get_package_versions_string()
75 def data_import_path(self, ctx, data):
76 return str(allmydata).replace("/", "/ ") # XXX kludge for wrapping
77 def data_my_nodeid(self, ctx, data):
78 return idlib.nodeid_b2a(self.introducer_node.nodeid)
80 def render_announcement_summary(self, ctx, data):
82 for (ann,when) in self.introducer_service.get_announcements().values():
83 (furl, service_name, ri_name, nickname, ver, oldest) = ann
84 if service_name not in services:
85 services[service_name] = 0
86 services[service_name] += 1
87 service_names = services.keys()
89 return ", ".join(["%s: %d" % (service_name, services[service_name])
90 for service_name in service_names])
92 def render_client_summary(self, ctx, data):
93 clients = self.introducer_service.get_subscribers()
94 service_names = clients.keys()
96 return ", ".join(["%s: %d" % (service_name, len(clients[service_name]))
97 for service_name in service_names])
99 def data_services(self, ctx, data):
100 introsvc = self.introducer_service
102 for (a,since) in introsvc.get_announcements().values()
103 if a[1] != "stub_client"]
104 ann.sort(lambda a,b: cmp( (a[1][1], a), (b[1][1], b) ) )
107 def render_service_row(self, ctx, (since,announcement)):
108 (furl, service_name, ri_name, nickname, ver, oldest) = announcement
111 advertised = self.show_location_hints(sr)
112 ctx.fillSlots("peerid", nodeid)
113 ctx.fillSlots("nickname", nickname)
114 ctx.fillSlots("advertised", " ".join(advertised))
115 ctx.fillSlots("connected", "?")
116 TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
117 ctx.fillSlots("announced",
118 time.strftime(TIME_FORMAT, time.localtime(since)))
119 ctx.fillSlots("version", ver)
120 ctx.fillSlots("service_name", service_name)
123 def data_subscribers(self, ctx, data):
124 # use the "stub_client" announcements to get information per nodeid
126 for (ann,when) in self.introducer_service.get_announcements().values():
127 if ann[1] != "stub_client":
129 (furl, service_name, ri_name, nickname, ver, oldest) = ann
132 clients[nodeid] = ann
134 # then we actually provide information per subscriber
136 introsvc = self.introducer_service
137 for service_name, subscribers in introsvc.get_subscribers().items():
138 for (rref, timestamp) in subscribers.items():
139 sr = rref.getSturdyRef()
141 ann = clients.get(nodeid)
142 s.append( (service_name, rref, timestamp, ann) )
146 def render_subscriber_row(self, ctx, s):
147 (service_name, rref, since, ann) = s
151 (furl, service_name_2, ri_name, nickname, version, oldest) = ann
153 sr = rref.getSturdyRef()
154 # if the subscriber didn't do Tub.setLocation, nodeid will be None
155 nodeid = sr.tubID or "?"
156 ctx.fillSlots("peerid", nodeid)
157 ctx.fillSlots("nickname", nickname)
158 advertised = self.show_location_hints(sr)
159 ctx.fillSlots("advertised", " ".join(advertised))
160 remote_host = rref.tracker.broker.transport.getPeer()
161 if isinstance(remote_host, address.IPv4Address):
162 remote_host_s = "%s:%d" % (remote_host.host, remote_host.port)
164 # loopback is a non-IPv4Address
165 remote_host_s = str(remote_host)
166 ctx.fillSlots("connected", remote_host_s)
167 TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
168 ctx.fillSlots("since",
169 time.strftime(TIME_FORMAT, time.localtime(since)))
170 ctx.fillSlots("version", version)
171 ctx.fillSlots("service_name", service_name)
174 def show_location_hints(self, sr, ignore_localhost=True):
176 for hint in sr.locationHints:
177 if isinstance(hint, str):
178 # Foolscap-0.2.5 and earlier used strings in .locationHints
179 if ignore_localhost and hint.startswith("127.0.0.1"):
181 advertised.append(hint.split(":")[0])
183 # Foolscap-0.2.6 and later use tuples of ("ipv4", host, port)
184 if hint[0] == "ipv4":
186 if ignore_localhost and host == "127.0.0.1":
188 advertised.append(hint[1])