Skip to content

Commit

Permalink
Final functionality commit
Browse files Browse the repository at this point in the history
Program is done and working
may have rough edges
needs better comments and refactoring and prints
  • Loading branch information
Fdrph committed Oct 11, 2018
1 parent c768d76 commit 8c91d66
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 36 deletions.
114 changes: 88 additions & 26 deletions bs/BS.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
hostname = [(s.connect(('10.255.255.255', 1)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]
# hostname = '127.0.0.1'
sel = selectors.DefaultSelector()
# credentials
cred = ()

def aut(args, sock, cred):
""" Authenticates user received in args.
Expand All @@ -33,7 +35,7 @@ def aut(args, sock, cred):
out: AUR OK or NOK
"""

# args = args.decode().rstrip('\n').split()
response = b'AUR '
success = False
username = args[0]
Expand All @@ -57,16 +59,64 @@ def aut(args, sock, cred):
sock.sendall(response)
return success

def upl(args, sock, cred):
def save_files(data, user):


def rd_to_space(data):
val = ''
for byte in data:
if byte==32: break #32=space
val += chr(byte)
data = data[len(val)+1:] #remove what we read
# print(val)
return val, data

directory, data = rd_to_space(data)
path = os.getcwd()+'/user_'+user+'/'+directory
if not os.path.exists(path): os.mkdir(path)

print(directory+':')
N, data = rd_to_space(data)
N = int(N) #number of files
# FOR EACH FILE
for i in range(0, N):
filename, data = rd_to_space(data)
t1, data = rd_to_space(data)
t2, data = rd_to_space(data)
size, data = rd_to_space(data)
size = int(size)
file = data[:size]
with open(path+'/'+filename, 'wb') as f: f.write(file)
# print(t1+' '+t2)
t = time.mktime(time.strptime(t1+' '+t2, '%d.%m.%Y %H:%M:%S'))
os.utime(path+'/'+filename, (t,t))
data = data[size+1:]
print(filename+' '+str(size)+' Bytes received')

def upl(sock, cred):
""" Receive backup of dir from user.
Receives files to backup and puts them in dir
in: UPL dir N (filename date time size data)*
out: UPR OK or NOK
"""
# username = cred[0]
# print(cred)
# print('----------------------')
msg = b''
while True:
try:
slic = sock.recv(65535)
except BlockingIOError:
break
# print(slic)
if not slic: break
msg += slic

save_files(msg, cred[0])

print(args)
sock.sendall(b'UPR OK\n')


def rsb(args, sock, cred):
Expand All @@ -77,8 +127,9 @@ def rsb(args, sock, cred):
out: RBR N (filename date time size data)*
"""
print(args)
path = os.getcwd()+'/user_'+cred[0]+'/'+args[0]
username = cred[0]
args = args.decode().rstrip('\n').split()
path = os.getcwd()+'/user_'+username+'/'+args[0]
try:
files = os.listdir(path)
except OSError:
Expand Down Expand Up @@ -106,41 +157,52 @@ def get_msg(sock):
try:
slic = sock.recv(1024)
if not slic:
msg = b''
break
msg += slic
if msg.find(b'\n') != -1: break
except socket.error as e:
print(e)
exit()
return msg.decode().rstrip('\n')
return msg

# TCP session with user
def tcp_session(sock):
sel.unregister(sock)
sock.setblocking(True)
# sel.unregister(sock)
# sock.setblocking(True)

global cred

cred = ()

actions = {
'AUT':aut,
'UPL':upl,
'RSB':rsb,
}
while True:
message = get_msg(sock)
if not message: break
args = message.split()
callable = actions.get(args[0])
if callable is None:
msg = b''
try:
cmd = sock.recv(4)
except BlockingIOError:
continue
print(cmd)
if cmd == b'UPL ':
upl(sock, cred)
sel.unregister(sock)
sock.close()
return

while True:
slic = sock.recv(8192)
msg += slic
if msg.find(b'\n') != -1: break
if cmd == b'RSB ':
rsb(msg, sock, cred)
sel.unregister(sock)
sock.close()
return
if cmd == b'AUT ':
msg = msg.decode().rstrip('\n').split()
if aut(msg,sock,cred): cred = (msg[0], msg[1])
else:
sock.sendall(b'ERR\n')
break
if callable(args[1:], sock, cred):
cred = (args[1], args[2])

sock.close()

print('closing ', sock.getsockname())
sock.close()
# print('closing ', sock.getsockname())

def tcp_accept(sock):
connection, client_address = sock.accept()
Expand Down
1 change: 1 addition & 0 deletions cs/CS.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def bck(args, user_socket, cred):
user_socket.sendall(b'BKR EOF\n')
return
resp = 'BKR '+' '.join(bs_ip)+' '+args[1]+' '+' '.join(args[2:])+'\n'
os.mkdir(os.getcwd()+'/user_'+cred[0]+'/'+args[0])
with open(path, 'w+') as f: f.write(bs_ip[0]+' '+bs_ip[1])
user_socket.sendall(resp.encode())
else:
Expand Down
107 changes: 97 additions & 10 deletions user.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,14 @@ def authenticate(user, passwd, sock):


def deluser(args, credentials, server_info):
""" Delete user from CS
Try to delete user from CS, user is deleted if no information is stored
for this user
out: DLU
in: DLR OK or NOK
"""
sock = create_tcp_socket(server_info)

if not authenticate(credentials['user'], credentials['password'], sock):
Expand Down Expand Up @@ -156,36 +163,96 @@ def rd_to_space(data):


def backup(args, credentials, server_info):
""" Backup directory.
Gets BS info and which files to send from CS. Uploads these files to BS
out to cs: BCK dir N (filename date time size)*
in: BKR ipBS portBS N (filename date time size)*
out to bs: UPL dir N (filename date time size data)*
in: UPR OK or NOK
"""
directory = args[0]
# AUT with CS
sock = create_tcp_socket(server_info)
if not authenticate(credentials['user'], credentials['password'], sock):
sock.close()
return

path = os.getcwd()+'/'+args[0]
try:
files = os.listdir(path)
except OSError:
path = os.getcwd()+'/'+directory
if not os.path.isdir(path):
print('Directory does not exist')
sock.close()
return
files = os.listdir(path)
if not files:
print('Directory is empty')
socket.close()
return

msg = 'BCK '+args[0]+' '+str(len(files))+' '
msg = 'BCK '+directory+' '+str(len(files))+' '
for file in files:
filepath = path+'/'+file
size = str(os.path.getsize(filepath))
date_time = time.strftime('%d.%m.%Y %H:%M:%S', time.gmtime(os.path.getmtime(filepath)) )
msg += ' '.join([file,date_time,size]) + ' '

# Ask CS for files to UPL
response = send_msg_sock(msg, sock).split()
sock.close()
print(response)
if response[1] == 'EOF':
print('BS responded with an error')
return
if response[3] == '0':
print('No more files need backup')
return
# print(response)
# Now we talk to BS
# AUT with BS
bs_sock = create_tcp_socket({"csname":response[1], "csport":int(response[2])})
if not authenticate(credentials['user'], credentials['password'], bs_sock):
sock.close()
return
files = [x for x in files if x in response]
tosend = 'UPL '+directory+' '+str(len(files))+' '
tosend = tosend.encode()
for file in files:
filepath = path+'/'+file
size = str(os.path.getsize(filepath))
date_time = time.strftime('%d.%m.%Y %H:%M:%S', time.localtime(os.path.getmtime(filepath)) )
with open(filepath, 'rb') as f: data = f.read()
file_b = ' '.join([file,date_time,size])+' '
file_b = file_b.encode() + data + b' '
tosend += file_b
# print(tosend)

bs_sock.sendall(tosend)
resp = bs_sock.recv(8192).decode().rstrip('\n')
if resp == 'UPR NOK':
print('Could not backup directory!')
else:
print('completed - '+directory+': ', end='')
[print(' '+x, end='') for x in files]
print()
bs_sock.close()



def restore(args, credentials, server_info):

""" Restore Directory.
Get BS info from CS, then get files from BS for dir provided
out to cs: RST dir
in: RSR ipBS portBS
out to bs: RSB dir
in: RBR N (filename date time size data)*
"""

# we ask the cs for bs ip and port
sock = create_tcp_socket(server_info)
if not authenticate(credentials['user'], credentials['password'], sock):
Expand All @@ -204,7 +271,7 @@ def restore(args, credentials, server_info):
print("Couldn't authenticate with BS!")
bs_sock.close()
return

tosend = 'RSB '+args[0]+'\n'
bs_sock.sendall(tosend.encode())
resp = b''
Expand All @@ -214,6 +281,10 @@ def restore(args, credentials, server_info):
if len(slic)<8192:
break
bs_sock.close()
# print(resp)
if resp == b'RBR EOF\n':
print("Couldn't receive files from BS!")
return
resp = resp[4:] # remove 'RGR '
save_files(resp, args[0])

Expand All @@ -226,7 +297,12 @@ def restore(args, credentials, server_info):


def dirlist(args, credentials, server_info):

""" List all directories.
out to cs: LSD
in: LDR N (dirname)*
"""
sock = create_tcp_socket(server_info)

if not authenticate(credentials['user'], credentials['password'], sock):
Expand All @@ -242,7 +318,12 @@ def dirlist(args, credentials, server_info):


def filelist(args, credentials, server_info):

""" List all files in directory.
out: LSF dir
in: LFD BSip BSport N (filename date time size)*
"""
sock = create_tcp_socket(server_info)

if not authenticate(credentials['user'], credentials['password'], sock):
Expand All @@ -263,6 +344,12 @@ def filelist(args, credentials, server_info):


def delete(args, credentials, server_info):
""" Delete specified folder
out: DEL dir
in: DDR OK or NOK
"""
if len(args) < 1:
print('Must provide a folder to delete')
return
Expand Down

0 comments on commit 8c91d66

Please sign in to comment.