]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - misc/cpu-watcher-subscribe.py
4c560e2c6ce2668d7733b5cf0038112c930098da
[tahoe-lafs/tahoe-lafs.git] / misc / cpu-watcher-subscribe.py
1 # -*- python -*-
2
3 from twisted.internet import reactor
4 import sys
5
6 import os.path, pprint
7 from twisted.application import service
8 from twisted.python import log
9 from foolscap import Tub, Referenceable, RemoteInterface
10 from foolscap.schema import ListOf, TupleOf
11 from zope.interface import implements
12
13 Averages = ListOf( TupleOf(str, float, float, float) )
14 class RICPUWatcherSubscriber(RemoteInterface):
15     def averages(averages=Averages):
16         return None
17
18 class CPUWatcherSubscriber(service.MultiService, Referenceable):
19     implements(RICPUWatcherSubscriber)
20     def __init__(self, furlthing):
21         service.MultiService.__init__(self)
22         if furlthing.startswith("pb://"):
23             furl = furlthing
24         else:
25             furlfile = os.path.expanduser(furlthing)
26             if os.path.isdir(furlfile):
27                 furlfile = os.path.join(furlfile, "watcher.furl")
28             furl = open(furlfile, "r").read().strip()
29         tub = Tub()
30         tub.setServiceParent(self)
31         tub.connectTo(furl, self.connected)
32
33     def connected(self, rref):
34         print "subscribing"
35         d = rref.callRemote("get_averages")
36         d.addCallback(self.remote_averages)
37         d.addErrback(log.err)
38
39         d = rref.callRemote("subscribe", self)
40         d.addErrback(log.err)
41
42     def remote_averages(self, averages):
43         pprint.pprint(averages)
44
45
46 c = CPUWatcherSubscriber(sys.argv[1])
47 c.startService()
48 reactor.run()
49