Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 143 additions & 11 deletions client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import socket
import time
import matplotlib.pyplot as plt
import math


START = "<START>"
Expand All @@ -24,6 +26,76 @@ def receive(client):
return data_back_split


def smooth(data, smooth_factor):
smoothed_data = []
for n in range(len(data) - smooth_factor):
sum = 0
highest = 0
for k in range(smooth_factor):
highest = max(highest, data[n + k])
sum += data[n + k]
smoothed_data.append(highest)
# smoothed_data.append(sum / smooth_factor)

return smoothed_data


def upload_plot():
speeds = []
times = []
with open("upload_speed.txt") as file:
for line in file:
split_line = line.split()
speeds.append(float(split_line[0]) / (10 ** 6))
times.append(float(split_line[1]))

smooth_speeds = smooth(speeds, 10)
smooth_times = smooth(times, 10)

# print(times)
# print(speeds)
# plt.scatter(times, speeds, s=1)
# plt.show()
fig, ax = plt.subplots()
ax.plot(smooth_times, smooth_speeds, lw=1)
scale = 1
while 10 * scale < math.ceil(max(smooth_times)):
scale *= 10
plt.xticks(range(0, math.ceil(max(smooth_times)) + 1, scale))
plt.title("Upload Speed vs. Time")
plt.xlabel("Time (sec)")
plt.ylabel("Speed (MB/sec)")
plt.show()


def download_plot():
speeds = []
times = []
with open("download_speed.txt") as file:
for line in file:
split_line = line.split()
speeds.append(float(split_line[0]) / (10 ** 6))
times.append(float(split_line[1]))

smooth_speeds = smooth(speeds, 10)
smooth_times = smooth(times, 10)

# print(times)
# print(speeds)
# plt.scatter(times, speeds, s=1)
# plt.show()
fig, ax = plt.subplots()
ax.plot(smooth_times, smooth_speeds, lw=0.5)
scale = 1
while 10 * scale < math.ceil(max(smooth_times)):
scale *= 10
plt.xticks(range(0, math.ceil(max(smooth_times)) + 1, scale))
plt.title("Download Speed vs. Time")
plt.xlabel("Time (sec)")
plt.ylabel("Speed (MB/sec)")
plt.show()


def main():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create socket
while True:
Expand All @@ -45,17 +117,6 @@ def main():
print("ERROR")
continue
print(data_back[0])

username, password = map(str,input().split())
data_to_send += "VERIFY" + SPLIT + username + SPLIT + password
client.send(data_to_send.encode(FORMAT))

data_back = receive(client)
if data_back[0] == "LOGOUT":
cmd = "LOGOUT"
print("Access Denied")
else:
print(data_back[0])

if cmd == "LOGOUT": # disconnect from server
data_to_send += "LOGOUT"
Expand Down Expand Up @@ -85,12 +146,27 @@ def main():
to_print += item + "\t"
print(to_print)

if cmd == "MKDIR": # make directory with the name given
new_dir_name = line_in_split[1]
data_to_send += "MKDIR" + SPLIT + new_dir_name
client.send(data_to_send.encode(FORMAT))

if cmd == "CD": # change directory
new_dir_name = line_in_split[1]
data_to_send += "CD" + SPLIT + new_dir_name
client.send(data_to_send.encode(FORMAT))

if cmd == "DELETE": # delete file from folder, takes one arg: filename
fname = line_in_split[1]
data_to_send += "DELETE"+SPLIT+fname
client.send(data_to_send.encode(FORMAT))
# add in error handling

if cmd == "DELDIR": # delete folder, takes one arg: directory name
dir_name = line_in_split[1]
data_to_send += "DELDIR" + SPLIT + dir_name
client.send(data_to_send.encode(FORMAT))

if cmd == "UPLOAD": # upload specified file to folder
fname = line_in_split[1]
f = open(fname, "rb")
Expand All @@ -104,19 +180,46 @@ def main():
if data_back == -1:
print("ERROR: PROBLEM IN READY ACK")
continue
file = open("upload_speed.txt", "w") #
file.close() #
base_time = time.time_ns() #
num_bytes = 0 #
file = open("upload_speed.txt", "a") #
print_time = time.time() #
print_bytes = 0 #
total_bytes = 0
while f.tell() < length:
buff = f.read(BUFFLEN)
# print(f"Sending {buff}")
start_time = time.time_ns() #
client.send(buff) # send buffer of data
# print("Buffer sent")
# print("Waiting for ready ack")
data_back = receive(client) # wait for server to be ready for next
end_time = time.time_ns() #
if data_back[0] != "READY":
print("ERROR: DID NOT RECEIVE READY ACK")
return
else:
num_bytes += len(buff) #
print_bytes += len(buff) #
total_bytes += len(buff) #
if end_time > start_time: #
time_passed = end_time - start_time #
speed = num_bytes / (time_passed / (10 ** 9)) #
current_time = end_time - base_time #
file.write(str(speed) + " " + str(current_time / (10 ** 9)) + "\n") #
num_bytes = 0 #
if time.time() >= print_time + 1: #
print_speed = print_bytes / (time.time() - print_time) #
print_time = time.time() #
print_bytes = 0 #
percent_done = round(100 * total_bytes / length)
print("\r" + str(round(print_speed / (10 ** 6), 3)) + " MB/sec - " + str(percent_done) + "%", end="") #
# print("Ready received")
pass
file.close() #
print()
end_message = START + SPLIT + "DONE"
client.send(end_message.encode(FORMAT)) # tell server finished sending

