Python Manual User Manual

Page of 124
Monitoring Reports
49
a subscription of "
system.*
" will match "
system.time
" and "
system.statistics.us-
ers
", but not "
system
". To subscribe to any reports from a particular publisher, "
*
" would be used.
Note that the subscription pattern described here is the default. It is actually possible within the C++
implementation of a service agent to override the default and supply an alternate matching algorithm.
For example, in a bridge to the TIB/Rendevous package, a service agent would most likely redefine
the matching algorithm to match that of that package. Therefore, when subscribing to a service agent,
always check first exactly which scheme it uses.
class Publisher(netsvc.Service):
def __init__(self):
netsvc.Service.__init__("publisher")
self.joinGroup("publishers")
self.publishReport("system.ctime",netsvc.DateTime(),-1)
self.startTimer(self.timeout,10,"heartbeat")
def timeout(self,tag):
self.publishReport("system.time",netsvc.DateTime())
self.startTimer(self.timeout,10,"heartbeat")
class Subscriber(netsvc.Service):
def __init__(self):
netsvc.Service.__init__(self)
# subscribe to any service agent with name "publisher"
self.monitorReports(self.report,"publisher","system.*")
def report(self,service,subject,content):
binding = self.currentReport().publisher()
identity = binding.agentIdentity()
publisher = "(%s/%s)" % (‘service‘,identity)
if subject == "system.ctime":
now = str(netsvc.DateTime())
print "%s became available at %s" % (publisher,now)
print "%s originally started at %s" % (publisher,str(content))
elif subject == "system.time":
print "%s was still alive at %s" % (publisher,str(content))
When called, the callback supplied by the subscriber will be passed three arguments. These are the
service binding object for the service agent which published the report, the subject under which the
report was published and the content of the report.
The service binding object for the service agent which published the report is provided for a number
of reasons. The first is that since more than one service agent may use the same service name, it is pos-
sible that a subscription based on service name might result in responses from more than once service
agent. The service binding object is therefore supplied so that it is possible to distinguish from whom
a report originated. The service binding object may also be used to identity a particular service agent
and send a request to it. This may be less of an issue if when subscribing to a service agent, the service
binding object for the specific service agent of interest is used as opposed to a service name. This elim-
inates the possibility of getting reports from unrelated service agents using the same service name.