Message Exchange

print "%s originally started at %s" % (publisher,str(content)) elif subject == "system.time":

print "%s was still alive at %s" % (publisher,str(content))

dispatcher = netsvc.Dispatcher() dispatcher.monitor(signal.SIGINT)

exchange = netsvc.Exchange(netsvc.EXCHANGE_CLIENT) exchange.connect("localhost",11111,5) dispatcher.run()

The only difference is that a message exchange client has been added to each, the actual services are identical to what they were when used in the same process.

In regard to announcements of service availability and their subsequent withdrawal, when everything is in the same process, such an announcements means that the service had been created or destroyed. In the context of a distributed system, such an announcement means that a service is now visible or is no longer visible. Such an announcement doesn’t mean that the service was necessarily destroyed as it could be the case that the message exchange server process was shutdown. Thus the service could still exist, it just may not be reachable.

Because services may become unavailable, or connections lost and also because connections between processes will automatically restart when possible, it is important that client services take notice of an- nouncements regarding the availability of a service it is using. A client service should not assume that a service it is using will always be available and should be programmed to accommodate this fact.

Connection Announcements

Monitoring the existence of services gives precise information about when such services become avail- able. This however may be too much fine detail. If a client process needs to merely know when a con- nection had been established to the message exchange server, it is possible to create a derived version of the Exchange class and override the "handleConnection()" member function.

This member function will be called when a client has successfully connected to a server, when that connection is subsequently lost, or when an initial connection attempt fails. On the server side, the member function is called when a connection is accepted and when it is lost.

class Exchange(netsvc.Exchange): def __init__(self,type):

netsvc.Exchange.__init__(self,type) def handleConnection(self,announcement):

state = "INACTIVE"

if announcement.state() == netsvc.CONNECTION_ACTIVE: state = "ACTIVE"

process = announcement.remoteProcess() address = announcement.remoteAddress()

message = "%s %s (%s)" % (state,process,address) logger.notify(netsvc.LOG_NOTICE,message)

74

Page 74
Image 74
Python Python Manual, 7.0pl5 manual Connection Announcements