-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.py
247 lines (182 loc) · 9.99 KB
/
tracker.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
from server import Server
from peer import Peer
from manifest import ManifestFile
from datetime import datetime
from timeChecker import timeLimitExceeded
import math , os , random , time , threading
import threading
from socket import *
class Tracker(Server):
def __init__(self):
super().__init__(5000 , isTracker= True)
self.connectedPeers = {}
self.beats_status = {}
self.newConnectionsHandler = self.handlePeerArrival
self.extraOperations = [self.checkForNewFiles , self.monitorUDPBeats]
self.fileLimit = 3
def handlePeerArrival(self, name, connection):
request = connection.recv(1024)
request = request.decode()
print(F"Incoming Request: {request}")
if(request[:3] == "NEW"): # NEW command is for new peers connecting to the server
peerID = (len(self.connectedPeers)+1)
connection.send(f"{peerID}".encode())
information = request[4:].split(":")
# we save the information of the new peer and store a Peer object inside the dirctionary
self.connectedPeers[peerID] = Peer(str(information[0]),
portNumber=int(information[1]))
elif(request[:3] == "REQ"): # REQ command is for requesting a file
requestedFile = request[4:]
if Server.fileExists(f"Server_files/{requestedFile}"):
self.sendFile("",connection, f"./Server_files/{requestedFile}")
elif Tracker.manifestExists(requestedFile):
self.sendManifestFile(connection , requestedFile)
else:
connection.send("ERR".encode())
connection.close()
for peer in self.connectedPeers:
print(f"Name: {self.connectedPeers[peer].name} - Port: {self.connectedPeers[peer].portNo}")
def sendCunkToVolunteer(self, portNo, fileName):
try: #if the socket is busy
self.tempSocket = self.establishTCPConnection(portNo)
tempSock = self.tempSocket
except: #we create a new one
tempSock = self.establishTCPConnection(portNo)
finally:
if tempSock!= None:
time.sleep(3)
tempSock.send(f"SAVE|{os.path.getsize(f'./DividedFiles/{fileName}')}|{fileName}".encode())
peerResponse = tempSock.recv(1024).decode()
if peerResponse[:2] == "OK":
with open(f"DividedFiles/{fileName}", "rb") as fileChunk:
bytesToSend = fileChunk.read(1024)
tempSock.send(bytesToSend)
while bytesToSend != b'':
bytesToSend = fileChunk.read(1024)
tempSock.send(bytesToSend)
tempSock.close()
def divideFileToChunks(self, fileName, numberOfChunks = 1):
manifestFile = ManifestFile(fileName)
manifestFile.prepareManifestFile(numberOfChunks)
if len(self.beats_status) < numberOfChunks:
numberOfChunks = len(self.beats_status)
#the division could lead to a float , but the file can read # bytes that are integers.
CHUNK_SIZE = math.ceil(os.path.getsize(
f"Server_files/{fileName}") / numberOfChunks) # We take the cieling of the divison result and not the floor to avoid losing data
chunkNO = 1
choshenReciver = None #we pick a certain receiver at certain points:
#EXPLANATION:
#in cases like 3 peers, if the first two chunks and their respective copies were sent to the same peers
#the last peer will only be able to get only one chunk and this chunk will not have a copy at one of the peers
with open(f"Server_files/{fileName}", "rb") as chosenFile:
chosenPeersIDs = { id : 0 for id in self.beats_status} #used to keep track of the selected peers to avoid sending to the same peer more than once
newFileName = fileName.split('.')[0]
while (newChunk:= chosenFile.read(CHUNK_SIZE)) != b'': #if what we read is not empty then we assign what was read to newChunk
if chunkNO > numberOfChunks: #in case things go bad
break
fileExtention = fileName.split('.')[1]
fileName = f"{newFileName}_chunk{chunkNO}.{fileExtention}"
if not os.path.exists("DividedFiles"):
os.makedirs(f"./DividedFiles")
with open(f"DividedFiles/{fileName}", "wb") as fileChunk:
fileChunk.write(newChunk)
chosenIDs = []
for i in range(2):
chosenPort = random.choice(list(self.beats_status))
if(len(chosenPeersIDs) != 1 or choshenReciver != None ):
while chosenPeersIDs[chosenPort] == 2 or chosenPort in chosenIDs or chosenPort == choshenReciver: #we set it to two so two peers could have a copy of the same file
chosenPort = random.choice(list(self.beats_status))
if(chunkNO % 2 != 0 and choshenReciver == None): #take one of the chosen peers, wait for the next new chunk to be sent
choshenReciver = chosenPort
elif(chunkNO % 2 == 0): # when we skip a chunk, we use the port and send the next chunk to it
choshenReciver = None
chosenPeersIDs[chosenPort] += 1
chosenIDs.append(chosenPort)
self.sendCunkToVolunteer(int(chosenPort), fileName)
manifestFile.addChunkDetails(chunkNO , "127.0.0.1" , int(chosenPort))
# manifestFile.addChunkDetails(chunkNO , "127.0.0.1" , self.connectedPeers[chosenPort].portNo)
os.remove(f"DividedFiles/{fileName}")
chunkNO += 1
manifestFile.saveAsFile()
def deleteFile(self , fileName):
filePath = f"Server_files/{fileName}"
if os.path.exists(filePath):
self.divideFileToChunks(fileName , len(self.beats_status))
os.remove(filePath)
def checkForNewFiles(self):
while True:
if len(self.beats_status) > 0:
fileList = os.listdir('./Server_files')
if(len(fileList) > self.fileLimit):
self.deleteFile(random.choice(fileList))
# else:
# time.sleep(10)
@staticmethod
def getManifestFileName(fileName):
fileName = fileName.split('.')[0]
manifestFileName = fileName + "_manifest.json"
return manifestFileName
@staticmethod
def manifestExists(fileName):
return Server.fileExists(f"Manifests/{Tracker.getManifestFileName(fileName)}")
def sendManifestFile(self, connection , fileName):
fileName = Tracker.getManifestFileName(fileName)
filePath = f"Manifests/{fileName}"
print(filePath)
self.sendFile("" , connection , filePath)
def monitorUDPBeats(self):
self.UDP_socket = socket(AF_INET , SOCK_DGRAM)
self.UDP_socket.bind(('', 5500)) #create a seperate socket with a different port to listen to the UPD heartbeats
while True:
try:
checkerThread = threading.Thread(target=self.checkPeerStatus) #
checkerThread.start()
listeningThread = threading.Thread(target=self.listenToBeats)
listeningThread.start()
time.sleep(2)
except :
print("Cannot listen to beats anymore")
break
self.UDP_socket.close()
def updatePeerStatus(self , address , portNo, timeStamp):
self.beats_status[portNo] = timeStamp #we keep track of the peers by saving their ports along with the timestamp of the last beat
def listenToBeats(self):
message, address = self.UDP_socket.recvfrom(1024)
messageContent = message.decode().split("|")
timeStamp = messageContent[2]
portNo = messageContent[1]
print(f"UDP Beat from < port: {portNo} >")
self.updatePeerStatus( address , portNo, timeStamp)
def checkPeerStatus(self):
if len(self.beats_status) > 0: #no need to check if we have nothing stored
dateTimeObj = datetime.now()
current_Statuses = self.beats_status.copy() #the dict size might change while looping so this will cause an error
for port in current_Statuses:
timeStamp = current_Statuses[port].split("_")[1]
if port in self.beats_status.keys():
if timeLimitExceeded( timeStamp , dateTimeObj , port):
print(f"No more beats from port: {port}")
self.handlePeerChurn(port)
try:
self.beats_status.pop(port) #if there are no beats for the specific port, then remove it from the dict
except KeyError:
pass
def sendFileToNewPeer(self , * ,sourcePortNo , destinationPortNo , chunkFileName):
try: #if the socket is busy
self.tempSocket = self.establishTCPConnection(sourcePortNo)
tempSock = self.tempSocket
except: #we create a new one
tempSock = self.establishTCPConnection(sourcePortNo)
finally:
tempSock.send(f"GET|{destinationPortNo}|{chunkFileName}".encode())
# peerResponse = self.tempSocket.recv(1024).decode()
tempSock.close()
def handlePeerChurn(self , portNo):
manifestFiles = os.listdir('./Manifests')
for file in manifestFiles:
ManifestFile().reconstructManifest(portNo , manifestFile= file , sendFileToNewPeer= self.sendFileToNewPeer)
def Main():
server = Tracker()
server.start()
if __name__ == "__main__":
Main()