def deleteFile(self, file): # Function to handle delete request from file print "Client called deleteFile( ", file.filename, ")" out = response() if file.filename in self.file_list: del (self.file_list)[file.filename] print " Deleted file from meta database" else: print " File not in meta database" try: print "Updating others" print "Connecting to ", portList[0], " back up server" t2 = TSocket.TSocket('localhost', portList[0]) t2 = TTransport.TBufferedTransport(t2) p2 = TBinaryProtocol.TBinaryProtocol(t2) c2 = MetadataServerService.Client(p2) t2.open() c2.updateDelete(file) t2.close() print "Connecting to ", portList[0], " back up server" t3 = TSocket.TSocket('localhost', portList[1]) t3 = TTransport.TBufferedTransport(t3) p3 = TBinaryProtocol.TBinaryProtocol(t3) c3 = MetadataServerService.Client(p3) t3.open() c3.updateDelete(file) t3.close() except: pass out.message = responseType.OK return out
def storeFile(self, file): # Function to handle upload request print "Client called storeFile(", file.filename, ")" # Add file to meta database #(self.file_list)[file.filename] = file (self.transport).open() missing_blocks = [] # list of missing blocks print file.hashList # Call block server on each hash to see if exists for hash_val in file.hashList: response = (self.client).hasBlock(hash_val) if response.status == hasBlockResponseType.MISSING: missing_blocks.append(hash_val) print "The following blocks are missing" out = uploadResponse() out.status = uploadResponseType.MISSING_BLOCKS if len(missing_blocks) == 0: print " [none]" out.status = uploadResponseType.OK for b in missing_blocks: print " ", b (self.transport).close() if len(missing_blocks) == 0: (self.file_list)[file.filename] = file try: #update other servers print "Updating others" print "Connecting to ", portList[0], " back up server" t2 = TSocket.TSocket('localhost', portList[0]) t2 = TTransport.TBufferedTransport(t2) p2 = TBinaryProtocol.TBinaryProtocol(t2) c2 = MetadataServerService.Client(p2) t2.open() c2.updateFile(file) t2.close() print "Connecting to ", portList[1], " back up server" t3 = TSocket.TSocket('localhost', portList[1]) t3 = TTransport.TBufferedTransport(t3) p3 = TBinaryProtocol.TBinaryProtocol(t3) c3 = MetadataServerService.Client(p3) t3.open() c3.updateFile(file) t3.close() except: pass # Compose response out.hashList = missing_blocks return out
def getSocket(port, serverName): # This function creates a socket to block server and returns it # Make socket transport = TSocket.TSocket('localhost', port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) if serverName == 'block': # Block socket client = BlockServerService.Client(protocol) elif serverName == serverName: # Meta socket client = MetadataServerService.Client(protocol) # Connect! print("Trying connection to %s server on port: %d" % (serverName, port)) try: transport.open() except Exception: return None, None return client, transport
def getMetaServerSocket(port): transport = TSocket.TSocket('localhost', port) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = MetadataServerService.Client(protocol) try: transport.open() except Exception as e: client = None return client
def getConnection(self, portNumber, serverType): transport = TSocket.TSocket('localhost', portNumber) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) if serverType =="block": client = BlockServerService.Client(protocol) else: client = MetadataServerService.Client(protocol) try: transport.open() except Exception as e: print "Error while opening socket to server\n", e exit(1) return client, transport
def getMetaServerSocket(port): # This function creates a socket to block server and returns it # Make socket transport = TSocket.TSocket('localhost', port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = MetadataServerService.Client(protocol) # Connect! #print "Connecting to block server on port", port try: transport.open() except Exception as e: print "ERROR: Exception while connecting to block server, check if server is running on port", port print e exit(1) return client
# Add additional classes and functions here if needed if __name__ == "__main__": if len(sys.argv) < 3: print "Invocation <executable> <config_file> <id>" exit(-1) config_path = sys.argv[1] my_id = sys.argv[2] print "Initializing metadata server" handler = MetadataServerHandler(config_path, my_id) metaPort = handler.readServerPort() # Define parameters for thrift server processor = MetadataServerService.Processor(handler) transport = TSocket.TServerSocket(port=metaPort) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() # Create a server object server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) print "Starting server on port : ", metaPort try: server.serve() except (Exception, KeyboardInterrupt) as e: print "\nExecption / Keyboard interrupt occured: ", e exit(0)
def start_client(block_port, serverport, fname, dirf, op): try: f = open(os.devnull, 'w') old = sys.stderr sys.stderr = f #print "Connecting to Metadata server...", transport = TSocket.TSocket('localhost', serverport) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = MetadataServerService.Client(protocol) #print "SUCCESS" #print "Connecting to Block Server...", b_trans = TSocket.TSocket('localhost', block_port) b_trans = TTransport.TBufferedTransport(b_trans) b_proto = TBinaryProtocol.TBinaryProtocol(b_trans) b_client = BlockServerService.Client(b_proto) #print "SUCCESS" #b_trans.open() #b_client.hasBlock("test client") #b_trans.close() #client.ping() #print "Creating file" aFile = file() aFile.filename = fname aFile.version = 1 aFile.status = responseType.OK #print "Mode: ", op if op == "upload": tmp = separate_file(dirf, fname) aFile.hashList = tmp[0] #print " Sending file metadata..." transport.open() res = client.storeFile(aFile) transport.close() #print " The following blocks will be sent" b_trans.open() for b_hash in res.hashList: #print b_hash out = hashBlock() out.hash = b_hash out.block = (tmp[2])[b_hash] b_client.storeBlock(out) b_trans.close() transport.open() res = client.storeFile(aFile) transport.close() if res.status != uploadResponseType.OK: print "ERROR" return 2 # if op is get if op == "download": transport.open() hashBlockList = list() res = client.getFile(fname) if res.status == responseType.ERROR: print "ERROR" return 2 #print "The following blocks will be fetched:" #for hb in res.hashList: # print " ", hb for hb in res.hashList: b_trans.open() # chech block server has block res2 = b_client.hasBlock(hb) #print " Checking block ", hb if res2.status == hasBlockResponseType.OK: #print " OK - appending to hashBlock list" tmp = b_client.getBlock(hb) hashBlockList.append(tmp) else: #print "Block not in server" print "ERROR" return 2 b_trans.close() bl = join(hashBlockList, dirf, fname) if op == "delete": transport.open() res = client.deleteFile(aFile) if res.message != responseType.OK: print "ERROR" return 2 transport.close() #close transport print "OK" sys.stderr = old except: return 3 return 1