Question

I am trying to implement a ring algorithm for token passing. The algorithm will be later tested using different nodes in a cluster. I am new to python and network programming. I want to start simple and have two nodes in the ring. A node will send the token to another token whenever the token value is equal to node number, when it is not, then the nodes wait for the token. So here in my code node 1 starts and node 2 waits for the token and it should loop. My code works for just 2 rounds!and I know there is a problem with connection(opening and closing it). I am closing the connection everytime but it can stay open if you know how to do it. So how can I change it in a way that it loops as much as I want, not just 2 rounds! This is how I run it: I run the node 1 code, then node 2 but ultimately, there should be just one code running on any node!Any help and suggestion would be appreciated.

#node 1
import socket       
import time        

s = socket.socket()
ss = socket.socket()         
host = socket.gethostname() 
other_port = 12345 
my_port= 5454
token = 1   
time.sleep( 5 )     #to wait to other node to run the program
while True:
    print "node 1 is here"
    if token != 1:                                     
        while True:
            s.bind((host, my_port)) 
            s.listen(5)
            c, addr = s.accept()     
            print 'Node 1 Got connection from', addr
            data = c.recv(1024)
            if int(data) == 1:
                print "ifffffffffff node 1"
                token = 1
                c.close()
                break    
    else:
        ss.connect((host, other_port))
        token = token+1
        ss.sendall('2')
        print "Node 1 sent the",token
        ss.close()

and:

#node 2
import socket
import time                

s = socket.socket()
ss = socket.socket()        
host = socket.gethostname() 
my_port = 12345                
other_port = 5454
token = 1 
while True:
    if token != 2:                                       
        while True:
            s.bind((host, my_port)) 
            s.listen(5)
            c, addr = s.accept()     
            print 'node 2 Got connection from', addr
            data = c.recv(1024)
            if int(data) == 2:
                token = 2
                c.close()
                break    
    else:
        ss.connect((host, other_port))
        token = token-1
        ss.sendall('1')
        print "Node 2 sent the",token
        ss.close()
Was it helpful?

Solution

I used UDP instead of TCP to solve my problem. Here is how I did it in python: There were some requirements about how the nodes in the ring are organized and when the token is passed and how the token number is changed and when all the processes should terminate. I just added the code to show the logic of the UDP that I used.

address = socket.gethostname()                                                            
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)                                     
has_token = False                                                                        
token = 1                                                                                
if id == 1:                                                                              
        has_token = True
        token = random.randrange(1,11,1)
        time.sleep( 5 )                                                                  
s.bind((address, port))
while token < max_token:                                                                 
        while not has_token:                                                             
                data_token, addr = s.recvfrom(8192)
                if data_token:
                        has_token = True                                                 
                        token = int(data_token)                                       
        if (token % 7) != id:                                                            
                token = increment_token(token)                                            
                s.sendto(str(token),(next_address,next_port))
                has_token = False                                                
        else:                                                                            
                token = set_random_token(token)
                s.sendto(str(token),(next_address,next_port))
                has_token = False

OTHER TIPS

You can not connect twice:

ss.connect((host, other_port))

Tou need a new socket for each connection. So you must put

ss = socket.socket()  # before
ss.connect((host, other_port))

Also you can not bind twice.

host = socket.gethostname() 
my_port = 12345                
other_port = 5454

s = socket.socket()
s.bind((host, my_port)) 
s.listen(5)
token = 1 
while True: ...

Then it works for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top