Python 7.0pl5 Electronic Accessory User Manual


 
Identity of Subscribers
53
A further use of the mechanism for identifying a subscribers identity, is so that subscriptions can be
tracked and for processing or interception of data only to be undertaken while there are subscribers in-
terested in the results. This avoids unnecessarily publishing reports when it is known there would be
no one to send them to.
class LogMonitor(netsvc.Service):
def __init__(self):
name = "logmon@%s" % netsvc.processIdentity()
netsvc.Service.__init__(self,name)
self._logger = netsvc.Logger()
self._channels = {}
def notify(self,channel,level,message):
agent = channel[1:-1]
report = {}
report["agent"] = agent
report["level"] = level
report["message"] = message
self.publishReport(agent,report)
def handleSubscription(self,subscription):
agent = subscription.subject()
channel = "(%s)" % agent
if subscription.status() == netsvc.SUBSCRIPTION_REQUESTED:
if len(agent) != 0:
subscriber = subscription.subscriber().agentIdentity()
if not self._channels.has_key(channel):
self._channels[channel] = []
self._logger.monitorChannel(channel,self.notify)
self._channels[channel].append(subscriber)
else:
if self._channels.has_key(channel):
subscriber = subscription.subscriber().agentIdentity()
if subscriber in self._channels[channel]:
index = self._channels[channel].index(subscriber)
del self._channels[channel][index]
if len(self._channels[channel]) == 0:
del self._channels[channel]
self._logger.monitorChannel(channel,None)
logger = netsvc.Logger()
class Publisher(netsvc.Service):
def __init__(self):
netsvc.Service.__init__(self,"publisher")
self._channel = "(%s)" % self.agentIdentity()
def debug(self,message):
logger.notifyChannel(self._channel,netsvc.LOG_DEBUG,message)
In this use of subscription information, the subscription to a specific subject is used to trigger intercep-
tion of messages logged via the logger interface. For the time that subscriptions exist for a particular