-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunkServer.py
More file actions
350 lines (321 loc) · 12.3 KB
/
chunkServer.py
File metadata and controls
350 lines (321 loc) · 12.3 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
import os
import socket
import json
import time
import threading
import random
def load_env_file():
with open("../ds_cp/ds-course_project/.env") as file:
for line in file:
if line.startswith("#") or not line.strip():
continue
key, value = line.strip().split("=", 1)
os.environ[key] = value
load_env_file()
listOfChunks=[]
serverclientPort = str(os.environ["SERVER_CHUNKSERVER_PORT"])
# lock = threading.Lock()
listenPort = 0
currLoad=0
lock=threading.Lock()
def sendPing(listenAddr,listePort):
global listOfChunks
global serverclientPort
global currLoad
global lock
while True:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
currListOfChunks = []
with lock:
currListOfChunks = listOfChunks.copy()
sock.connect(("127.0.0.1", int(serverclientPort)))
dataToSend = {"operation": "ping","chunkAddr":listenAddr,"chunkPort":listePort,"chunks":currListOfChunks,"load":currLoad}
dataToSend = json.dumps(dataToSend)
sock.send(dataToSend.encode())
with lock:
currLoad=0
message = sock.recv(1024)
# print(message.decode())
sock.close()
time.sleep(3)
def uploadChunk(chunHandle,chunkData,listofRecipients):
global listOfChunks
global listenPort
global lock
with lock:
listOfChunks.append(chunHandle)
chunHandle=str(chunHandle)
if ["127.0.0.1",int(listenPort)] in listofRecipients:
with open(chunHandle, "w") as f:
f.write(chunkData)
print(type(listofRecipients[0]))
listofRecipients.remove(["127.0.0.1",int(listenPort)])
if len(listofRecipients) == 0:
return
randomReciever = random.choice(listofRecipients)
toSend = {
"operation": "upload",
"chunkData":chunkData,
"chunkHandle": chunHandle,
"listofRecipients": listofRecipients,
}
toSend = json.dumps(toSend)
toSend = toSend.encode("utf-8")
lenToSend = len(toSend)
currSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
currSock.connect((randomReciever[0], randomReciever[1]))
currSock.send(str(lenToSend).encode("utf-8").ljust(10))
currSock.sendall(toSend)
currSock.close()
# def writechunk(chunkHandle, data, offset, listOfRecipients):
# global listOfChunks
# global listenPort
# try :
# with open(chunkHandle, "r+") as f:
# print("Offset: " + str(offset))
# f.seek(offset)
# f.write(data)
# listOfRecipients.remove(["127.0.0.1",listenPort])
# if len(listOfRecipients) == 0:
# return "writeSuccess"
# randomReciever = random.choice(listOfRecipients)
# toSend = {
# "operation": "write",
# "chunkHandle": chunkHandle,
# "data": data,
# "offset": offset,
# "listOfRecipients": listOfRecipients,
# }
# toSend = json.dumps(toSend)
# toSend = toSend.encode("utf-8")
# lenToSend = len(toSend)
# currSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# currSock.connect((randomReciever[0], randomReciever[1]))
# currSock.send(str(lenToSend).encode("utf-8").ljust(10))
# currSock.sendall(toSend)
# lenTorecieve = int(currSock.recv(10).decode("utf-8").strip())
# recivedData = bytearray()
# while len(recivedData) < lenTorecieve:
# toRecieveLength = min(lenTorecieve - len(recivedData), 2048)
# recivedData.extend(currSock.recv(toRecieveLength))
# recivedData = recivedData.decode("utf-8")
# recivedData = json.loads(recivedData)
# response = recivedData["response"]
# if response == "writeSuccess":
# return "writeSuccess"
# else:
# return "writeFailed"
# except:
# return "writeFailed"
def writechunk(chunkHandle, data, offset, listOfRecipients):
global listOfChunks
global listenPort
try:
with open(str(chunkHandle), "r+") as f:
print("Offset:", offset)
f.seek(offset)
# Ensure data is a string for text mode writing
if not isinstance(data, str):
data = str(data)
f.write(data)
# Confirm write action
print(f"Data '{data}' written to file at offset {offset}.")
# Remove local IP from recipients
listOfRecipients = [r for r in listOfRecipients if r != ["127.0.0.1", listenPort]]
if not listOfRecipients:
return "writeSuccess"
# Send data to another recipient
randomReciever = random.choice(listOfRecipients)
toSend = {
"operation": "write",
"chunkHandle": chunkHandle,
"data": data,
"offset": offset,
"listOfRecipients": listOfRecipients,
}
toSend = json.dumps(toSend).encode("utf-8")
lenToSend = len(toSend)
# Set up socket connection
currSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
currSock.connect((randomReciever[0], randomReciever[1]))
currSock.send(str(lenToSend).encode("utf-8").ljust(10))
currSock.sendall(toSend)
# Receive confirmation
lenToReceive = int(currSock.recv(10).decode("utf-8").strip())
receivedData = bytearray()
while len(receivedData) < lenToReceive:
toReceiveLength = min(lenToReceive - len(receivedData), 2048)
receivedData.extend(currSock.recv(toReceiveLength))
receivedData = json.loads(receivedData.decode("utf-8"))
response = receivedData.get("response")
return "writeSuccess" if response == "writeSuccess" else "writeFailed"
except Exception as e:
print(f"Error occurred: {e}")
return "writeFailed"
def clientOperation(currSock):
global listOfChunks
global currLoad
global lock
# global currLoad
with lock:
currLoad+=1
numberOfBytes = currSock.recv(10).decode("utf-8").strip()
numberOfBytes = int(numberOfBytes)
recivedData = bytearray()
while len(recivedData) < numberOfBytes:
toRecieveLength = min(numberOfBytes - len(recivedData), 2048)
recivedData.extend(currSock.recv(toRecieveLength))
recivedData = recivedData.decode("utf-8")
# print("Recived Data: " + recivedData)
recivedData = json.loads(recivedData)
operation = recivedData["operation"]
if operation == "upload":
chunkData = recivedData["chunkData"]
chunkHandle = recivedData["chunkHandle"]
listofRecipients = recivedData["listofRecipients"]
uploadChunk(chunkHandle,chunkData,listofRecipients)
elif operation == "read":
chunkHandle = recivedData["chunkHandle"]
start = int(recivedData["start"])
end = int(recivedData["end"])
try:
with open(str(chunkHandle), "r") as f:
f.seek(start)
data = f.read(end-start)
toSend = {
"response":"readSuccess",
"data":data
}
toSend = json.dumps(toSend)
toSend = toSend.encode("utf-8")
currSock.send(str(len(toSend)).encode("utf-8").ljust(10))
currSock.sendall(toSend)
except:
toSend = {
"response":"readFailed"
}
toSend = json.dumps(toSend)
toSend = toSend.encode("utf-8")
currSock.send(str(len(toSend)).encode("utf-8").ljust(10))
currSock.sendall(toSend)
elif operation == "write":
chunkHandle = recivedData["chunkHandle"]
data = recivedData["data"]
offset = int(recivedData["offset"])
res=""
try:
res = writechunk(chunkHandle, data, offset, recivedData["listOfRecipients"])
except:
res = "writeFailed"
response = {
"response":res
}
response = json.dumps(response)
response = response.encode("utf-8")
currSock.send(str(len(response)).encode("utf-8").ljust(10))
currSock.sendall(response)
elif operation == "get_size":
chunkHandle = recivedData["chunkHandle"]
try:
print(f"Getting size of file '{chunkHandle}'")
# outside current directory so need to specify path
with open(str(chunkHandle), 'r', encoding="utf-8") as file:
contents = file.read()
size = len(contents) # Number of characters in the file
print(f"Size of file '{chunkHandle}': {size}")
response = {
"response": "sizeSuccess",
"size": size
}
response = json.dumps(response)
response = response.encode("utf-8")
currSock.send(str(len(response)).encode("utf-8").ljust(10))
currSock.sendall(response)
print(f"Size of file '{chunkHandle}' sent to client")
except Exception as e:
response = {
"response": "sizeFailed",
"error": str(e)
}
response = json.dumps(response)
response = response.encode("utf-8")
currSock.send(str(len(response)).encode("utf-8").ljust(10))
currSock.sendall(response)
elif operation == "replicate":
chunkHandle = recivedData["chunkHandle"]
listOfRecipients = recivedData["listOfRecipients"]
try:
with open(str(chunkHandle), "r") as f:
chunkData = f.read()
uploadChunk(chunkHandle, chunkData, listOfRecipients)
response = {
"response": "replicateSuccess"
}
response = json.dumps(response)
response = response.encode("utf-8")
currSock.send(str(len(response)).encode("utf-8").ljust(10))
currSock.sendall(response)
except Exception as e:
print(f"Error occurred: {e}")
response = {
"response": "replicateFailed",
"error": str(e)
}
response = json.dumps(response)
response = response.encode("utf-8")
currSock.send(str(len(response)).encode("utf-8").ljust(10))
currSock.sendall(response)
elif operation == "delete":
print("Deleting chunk")
# global listOfChunks
# global lock
chunkHandle = recivedData["chunkHandle"]
try:
os.remove(str(chunkHandle))
with lock:
listOfChunks.remove(str(chunkHandle))
response = {
"response": "deleteSuccess"
}
response = json.dumps(response)
response = response.encode("utf-8")
currSock.send(str(len(response)).encode("utf-8").ljust(10))
currSock.sendall(response)
except Exception as e:
response = {
"response": "deleteFailed",
"error": str(e)
}
response = json.dumps(response)
response = response.encode("utf-8")
currSock.send(str(len(response)).encode("utf-8").ljust(10))
currSock.sendall(response)
currSock.close()
def clientThread(clientSocket):
global listOfChunks
while True:
client, addr = clientSocket.accept()
print("Connection from: " + str(addr))
clientOperation(client)
# messageRecieved = client.recv(1024)
# messageRecieved = messageRecieved.decode()
# print("Message Recieved: " + messageRecieved)
# messageRecieved=json.loads(messageRecieved)
# operation=messageRecieved["operation"]
# print("Operation: "+operation)
# if operation=="upload":
# chunkHandle = messageRecieved["fileName"]
# print("chunkHandle: " + chunkHandle)
# listOfChunks.append(chunkHandle)
# client.send("Hello from chunk server".encode())
if __name__ == "__main__":
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.bind(("127.0.0.1", 0))
clientSocket.listen(100)
listenPort = clientSocket.getsockname()[1]
pingThread = threading.Thread(target=sendPing, args=("127.0.0.1",listenPort))
pingThread.start()
cltThread = threading.Thread(target=clientThread, args=(clientSocket,))
cltThread.start()
pingThread.join()
cltThread.join()