Python Manual Benutzerhandbuch

Seite von 124
Service Announcements
43
Service Announcements
If a service agent subscribes to the registry using a specific service name, the service agent will be no-
tified when any service with that name becomes available or is subsequently withdrawn. When sub-
scribing to the registry using a specific service name, no notification is given regarding groups that
those same services may join.
class ServiceMonitor(netsvc.Monitor):
def __init__(self,name):
netsvc.Monitor.__init__(self)
self.subscribeServiceName(self.announce,name)
def announce(self,binding,status):
action = "WITHDRAWN"
if status == netsvc.SERVICE_AVAILABLE:
action = "AVAILABLE"
name = binding.serviceName()
identity = binding.agentIdentity()
print "SERVICE-%s: %s (%s)" % (action,`name`,identity)
The name of the member function for subscribing to the existance of a service agent by name is "
sub-
scribeServiceName()
". A subscription can be cancelled by calling the member function "
un-
subscribeServiceName()
".
Having identified a particular service agent, it is often useful to know when that specific service agent
is no longer available. The notifications provided when you call the member function "
subscribe-
ServiceName()
" will tell you that, but if the service binding had been received through some other
means and you weren’t receiving the notifications, it is preferable that you be able to receive a notifi-
cation just in relation to the specific service agent you are using. In this case, the service address can
be obtained from the service binding by calling "
serviceAddress()
" and the member function
"
subscribeServiceAddress()
" used instead. This subscription can be cancelled by calling the
"
unsubscribeServiceAddress()
" member function.
class ClientService(netsvc.Service):
def __init__(self,binding):
netsvc.Service.__init__(self)
address = binding.serviceAddress()
self.subscribeServiceAddress(self.announce,address)
self._binding = binding
# start using service
def self.announce(self,binding,group,status):
if group == None:
if binding.agentIdentity() == self._binding.agentIdentity():
if status == netsvc.SERVICE_WITHDRAWN:
self.unsubscribeServiceAddress(binding.serviceAddress())
self._binding = None
# stop using service