]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/introweb.py
Change relative imports to absolute
[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 allmydata.web.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     # 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)
74
75     def render_announcement_summary(self, ctx, data):
76         services = {}
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()
83         service_names.sort()
84         return ", ".join(["%s: %d" % (service_name, services[service_name])
85                           for service_name in service_names])
86
87     def render_client_summary(self, ctx, data):
88         clients = self.introducer_service.get_subscribers()
89         service_names = clients.keys()
90         service_names.sort()
91         return ", ".join(["%s: %d" % (service_name, len(clients[service_name]))
92                           for service_name in service_names])
93
94     def data_services(self, ctx, data):
95         introsvc = self.introducer_service
96         ann = [(since,a)
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) ) )
100         return ann
101
102     def render_service_row(self, ctx, (since,announcement)):
103         (furl, service_name, ri_name, nickname, ver, oldest) = announcement
104         sr = SturdyRef(furl)
105         nodeid = sr.tubID
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)
115         return ctx.tag
116
117     def data_subscribers(self, ctx, data):
118         # use the "stub_client" announcements to get information per nodeid
119         clients = {}
120         for (ann,when) in self.introducer_service.get_announcements().values():
121             if ann[1] != "stub_client":
122                 continue
123             (furl, service_name, ri_name, nickname, ver, oldest) = ann
124             sr = SturdyRef(furl)
125             nodeid = sr.tubID
126             clients[nodeid] = ann
127
128         # then we actually provide information per subscriber
129         s = []
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()
134                 nodeid = sr.tubID
135                 ann = clients.get(nodeid)
136                 s.append( (service_name, rref, timestamp, ann) )
137         s.sort()
138         return s
139
140     def render_subscriber_row(self, ctx, s):
141         (service_name, rref, since, ann) = s
142         nickname = "?"
143         version = "?"
144         if ann:
145             (furl, service_name_2, ri_name, nickname, version, oldest) = ann
146
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)
156         else:
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)
165         return ctx.tag
166
167     def show_location_hints(self, sr, ignore_localhost=True):
168         advertised = []
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"):
173                     continue
174                 advertised.append(hint.split(":")[0])
175             else:
176                 # Foolscap-0.2.6 and later use tuples of ("ipv4", host, port)
177                 if hint[0] == "ipv4":
178                     host = hint[1]
179                 if ignore_localhost and host == "127.0.0.1":
180                     continue
181                 advertised.append(hint[1])
182         return advertised
183
184