3 from nevow import rend, inevow
4 from nevow.static import File as nevow_File
5 from nevow.util import resource_filename
8 from allmydata import get_package_versions_string
9 from allmydata.util import idlib
10 from allmydata.web.common import getxmlfile, get_arg, TIME_FORMAT
13 class IntroducerRoot(rend.Page):
16 docFactory = getxmlfile("introducer.xhtml")
18 child_operations = None
20 def __init__(self, introducer_node):
21 self.introducer_node = introducer_node
22 self.introducer_service = introducer_node.getServiceNamed("introducer")
23 rend.Page.__init__(self, introducer_node)
24 static_dir = resource_filename("allmydata.web", "static")
25 for filen in os.listdir(static_dir):
26 self.putChild(filen, nevow_File(os.path.join(static_dir, filen)))
28 def renderHTTP(self, ctx):
29 t = get_arg(inevow.IRequest(ctx), "t")
31 return self.render_JSON(ctx)
32 return rend.Page.renderHTTP(self, ctx)
34 def render_JSON(self, ctx):
38 for s in self.introducer_service.get_subscribers():
39 if s.service_name not in counts:
40 counts[s.service_name] = 0
41 counts[s.service_name] += 1
42 res["subscription_summary"] = counts
44 announcement_summary = {}
46 for ad in self.introducer_service.get_announcements():
47 service_name = ad.service_name
48 if service_name not in announcement_summary:
49 announcement_summary[service_name] = 0
50 announcement_summary[service_name] += 1
51 if service_name not in service_hosts:
52 service_hosts[service_name] = set()
53 # it's nice to know how many distinct hosts are available for
54 # each service. We define a "host" by a set of addresses
55 # (hostnames or ipv4 addresses), which we extract from the
56 # connection hints. In practice, this is usually close
57 # enough: when multiple services are run on a single host,
58 # they're usually either configured with the same addresses,
59 # or setLocationAutomatically picks up the same interfaces.
60 host = frozenset(ad.advertised_addresses)
61 service_hosts[service_name].add(host)
62 res["announcement_summary"] = announcement_summary
63 distinct_hosts = dict([(name, len(hosts))
65 in service_hosts.iteritems()])
66 res["announcement_distinct_hosts"] = distinct_hosts
68 return simplejson.dumps(res, indent=1) + "\n"
70 # FIXME: This code is duplicated in root.py and introweb.py.
71 def data_rendered_at(self, ctx, data):
72 return time.strftime(TIME_FORMAT, time.localtime())
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)
80 def render_announcement_summary(self, ctx, data):
82 for ad in self.introducer_service.get_announcements():
83 if ad.service_name not in services:
84 services[ad.service_name] = 0
85 services[ad.service_name] += 1
86 service_names = services.keys()
88 return ", ".join(["%s: %d" % (service_name, services[service_name])
89 for service_name in service_names])
91 def render_client_summary(self, ctx, data):
93 for s in self.introducer_service.get_subscribers():
94 if s.service_name not in counts:
95 counts[s.service_name] = 0
96 counts[s.service_name] += 1
97 return ", ".join([ "%s: %d" % (name, counts[name])
98 for name in sorted(counts.keys()) ] )
100 def data_services(self, ctx, data):
101 services = self.introducer_service.get_announcements(False)
102 services.sort(key=lambda ad: (ad.service_name, ad.nickname))
105 def render_service_row(self, ctx, ad):
106 ctx.fillSlots("serverid", ad.serverid)
107 ctx.fillSlots("nickname", ad.nickname)
108 ctx.fillSlots("advertised", " ".join(ad.advertised_addresses))
109 ctx.fillSlots("connected", "?")
110 when_s = time.strftime("%H:%M:%S %d-%b-%Y", time.localtime(ad.when))
111 ctx.fillSlots("announced", when_s)
112 ctx.fillSlots("version", ad.version)
113 ctx.fillSlots("service_name", ad.service_name)
116 def data_subscribers(self, ctx, data):
117 return self.introducer_service.get_subscribers()
119 def render_subscriber_row(self, ctx, s):
120 ctx.fillSlots("nickname", s.nickname)
121 ctx.fillSlots("tubid", s.tubid)
122 ctx.fillSlots("connected", s.remote_address)
123 since_s = time.strftime("%H:%M:%S %d-%b-%Y", time.localtime(s.when))
124 ctx.fillSlots("since", since_s)
125 ctx.fillSlots("version", s.version)
126 ctx.fillSlots("service_name", s.service_name)