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 allmydata.web.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 # FIXME: This code is duplicated in root.py and introweb.py.
68 def data_version(self, ctx, data):
69 return get_package_versions_string()
70 def data_import_path(self, ctx, data):
71 return str(allmydata).replace("/", "/ ") # XXX kludge for wrapping
72 def data_my_nodeid(self, ctx, data):
73 return idlib.nodeid_b2a(self.introducer_node.nodeid)
75 def render_announcement_summary(self, ctx, data):
77 for (ann,when) in self.introducer_service.get_announcements().values():
78 (furl, service_name, ri_name, nickname, ver, oldest) = ann
79 if service_name not in services:
80 services[service_name] = 0
81 services[service_name] += 1
82 service_names = services.keys()
84 return ", ".join(["%s: %d" % (service_name, services[service_name])
85 for service_name in service_names])
87 def render_client_summary(self, ctx, data):
88 clients = self.introducer_service.get_subscribers()
89 service_names = clients.keys()
91 return ", ".join(["%s: %d" % (service_name, len(clients[service_name]))
92 for service_name in service_names])
94 def data_services(self, ctx, data):
95 introsvc = self.introducer_service
97 for (a,since) in introsvc.get_announcements().values()
98 if a[1] != "stub_client"]
99 ann.sort(lambda a,b: cmp( (a[1][1], a), (b[1][1], b) ) )
102 def render_service_row(self, ctx, (since,announcement)):
103 (furl, service_name, ri_name, nickname, ver, oldest) = announcement
106 advertised = self.show_location_hints(sr)
107 ctx.fillSlots("peerid", "%s %s" % (nodeid, nickname))
108 ctx.fillSlots("advertised", " ".join(advertised))
109 ctx.fillSlots("connected", "?")
110 TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
111 ctx.fillSlots("announced",
112 time.strftime(TIME_FORMAT, time.localtime(since)))
113 ctx.fillSlots("version", ver)
114 ctx.fillSlots("service_name", service_name)
117 def data_subscribers(self, ctx, data):
118 # use the "stub_client" announcements to get information per nodeid
120 for (ann,when) in self.introducer_service.get_announcements().values():
121 if ann[1] != "stub_client":
123 (furl, service_name, ri_name, nickname, ver, oldest) = ann
126 clients[nodeid] = ann
128 # then we actually provide information per subscriber
130 introsvc = self.introducer_service
131 for service_name, subscribers in introsvc.get_subscribers().items():
132 for (rref, timestamp) in subscribers.items():
133 sr = rref.getSturdyRef()
135 ann = clients.get(nodeid)
136 s.append( (service_name, rref, timestamp, ann) )
140 def render_subscriber_row(self, ctx, s):
141 (service_name, rref, since, ann) = s
145 (furl, service_name_2, ri_name, nickname, version, oldest) = ann
147 sr = rref.getSturdyRef()
148 # if the subscriber didn't do Tub.setLocation, nodeid will be None
149 nodeid = sr.tubID or "?"
150 ctx.fillSlots("peerid", "%s %s" % (nodeid, nickname))
151 advertised = self.show_location_hints(sr)
152 ctx.fillSlots("advertised", " ".join(advertised))
153 remote_host = rref.tracker.broker.transport.getPeer()
154 if isinstance(remote_host, address.IPv4Address):
155 remote_host_s = "%s:%d" % (remote_host.host, remote_host.port)
157 # loopback is a non-IPv4Address
158 remote_host_s = str(remote_host)
159 ctx.fillSlots("connected", remote_host_s)
160 TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
161 ctx.fillSlots("since",
162 time.strftime(TIME_FORMAT, time.localtime(since)))
163 ctx.fillSlots("version", version)
164 ctx.fillSlots("service_name", service_name)
167 def show_location_hints(self, sr, ignore_localhost=True):
169 for hint in sr.locationHints:
170 if isinstance(hint, str):
171 # Foolscap-0.2.5 and earlier used strings in .locationHints
172 if ignore_localhost and hint.startswith("127.0.0.1"):
174 advertised.append(hint.split(":")[0])
176 # Foolscap-0.2.6 and later use tuples of ("ipv4", host, port)
177 if hint[0] == "ipv4":
179 if ignore_localhost and host == "127.0.0.1":
181 advertised.append(hint[1])