]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/introweb.py
switch all foolscap imports to use foolscap.api or foolscap.logging
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / introweb.py
1
2 import time
3 from nevow import rend, inevow
4 from foolscap.api import SturdyRef
5 from twisted.internet import address
6 import allmydata
7 import simplejson
8 from allmydata import get_package_versions_string
9 from allmydata.util import idlib
10 from common import getxmlfile, get_arg
11
12 class IntroducerRoot(rend.Page):
13
14     addSlash = True
15     docFactory = getxmlfile("introducer.xhtml")
16
17     child_operations = None
18
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)
23
24     def renderHTTP(self, ctx):
25         t = get_arg(inevow.IRequest(ctx), "t")
26         if t == "json":
27             return self.render_JSON(ctx)
28         return rend.Page.renderHTTP(self, ctx)
29
30     def render_JSON(self, ctx):
31         res = {}
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
36
37         announcement_summary = {}
38         service_hosts = {}
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]
56                               for hint in locations
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))
61                                for (name, hosts)
62                                in service_hosts.iteritems()])
63         res["announcement_distinct_hosts"] = distinct_hosts
64
65         return simplejson.dumps(res, indent=1) + "\n"
66
67     def data_version(self, ctx, data):
68         return get_package_versions_string()
69     def data_import_path(self, ctx, data):
70         return str(allmydata)
71     def data_my_nodeid(self, ctx, data):
72         return idlib.nodeid_b2a(self.introducer_node.nodeid)
73
74     def render_announcement_summary(self, ctx, data):
75         services = {}
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()
82         service_names.sort()
83         return ", ".join(["%s: %d" % (service_name, services[service_name])
84                           for service_name in service_names])
85
86     def render_client_summary(self, ctx, data):
87         clients = self.introducer_service.get_subscribers()
88         service_names = clients.keys()
89         service_names.sort()
90         return ", ".join(["%s: %d" % (service_name, len(clients[service_name]))
91                           for service_name in service_names])
92
93     def data_services(self, ctx, data):
94         introsvc = self.introducer_service
95         ann = [(since,a)
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) ) )
99         return ann
100
101     def render_service_row(self, ctx, (since,announcement)):
102         (furl, service_name, ri_name, nickname, ver, oldest) = announcement
103         sr = SturdyRef(furl)
104         nodeid = sr.tubID
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)
114         return ctx.tag
115
116     def data_subscribers(self, ctx, data):
117         # use the "stub_client" announcements to get information per nodeid
118         clients = {}
119         for (ann,when) in self.introducer_service.get_announcements().values():
120             if ann[1] != "stub_client":
121                 continue
122             (furl, service_name, ri_name, nickname, ver, oldest) = ann
123             sr = SturdyRef(furl)
124             nodeid = sr.tubID
125             clients[nodeid] = ann
126
127         # then we actually provide information per subscriber
128         s = []
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()
133                 nodeid = sr.tubID
134                 ann = clients.get(nodeid)
135                 s.append( (service_name, rref, timestamp, ann) )
136         s.sort()
137         return s
138
139     def render_subscriber_row(self, ctx, s):
140         (service_name, rref, since, ann) = s
141         nickname = "?"
142         version = "?"
143         if ann:
144             (furl, service_name_2, ri_name, nickname, version, oldest) = ann
145
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)
155         else:
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)
164         return ctx.tag
165
166     def show_location_hints(self, sr, ignore_localhost=True):
167         advertised = []
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"):
172                     continue
173                 advertised.append(hint.split(":")[0])
174             else:
175                 # Foolscap-0.2.6 and later use tuples of ("ipv4", host, port)
176                 if hint[0] == "ipv4":
177                     host = hint[1]
178                 if ignore_localhost and host == "127.0.0.1":
179                     continue
180                 advertised.append(hint[1])
181         return advertised
182
183