]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/introweb.py
0fc456337aef840530a50d9fd881ef2ebf200d4a
[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 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         static_dir = resource_filename("allmydata.web", "static")
24         for filen in os.listdir(static_dir):
25             self.putChild(filen, nevow_File(os.path.join(static_dir, filen)))
26
27     def renderHTTP(self, ctx):
28         t = get_arg(inevow.IRequest(ctx), "t")
29         if t == "json":
30             return self.render_JSON(ctx)
31         return rend.Page.renderHTTP(self, ctx)
32
33     def render_JSON(self, ctx):
34         res = {}
35
36         counts = {}
37         for s in self.introducer_service.get_subscribers():
38             if s.service_name not in counts:
39                 counts[s.service_name] = 0
40             counts[s.service_name] += 1
41         res["subscription_summary"] = counts
42
43         announcement_summary = {}
44         service_hosts = {}
45         for ad in self.introducer_service.get_announcements():
46             service_name = ad.service_name
47             if service_name not in announcement_summary:
48                 announcement_summary[service_name] = 0
49             announcement_summary[service_name] += 1
50             if service_name not in service_hosts:
51                 service_hosts[service_name] = set()
52             # it's nice to know how many distinct hosts are available for
53             # each service. We define a "host" by a set of addresses
54             # (hostnames or ipv4 addresses), which we extract from the
55             # connection hints. In practice, this is usually close
56             # enough: when multiple services are run on a single host,
57             # they're usually either configured with the same addresses,
58             # or setLocationAutomatically picks up the same interfaces.
59             host = frozenset(ad.advertised_addresses)
60             service_hosts[service_name].add(host)
61         res["announcement_summary"] = announcement_summary
62         distinct_hosts = dict([(name, len(hosts))
63                                for (name, hosts)
64                                in service_hosts.iteritems()])
65         res["announcement_distinct_hosts"] = distinct_hosts
66
67         return simplejson.dumps(res, indent=1) + "\n"
68
69     # FIXME: This code is duplicated in root.py and introweb.py.
70     def data_version(self, ctx, data):
71         return get_package_versions_string()
72     def data_import_path(self, ctx, data):
73         return str(allmydata).replace("/", "/ ") # XXX kludge for wrapping
74     def data_my_nodeid(self, ctx, data):
75         return idlib.nodeid_b2a(self.introducer_node.nodeid)
76
77     def render_announcement_summary(self, ctx, data):
78         services = {}
79         for ad in self.introducer_service.get_announcements():
80             if ad.service_name not in services:
81                 services[ad.service_name] = 0
82             services[ad.service_name] += 1
83         service_names = services.keys()
84         service_names.sort()
85         return ", ".join(["%s: %d" % (service_name, services[service_name])
86                           for service_name in service_names])
87
88     def render_client_summary(self, ctx, data):
89         counts = {}
90         for s in self.introducer_service.get_subscribers():
91             if s.service_name not in counts:
92                 counts[s.service_name] = 0
93             counts[s.service_name] += 1
94         return ", ".join([ "%s: %d" % (name, counts[name])
95                            for name in sorted(counts.keys()) ] )
96
97     def data_services(self, ctx, data):
98         services = self.introducer_service.get_announcements(False)
99         services.sort(key=lambda ad: (ad.service_name, ad.nickname))
100         return services
101
102     def render_service_row(self, ctx, ad):
103         ctx.fillSlots("serverid", ad.serverid)
104         ctx.fillSlots("nickname", ad.nickname)
105         ctx.fillSlots("advertised", " ".join(ad.advertised_addresses))
106         ctx.fillSlots("connected", "?")
107         when_s = time.strftime("%H:%M:%S %d-%b-%Y", time.localtime(ad.when))
108         ctx.fillSlots("announced", when_s)
109         ctx.fillSlots("version", ad.version)
110         ctx.fillSlots("service_name", ad.service_name)
111         return ctx.tag
112
113     def data_subscribers(self, ctx, data):
114         return self.introducer_service.get_subscribers()
115
116     def render_subscriber_row(self, ctx, s):
117         ctx.fillSlots("nickname", s.nickname)
118         ctx.fillSlots("peerid", s.tubid)
119         ctx.fillSlots("advertised", " ".join(s.advertised_addresses))
120         ctx.fillSlots("connected", s.remote_address)
121         since_s = time.strftime("%H:%M:%S %d-%b-%Y", time.localtime(s.when))
122         ctx.fillSlots("since", since_s)
123         ctx.fillSlots("version", s.version)
124         ctx.fillSlots("service_name", s.service_name)
125         return ctx.tag
126