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