]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/introweb.py
Change deep-size/stats/check/manifest to a start+poll model instead of a single long...
[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         for (ann,when) in i.get_announcements().values():
35             (furl, service_name, ri_name, nickname, ver, oldest) = ann
36             if service_name not in announcement_summary:
37                 announcement_summary[service_name] = 0
38             announcement_summary[service_name] += 1
39         res["announcement_summary"] = announcement_summary
40
41         return simplejson.dumps(res, indent=1) + "\n"
42
43     def data_version(self, ctx, data):
44         return get_package_versions_string()
45     def data_import_path(self, ctx, data):
46         return str(allmydata)
47     def data_my_nodeid(self, ctx, data):
48         return idlib.nodeid_b2a(IClient(ctx).nodeid)
49
50     def render_announcement_summary(self, ctx, data):
51         i = IClient(ctx).getServiceNamed("introducer")
52         services = {}
53         for (ann,when) in i.get_announcements().values():
54             (furl, service_name, ri_name, nickname, ver, oldest) = ann
55             if service_name not in services:
56                 services[service_name] = 0
57             services[service_name] += 1
58         service_names = services.keys()
59         service_names.sort()
60         return ", ".join(["%s: %d" % (service_name, services[service_name])
61                           for service_name in service_names])
62
63     def render_client_summary(self, ctx, data):
64         i = IClient(ctx).getServiceNamed("introducer")
65         clients = i.get_subscribers()
66         service_names = clients.keys()
67         service_names.sort()
68         return ", ".join(["%s: %d" % (service_name, len(clients[service_name]))
69                           for service_name in service_names])
70
71     def data_services(self, ctx, data):
72         i = IClient(ctx).getServiceNamed("introducer")
73         ann = [(since,a)
74                for (a,since) in i.get_announcements().values()
75                if a[1] != "stub_client"]
76         ann.sort(lambda a,b: cmp( (a[1][1], a), (b[1][1], b) ) )
77         return ann
78
79     def render_service_row(self, ctx, (since,announcement)):
80         (furl, service_name, ri_name, nickname, ver, oldest) = announcement
81         sr = SturdyRef(furl)
82         nodeid = sr.tubID
83         advertised = self.show_location_hints(sr)
84         ctx.fillSlots("peerid", "%s %s" % (nodeid, nickname))
85         ctx.fillSlots("advertised", " ".join(advertised))
86         ctx.fillSlots("connected", "?")
87         TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
88         ctx.fillSlots("announced",
89                       time.strftime(TIME_FORMAT, time.localtime(since)))
90         ctx.fillSlots("version", ver)
91         ctx.fillSlots("service_name", service_name)
92         return ctx.tag
93
94     def data_subscribers(self, ctx, data):
95         i = IClient(ctx).getServiceNamed("introducer")
96         # use the "stub_client" announcements to get information per nodeid
97         clients = {}
98         for (ann,when) in i.get_announcements().values():
99             if ann[1] != "stub_client":
100                 continue
101             (furl, service_name, ri_name, nickname, ver, oldest) = ann
102             sr = SturdyRef(furl)
103             nodeid = sr.tubID
104             clients[nodeid] = ann
105
106         # then we actually provide information per subscriber
107         s = []
108         for service_name, subscribers in i.get_subscribers().items():
109             for (rref, timestamp) in subscribers.items():
110                 sr = rref.getSturdyRef()
111                 nodeid = sr.tubID
112                 ann = clients.get(nodeid)
113                 s.append( (service_name, rref, timestamp, ann) )
114         s.sort()
115         return s
116
117     def render_subscriber_row(self, ctx, s):
118         (service_name, rref, since, ann) = s
119         nickname = "?"
120         version = "?"
121         if ann:
122             (furl, service_name_2, ri_name, nickname, version, oldest) = ann
123
124         sr = rref.getSturdyRef()
125         # if the subscriber didn't do Tub.setLocation, nodeid will be None
126         nodeid = sr.tubID or "?"
127         ctx.fillSlots("peerid", "%s %s" % (nodeid, nickname))
128         advertised = self.show_location_hints(sr)
129         ctx.fillSlots("advertised", " ".join(advertised))
130         remote_host = rref.tracker.broker.transport.getPeer()
131         if isinstance(remote_host, address.IPv4Address):
132             remote_host_s = "%s:%d" % (remote_host.host, remote_host.port)
133         else:
134             # loopback is a non-IPv4Address
135             remote_host_s = str(remote_host)
136         ctx.fillSlots("connected", remote_host_s)
137         TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
138         ctx.fillSlots("since",
139                       time.strftime(TIME_FORMAT, time.localtime(since)))
140         ctx.fillSlots("version", version)
141         ctx.fillSlots("service_name", service_name)
142         return ctx.tag
143
144     def show_location_hints(self, sr, ignore_localhost=True):
145         advertised = []
146         for hint in sr.locationHints:
147             if isinstance(hint, str):
148                 # Foolscap-0.2.5 and earlier used strings in .locationHints
149                 if ignore_localhost and hint.startswith("127.0.0.1"):
150                     continue
151                 advertised.append(hint.split(":")[0])
152             else:
153                 # Foolscap-0.2.6 and later use tuples of ("ipv4", host, port)
154                 if hint[0] == "ipv4":
155                     host = hint[1]
156                 if ignore_localhost and host == "127.0.0.1":
157                     continue
158                 advertised.append(hint[1])
159         return advertised
160
161