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, render_time
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 = {}
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 res["announcement_summary"] = announcement_summary
52 return simplejson.dumps(res, indent=1) + "\n"
54 # FIXME: This code is duplicated in root.py and introweb.py.
55 def data_rendered_at(self, ctx, data):
56 return render_time(time.time())
57 def data_version(self, ctx, data):
58 return get_package_versions_string()
59 def data_import_path(self, ctx, data):
60 return str(allmydata).replace("/", "/ ") # XXX kludge for wrapping
61 def data_my_nodeid(self, ctx, data):
62 return idlib.nodeid_b2a(self.introducer_node.nodeid)
64 def render_announcement_summary(self, ctx, data):
66 for ad in self.introducer_service.get_announcements():
67 if ad.service_name not in services:
68 services[ad.service_name] = 0
69 services[ad.service_name] += 1
70 service_names = services.keys()
72 return ", ".join(["%s: %d" % (service_name, services[service_name])
73 for service_name in service_names])
75 def render_client_summary(self, ctx, data):
77 for s in self.introducer_service.get_subscribers():
78 if s.service_name not in counts:
79 counts[s.service_name] = 0
80 counts[s.service_name] += 1
81 return ", ".join([ "%s: %d" % (name, counts[name])
82 for name in sorted(counts.keys()) ] )
84 def data_services(self, ctx, data):
85 services = self.introducer_service.get_announcements(False)
86 services.sort(key=lambda ad: (ad.service_name, ad.nickname))
89 def render_service_row(self, ctx, ad):
90 ctx.fillSlots("serverid", ad.serverid)
91 ctx.fillSlots("nickname", ad.nickname)
92 ctx.fillSlots("connection-hints",
93 "connection hints: " + " ".join(ad.connection_hints))
94 ctx.fillSlots("connected", "?")
95 when_s = render_time(ad.when)
96 ctx.fillSlots("announced", when_s)
97 ctx.fillSlots("version", ad.version)
98 ctx.fillSlots("service_name", ad.service_name)
101 def data_subscribers(self, ctx, data):
102 return self.introducer_service.get_subscribers()
104 def render_subscriber_row(self, ctx, s):
105 ctx.fillSlots("nickname", s.nickname)
106 ctx.fillSlots("tubid", s.tubid)
107 ctx.fillSlots("connected", s.remote_address)
108 since_s = render_time(s.when)
109 ctx.fillSlots("since", since_s)
110 ctx.fillSlots("version", s.version)
111 ctx.fillSlots("service_name", s.service_name)