def main(): global content_dict while(True): try: try: sock = socket.socket() sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) print('Socket successfully created') except socket.error as err: print('Socket creation failed with error %s', err) return host = ORIGIN_SERVER_IP_1 port = ORIGIN_CONTENT_PROVIDER_PORT_1 sock.connect((host, port)) print("Connected to origin Primary") except: try: try: sock = socket.socket() sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) print('Socket successfully created') except socket.error as err: print('Socket creation failed with error %s', err) return host = ORIGIN_SERVER_IP_2 port = ORIGIN_CONTENT_PROVIDER_PORT_2 sock.connect((host, port)) print("Connected to origin Secondary") except: print("Connection to origin failed, retrying") time.sleep(1) continue try: filename = sys.argv[1] content_id = int(sys.argv[2]) file_size = int(os.stat(filename).st_size) file_des = FileDescriptionMessage(content_id,file_size,filename,md5(filename)) file_des.send(sock) f = open(filename, 'rb') l = f.read(1018) i = 0 while (l): # if message.seq_no <= i: msg = ContentMessage(content_id, i) msg.data = l msg.packet_size = len(l) msg.send(sock) i += 1 l = f.read(1018) f.close() sock.close() break except socket.error as err: print(err) print("Retrying")
def serve_content_provider_helper(c, addr): global content_dict, content_dictL file_des = FileDescriptionMessage(0, 0, '', '') file_des.receive(c) print(file_des.file_name) print(file_des.content_id) print(file_des.file_size) content_dictL.acquire() content_dict[file_des.content_id] = Content(file_des.content_id, file_des.file_name, ContentStatus.INCOMPLETE) dump() content_dictL.release() with open('data/' + file_des.file_name, 'wb') as f: recv_size = 0 file_size = file_des.file_size while True: mes = ContentMessage(0, 0) print('receiving data...') mes.receive(c, file_size, recv_size) print(mes.content_id) print(mes.seq_no) data = mes.data if not data: break f.write(data) recv_size += len(data) print("successfully received the file") if md5('data/' + file_des.file_name) == file_des.md5_val: print("MD5 Matched!") else: print("MD5 didn't match") content_dictL.acquire() content_dict[file_des.content_id].status = ContentStatus.UNSYNCED dump() content_dictL.release() print_dict() c.close() print('closed')
def serve_edge_server_helper(conn, addr): global content_dict try: message = ContentRequestMessage(0, 0) message.receive(conn) # Get filename from file if message.received == False: return # Check if file is present in edge server if message.content_id in content_dict: filename = content_dict[message.content_id].filename # before sending the file, send its details plus a checksum file_size = int(os.stat('data/' + filename).st_size) print("filename: ", filename) file_des = FileDescriptionMessage(message.content_id, file_size, filename, md5('data/' + filename)) file_des.send(conn) f = open('data/' + filename, 'rb') l = f.read(1018) i = 0 while (l): # if i == 10: # print('Hi') # exit() if message.seq_no <= i: msg = ContentMessage(message.content_id, i) msg.data = l msg.packet_size = len(l) msg.send(conn) i += 1 l = f.read(1018) f.close() else: # Get chunks of data from origin and send to client pass conn.close() except Exception as e: print(e)
def requestFile(edgeIP, edgePort, content_id, seq_no=0): ## Sequence number is zero for initial request ## returns last sequence number it received ## -2 if complete file is received ## -1 if nothing is received ## else the sequence number soc = socket.socket() # Create a socket object soc.settimeout(30) try: print("Connecting to edge server ip: ", edgeIP) sys.stdout.flush() soc.connect((edgeIP, edgePort)) except Exception as e: print("Unable to connect to edge server ip: ", edgeIP) return -1 last_seq_number_recv = -1 message = ContentRequestMessage(content_id, seq_no) message.send(soc) file_des = FileDescriptionMessage(0, 0, '', '') try: file_des.receive(soc) except: print("Unable to get file details") print("Last Sequence Number received: ", last_seq_number_recv) return last_seq_number_recv print(file_des.file_name) print(file_des.content_id) print(file_des.file_size) if seq_no != 0: param = 'ab' else: param = 'wb' with open('rec_' + file_des.file_name, param) as f: print('file opened') print("Content ID: ", file_des.content_id) if seq_no != 0: f.seek(seq_no * 1018) file_size = file_des.file_size total_received = seq_no * 1018 while True: msg = ContentMessage(content_id, seq_no) try: msg.receive(soc, file_size, total_received) except Exception as e: print("Last Sequence Number received: ", last_seq_number_recv) print(e) return last_seq_number_recv print("Sequence no: ", msg.seq_no) last_seq_number_recv = msg.seq_no data = msg.data total_received += len(data) # print(len(data)) if not data: break f.write(data) f.close() soc.close() ############# Verify file, close connections and show success if md5('rec_' + file_des.file_name) == file_des.md5_val: print("File download success!") else: print("MD5 does not match") os.remove('rec_' + file_des.file_name) print("Try downloading again") ## TODO What to do with the file then??? return -2
def synchronizer(): global content_dict, content_dictL while (True): sock = socket.socket() sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) host = ORIGIN_SERVER_IP_2 port = ORIGIN_SYNCHRONIZER_PORT_2 sock.bind((host, port)) sock.listen(1) conn, addr = sock.accept() print('Accepted', conn, 'from', addr) print('Connected to other Origin Server') while (True): print("Looking for UNSYNCED files") try: for file in content_dict.values(): if file.status == ContentStatus.UNSYNCED: # Sync this file print("Syncing file", file.filename, "with content id", file.content_id) file_size = int( os.stat('data/' + file.filename).st_size) file_des = FileDescriptionMessage( file.content_id, file_size, file.filename, md5('data/' + file.filename)) print(file.content_id, file_size, file.filename, md5('data/' + file.filename)) file_des.send(conn) # receive response from other server msg = OriginHeartbeatMessage(0) msg.receive(conn) if msg.file_exists: content_dictL.acquire() content_dict[ file_des. content_id].status = ContentStatus.STORED dump() content_dictL.release() continue f = open('data/' + file.filename, 'rb') l = f.read(1018) i = 0 while (l): # if message.seq_no <= i: msg = ContentMessage(file.content_id, i) msg.data = l msg.packet_size = len(l) msg.send(conn) i += 1 l = f.read(1018) f.close() content_dictL.acquire() content_dict[ file_des.content_id].status = ContentStatus.STORED dump() content_dictL.release() time.sleep(ORIGIN_HEARTBEAT_TIME) except Exception as e: print(e) break