Skip to content

Commit

Permalink
progress on udp in cs
Browse files Browse the repository at this point in the history
trying to figure out how to get udp to work on cs-bs communications
  • Loading branch information
Fdrph committed Oct 6, 2018
1 parent b783361 commit c6cd60b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 48 deletions.
20 changes: 11 additions & 9 deletions BS.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@

# Argument Parser for BSname and BSport
parser = argparse.ArgumentParser(description='Backup Server')
parser.add_argument('-b', '--bsport', type=int, default=58000, help='Backup Server port')
parser.add_argument('-b', '--bsport', type=int, default=59000, help='Backup Server port')
parser.add_argument('-n', '--bsname', default='localhost', help='Backup Server name')
parser.add_argument('-p', '--csport', type=int, default=58008, help='Central Server port')
input_args = vars(parser.parse_args())

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('localhost', 58000))
addr = ('localhost', input_args['csport'])
# sock.bind(addr)


while True:
msg1 = b'RECEIVED: OKAY.'
print ("waiting for data....")
data, addr = sock.recvfrom(4096) # buffer size is 1024 bytes
print ("received message:", data)
sock.sendto(msg1, addr)
#print (addr)
sock.sendto(b'hello', addr)
# while True:
# msg1 = b'RECEIVED: OKAY.'
# print ("waiting for data....")
# data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
# print ("received message:", data)
# sock.sendto(msg1, addr)
# #print (addr)
100 changes: 62 additions & 38 deletions CS.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,6 @@
cmd_line_args = vars(parser.parse_args())


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

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

# try:

# # 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))

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



# deals with an AUT request
Expand Down Expand Up @@ -76,25 +55,27 @@ def aut(args, user_socket):

user_socket.sendall(aur)

actions = {
'AUT':aut
}


def deal_with_message(msg, user_socket):
actions = {
'AUT':aut
}
args = msg.split()
callable = actions.get(args[0]) # AUT user pass
callable(args[1:], user_socket) # aut( [user,pass] )


def deal_with_user(user_socket, user_address):

def deal_with_user(user_socket, user_address, udp_sock):
data, server = udp_sock.recvfrom(1024)
print('received {!r}'.format(data))
print('THIS IS FROM ANOTHER PROCESS')
time.sleep(2)
time.sleep(5)

# Main Loop to user
msg = b''
while True:
slic = user_socket.recv(16)
slic = user_socket.recv(1024)
# 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
Expand All @@ -108,24 +89,63 @@ def deal_with_user(user_socket, user_address):
print('remote end is closed so closing user_socket')
user_socket.close()

def deal_with_bs(cmd_line_args, udp_sock):
print('')
# # Create a UDP socket
# sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# server_address = ('localhost', cmd_line_args['csport'])
# sock1.bind(server_address)

def sig_handler(sig, frame):
print("Exiting UDP child process -> SIGINT...")
exit()
signal.signal(signal.SIGINT, sig_handler)

data, server = udp_sock.recvfrom(1024)
print('received {!r}'.format(data))

# try:

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

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

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

if __name__ == '__main__':


#Create socket TCP/IP
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Create a UDP socket
udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

server_address = ('localhost', cmd_line_args['csport'])
udp_sock.bind(server_address)

# data, server = sock1.recvfrom(1024)
# print('received {!r}'.format(data))


# Create socket TCP/IP to listen to users
tcp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

local_hostname = socket.gethostname()
# server_address = (socket.gethostbyname(local_hostname), cmd_line_args["csport"])
server_address = ('localhost', cmd_line_args["csport"])
print("starting up on: %s port: %s" % server_address)

# Bind socket to port
sock.bind(server_address)
tcp_sock.bind(server_address)

# Listen for incoming connections
# Listen for incoming connections from users
print('listening...')
sock.listen(1)
tcp_sock.listen(1)



Expand All @@ -134,19 +154,23 @@ def deal_with_user(user_socket, user_address):
processes = []

def sig_handler(sig, frame):
print("Exiting Cloud Backup central server...")
for p in processes:
p.join()
sock.close()
print("Exiting Cloud Backup central server...")
tcp_sock.close()
# delete BS file here
exit()
signal.signal(signal.SIGINT, sig_handler)

pbs = multiprocessing.Process(target=deal_with_bs, args=(cmd_line_args, udp_sock,))
processes.append(pbs)
pbs.start()
while True:
# Accept conection from user
client_sck, client_address = sock.accept()
client_sck, client_address = tcp_sock.accept()
print('Accepting conection from: ', client_address)

p = multiprocessing.Process(target=deal_with_user, args=(client_sck, client_address,))
p = multiprocessing.Process(target=deal_with_user, args=(client_sck, client_address, udp_sock,))
processes.append(p)
p.start()

2 changes: 1 addition & 1 deletion user.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def send_msg_sock(msg, sock):

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

0 comments on commit c6cd60b

Please sign in to comment.