-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdht.py
More file actions
1160 lines (922 loc) · 38.1 KB
/
dht.py
File metadata and controls
1160 lines (922 loc) · 38.1 KB
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import socket
import os
import sys
import _thread
import threading
import hashlib
import time
from tkinter import filedialog
import tkinter as tk
class fingerTableEntry:
key = -1
port = -1
IPadd = '127.0.0.1'
value = -1
# key(int): key value of the node
# port(int): port number used by node
# IP(string): ip address of node
# value: the key value this node is representing in the fingertable
def __init__(self, pKey=-1, pPort=-1, pIP='127.0.0.1', pValue=-1):
self.key = pKey
self.port = pPort
self.IPadd = pIP
self.value = pValue
class node:
# own details
port = -1
ip = '127.0.0.1'
key = -1
port_str = '-1'
key_str = '-1'
# predecessor details
predKey = -1
predPort = -1
# successor's details (only to be used when fingertable not made)
successorKey = -1
successorPort = -1
# successor's successor details
suc_suc = -1
suc_suc_key = -1
# fingertable containing addresses of other node
fingerTable = []
# thread loops condition
consequence = True
# size of DHT = 2^m
m = 6
# increased when unable to contact successor
count = 0
# Lets all threads know if successor suspected to be offline
suc_unsure = False
# key of node that is currently leaving
lastLeft = -1
# key of node that just joined
lastNewJoin = -1
# flag indicating if fingertable available for use
fingertableSet = False
# contains (key, port) tuples for any new joins that
# informed of their arrival while fingertable was being configured
newJoin = []
############################################# Setup Node ########################################################
def __init__(self, own_port, other_port=None):
# data check:
try:
portInt = int(own_port)
if(portInt > 65535 or portInt < 0):
raise ValueError
except:
print("port needs to be an integer between 0 - 65535")
sys.exit()
# If there is no reference node given, this is the first node in the chord.
if other_port is None:
self.port = portInt
self.port_str = own_port
self.key = self.hashery(own_port)
self.key_str = str(self.key)
self.predKey = self.key
self.predPort = self.port
for i in range(0, self.m):
self.fingerTable.append(fingerTableEntry())
self.fingerTable[i].key = self.key
self.fingerTable[i].port = self.port
self.fingerTable[i].IPadd = self.ip
self.fingerTable[i].value = ((self.key+(2**i)) % (2**self.m))
self.fingertableSet = True
# start listening at port
t1 = threading.Thread(target=self.listener, args=())
t1.start()
# NOT the first node to join
else:
# data check
try:
otherPortInt = int(other_port)
if(otherPortInt > 65535 or otherPortInt < 0):
raise ValueError
except:
print("port needs to be an integer between 0 - 65535")
sys.exit()
self.port = portInt
self.port_str = own_port
self.key = self.hashery(own_port)
self.key_str = str(self.key)
self.connectToChord(otherPortInt)
# start listening at port assigned as necessary for fingertable construction
t1 = threading.Thread(target=self.listener, args=())
t1.start()
self.createFingerTable()
self.fileGet()
self.mainController()
# Name: mainController
# Purpose: Control main functions of node concurrently using multithreading
# thread 1 allows the node to deal with any incoming requests (called in constructor)
# thread 2 is the UI that user has access to perform the functions available
# thread 3 responsible for maintaining the dht
# parem: none
# returns: none
def mainController(self):
t2 = threading.Thread(target=self.options, args=())
t3 = threading.Thread(target=self.checkSuccessor, args=())
t2.start()
t3.start()
# Name: connectToChord
# Purpose: setup successor node using reference node and
# setup predecessor using the successor node
# parem: otherPort (string): reference node given
# returns: none
def connectToChord(self, otherPort):
try:
z = socket.socket()
z.connect((self.ip, otherPort))
except:
print("the reference node is not online. Kindly find another")
sys.exit()
# find successor using reference node
while True:
toSend = 'findSuccessor ' + self.key_str
z.send(toSend.encode())
ans = z.recv(1024).decode()
ansSplit = ans.split(' ')
self.successorPort = int(float(ansSplit[0]))
self.successorKey = int(float(ansSplit[1]))
existsAlready = ansSplit[2]
if(existsAlready == 'False'):
break
# if node with key exists then give it new one
else:
self.key = (self.key+1) % (2**self.m)
self.key_str = str(self.key)
z.send('end'.encode())
t = socket.socket()
t.connect((self.ip, self.successorPort))
# find predecessor of successor as that is now our predecessor
t.send('getPredInfo'.encode())
ans = t.recv(1024).decode()
nextPred = ans.split(' ')
self.predPort = int(float(nextPred[0]))
self.predKey = int(float(nextPred[1]))
# inform successor that we are it's new predecessor
toSend = 'updatePred ' + self.port_str + ' '+self.key_str
t.send(toSend.encode())
dump = t.recv(1024).decode()
# Let the nodes in the DHT know of new node joining
toSend = 'newJoin ' + self.key_str + ' ' + self.port_str
t.send(toSend.encode())
ans = t.recv(1024).decode()
t.send('end'.encode())
# Name: createFingerTable
# Purpose: creates fingertable for the node using the successor
# fingertable contains entries for addresses of nodes
# responsible for key value x + 2^k
# where x is node's own key value and
# k is from 0 - m
# parem: none
# returns: none
def createFingerTable(self):
totalEntries = self.m
predictedEntries = []
DHTsize = 2**self.m
for i in range(0, totalEntries):
predictedEntries.append((self.key+(2**i)) % DHTsize)
tmpfingerTable = fingerTableEntry(
self.successorKey, self.successorPort, self.ip, int(predictedEntries[0]))
self.fingerTable.append(tmpfingerTable)
for i in range(1, totalEntries):
# successor will help fill our fingertable
s = socket.socket()
s.connect((self.ip, self.successorPort))
toSend = 'findSuccessor ' + str(predictedEntries[i])
s.send(toSend.encode())
temp = s.recv(1024).decode()
# convert temp to port and key then store in finger table
tempSplit = temp.split(' ')
tmpfingerTable = fingerTableEntry()
tmpfingerTable.key = int(tempSplit[1])
tmpfingerTable.port = int(tempSplit[0])
tmpfingerTable.IPadd = self.ip
tmpfingerTable.value = int(predictedEntries[i])
self.fingerTable.append(tmpfingerTable)
s.send('end'.encode())
self.fingertableSet = True
self.stablizeFingertable()
def stablizeFingertable(self):
for newEntry in self.newJoin:
for node in self.fingerTable:
if (newEntry[0] >= node.value and newEntry[0] < node.key):
node.key = newEntry[0]
node.port = newEntry[1]
elif (node.key < node.value and newEntry[0] < node.key):
node.key = newEntry[0]
node.port = newEntry[1]
elif (node.key < node.value and newEntry[0] >= node.value):
node.key = newEntry[0]
node.port = newEntry[1]
self.newJoin = []
# Name: fileGet
# Purpose: Node gets files that fall under it's key space
# from successor after joining
# parem: none
# returns: none
def fileGet(self):
s = socket.socket()
s.connect((self.ip, self.fingerTable[0].port))
toSend = 'fileGet '+self.key_str
s.send(toSend.encode())
numOfFiles = int(s.recv(1024).decode())
s.send('ack'.encode())
while(numOfFiles):
ans = s.recv(1024).decode()
fileList = ans.split(' ')
filename = fileList[0]
size = int(fileList[1])
file = os.path.join(self.key_str, filename)
s.send('ack'.encode())
if(not os.path.exists(self.key_str)):
os.makedirs(self.key_str)
total_recieved = 0
with open(file, 'wb+') as f:
while total_recieved < size:
string = s.recv(1024)
total_recieved += len(string)
f.write(string)
s.send('ack'.encode())
numOfFiles -= 1
############################################# User Handler (thread 2 - t2) #######################################################
# Name: options
# Purpose: Allow user to use functions provided by the node
# parem: none
# returns: none
def options(self):
print("Welcome to the DHT. Kindly pick one of the options to proceed")
while(self.consequence): # change this to global etc etc explained above
print("")
print("choose an option:")
print("1. Store a File")
print("2. Find a File")
print("3. Files stored here")
print("4. FingerTable details")
print("5. logout")
print("")
choice = input("=> ")
if choice == '1':
self.PUT()
elif choice == '2':
self.GET()
elif choice == '3':
self.viewFiles()
elif choice == '4':
self.printFingerTable()
elif choice == '5':
self.handleLogout()
else:
print("invalid choice\n")
# Name: PUT
# Purpose: Store files into the system by creating it's key.
# Find the correct node to store the file and then send it to it.
# parem: none
# returns: none
def PUT(self):
filepath = self.fileDialog('getFile')
if os.path.isfile(filepath):
size = os.path.getsize(filepath)
filename = filepath.split('/')[-1]
print('filename: ', filename)
file_key = self.hashery(filename)
print('filename key: ', file_key)
node = self.findSuccessor(file_key)
user = node.split(' ')
z = socket.socket()
z.connect((self.ip, int(user[0])))
toSend = 'store ' + filename + ' ' + str(size)
z.send(toSend.encode())
ack = z.recv(1024).decode()
f = open(filepath, 'rb')
for chunk in iter(lambda: f.read(1024), b''):
z.send(chunk)
ack = z.recv(1024).decode()
f.close()
z.send('end'.encode())
else:
print("either file doesnot exist or no file selected")
# Name: GET
# Purpose: Find the required file from the system using it's key.
# Contact the node with the file and get the file if exists
# parem: none
# returns: none
def GET(self):
filename = input('name of file: ')
hash_of_file = self.hashery(filename)
node_with_file = self.findSuccessor(hash_of_file)
user = node_with_file.split(' ')
z = socket.socket()
z.connect((self.ip, int(user[0])))
toSend = 'get ' + filename
z.send(toSend.encode())
reply = z.recv(1024).decode()
if(reply == 'file not found'):
print('\n file doesnot exist \n')
else:
data = reply.split(' ')
filename = ' '.join(data[0:-1])
size = int(data[-1])
z.send('ack'.encode())
dir = self.fileDialog('getDir')
file = os.path.join(dir, filename)
f = open(file, "wb+")
total_recieved = 0
while total_recieved < size:
string = z.recv(1024)
total_recieved += len(string)
f.write(string)
print('file downloaded')
z.send('end'.encode())
# Name: viewFiles
# Purpose: Lets user view names of files stored at own node
# parem: none
# returns: none
def viewFiles(self):
if(os.path.exists(self.key_str)):
fileList = os.listdir(self.key_str)
if(len(fileList) > 0):
print('\n files present: ')
print(fileList)
print("")
else:
print("\n No files present \n")
else:
print('\n no files present \n')
# Name: printFingerTable
# Purpose: Lets user view the address of node responsible for position x + 2^(k)
# where x is the node's own position value
# k any integer from 0 - log2(total space of DHT)
# parem: none
# returns: none
def printFingerTable(self):
print("-----------------------")
print("My Key : " + self.key_str)
print("Pred Info " + str(self.predKey) + ' ' + str(self.predPort))
print("suc of suc " + str(self.suc_suc_key) + ' '+str(self.suc_suc))
print()
print('Key | Port Num | value')
print("-----------------------")
for entry in self.fingerTable:
if(entry.key % 10 == entry.key and entry.port % 10000 == entry.port):
print(' ' + str(entry.key) + ' | ' +
str(entry.port) + ' | ' + str(entry.value))
elif(entry.key % 10 == entry.key):
print(' ' + str(entry.key) + ' | ' +
str(entry.port) + ' | ' + str(entry.value))
elif(entry.port % 10000 == entry.port):
print(' ' + str(entry.key) + ' | ' +
str(entry.port) + ' | ' + str(entry.value))
else:
print(' ' + str(entry.key) + ' | ' +
str(entry.port) + ' | ' + str(entry.value))
print("-----------------------")
# Name: handleLogout
# Purpose: called when user chooses to shut node down.
# informs other nodes its about to leave and sends
# its files to their successor
# parem: none
# returns: none
def handleLogout(self):
# if only node in the DHT
if(self.fingerTable[0].port == self.port):
self.consequence = False
print("logging out")
_thread.exit()
self.logoutFileHandler()
self.informPred()
self.nodeLeft(
self.port, self.fingerTable[0].port, self.fingerTable[0].key)
# shuts down infinite loops in all three threads
self.consequence = False
# send one last request to self in case it is blocking
try:
z = socket.socket()
z.connect((self.ip, self.port))
z.send('end'.encode())
print('logging out')
except:
print('logging out')
# Name: logoutFileHandler
# Purpose: sends current node's files to their will be
# successor after this node goes offline
# parem: none
# returns: none
def logoutFileHandler(self):
if(os.path.exists(self.key_str)):
if(self.suc_unsure == True):
time.sleep(6)
# in every case, the node's successor will be the inheritor of the files
try:
z = socket.socket()
z.connect((self.ip, int(self.fingerTable[0].port)))
except:
print('cannot contact successor')
return
file_list = os.listdir(self.key_str)
for file in file_list:
filename = os.path.join(self.key_str, file)
toSend = 'store ' + \
file + ' ' + \
str(os.path.getsize(filename))
z.send(toSend.encode())
ack = z.recv(1024).decode()
f = open(filename, 'rb')
for chunk in iter(lambda: f.read(1024), b''):
z.send(chunk)
ack = z.recv(1024).decode()
f.close()
os.remove(filename)
os.rmdir(self.key_str)
z.send('end'.encode())
# Name: informPred
# Purpose: informs Predecessor about it's new successor
# for a smooth transition after the node goes offline
# parem: none
# returns: none
def informPred(self):
try:
z = socket.socket()
z.connect((self.ip, int(self.predPort)))
except:
print('cannot contact pred')
return
toSend = 'your new successor ' + \
str(self.fingerTable[0].port)
z.send(toSend.encode())
ack = z.recv(1024).decode()
z.send('end'.encode())
############################################## Maintaining DHT (thread 3 - t3) ###################################################
# Name: checkSuccessor
# Purpose: pings it's successor every 2 seconds to see if it is alive.
# if successor cannot be connected 3 times in a row, it
# considers it dead and considers it's successor's successor
# it's new successor. informs the rest of the dht about it's leaving.
# parem: none
# returns: none
def checkSuccessor(self):
while(self.consequence):
time.sleep(2)
try:
z = socket.socket()
z.connect((self.ip, int(self.fingerTable[0].port)))
z.send('hello'.encode())
ans = z.recv(1024).decode()
anssplit = ans.split(' ')
self.suc_suc = int(float(anssplit[0]))
self.suc_suc_key = int(float(anssplit[1]))
z.send('end'.encode())
except:
if(self.count < 1):
self.count += 1
self.suc_unsure = True
else:
# successor needs to be updated as he is lost
print("successor lost. new suc: ", self.suc_suc)
self.count = 0
self.suc_unsure = False
z = socket.socket()
z.connect((self.ip, int(self.suc_suc)))
toSend = 'updatePred ' + \
self.port_str + ' ' + self.key_str
z.send(toSend.encode())
ack = z.recv(1024).decode()
z.send('end'.encode())
self.nodeLeft(
self.fingerTable[0].key, self.suc_suc, self.suc_suc_key)
######################################### Request Handler (thread 1 - t1)##########################################
# Name: listener
# Purpose: socket bound to address and port given at run time
# listening for any connections.
# parem: none
# returns: none
def listener(self):
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((self.ip, self.port))
s.listen(10)
while(self.consequence):
peer, addr = s.accept()
_thread.start_new_thread(self.dealer, (peer,))
# Name: dealer
# Purpose: communicates with connecting node to perform
# functions needed by the connecting node
# parem: peer(socket object): socket object of the connecting node
# returns: none
def dealer(self, peer):
string = ''
while(True):
try:
string = peer.recv(1024).decode()
except:
string = 'end'
string_in_list = string.split(' ')
# edge case. no peer should be sending an empty string
if(string == ''):
_thread.exit()
# Predecessor checking to see if alive
if(string == 'hello'):
self.handlePing(peer)
# new node has joined the DHT
elif (string_in_list[0] == 'newJoin'):
self.newJoinHandler(string_in_list[1], string_in_list[2])
peer.send('done'.encode())
# file needs to be stored on this node
elif(string_in_list[0] == 'store'):
self.store(peer, string)
# file needs to be sent from this node
elif(string_in_list[0] == 'get'):
self.upload(peer, string_in_list[1])
# new predecessor asking for files that belong to it
elif(string_in_list[0] == 'fileGet'):
self.fileSend(peer, string_in_list)
# find node responsible for a certain key
elif (string_in_list[0] == 'findSuccessor'):
ans = self.findSuccessor(int(string_in_list[1]))
peer.send(ans.encode())
# asking for Predecessor info
elif(string == 'getPredInfo'):
ans = str(self.predPort) + ' ' + str(self.predKey)
peer.send(ans.encode())
# node informed of it's new successor
elif(string_in_list[0] == 'your'):
self.contactSuc(int(string_in_list[3]))
peer.send('ack'.encode())
# node informed of it's new predecessor
elif(string_in_list[0] == 'updatePred'):
self.predPort = int(string_in_list[1])
self.predKey = int(string_in_list[2])
peer.send('ack'.encode())
# a node has left the system
elif(string_in_list[0] == 'nodeLeft'):
ans = self.nodeLeft(int(string_in_list[1]), int(
string_in_list[2]), int(string_in_list[3]))
peer.send(ans.encode())
elif(string_in_list[0] == 'completedJoin'):
self.removeNewJoin(int(string_in_list[1]))
peer.send('ack'.encode())
# peer wants to end the connection
elif(string == 'end'):
peer.close()
_thread.exit()
# Name: handlePing
# Purpose: responds to node (predecessor) when it checks if alive
# parem: peer(socket object): socket object of the node
# returns: none
def handlePing(self, peer):
toSend = ''
if(self.fingertableSet):
toSend = str(self.fingerTable[0].port) + \
' ' + str(self.fingerTable[0].key)
else:
toSend = str(self.successorPort) + ' ' + str(self.successorKey)
peer.send(toSend.encode())
# Name: store
# Purpose: stores a file which another node sends
# parem: peer(socket object): socket object of the connecting node
# msg(string): message from peer containing filename and size
# returns: none
def store(self, peer, msg):
linesplit = msg.split(' ')
filename = ' '.join(linesplit[1:-1])
size = int(linesplit[-1])
peer.send('ack'.encode())
file = os.path.join(self.key_str, filename)
if(not os.path.exists(self.key_str)):
os.makedirs(self.key_str)
f = open(file, 'wb+')
totalRecieved = 0
while totalRecieved < size:
string = peer.recv(1024)
totalRecieved += len(string)
f.write(string)
peer.send('ack'.encode())
f.close()
# Name: fileSend
# Purpose: sends connecting node (predecessor) the files that
# fall under it's key value
# parem: peer(socket object): socket object of the connecting node
# data(string): data sent by the connecting node
# returns: none
def fileSend(self, peer, data):
if(os.path.exists(self.key_str)):
k = int(data[1])
listOfFiles = os.listdir(self.key_str)
toSend = []
s = socket.socket()
s.connect((self.ip, self.predPort))
s.send('getPredInfo'.encode())
ans = s.recv(1024).decode()
predOfPred = ans.split(' ')
predOfPredKey = int(predOfPred[1])
# edge case predecessor is largest node in dht
if(k > self.key and predOfPredKey < k):
toSend = list(
filter((lambda x: self.hashery(x) <= k
and self.hashery(x) > predOfPredKey), listOfFiles)
)
# edge case predecessor is smallest node in dht
elif(k < self.key and predOfPredKey > k):
toSend = list(
filter((lambda x: self.hashery(x) <= k and self.hashery(
x) > predOfPredKey), listOfFiles)
)
# general case
elif(k < self.key):
toSend = list(
filter((lambda x: self.hashery(x) <= k), listOfFiles))
peer.send(str(len(toSend)).encode())
ack = peer.recv(1024).decode()
for filename in toSend:
file = os.path.join(self.key_str, filename)
toSend = filename + ' ' + str(os.path.getsize(file))
peer.send(toSend.encode())
ack = peer.recv(1024).decode()
f = open(file, 'rb')
for chunk in iter(lambda: f.read(1024), b''):
peer.send(chunk)
ack = peer.recv(1024).decode()
f.close()
os.remove(file)
else:
peer.send('0'.encode())
# Name: upload
# Purpose: sends requested file to connecting node
# parem: peer(socket object): socket object of the connecting node
# filename(string): name of file to send to connecting node
# returns: none
def upload(self, peer, filename):
file = os.path.join(self.key_str, filename)
if(os.path.exists(file)):
size = os.path.getsize(file)
toSend = filename + ' ' + str(size)
peer.send(toSend.encode())
ack = peer.recv(1024).decode()
f = open(file, 'rb')
for chunk in iter(lambda: f.read(1024), b''):
peer.send(chunk)
else:
peer.send('file not found'.encode())
# Name: newJoinHandler
# Purpose: updates it's fingertables with the new node if needed
# parem: pKey(string): key value of new node
# pPort(string): port num of new node
# returns: none
def newJoinHandler(self, pKey, pPort): # Handle changes!!!!!!!!!!!!!!!!!
iKey = int(pKey)
iPort = int(pPort)
if (iKey == self.key or iKey == self.lastNewJoin):
return
else:
self.lastNewJoin = iKey
if(self.fingertableSet == False):
if((iKey, iPort) not in self.newJoin):
self.newJoin.append((iKey, iPort))
s = socket.socket()
s.connect((self.ip, self.successorPort))
toSend = 'newJoin ' + pKey + ' ' + pPort
s.send(toSend.encode())
ack = s.recv(1024).decode()
s.send('end'.encode())
return
# update fingertable
for entry in self.fingerTable:
if (iKey >= entry.value and iKey < entry.key):
entry.key = iKey
entry.port = iPort
elif (entry.key < entry.value and iKey < entry.key):
entry.key = iKey
entry.port = iPort
elif (entry.key < entry.value and iKey >= entry.value):
entry.key = iKey
entry.port = iPort
prevKey = -1
for entry in self.fingerTable:
if (entry.key != iKey and entry.key != self.key and entry.key != prevKey):
prevKey = entry.key
s = socket.socket()
s.connect((self.ip, entry.port))
toSend = 'newJoin ' + pKey + ' ' + pPort
s.send(toSend.encode())
ack = s.recv(1024).decode()
s.send('end'.encode())
# Name: contactSuc
# Purpose: contacts successor to inform that you are his predecessor.
# parem: suc_port(int): port number of successor
# returns: none
def contactSuc(self, suc_port):
try:
s = socket.socket()
s.connect((self.ip, suc_port))
except:
print('cannot contact given successor')
return
toSend = 'updatePred ' + self.port_str + ' ' + self.key_str
s.send(toSend.encode())
ack = s.recv(1024).decode()
s.send('end'.encode())
############################################### Micellaneous ##################3##################################
# Name: hashery
# Purpose: finds hash key (within 0 - m) of given value
# where m is the size of the dht
# parem: to_hash (string): value to find hash of
# returns: (int): hashed value
def hashery(self, to_hash):
hashie = hashlib.sha1(to_hash.encode())
hex = hashie.hexdigest()
num = 0
multiplier = 2**40
for each in hex:
if (each >= '0' and each <= '9'):
num += multiplier * int(each)
multiplier /= 2
else:
if(each == 'a'):
num += multiplier * 10
multiplier /= 2
elif(each == 'b'):
num += multiplier * 11
multiplier /= 2
elif(each == 'c'):
num += multiplier * 12
multiplier /= 2
elif(each == 'd'):
num += multiplier * 13
multiplier /= 2
elif(each == 'e'):
num += multiplier * 14
multiplier /= 2
elif(each == 'f'):
num += multiplier * 15
multiplier /= 2
finalHash = int(num % (2**self.m))
return finalHash
# Name: findSuccessor
# Purpose: finds successor node of given key value
# parem: key (int): key value to find successor of
# returns: (string): three components:
# port of successor node
# key of successor node
# boolean indicating exact match
def findSuccessor(self, key):
# fingertable not operational
if(self.fingertableSet == False):
# self = key
if(self.key == key):
return self.port_str + ' ' + self.key_str + ' True'
# edge case suc < self < key e.g 8 < 18 < 56
if(self.key < key and self.successorKey < key and self.key > self.successorKey):
return str(self.successorPort) + ' ' + str(self.successorKey) + ' False'
# edge case key < suc < self e.g 8 < 18 < 56
if(self.key > key and self.successorKey > key and self.key > self.successorKey):
return str(self.successorPort) + ' ' + str(self.successorKey) + ' False'
# self < key < suc
if(self.key < key and key < self.successorKey and self.key < self.successorKey):
return str(self.successorPort) + ' ' + str(self.successorKey) + ' False'
# self < key = suc
if(self.key < key and key == self.successorKey and self.key < self.successorKey):
return str(self.successorPort) + ' ' + str(self.successorKey) + ' True'
# send to successor to handle
s = socket.socket()
s.connect((self.ip, self.successorPort))
toSend = 'findSuccessor ' + str(key)
s.send(toSend.encode())
ans = s.recv(1024).decode()
s.send('end'.encode())
return ans
# else if fingertable is set
successor = self.fingerTable[0]
# self = key
if(self.key == key):
return self.port_str + ' ' + self.key_str + ' True'