]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/introweb.py
introducer web page: add CSS styling, roughly match client Welcome page
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / introweb.py
1
2 import time, os
3 from nevow import rend, inevow
4 from nevow.static import File as nevow_File
5 from nevow.util import resource_filename
6 from foolscap.api import SturdyRef
7 from twisted.internet import address
8 import allmydata
9 import simplejson
10 from allmydata import get_package_versions_string
11 from allmydata.util import idlib
12 from allmydata.web.common import getxmlfile, get_arg
13
14 class IntroducerRoot(rend.Page):
15
16     addSlash = True
17     docFactory = getxmlfile("introducer.xhtml")
18
19     child_operations = None
20
21     def __init__(self, introducer_node):
22         self.introducer_node = introducer_node
23         self.introducer_service = introducer_node.getServiceNamed("introducer")
24         rend.Page.__init__(self, introducer_node)
25         static_dir = resource_filename("allmydata.web", "static")
26         for filen in os.listdir(static_dir):
27             self.putChild(filen, nevow_File(os.path.join(static_dir, filen)))
28
29     def renderHTTP(self, ctx):
30         t = get_arg(inevow.IRequest(ctx), "t")
31         if t == "json":
32             return self.render_JSON(ctx)
33         return rend.Page.renderHTTP(self, ctx)
34
35     def render_JSON(self, ctx):
36         res = {}
37         clients = self.introducer_service.get_subscribers()
38         subscription_summary = dict([ (name, len(clients[name]))
39                                       for name in clients ])
40         res["subscription_summary"] = subscription_summary
41
42         announcement_summary = {}
43         service_hosts = {}
44         for (ann,when) in self.introducer_service.get_announcements().values():
45             (furl, service_name, ri_name, nickname, ver, oldest) = ann
46             if service_name not in announcement_summary:
47                 announcement_summary[service_name] = 0
48             announcement_summary[service_name] += 1
49             if service_name not in service_hosts:
50                 service_hosts[service_name] = set()
51             # it's nice to know how many distinct hosts are available for
52             # each service. We define a "host" by a set of addresses
53             # (hostnames or ipv4 addresses), which we extract from the
54             # connection hints. In practice, this is usually close
55             # enough: when multiple services are run on a single host,
56             # they're usually either configured with the same addresses,
57             # or setLocationAutomatically picks up the same interfaces.
58             locations = SturdyRef(furl).getTubRef().getLocations()
59             # list of tuples, ("ipv4", host, port)
60             host = frozenset([hint[1]
61                               for hint in locations
62                               if hint[0] == "ipv4"])
63             service_hosts[service_name].add(host)
64         res["announcement_summary"] = announcement_summary
65         distinct_hosts = dict([(name, len(hosts))
66                                for (name, hosts)
67                                in service_hosts.iteritems()])
68         res["announcement_distinct_hosts"] = distinct_hosts
69
70         return simplejson.dumps(res, indent=1) + "\n"
71
72     # FIXME: This code is duplicated in root.py and introweb.py.
73     def data_version(self, ctx, data):
74         return get_package_versions_string()
75     def data_import_path(self, ctx, data):
76         return str(allmydata).replace("/", "/ ") # XXX kludge for wrapping
77     def data_my_nodeid(self, ctx, data):
78         return idlib.nodeid_b2a(self.introducer_node.nodeid)
79
80     def render_announcement_summary(self, ctx, data):
81         services = {}
82         for (ann,when) in self.introducer_service.get_announcements().values():
83             (furl, service_name, ri_name, nickname, ver, oldest) = ann
84             if service_name not in services:
85                 services[service_name] = 0
86             services[service_name] += 1
87         service_names = services.keys()
88         service_names.sort()
89         return ", ".join(["%s: %d" % (service_name, services[service_name])
90                           for service_name in service_names])
91
92     def render_client_summary(self, ctx, data):
93         clients = self.introducer_service.get_subscribers()
94         service_names = clients.keys()
95         service_names.sort()
96         return ", ".join(["%s: %d" % (service_name, len(clients[service_name]))
97                           for service_name in service_names])
98
99     def data_services(self, ctx, data):
100         introsvc = self.introducer_service
101         ann = [(since,a)
102                for (a,since) in introsvc.get_announcements().values()
103                if a[1] != "stub_client"]
104         ann.sort(lambda a,b: cmp( (a[1][1], a), (b[1][1], b) ) )
105         return ann
106
107     def render_service_row(self, ctx, (since,announcement)):
108         (furl, service_name, ri_name, nickname, ver, oldest) = announcement
109         sr = SturdyRef(furl)
110         nodeid = sr.tubID
111         advertised = self.show_location_hints(sr)
112         ctx.fillSlots("peerid", nodeid)
113         ctx.fillSlots("nickname", nickname)
114         ctx.fillSlots("advertised", " ".join(advertised))
115         ctx.fillSlots("connected", "?")
116         TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
117         ctx.fillSlots("announced",
118                       time.strftime(TIME_FORMAT, time.localtime(since)))
119         ctx.fillSlots("version", ver)
120         ctx.fillSlots("service_name", service_name)
121         return ctx.tag
122
123     def data_subscribers(self, ctx, data):
124         # use the "stub_client" announcements to get information per nodeid
125         clients = {}
126         for (ann,when) in self.introducer_service.get_announcements().values():
127             if ann[1] != "stub_client":
128                 continue
129             (furl, service_name, ri_name, nickname, ver, oldest) = ann
130             sr = SturdyRef(furl)
131             nodeid = sr.tubID
132             clients[nodeid] = ann
133
134         # then we actually provide information per subscriber
135         s = []
136         introsvc = self.introducer_service
137         for service_name, subscribers in introsvc.get_subscribers().items():
138             for (rref, timestamp) in subscribers.items():
139                 sr = rref.getSturdyRef()
140                 nodeid = sr.tubID
141                 ann = clients.get(nodeid)
142                 s.append( (service_name, rref, timestamp, ann) )
143         s.sort()
144         return s
145
146     def render_subscriber_row(self, ctx, s):
147         (service_name, rref, since, ann) = s
148         nickname = "?"
149         version = "?"
150         if ann:
151             (furl, service_name_2, ri_name, nickname, version, oldest) = ann
152
153         sr = rref.getSturdyRef()
154         # if the subscriber didn't do Tub.setLocation, nodeid will be None
155         nodeid = sr.tubID or "?"
156         ctx.fillSlots("peerid", nodeid)
157         ctx.fillSlots("nickname", nickname)
158         advertised = self.show_location_hints(sr)
159         ctx.fillSlots("advertised", " ".join(advertised))
160         remote_host = rref.tracker.broker.transport.getPeer()
161         if isinstance(remote_host, address.IPv4Address):
162             remote_host_s = "%s:%d" % (remote_host.host, remote_host.port)
163         else:
164             # loopback is a non-IPv4Address
165             remote_host_s = str(remote_host)
166         ctx.fillSlots("connected", remote_host_s)
167         TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
168         ctx.fillSlots("since",
169                       time.strftime(TIME_FORMAT, time.localtime(since)))
170         ctx.fillSlots("version", version)
171         ctx.fillSlots("service_name", service_name)
172         return ctx.tag
173
174     def show_location_hints(self, sr, ignore_localhost=True):
175         advertised = []
176         for hint in sr.locationHints:
177             if isinstance(hint, str):
178                 # Foolscap-0.2.5 and earlier used strings in .locationHints
179                 if ignore_localhost and hint.startswith("127.0.0.1"):
180                     continue
181                 advertised.append(hint.split(":")[0])
182             else:
183                 # Foolscap-0.2.6 and later use tuples of ("ipv4", host, port)
184                 if hint[0] == "ipv4":
185                     host = hint[1]
186                 if ignore_localhost and host == "127.0.0.1":
187                     continue
188                 advertised.append(hint[1])
189         return advertised
190
191