Expand All @@ -137,15 +240,42 @@ def main():
send_data += "READY"
client.send(send_data.encode(FORMAT))
buff = bytes()
file = open("download_speed.txt", "w") #
file.close() #
base_time = time.time_ns() #
start_time = 1000000000 #
num_bytes = 0 #
file = open("download_speed.txt", "a") #
print_time = time.time() #
print_bytes = 0
while len(buff) < data_length:
data_in = client.recv(BUFFLEN)
end_time = time.time_ns() #
num_bytes += len(data_in) #
print_bytes += len(data_in) #
if end_time > start_time: #
time_passed = end_time - start_time #
speed = num_bytes / (time_passed / (10 ** 9)) #
current_time = end_time - base_time #
file.write(str(speed) + " " + str(current_time / (10 ** 9)) + "\n") #
num_bytes = 0 #
if time.time() >= print_time + 1: #
print_speed = print_bytes / (time.time() - print_time) #
print_time = time.time() #
print_bytes = 0 #
percent_done = round(100 * len(buff) / data_length)
print("\r" + str(round(print_speed / (10 ** 6), 3)) + " MB/sec - " + str(percent_done) + "%", end="") #
buff += data_in
# print(buff)
send_data = START+SPLIT+"READY" # send ready ack
if end_time > start_time: #
start_time = time.time_ns() #
client.send(send_data.encode(FORMAT))
file.close() #
f.write(buff) # write bytes from buffer to file
data_in = receive(client) # get acknowledgement that client has finished
if data_in[0] == "DONE":
print()
print("Done receiving")
else:
print("ERROR: DID NOT RECEIVE DONE")
Expand All @@ -155,3 +285,5 @@ def main():

if __name__ == "__main__":
main()
# upload_plot()
# download_plot()
34 changes: 23 additions & 11 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
SPLIT = "<SPLIT>"
working_dir = list()
file_dict = dict()
usernames = ["CTalbot", "JTReagor", "TCrist"]
passwords = ["123", "456", "789"]


class File:
Expand All @@ -41,7 +39,7 @@ def receive(client, size=SIZE):

def handle_client(conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
welcome = START + SPLIT + "Welcome to the server.\nInput Username and Password: "
welcome = START + SPLIT + "Welcome to the server."
conn.send(welcome.encode(FORMAT))

while True:
Expand All @@ -54,14 +52,6 @@ def handle_client(conn, addr):
cmd = data[0]

send_data = START + SPLIT # prep response message
if cmd == "VERIFY":
if data[1] in usernames and data[2] in passwords:
send_data += "Access Granted"
conn.send(send_data.encode(FORMAT))
else:
send_data += "LOGOUT"
conn.send(send_data.encode(FORMAT))


if cmd == "LOGOUT":
send_data += "DISCO"
Expand All @@ -75,6 +65,8 @@ def handle_client(conn, addr):

if cmd == "DIR":
send_data = START
for file in os.listdir(''.join(working_dir)): # initialize file dictionary for metadata
file_dict[''.join(working_dir) + file] = File(file)
for file in os.listdir(''.join(working_dir)):
file_obj = file_dict[''.join(working_dir) + file]

Expand All @@ -84,11 +76,31 @@ def handle_client(conn, addr):
conn.send(send_data.encode(FORMAT))
print(f"DIR returned")

if cmd == "MKDIR":
new_dir_name = data[1]
os.mkdir("./sharedfolder/" + new_dir_name)
official_new_dir_name = ''.join(working_dir) + data[1]
new_file = File(new_dir_name)
file_dict[official_new_dir_name] = new_file
print(f"DIR {new_dir_name} created")

if cmd == "CD":
new_dir_name = data[1]
if new_dir_name != "..":
working_dir.append(new_dir_name + "/")
else:
del working_dir[-1]

if cmd == "DELETE":
file_name = ''.join(working_dir) + data[1]
os.remove(file_name)
print(f"Deleted {file_name}")

if cmd == "DELDIR":
dir_name = ''.join(working_dir) + data[1]
os.rmdir(dir_name)
print(f"Deleted {dir_name}")

if cmd == "UPLOAD": # receives UPLOAD<SPLIT><length>
data_length = int(data[1])
file_name = ''.join(working_dir) + data[2] # change this later
Expand Down