Skip to content

Commit

Permalink
replace end of message with \n and fixed some control flow issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdrph committed Oct 4, 2018
1 parent 6435c2b commit e31c77c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 56 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@
!.vscode/launch.json
!.vscode/extensions.json

cloud-backup.code-workspace
cloud-backup.code-workspace
.vscode/launch.json
43 changes: 21 additions & 22 deletions CS.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,29 @@
parser = argparse.ArgumentParser(description='User Server')
parser.add_argument('-p', '--csport', type=int, default=58008, help='Central Server port')
args = vars(parser.parse_args())

# debug
print(args)
# print(args)

# Create a UDP socket
sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# # Create a UDP socket
# sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

server_address = ('localhost', 58000)
message = b'This is the message.'
# server_address = ('localhost', 58000)
# message = b'This is the message.'

try:
# try:

# Send data
print('sending...')
sent = sock1.sendto(message, server_address)
# # Send data
# print('sending...')
# sent = sock1.sendto(message, server_address)

# Receive response
print('waiting to receive....')
data, server = sock1.recvfrom(4096)
print('received {!r}'.format(data))
# # Receive response
# print('waiting to receive....')
# data, server = sock1.recvfrom(4096)
# print('received {!r}'.format(data))

finally:
print('closing socket')
sock1.close()
# finally:
# print('closing socket')
# sock1.close()

#Create socket TCP/IP
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Expand Down Expand Up @@ -78,19 +77,19 @@ def aut(args):
for value in users:
if value == args:
# user exists and password is correct
aur += b' OK\x00'
aur += b' OK\n'
nouser = False
print("User: " + args[0])
break
elif value[0] == args[0]:
# user exists password is wrong
aur += b' NOK\x00'
aur += b' NOK\n'
nouser = False
break

if nouser:
#create user in file and return AUR NEW
aur += b' NEW\x00'
aur += b' NEW\n'
f.write(args[0]+' '+args[1]+'\n')
print("New user: " + args[0])

Expand Down Expand Up @@ -118,8 +117,8 @@ def deal_with_message(msg):
# when remote end is closed and there is no more data the string is empty so we exit loop
if not slic: break
msg += slic
if msg.find(b'\x00') != -1:
message = msg.strip(b'\x00').decode('utf-8')
if msg.find(b'\n') != -1:
message = msg.strip(b'\n').decode('utf-8')
# do something with message
deal_with_message(message)
msg = b''
Expand Down
76 changes: 43 additions & 33 deletions user.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,38 @@
#
# group 8

# Global variables storing the currently loged in user's
# username and password
username = ''
password = ''

# Argument Parser for CSname and CSport
parser = argparse.ArgumentParser(description='User Server')
parser.add_argument('-n', '--csname', default='localhost', help='Central Server name')
parser.add_argument('-p', '--csport', type=int, default=58008, help='Central Server port')
input_args = vars(parser.parse_args())

cmd_line_args = vars(parser.parse_args())
# debug
# print(args)

# creates socket and returns it
def create_socket(args):
print(args)
# Create socket TCP/IP

def create_tcp_socket(server_info):
""" Creates a TCP socket and returns it
server_info is a dictionary:
csname: server host name
csport: server port
"""
# Create socket TCP
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except OSError as e:
print("Unexpected System Call error: "+ e.strerror + "\nExiting Cloud Backup..")
exit()

# Connect to server
server_address = (args["csname"], args["csport"])
print("connecting to central server: %s port: %d" % server_address)
# Connect to server provided in parameter
server_address = (server_info["csname"], server_info["csport"])
print("connecting to server: %s port: %d" % server_address)
try:
sock.connect(server_address)
except OSError as e:
Expand All @@ -41,40 +50,34 @@ def create_socket(args):



username = ''
password = ''



def send_to_cs(msg, sock):
""" Sends a message to the central server and returns the response
def send_msg_sock(msg, sock):
""" Sends a message to the socket provided and returns the response
receives a string with a null "\\0" character
sends and receives a string with a newline "\\n" character
to delimit end of message
returns a string
"""
print("sending: " + msg)
msg += '\n'
sock.sendall(msg.encode('utf-8'))

msg = b''
while True:
slic = sock.recv(16)
msg += slic
if msg.find(b'\x00') != -1:
if msg.find(b'\n') != -1:
break

print("response: " + msg.decode('utf-8'))
return msg.decode('utf-8')


def authenticate(user, passwd, sock):
response = send_to_cs("AUT"+" "+user+" "+passwd+"\0", sock)
response = send_msg_sock("AUT"+" "+user+" "+passwd, sock)
return response.split()[-1]






def deluser(args):
print(args)

Expand Down Expand Up @@ -112,13 +115,17 @@ def delete(args):


def logout(args):
print(args)
global username
username = ''
global password
password = ''

return True


def terminate(args):

exit()
print(args)


def login(args):
Expand All @@ -127,14 +134,15 @@ def login(args):
print("Missing username and password..")
return

global input_args
sock = create_socket(input_args)
global cmd_line_args
sock = create_tcp_socket(cmd_line_args)


response = authenticate(args[0], args[1], sock)
print(response)
#close conection
sock.close()
sock.close() # close conection
if response not in ['OK','NEW']:
return


global username
username = args[0]
Expand All @@ -158,12 +166,14 @@ def login(args):
if callable is None:
print("I didnt understand the request..")
else:
callable(args[1:])
if callable(args[1:]): break


while True:
init = input().split()
if init[0] == 'login':
login(init[1:])
elif init[0] == 'exit':
exit()
else:
print("I didnt understand the request..")
print("You need to login first!")

0 comments on commit e31c77c

Please sign in to comment.