Python 7.0pl5 Electronic Accessory User Manual


 
Multiple Exchange Groups
77
With respect to service visibility, a message exchange endpoint will only pass information about serv-
ices if the service audience is "*", or if the service audience is the same as the exchange group. The
only exception to this is when the exchange group is empty. In that case, an empty service audience
will still restrict visibility of a service to its own process.
By using multiple exchange groups within an application, it becomes possible to segment an applica-
tion into parts and restrict visibility of services to those parts of the applications which need to see
them. As an example, a service may act as a front end for multiple back end services which do the real
work and for which it is not necessary that they be visible.
In this example, the process containing the front end service, as well as creating a message exchange
client endpoint for the default exchange group, would create its own message exchange server end-
point. The default name for this exchange group would be overridden and a different port used for con-
nections. Back end processes would then connect to this new port, with all services in the back end
processes having a service audience matching that of the new exchange group.
class FrontEnd(netsvc.Service):
def __init__(self,name="database")
netsvc.Service.__init__(self,name)
self.subscribeServiceGroup(self.announce,"backend")
def announce(self,binding,group,status):
if binding.serviceAudience() == "database":
# this is one of ours
default = netsvc.Exchange(netsvc.EXCHANGE_CLIENT)
default.connect("localhost",11111,5)
backend = netsvc.Exchange(netsvc.EXCHANGE_SERVER,"database")
backend.listen(11112)
The front end service would use subscription to a service group to know about the existence of any
back end services. Each of the back end services would in turn add themselves to the same group so
the front end is aware of their existence. The front end service can check the service audience for a
service to know for sure that it is one of its back end services and not an imposter visible through the
default exchange group.
class BackEnd(netsvc.Service):
def __init__(self,name="",audience="database")
netsvc.Service.__init__(self,name,audience)
self.joinGroup("backend")
backend = netsvc.Exchange(netsvc.EXCHANGE_CLIENT,"database")
backend.connect("localhost",11112,5)
Having done this, any services within the back end process will only be visible from other back end
processes and the front end process. The services in the back end process will not be visible within any
process reachable from the front end process over the original message exchange client endpoint at-