3 from nevow import rend, inevow
4 from foolscap.referenceable 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, IClient
12 class IntroducerRoot(rend.Page):
15 docFactory = getxmlfile("introducer.xhtml")
17 child_operations = None
19 def renderHTTP(self, ctx):
20 t = get_arg(inevow.IRequest(ctx), "t")
22 return self.render_JSON(ctx)
23 return rend.Page.renderHTTP(self, ctx)
25 def render_JSON(self, ctx):
26 i = IClient(ctx).getServiceNamed("introducer")
28 clients = i.get_subscribers()
29 subscription_summary = dict([ (name, len(clients[name]))
30 for name in clients ])
31 res["subscription_summary"] = subscription_summary
33 announcement_summary = {}
35 for (ann,when) in i.get_announcements().values():
36 (furl, service_name, ri_name, nickname, ver, oldest) = ann
37 if service_name not in announcement_summary:
38 announcement_summary[service_name] = 0
39 announcement_summary[service_name] += 1
40 if service_name not in service_hosts:
41 service_hosts[service_name] = set()
42 # it's nice to know how many distinct hosts are available for
43 # each service. We define a "host" by a set of addresses
44 # (hostnames or ipv4 addresses), which we extract from the
45 # connection hints. In practice, this is usually close
46 # enough: when multiple services are run on a single host,
47 # they're usually either configured with the same addresses,
48 # or setLocationAutomatically picks up the same interfaces.
49 locations = SturdyRef(furl).getTubRef().getLocations()
50 # list of tuples, ("ipv4", host, port)
51 host = frozenset([hint[1]
53 if hint[0] == "ipv4"])
54 service_hosts[service_name].add(host)
55 res["announcement_summary"] = announcement_summary
56 distinct_hosts = dict([(name, len(hosts))
58 in service_hosts.iteritems()])
59 res["announcement_distinct_hosts"] = distinct_hosts
61 return simplejson.dumps(res, indent=1) + "\n"
63 def data_version(self, ctx, data):
64 return get_package_versions_string()
65 def data_import_path(self, ctx, data):
67 def data_my_nodeid(self, ctx, data):
68 return idlib.nodeid_b2a(IClient(ctx).nodeid)
70 def render_announcement_summary(self, ctx, data):
71 i = IClient(ctx).getServiceNamed("introducer")
73 for (ann,when) in i.get_announcements().values():
74 (furl, service_name, ri_name, nickname, ver, oldest) = ann
75 if service_name not in services:
76 services[service_name] = 0
77 services[service_name] += 1
78 service_names = services.keys()
80 return ", ".join(["%s: %d" % (service_name, services[service_name])
81 for service_name in service_names])
83 def render_client_summary(self, ctx, data):
84 i = IClient(ctx).getServiceNamed("introducer")
85 clients = i.get_subscribers()
86 service_names = clients.keys()
88 return ", ".join(["%s: %d" % (service_name, len(clients[service_name]))
89 for service_name in service_names])
91 def data_services(self, ctx, data):
92 i = IClient(ctx).getServiceNamed("introducer")
94 for (a,since) in i.get_announcements().values()
95 if a[1] != "stub_client"]
96 ann.sort(lambda a,b: cmp( (a[1][1], a), (b[1][1], b) ) )
99 def render_service_row(self, ctx, (since,announcement)):
100 (furl, service_name, ri_name, nickname, ver, oldest) = announcement
103 advertised = self.show_location_hints(sr)
104 ctx.fillSlots("peerid", "%s %s" % (nodeid, nickname))
105 ctx.fillSlots("advertised", " ".join(advertised))
106 ctx.fillSlots("connected", "?")
107 TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
108 ctx.fillSlots("announced",
109 time.strftime(TIME_FORMAT, time.localtime(since)))
110 ctx.fillSlots("version", ver)
111 ctx.fillSlots("service_name", service_name)
114 def data_subscribers(self, ctx, data):
115 i = IClient(ctx).getServiceNamed("introducer")
116 # use the "stub_client" announcements to get information per nodeid
118 for (ann,when) in i.get_announcements().values():
119 if ann[1] != "stub_client":
121 (furl, service_name, ri_name, nickname, ver, oldest) = ann
124 clients[nodeid] = ann
126 # then we actually provide information per subscriber
128 for service_name, subscribers in i.get_subscribers().items():
129 for (rref, timestamp) in subscribers.items():
130 sr = rref.getSturdyRef()
132 ann = clients.get(nodeid)
133 s.append( (service_name, rref, timestamp, ann) )
137 def render_subscriber_row(self, ctx, s):
138 (service_name, rref, since, ann) = s
142 (furl, service_name_2, ri_name, nickname, version, oldest) = ann
144 sr = rref.getSturdyRef()
145 # if the subscriber didn't do Tub.setLocation, nodeid will be None
146 nodeid = sr.tubID or "?"
147 ctx.fillSlots("peerid", "%s %s" % (nodeid, nickname))
148 advertised = self.show_location_hints(sr)
149 ctx.fillSlots("advertised", " ".join(advertised))
150 remote_host = rref.tracker.broker.transport.getPeer()
151 if isinstance(remote_host, address.IPv4Address):
152 remote_host_s = "%s:%d" % (remote_host.host, remote_host.port)
154 # loopback is a non-IPv4Address
155 remote_host_s = str(remote_host)
156 ctx.fillSlots("connected", remote_host_s)
157 TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
158 ctx.fillSlots("since",
159 time.strftime(TIME_FORMAT, time.localtime(since)))
160 ctx.fillSlots("version", version)
161 ctx.fillSlots("service_name", service_name)
164 def show_location_hints(self, sr, ignore_localhost=True):
166 for hint in sr.locationHints:
167 if isinstance(hint, str):
168 # Foolscap-0.2.5 and earlier used strings in .locationHints
169 if ignore_localhost and hint.startswith("127.0.0.1"):
171 advertised.append(hint.split(":")[0])
173 # Foolscap-0.2.6 and later use tuples of ("ipv4", host, port)
174 if hint[0] == "ipv4":
176 if ignore_localhost and host == "127.0.0.1":
178 advertised.append(hint[1])