]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/introweb.py
f1a6ad02638ddb68f3ddc3d69deccb3efdcc6ff9
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / introweb.py
1
2 import time
3 from nevow import rend, inevow
4 from foolscap.referenceable 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, IClient
11
12 class IntroducerRoot(rend.Page):
13
14     addSlash = True
15     docFactory = getxmlfile("introducer.xhtml")
16
17     child_operations = None
18
19     def renderHTTP(self, ctx):
20         t = get_arg(inevow.IRequest(ctx), "t")
21         if t == "json":
22             return self.render_JSON(ctx)
23         return rend.Page.renderHTTP(self, ctx)
24
25     def render_JSON(self, ctx):
26         i = IClient(ctx).getServiceNamed("introducer")
27         res = {}
28         clients = i.get_subscribers()
29         subscription_summary = dict([ (name, len(clients[name]))
30                                       for name in clients ])
31         res["subscription_summary"] = subscription_summary
32
33         announcement_summary = {}
34         service_hosts = {}
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]
52                               for hint in locations
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))
57                                for (name, hosts)
58                                in service_hosts.iteritems()])
59         res["announcement_distinct_hosts"] = distinct_hosts
60
61         return simplejson.dumps(res, indent=1) + "\n"
62
63     def data_version(self, ctx, data):
64         return get_package_versions_string()
65     def data_import_path(self, ctx, data):
66         return str(allmydata)
67     def data_my_nodeid(self, ctx, data):
68         return idlib.nodeid_b2a(IClient(ctx).nodeid)
69
70     def render_announcement_summary(self, ctx, data):
71         i = IClient(ctx).getServiceNamed("introducer")
72         services = {}
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()
79         service_names.sort()
80         return ", ".join(["%s: %d" % (service_name, services[service_name])
81                           for service_name in service_names])
82
83     def render_client_summary(self, ctx, data):
84         i = IClient(ctx).getServiceNamed("introducer")
85         clients = i.get_subscribers()
86         service_names = clients.keys()
87         service_names.sort()
88         return ", ".join(["%s: %d" % (service_name, len(clients[service_name]))
89                           for service_name in service_names])
90
91     def data_services(self, ctx, data):
92         i = IClient(ctx).getServiceNamed("introducer")
93         ann = [(since,a)
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) ) )
97         return ann
98
99     def render_service_row(self, ctx, (since,announcement)):
100         (furl, service_name, ri_name, nickname, ver, oldest) = announcement
101         sr = SturdyRef(furl)
102         nodeid = sr.tubID
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)
112         return ctx.tag
113
114     def data_subscribers(self, ctx, data):
115         i = IClient(ctx).getServiceNamed("introducer")
116         # use the "stub_client" announcements to get information per nodeid
117         clients = {}
118         for (ann,when) in i.get_announcements().values():
119             if ann[1] != "stub_client":
120                 continue
121             (furl, service_name, ri_name, nickname, ver, oldest) = ann
122             sr = SturdyRef(furl)
123             nodeid = sr.tubID
124             clients[nodeid] = ann
125
126         # then we actually provide information per subscriber
127         s = []
128         for service_name, subscribers in i.get_subscribers().items():
129             for (rref, timestamp) in subscribers.items():
130                 sr = rref.getSturdyRef()
131                 nodeid = sr.tubID
132                 ann = clients.get(nodeid)
133                 s.append( (service_name, rref, timestamp, ann) )
134         s.sort()
135         return s
136
137     def render_subscriber_row(self, ctx, s):
138         (service_name, rref, since, ann) = s
139         nickname = "?"
140         version = "?"
141         if ann:
142             (furl, service_name_2, ri_name, nickname, version, oldest) = ann
143
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)
153         else:
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)
162         return ctx.tag
163
164     def show_location_hints(self, sr, ignore_localhost=True):
165         advertised = []
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"):
170                     continue
171                 advertised.append(hint.split(":")[0])
172             else:
173                 # Foolscap-0.2.6 and later use tuples of ("ipv4", host, port)
174                 if hint[0] == "ipv4":
175                     host = hint[1]
176                 if ignore_localhost and host == "127.0.0.1":
177                     continue
178                 advertised.append(hint[1])
179         return advertised
180
181