-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.py
216 lines (192 loc) · 6.18 KB
/
node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import zmq
import sys
import signal
import time
from datetime import datetime
import threading
import shared
import os
import glob
initial = 0
haveTheToken = False
previousLeaving = False
publicToken = 0
node_id = sys.argv[1]
my_port = sys.argv[2]
receiver_port = sys.argv[3]
screen_port = shared.screen_port
sub_port = shared.sub_port
directory = "./Data"
dataFileName = directory + "/" + node_id + ".txt"
print("Node ID: " + node_id)
print("Listening: " + my_port)
print("Sending: " + receiver_port)
print("---------------")
# Prepare our context and sockets
context = zmq.Context()
# Subscriber socket
sub_socket = context.socket(zmq.SUB)
sub_socket.connect("tcp://localhost:%s" % sub_port)
# Screen node socket
screen_socket = context.socket(zmq.REQ)
screen_socket.connect("tcp://localhost:%s" % screen_port)
# Socket to receive token
receive_socket = context.socket(zmq.PULL)
receive_socket.bind("tcp://*:%s" % my_port)
# Socket to sent token
send_socket = context.socket(zmq.PUSH)
send_socket.connect("tcp://localhost:%s" % receiver_port)
# Catch SIGINT signal to exit the ring
def signal_handler(sig, frame):
if previousLeaving == True:
print("You cannot leave at current state!")
print("Try again in few seconds")
return
num_of_active_nodes = len(os.listdir(directory))
if num_of_active_nodes <= 2:
print("Only 2 nodes remained. You can not exit!")
return
print("I am leaving!")
print("Unbinding...")
deleteMyFile()
wantToleave()
print("Bye")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
def getCurrentTime():
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
return current_time
# Code to run when token received
def tokenReceived(token):
global haveTheToken
global publicToken
haveTheToken = True
publicToken = int(token)
try:
message = input("[Receive token: " + str(publicToken) + "] Enter a message and press ENTER.\n")
if len(message) > 0:
send_message = node_id + "|" + getCurrentTime() + "|" + message
screen_socket.send_string(str(send_message))
acceptance = screen_socket.recv()
except EOFError as e:
pass
publicToken += 1
# Send token
send_socket.send_string("token:" + str(publicToken))
haveTheToken = False
# Function to run once the node want to leave
def wantToleave():
#Close socket
receive_socket.close()
#Send my listening port so the next node can bind
if haveTheToken == False:
send_socket.send_string("leave:" + my_port)
else:
#If leaving while having the token, then send both my listening port
#and token to the next node
global publicToken
publicToken += 1
send_socket.send_string("token:" + str(publicToken) + "|leave:" + my_port)
#Inform screen that I am leaving(Just for logging)
send_message = "leave|" + node_id + "|" + getCurrentTime()
screen_socket.send_string(str(send_message))
acceptance = screen_socket.recv()
# Function to run once the previous node want to leave
# Close current listening port and bind it to the port he previous node was listening
def previousLeft(port):
global receive_socket
global my_port
my_port = port
receive_socket.close()
receive_socket = context.socket(zmq.PULL)
receive_socket.bind("tcp://*:%s" % port)
print("-------------------------------------------")
print("Previous node left, now Listening to: ", port)
print("-------------------------------------------")
return receive_socket
# Function to run once a new node entered in the ring before current node
# Close current listening port and bind it to the port that new node is listening
def newNodeEntered(port, id):
global receive_socket
global my_port
accept_entry_socket = context.socket(zmq.REQ)
accept_entry_socket.connect("tcp://localhost:%s" % port)
accept_entry_socket.send_string("Accept_Entry")
accept_entry_socket.close()
my_port = port
receive_socket.close()
receive_socket = context.socket(zmq.PULL)
receive_socket.bind("tcp://*:%s" % port)
print("-------------------------------------------------")
print("Node %s entered before me, now Listening to: %s" % (id, port))
print("-------------------------------------------------")
return receive_socket
def updateDataFile():
f = open(dataFileName, "w")
f.write(my_port + " " + receiver_port)
f.close()
def deleteMyFile():
os.remove(dataFileName)
def deleteAllFiles():
files = glob.glob('./Data/*')
for f in files:
os.remove(f)
# If is node 0, create and send the token
if initial == 0 and node_id == '0':
deleteAllFiles()
initial = 1
token = 1
haveTheToken = False
send_socket.send_string("token:" + str(token))
haveTheToken = False
else:
time.sleep(1)
updateDataFile()
# Subscribe to every topic(receive all)
sub_socket.setsockopt_string(zmq.SUBSCRIBE, "")
while True:
while True:
try:
chat_string = sub_socket.recv_string(zmq.DONTWAIT)
# Print only if not my message
string_tokens = chat_string.split(' ')
if string_tokens[2] != node_id:
print("-----" + chat_string)
except zmq.Again:
break
while True:
try:
# receive token
recv_str = receive_socket.recv_string(zmq.DONTWAIT)
# Previous node left while having the token
# Create a thread to manage token job so input() not block the listening function of the node
if ("token" in recv_str) and ("leave" in recv_str):
previousLeaving = True
tokenTemp, portTemp = recv_str.split('|')
receive_socket = previousLeft(portTemp[6:])
currentToken = tokenTemp[6:]
tokenThread = threading.Thread(target=tokenReceived, name="TokenTask", args=(currentToken,))
tokenThread.daemon = True
tokenThread.start()
updateDataFile()
previousLeaving = False
# Receive token from previous node
# Create a thread to manage token job so input() not block the listening function of the node
elif "token" in recv_str:
currentToken = recv_str[6:]
tokenThread = threading.Thread(target=tokenReceived, name="TokenTask", args=(currentToken,))
tokenThread.daemon = True
tokenThread.start()
# Previous node left
elif "leave" in recv_str:
previousLeaving = True
receive_socket = previousLeft(recv_str[6:])
updateDataFile()
previousLeaving = False
elif "new_node" in recv_str:
str_split = recv_str.split("|")
receive_socket = newNodeEntered(str_split[0][9:], str_split[1])
updateDataFile()
except zmq.Again:
break