def copyFromDFS(address, fname, path): """ Contact the metadata server to ask for the file blocks of the file fname. Get the data blocks from the data nodes. Saves the data in path. """ # Contact the metadata server to ask for information of fname # Create socket and connect to metadata server metaSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) metaSock.connect(address) # Create get packet and send request to metadata server p = Packet() p.BuildGetPacket(fname) metaSock.sendall(p.getEncodedPacket()) # Recieve chunk list msg = b"" while True: buffer = metaSock.recv(2048) msg += buffer if getsizeof(buffer) < 2048: break # If file not found, exit, if file found, decode packet if msg != b"NFOUND": p.DecodePacket(msg) else: exit("File not found, exiting...") # If there is no error response Retreive the data blocks # Get blocks blocks = p.getDataNodes() # Open file fd = open(path, "wb+") for ip, addr, blockId in blocks: # Create socket and connect to node nodeSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) nodeSock.connect((ip, addr)) # Build packet and send request to data node p.BuildGetDataBlockPacket(blockId) nodeSock.send(p.getEncodedPacket()) # Recieve block and write to file named path fd.write(nodeSock.recv(16384)) # Close socket nodeSock.close()
def handle(self): # Establish a connection with the local database db = mds_db("dfs.db") db.Connect() # Define a packet object to decode packet messages p = Packet() # Receive a msg from the list, data-node, or copy clients msg = b"" while True: buffer = self.request.recv(2048) msg += buffer if getsizeof(buffer) < 2048: break print(msg, type(msg)) # Decode the packet received try: p.DecodePacket(msg) except: print("Error in recieved packet") # Extract the command part of the received packet cmd = p.getCommand() # Invoke the proper action if cmd == "reg": # Registration client self.handle_reg(db, p) elif cmd == "list": self.handle_list(db) elif cmd == "put": # Client asking for servers to put data # Fill code self.handle_put(db, p) elif cmd == "get": # Client asking for servers to get data # Fill code self.handle_get(db, p) elif cmd == "dblks": # Client sending data blocks for file # Fill code self.handle_blocks(db, p) db.Close()
def handle(self): # Recieve packet msg = b"" while True: buffer = self.request.recv(4096) msg += buffer if getsizeof(buffer) < 4096: break print(msg, type(msg)) # Parse message as Packet p = Packet() p.DecodePacket(msg) # Run command in p cmd = p.getCommand() if cmd == "put": self.handle_put(p) elif cmd == "get": self.handle_get(p)