-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkUpdated.py
514 lines (429 loc) · 15.5 KB
/
NetworkUpdated.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
'''
@Author - Kunwar Abhinav Aditya, Sakshi Syal
The program below is an implemenation of client-server architecture, where-in new nodes can typically
join in and sign off from the existing network. A master node keeps track of the nodes in the network
and...'''
import threading
import xmlrpclib
import random
import time
import copy
from SimpleXMLRPCServer import SimpleXMLRPCServer
import cme
nodes = []
ip = 0
port = 0
myID = 0
masterNode = []
masterWordString = []
waitingQueue = []
algorithm = None
def rantomInt():
print (random.randint(0,9))
return
def initMasterString():
global masterWordString
masterWordString = ""
# Function to append string every-time
def appendString(newString, word):
newString += appendString(word)
return
def randomStringGenerator():
WORDS = ("apple", "banana", "carrot", "date", "eggplant", "fig", "guava")
word = random.choice(WORDS)
return word
''' User input to receive IP and port for the new node created!
The code snippet below also takes care of a likely warning message
if the user gives a not-a-number input for the port number '''
import socket
ip = socket.gethostbyname(socket.gethostname())
while True:
try:
port = int(raw_input('Assign me a port: '))
except ValueError:
print '\nPlease enter something in digits'
continue
break
# Code to check if id already exists
def checkID(clientID):
global myID
if clientID == myID:
print "\nID is the same as my ID...changing ID..."
clientID = createNewID()
clientID = checkID(clientID)
if len(nodes) > 0:
print "\nchecking if ID is unique..."
for entry in nodes:
if clientID == entry[2]:
print "\nID isn't unique...changing ID..."
clientID = createID()
clientID = checkID(clientID)
break
return clientID
# Joining
def joinRequest(clientIP, clientPort, clientID):
global nodes
myDetails = [ip, port, myID]
if checkInList(clientIP, clientPort) == False:
newID = checkID(clientID)
print "\nNew id..."+str(newID)
if newID == clientID:
print "\nID unique...joining node!"
else:
print "\nID wasn't unique. Sending new ID to new node!"
updateId(str(clientIP), int(clientPort), int(clientID))
if len(nodes) > 1:
sendToAll(str(clientIP), int(clientPort), int(clientID))
proxyServer = xmlrpclib.ServerProxy("http://"+clientIP+":"+clientPort+"/", allow_none=True)
proxyServer.receiver.nodeJoined(str(ip), int(port), int(myID))
for entry in nodes:
proxyServer.receiver.nodeJoined(str(entry[0]), int(entry[1]), int(entry[2]))
nodes.append([clientIP, clientPort, newID])
return
else:
print "\nNode already in network!"
return
def updateId(clientIP, clientPort, clientID):
proxyServer = xmlrpclib.ServerProxy("http://"+clientIP+":"+clientPort+"/", allow_none=True)
proxyServer.receiver.idUpdate(clientID)
return
# Code to update node's ID
def idUpdate(newID):
global myID
myID = newID
return
# Code to check in the existing list
def checkInList(clientIP, clientPort):
flag = False
# Code segment to check whether the calling node is the same node as the called node
if clientIP == ip and clientPort == port:
flag = True
print "\nThis is the same node as me!!"
return flag
print "\nchecking if node already exists..."
for entry in nodes:
if clientIP == entry[0] and clientPort == entry[1]:
flag = True
break
else:
continue
return flag
# Create a random Id for each node
def createID():
return random.randrange(0,10000)
# This code is executed by the node called by the joining node, if the id of the calling node is same as of one of the already existing nodes
def createNewID():
global nodes
newID = createID()
for entry in nodes:
if newID == entry[2]:
createNewID()
return newID
# Function to broadcast the message of new nodes joining the network
def sendToAll(clientIP, clientPort, clientID):
global nodes
for entry in nodes:
proxyServer = xmlrpclib.ServerProxy("http://"+str(entry[0])+":"+str(entry[1])+"/", allow_none=True)
proxyServer.receiver.nodeJoined(clientIP, clientPort, clientID)
return
# Function to acknowledge the joining of a new node to an already existing node
def nodeJoined(clientIP, clientPort, clientID):
global nodes
node = [clientIP, clientPort, clientID]
nodes.append(node)
print "\nNew node with IP: "+str(node[0])+" and port: "+str(node[1])+" added in the network"
return
from multiprocessing import Pool
from multiprocessing import TimeoutError
# Start Elections
def callForElection():
global ip
global port
global myID
global nodes
if len(nodes) < 1:
return
idList = []
for entry in nodes:
idList.append(entry[2])
if myID > max(idList):
print "\nHighest ID. I'm the Winner!"
setMasterNode()
else:
print "\nInitiating Master Node Election..."
nodes_loc = [(node, ip, port, myID) for node in nodes if node[2] > myID]
pool = Pool(processes = len(nodes_loc))
def callback(result):
entry = result[0]
res = pool.map_async(delegate, nodes_loc, chunksize=1, callback = callback)
try:
print res.wait(timeout = 5)
except TimeoutError:
print "\n No response. I'm the Winner!"
setMasterNode()
return
def init(clientIP, clientPort, clientID):
global ip
global port
global myID
delegate.ip = ip
delegate.port = port
delegate.myID = myID
def delegate(entry):
xmlrpclib.ServerProxy("http://"+str(entry[0][0])+":"+str(entry[0][1])+"/", allow_none=True).receiver.startElection(entry[1],entry[2],entry[3])
return entry
def isJoined():
if nodes == []:
return False
else:
return True
# Start read-write
def startReadWrite():
print ("\n Usage: start <algorithm (CME or RA)>: "),
global algorithm
algorithm = raw_input()
if str(algorithm).upper() == "CME":
centralizedMutualExclusion()
elif str(algorithm).upper() == "RA":
print "non-functional for now! :("
#ricartAgrawala()
else: print('unknown algorithm: ' + algorithm)
return
def sendStart(entry):
if entry == "CENTRALIZED_MUTUAL_EXCLUSION":
centralizedMutualExclusion()
#if entry == "RICART_AGRAWALA":
#ricartAgrawala()
return
#-----------------------------------------------#
#-----------------------------------------------#
#---------CENTRALIZED MUTUAL EXCLUSION----------#
#-----------------------------------------------#
#-----------------------------------------------#
def centralizedMutualExclusion():
#cme.master_node = masterNode
for node in nodes:
proxyServer = cme.get_proxy_server(node)
proxyServer.receiver.startDistributedReadWrite(0)
cme.start([ip, port, myID], nodes)
def readWriteStarted(entry):
print "received the message: "+str(entry)
def receiveRequest():
print "Receive request"
def requestMasterString(clientIP, clientPort, clientID, word):
if clientIP == masterNode[0] and clientPort == masterNode[1]:
wordString = [word, clientID]
masterWordString.append(wordString)
print "Finished Servicing: "+masterNode
else:
if checkCriticalSection() == False:
print "Permission granted"
else:
checkRequestQueue()
print "Added to the waiting queue"
waitingObject = [clientIP, clientPort, clientID]
waitingQueue.append(waitingObject)
return
def checkRequestQueue():
if waitingQueue == []:
return "OK"
else:
return
tLock = threading.Lock()
def checkCriticalSection():
if tLock.locked() == True:
return True
else:
return False
def syncBlock(word):
tLock.acquire()
appendString(masterWordString, word)
print "Updated String: "+masterWordString
tLock.release()
#-----------------------------------------------#
#-----------------------------------------------#
#---------------RICART AGRAWALA-----------------#
#-----------------------------------------------#
#-----------------------------------------------#
def ricartAgrawala():
ra.master_node = masterNode
for node in nodes:
proxyServer = ra.get_proxy_server(node)
proxyServer.receiver.startDistributedReadWrite(1)
ra.start([ip, port, myID], nodes)
def readWriteStarted(entry):
print "received the message: "+str(entry)
def receiveRequest():
print "Receive request"
def requestMasterString(clientIP, clientPort, clientID, word):
if clientIP == masterNode[0] and clientPort == masterNode[1]:
wordString = [word, clientID]
masterWordString.append(wordString)
print "Finished Servicing: "+masterNode
else:
if checkCriticalSection() == False:
print "Permission granted"
else:
checkRequestQueue()
print "Added to the waiting queue"
waitingObject = [clientIP, clientPort, clientID]
waitingQueue.append(waitingObject)
return
def checkRequestQueue():
if waitingQueue == []:
return "OK"
else:
return
tLock = threading.Lock()
def checkCriticalSection():
if tLock.locked() == True:
return True
else:
return False
def syncBlock(word):
tLock.acquire()
appendString(masterWordString, word)
print "Updated String: "+masterWordString
tLock.release()
def buildMasterNode():
if isJoined() == False:
print "You must join a network before you can start"
return
callForElection()
return
def notifyToTheClient(clientID, ClientPort, nodeID):
proxyServer = xmlrpclib.ServerProxy("http://"+str(clientID)+":"+str(ClientPort)+"/", allow_none=True)
proxyServer.receiver.electionResponse(ip, port, myID)
return
def electionResponse (nodeIP, nodePort, nodeID):
print "\nThe node with ID: "+str(nodeIP)+", port: "+str(nodePort)+" and ID: "+str(nodeID)+" has responded to take over the election"
print "\nI lost the election!"
return
# Notification For Election
def startElection(nodeIP, nodePort, nodeID):
print "\nMessage received: ELECTION from "+str(nodeIP)+", "+str(nodePort)+", "+str(nodeID)+""
print "\nMy ID is higher so I'll start my own election!"
notifyToTheClient(nodeIP, nodePort, nodeID)
callForElection()
return
# Set Master Node
def setMasterNode():
for entry in nodes:
proxyServer = xmlrpclib.ServerProxy("http://"+str(entry[0])+":"+str(entry[1])+"/", allow_none=True)
proxyServer.receiver.masterNodeAnnouncement(myID)
return
# Master Node Confirmation
def masterNodeAnnouncement(clientID):
global masterNode
for entry in nodes:
if entry[2] == clientID:
winner = copy.copy(entry)
print "\nThe node with ID: "+str(clientID)+" is the new master"
masterNode = winner
return
# Logging out
def nodeSignOff(nodeID):
global nodes
try:
for entry in nodes:
if nodeID == entry[2]:
nodes.remove(entry)
print ("\nNode with IP: "+str(entry[0])+" and Port: "+str(entry[1])+" has left the network")
break
except Exception:
print '\nNot able to remove this node'
return
def startDistributedReadWrite(algorithm_code):
global algorithm
#cme.master_node = masterNode
if algorithm_code == 0:
algorithm = "CME"
cme.start([ip, port, myID], nodes)
#if algorithm_code == 1:
#algorithm = "RA"
#ra.start([ip, port, myID], nodes)
def wordStringUpdate(value):
if algorithm.upper() == "CME":
threading.Thread(target=cme.receive_word_string, args=(value,)).start()
#if algorithm.upper() == "RA":
# threading.Thread(target=ra.receive_word_string, args=(value,)).start()
def wordStringRequest(requesterId, time):
if algorithm.upper() == "CME":
cme.receive_word_string_request(requesterId, time)
#if algorithm.upper() == "RA":
# ra.receive_word_string_request(requesterId, time)
def finalWordStringRequest(requesterId):
if algorithm.upper() == "CME":
cme.send_final_string(requesterId)
#if algorithm.upper() == "RA":
# ra.send_final_string(requesterId)
# Server implementation
def server():
server = SimpleXMLRPCServer((ip, port), allow_none=True)
server.register_function(initMasterString, "receiver.initMasterString")
server.register_function(joinRequest, "receiver.joinRequest")
server.register_function(electionResponse, "receiver.electionResponse")
server.register_function(nodeSignOff, "receiver.nodeSignOff")
server.register_function(idUpdate, "receiver.idUpdate")
server.register_function(nodeJoined, "receiver.nodeJoined")
server.register_function(masterNodeAnnouncement, "receiver.masterNodeAnnouncement")
server.register_function(requestMasterString, "receiver.requestMasterString")
server.register_function(startElection, "receiver.startElection")
server.register_function(cme.timeAdvance, "receiver.timeAdvance")
#server.register_function(ra.timeAdvance, "receiver.timeAdvance")
server.register_function(startDistributedReadWrite, "receiver.startDistributedReadWrite")
server.register_function(wordStringUpdate, "receiver.wordStringUpdate")
server.register_function(wordStringRequest, "receiver.wordStringRequest")
server.register_function(finalWordStringRequest, "receiver.finalWordStringRequest")
print "\nServer started and listening..."
server.serve_forever()
# Threading implementation
thr = threading.Thread(target = server)
thr.start()
def options():
global nodes, ip, port, myID
while True:
print("""
1. Join
2. Elect
3. Start Read/ Write
4. Sign off
""")
ans = raw_input("\nWhat would you like to do? ")
if ans == "1":
print '\nWhich server do you want to join?'
print '\nServer IP: ',
serverip = raw_input()
print '\n Server Port: ',
serverPort = raw_input()
if str(ip) == str(serverip) and str(port) == str(serverPort):
print '\nConnecting yourself to yourself? Not possible'
continue
proxyServer = xmlrpclib.ServerProxy("http://"+str(serverip)+":"+str(serverPort)+"/", allow_none=True)
proxyServer.receiver.joinRequest(str(ip), str(port), int(myID))
print '\nConnection established'
continue
elif ans == "2":
callForElection()
time.sleep(5)
continue
elif ans == "3":
startReadWrite()
continue
elif ans == "4":
for entry in nodes:
proxy = xmlrpclib.ServerProxy("http://"+str(entry[0])+":"+str(entry[1])+"/", allow_none=True)
proxy.receiver.nodeSignOff(int(myID))
nodes = []
continue
# Client implementation
def client():
global nodes
global ip
global port
global myID
myID = createID()
print "["+str(ip)+", "+str(port)+", "+str(myID)+"]"
thrOpt = threading.Thread(target = options)
thrOpt.start()
client()