3 from nevow import rend, inevow
4 from foolscap.api import SturdyRef
5 from twisted.internet import address
8 from allmydata import get_package_versions_string
9 from allmydata.util import idlib
10 from common import getxmlfile, get_arg
12 class IntroducerRoot(rend.Page):
15 docFactory = getxmlfile("introducer.xhtml")
17 child_operations = None
19 def __init__(self, introducer_node):
20 self.introducer_node = introducer_node
21 self.introducer_service = introducer_node.getServiceNamed("introducer")
22 rend.Page.__init__(self, introducer_node)
24 def renderHTTP(self, ctx):
25 t = get_arg(inevow.IRequest(ctx), "t")
27 return self.render_JSON(ctx)
28 return rend.Page.renderHTTP(self, ctx)
30 def render_JSON(self, ctx):
32 clients = self.introducer_service.get_subscribers()
33 subscription_summary = dict([ (name, len(clients[name]))
34 for name in clients ])
35 res["subscription_summary"] = subscription_summary
37 announcement_summary = {}
39 for (ann,when) in self.introducer_service.get_announcements().values():
40 (furl, service_name, ri_name, nickname, ver, oldest) = ann
41 if service_name not in announcement_summary:
42 announcement_summary[service_name] = 0
43 announcement_summary[service_name] += 1
44 if service_name not in service_hosts:
45 service_hosts[service_name] = set()
46 # it's nice to know how many distinct hosts are available for
47 # each service. We define a "host" by a set of addresses
48 # (hostnames or ipv4 addresses), which we extract from the
49 # connection hints. In practice, this is usually close
50 # enough: when multiple services are run on a single host,
51 # they're usually either configured with the same addresses,
52 # or setLocationAutomatically picks up the same interfaces.
53 locations = SturdyRef(furl).getTubRef().getLocations()
54 # list of tuples, ("ipv4", host, port)
55 host = frozenset([hint[1]
57 if hint[0] == "ipv4"])
58 service_hosts[service_name].add(host)
59 res["announcement_summary"] = announcement_summary
60 distinct_hosts = dict([(name, len(hosts))
62 in service_hosts.iteritems()])
63 res["announcement_distinct_hosts"] = distinct_hosts
65 return simplejson.dumps(res, indent=1) + "\n"
67 def data_version(self, ctx, data):
68 return get_package_versions_string()
69 def data_import_path(self, ctx, data):
71 def data_my_nodeid(self, ctx, data):
72 return idlib.nodeid_b2a(self.introducer_node.nodeid)
74 def render_announcement_summary(self, ctx, data):
76 for (ann,when) in self.introducer_service.get_announcements().values():
77 (furl, service_name, ri_name, nickname, ver, oldest) = ann
78 if service_name not in services:
79 services[service_name] = 0
80 services[service_name] += 1
81 service_names = services.keys()
83 return ", ".join(["%s: %d" % (service_name, services[service_name])
84 for service_name in service_names])
86 def render_client_summary(self, ctx, data):
87 clients = self.introducer_service.get_subscribers()
88 service_names = clients.keys()
90 return ", ".join(["%s: %d" % (service_name, len(clients[service_name]))
91 for service_name in service_names])
93 def data_services(self, ctx, data):
94 introsvc = self.introducer_service
96 for (a,since) in introsvc.get_announcements().values()
97 if a[1] != "stub_client"]
98 ann.sort(lambda a,b: cmp( (a[1][1], a), (b[1][1], b) ) )
101 def render_service_row(self, ctx, (since,announcement)):
102 (furl, service_name, ri_name, nickname, ver, oldest) = announcement
105 advertised = self.show_location_hints(sr)
106 ctx.fillSlots("peerid", "%s %s" % (nodeid, nickname))
107 ctx.fillSlots("advertised", " ".join(advertised))
108 ctx.fillSlots("connected", "?")
109 TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
110 ctx.fillSlots("announced",
111 time.strftime(TIME_FORMAT, time.localtime(since)))
112 ctx.fillSlots("version", ver)
113 ctx.fillSlots("service_name", service_name)
116 def data_subscribers(self, ctx, data):
117 # use the "stub_client" announcements to get information per nodeid
119 for (ann,when) in self.introducer_service.get_announcements().values():
120 if ann[1] != "stub_client":
122 (furl, service_name, ri_name, nickname, ver, oldest) = ann
125 clients[nodeid] = ann
127 # then we actually provide information per subscriber
129 introsvc = self.introducer_service
130 for service_name, subscribers in introsvc.get_subscribers().items():
131 for (rref, timestamp) in subscribers.items():
132 sr = rref.getSturdyRef()
134 ann = clients.get(nodeid)
135 s.append( (service_name, rref, timestamp, ann) )
139 def render_subscriber_row(self, ctx, s):
140 (service_name, rref, since, ann) = s
144 (furl, service_name_2, ri_name, nickname, version, oldest) = ann
146 sr = rref.getSturdyRef()
147 # if the subscriber didn't do Tub.setLocation, nodeid will be None
148 nodeid = sr.tubID or "?"
149 ctx.fillSlots("peerid", "%s %s" % (nodeid, nickname))
150 advertised = self.show_location_hints(sr)
151 ctx.fillSlots("advertised", " ".join(advertised))
152 remote_host = rref.tracker.broker.transport.getPeer()
153 if isinstance(remote_host, address.IPv4Address):
154 remote_host_s = "%s:%d" % (remote_host.host, remote_host.port)
156 # loopback is a non-IPv4Address
157 remote_host_s = str(remote_host)
158 ctx.fillSlots("connected", remote_host_s)
159 TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
160 ctx.fillSlots("since",
161 time.strftime(TIME_FORMAT, time.localtime(since)))
162 ctx.fillSlots("version", version)
163 ctx.fillSlots("service_name", service_name)
166 def show_location_hints(self, sr, ignore_localhost=True):
168 for hint in sr.locationHints:
169 if isinstance(hint, str):
170 # Foolscap-0.2.5 and earlier used strings in .locationHints
171 if ignore_localhost and hint.startswith("127.0.0.1"):
173 advertised.append(hint.split(":")[0])
175 # Foolscap-0.2.6 and later use tuples of ("ipv4", host, port)
176 if hint[0] == "ipv4":
178 if ignore_localhost and host == "127.0.0.1":
180 advertised.append(hint[1])