]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/introweb.py
introducer: record a timestamp with each announcement, and display it on the introduc...
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / introweb.py
1
2 import time
3 from nevow import rend
4 from foolscap.referenceable import SturdyRef
5 from twisted.internet import address
6 import allmydata
7 from allmydata import get_package_versions_string
8 from allmydata.util import idlib
9 from common import getxmlfile, IClient
10
11 class IntroducerRoot(rend.Page):
12
13     addSlash = True
14     docFactory = getxmlfile("introducer.xhtml")
15
16     def data_version(self, ctx, data):
17         return get_package_versions_string()
18     def data_import_path(self, ctx, data):
19         return str(allmydata)
20     def data_my_nodeid(self, ctx, data):
21         return idlib.nodeid_b2a(IClient(ctx).nodeid)
22
23     def render_announcement_summary(self, ctx, data):
24         i = IClient(ctx).getServiceNamed("introducer")
25         services = {}
26         for ann in i.get_announcements():
27             (furl, service_name, ri_name, nickname, ver, oldest) = ann
28             if service_name not in services:
29                 services[service_name] = 0
30             services[service_name] += 1
31         service_names = services.keys()
32         service_names.sort()
33         return ", ".join(["%s: %d" % (service_name, services[service_name])
34                           for service_name in service_names])
35
36     def render_client_summary(self, ctx, data):
37         i = IClient(ctx).getServiceNamed("introducer")
38         clients = i.get_subscribers()
39         service_names = clients.keys()
40         service_names.sort()
41         return ", ".join(["%s: %d" % (service_name, len(clients[service_name]))
42                           for service_name in service_names])
43
44     def data_services(self, ctx, data):
45         i = IClient(ctx).getServiceNamed("introducer")
46         ann = [(since,a)
47                for (a,since) in i.get_announcements().items()
48                if a[1] != "stub_client"]
49         ann.sort(lambda a,b: cmp( (a[1][1], a), (b[1][1], b) ) )
50         return ann
51
52     def render_service_row(self, ctx, (since,announcement)):
53         (furl, service_name, ri_name, nickname, ver, oldest) = announcement
54         sr = SturdyRef(furl)
55         nodeid = sr.tubID
56         advertised = [loc.split(":")[0] for loc in sr.locationHints
57                       if not loc.startswith("127.0.0.1:")]
58         ctx.fillSlots("peerid", "%s %s" % (nodeid, nickname))
59         ctx.fillSlots("advertised", " ".join(advertised))
60         ctx.fillSlots("connected", "?")
61         TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
62         ctx.fillSlots("announced",
63                       time.strftime(TIME_FORMAT, time.localtime(since)))
64         ctx.fillSlots("version", ver)
65         ctx.fillSlots("service_name", service_name)
66         return ctx.tag
67
68     def data_subscribers(self, ctx, data):
69         i = IClient(ctx).getServiceNamed("introducer")
70         # use the "stub_client" announcements to get information per nodeid
71         clients = {}
72         for ann in i.get_announcements():
73             if ann[1] != "stub_client":
74                 continue
75             (furl, service_name, ri_name, nickname, ver, oldest) = ann
76             sr = SturdyRef(furl)
77             nodeid = sr.tubID
78             clients[nodeid] = ann
79
80         # then we actually provide information per subscriber
81         s = []
82         for service_name, subscribers in i.get_subscribers().items():
83             for (rref, timestamp) in subscribers.items():
84                 sr = rref.getSturdyRef()
85                 nodeid = sr.tubID
86                 ann = clients.get(nodeid)
87                 s.append( (service_name, rref, timestamp, ann) )
88         s.sort()
89         return s
90
91     def render_subscriber_row(self, ctx, s):
92         (service_name, rref, since, ann) = s
93         nickname = "?"
94         version = "?"
95         if ann:
96             (furl, service_name_2, ri_name, nickname, version, oldest) = ann
97
98         sr = rref.getSturdyRef()
99         # if the subscriber didn't do Tub.setLocation, nodeid will be None
100         nodeid = sr.tubID or "?"
101         ctx.fillSlots("peerid", "%s %s" % (nodeid, nickname))
102         advertised = [loc.split(":")[0] for loc in sr.locationHints
103                       if not loc.startswith("127.0.0.1:")]
104         ctx.fillSlots("advertised", " ".join(advertised))
105         remote_host = rref.tracker.broker.transport.getPeer()
106         if isinstance(remote_host, address.IPv4Address):
107             remote_host_s = "%s:%d" % (remote_host.host, remote_host.port)
108         else:
109             # loopback is a non-IPv4Address
110             remote_host_s = str(remote_host)
111         ctx.fillSlots("connected", remote_host_s)
112         TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
113         ctx.fillSlots("since",
114                       time.strftime(TIME_FORMAT, time.localtime(since)))
115         ctx.fillSlots("version", version)
116         ctx.fillSlots("service_name", service_name)
117         return ctx.tag
118
119