diff --git a/Code/p2p_c_node.py b/Code/p2p_c_node.py index 8a5f3d1..746068a 100644 --- a/Code/p2p_c_node.py +++ b/Code/p2p_c_node.py @@ -26,6 +26,8 @@ PORT = int(row['iport']) XPORT = int(row['xport']) HOST = (row['hostip']) + MYIP = (row['publicip']) + #### CONFIG FILE #### @@ -87,7 +89,7 @@ def handle_client(conn, addr): print('Sending initial details') STARTUP = str(STARTUP).encode(FORMAT) -INFO = str(XPORT).encode(FORMAT) +INFO = str([MYIP, XPORT]).encode(FORMAT) c_socket.send(STARTUP) c_socket.send(INFO) @@ -131,6 +133,8 @@ def min_checker(): c_socket.send(message) + print('Sent message') + time.sleep(30) time.sleep(5) diff --git a/Code/p2p_d_node.py b/Code/p2p_d_node.py index 9891e02..6dc3058 100644 --- a/Code/p2p_d_node.py +++ b/Code/p2p_d_node.py @@ -39,11 +39,12 @@ def setup(self): if c_node == '!NONE': print('No available server nodes \n') - time.sleep(120) + time.sleep(20) if c_node != '!NONE': # [addr,xport,connection_count] - ADDR = (c_node[0], int(c_node[1])) + ADDR = (str(c_node[0]), int(c_node[1])) + print(ADDR) self.s.connect(ADDR) ex_cons.append(c_node[0]) print('Connected to Server node \n') @@ -63,8 +64,10 @@ def listen(self, addr): self.setup() except: + print('Server disconnected') self.s.close() - ex_cons.remove(addr) + if addr in ex_cons: + ex_cons.remove(addr) self.setup() @@ -113,6 +116,8 @@ def recieve_c_node(): thread3 = threading.Thread(target=con3) ''' +# could have a time related arg to spread out classes evenly + thread1 = threading.Thread(target=connection) thread2 = threading.Thread(target=connection) thread3 = threading.Thread(target=connection) diff --git a/Code/p2p_host.py b/Code/p2p_host.py index a3ed2bf..6df28da 100644 --- a/Code/p2p_host.py +++ b/Code/p2p_host.py @@ -44,6 +44,7 @@ cnodes = list() +cconns = set() dnodes = 0 @@ -54,85 +55,92 @@ def dy_min(): while True: - num_cnodes = len(cnodes) + print('Dy_min') + + num_cnodes = len(cconns) # 3 is number of connections per dnode try: min = ((dnodes*3)//num_cnodes)//2 except: min = 1 + print(f'Min = {min}') - for x in cnodes: - conn = x[0] + for conn in cconns: message_type = 'min'.encode(FORMAT) message = str(min).encode(FORMAT) conn.send(message_type) conn.send(message) + print('Sent dynamic minimum') - time.sleep(5) + time.sleep(10) def ordered_c_list(): - o = cnodes.copy() - o = sorted(o, key=lambda x: x[3], reverse=False) + o = sorted(o, key=lambda x: x[2], reverse=False) return o def lost_conn(addr): - for x in cnodes: - if x[1] == addr: - x[3] += -1 + if x[0] == addr: + x[2] += -1 + print(f'{addr} gained a connection') def gain_conn(addr): - for x in cnodes: - if x[1] == addr: - x[3] += -1 + if x[0] == addr: + x[2] += -1 + print(f'{addr} gained a connection') def kick_dnodes(low_conn): o = ordered_c_list() - conn = o[len(o)-1][0] - + conn = o[len(o)-1][3] message_type = 'lose'.encode(FORMAT) message = ''.encode(FORMAT) if low_conn != conn: - + print('Sending kick message') conn.send(message_type) conn.send(message) + print('Sent kick message') -def cnode_handler(conn, addr, xport): +def cnode_handler(conn, addr, info): print(f'CNODE : {addr} has connected') - cnodes.append([conn, addr, xport, 0]) + # [addr, xport, 0] + + new_info = [info[0], info[1], 0, conn] + + addr2 = info[0] + + cnodes.append(new_info) + cconns.add(conn) + + print(f'CNODE HANDLER added {new_info} to {cnodes}') connected = True while connected: - try: - message_type = conn.recv(1024).decode(FORMAT) - - except: - message_type = DISCONNECT + message_type = conn.recv(1024).decode(FORMAT) # message = conn.recv(1024).decode(FORMAT) dont think i need this if message_type == 'l': - lost_conn(addr) + lost_conn(addr2) if message_type == 'g': - gain_conn(addr) + gain_conn(addr2) if message_type == 'll': # under connection bracket (dy_min) print(f'{addr} needs more clients') @@ -141,12 +149,15 @@ def cnode_handler(conn, addr, xport): # PUT MORE HERE if message_type == DISCONNECT: + print('Disconnected') connected = False - for x in cnodes: - if x[1] == addr: - cnodes.remove(x) - conn.close() + for x in cnodes: + if x[0] == addr2: + cnodes.remove(x) + + cconns.remove(conn) + conn.close() #### C NODE CLASS #### @@ -160,14 +171,17 @@ def send_conn(conn, addr, excons): o = [x for x in o if x[1] not in excons] + print(f'avilable cnodes : {o}') + new_node = '' if o == []: new_node = '!NONE' if o != []: - new_node = [o[0][1], o[0][2]] + new_node = [o[0][0], o[0][1]] + print(new_node) new_node = (str(new_node)).encode(FORMAT) conn.send(new_node) @@ -212,10 +226,12 @@ def start(): s.listen() print( f'Cache server listening for any new connections on {HOST}, internal port: {PORT}, external port: {XPORT}') + dynamic_minimum = threading.Thread(target=dy_min) dynamic_minimum.start() while True: + print('Listening') conn, addr = s.accept() startup_message = conn.recv(1024).decode( @@ -226,9 +242,10 @@ def start(): if startup_message: if startup_message == 1: - xport = conn.recv(1024).decode(FORMAT) + info = conn.recv(1024).decode(FORMAT) + info = eval(info) thread = threading.Thread( - target=cnode_handler, args=(conn, addr, xport)) + target=cnode_handler, args=(conn, addr, info)) thread.start() if startup_message == 2: diff --git a/Code/s_config.csv.txt b/Code/s_config.csv.txt index e8ff22d..fcb1eef 100644 --- a/Code/s_config.csv.txt +++ b/Code/s_config.csv.txt @@ -1,2 +1,2 @@ -iport,xport,hostip -5007,5002,0.0.0.0 \ No newline at end of file +iport,xport,hostip,publicip +5007,5007,0.0.0.0,127.0.0.1 \ No newline at end of file