Implementing chain topology

To implement a chain topology using Pyro4, we need to implement a chain object and the client and server objects. The Chain class allows the call to be redirected to the next server by processing the input message and reconstructing the server address to which the request should be addressed.

Also note, in this case, the @Pyro4.expose decorator, which allows all the methods of the class (chainTopology.py) to be exposed:

import Pyro4

@Pyro4.expose
class Chain(object):
def __init__(self, name, next_server):
self.name = name
self.next_serverName = next_server
self.next_server = None

def process(self, message):
if self.next_server is None:
self.next_server = Pyro4.core.Proxy("PYRONAME:example.
chainTopology." + self.next_serverName)

If the chain is closed (the last call is done from server_chain_3.py to server_chain_1.py), then a closing message is printed out:

       if self.name in message:
print("Back at %s;the chain is closed!" % self.name)
return ["complete at " + self.name]

A forwarding message is printed out if there is a next element in the chain:

        else:
print("%s forwarding the message to the object %s" %
(self.name, self.next_serverName))
message.append(self.name)
result = self.next_server.process(message)
result.insert(0, "passed on from " + self.name)
return result

Next, we have the source code for the client (client_chain.py):

import Pyro4

obj = Pyro4.core.Proxy("PYRONAME:example.chainTopology.1")
print("Result=%s" % obj.process(["hello"]))

Following this is the source code for the first server (namely, server_1) in the chain that is called from the client (server_chain_1.py). Here, the relevant libraries are imported. Note, the import to the chainTopology.py file that was described previously:

import Pyro4
import chainTopology

Note also that the source code for the servers only differs as regards the definitions of the current and the next servers of the chain:

current_server= "1"
next_server = "2"

The remaining lines of code define the communication with the next element in the chain:

servername = "example.chainTopology." + current_server
daemon = Pyro4.core.Daemon()
obj = chainTopology.Chain(current_server, next_server)
uri = daemon.register(obj)
ns = Pyro4.locateNS()
ns.register(servername, uri)
print("server_%s started " % current_server)
daemon.requestLoop()

To execute this example, first run the Pyro4 nameserver:

C:>python -m Pyro4.naming
Not starting broadcast server for localhost.
NS running on localhost:9090 (127.0.0.1)
Warning: HMAC key not set. Anyone can connect to this server!
URI = PYRO:Pyro.NameServer@localhost:9090

Run the three servers in three different terminals, typing each of them respectively  (Windows terminals are used here):

The first server (server_chain_1.pyin the first terminal:

C:>python server_chain_1.py

Followed by the second server (server_chain_2.pyin the second terminal:

C:>python server_chain_2.py

And finally, the third server  (server_chain_3.pyin the third terminal:

C:>python server_chain_3.py

Then, run the client_chain.py script from another terminal:

C:>python client_chain.py

This is the output, as shown in the Command Prompt:

Result=['passed on from 1','passed on from 2','passed on from 3','complete at 1']

The preceding message is displayed as a result of the forwarding request passed across the three servers after it returned the fact that the task is completed to server_chain_1.

Also, we can focus on the behavior of the object servers while the request is forwarded to the next object in the chain (refer to the message underneath the start message):

  1. server_ 1 is started and the following message is forwarded to server_ 2:
server_1 started
1 forwarding the message to the object 2
  1. server_ 2 forwards the following message to server_ 3:
server_2 started
2 forwarding the message to the object 3
  1. server_ 3 forwards the following message to server_ 1:
server_3 started
3 forwarding the message to the object 1
  1. Finally, the message returns to the starting point (in other words, server_1), closing the chain :
server_1 started
1 forwarding the message to the object 2
Back at 1; the chain is closed!
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset