def __init__(self, config_path, my_id): # Initialize block global block_port print "Other servers at: ", portList self.file_list = {} print "Connecting to Block Server", block_port self.transport = TSocket.TSocket('localhost', block_port) self.transport = TTransport.TBufferedTransport(self.transport) self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport) self.client = BlockServerService.Client(self.protocol) return
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 getBlockServerSocketPortMeta(self, 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 = BlockServerService.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) < 2: print "Invocation <executable> <config_file>" exit(-1) config_path = sys.argv[1] print "Initializing block server" handler = BlockServerHandler(config_path) # Retrieve the port number from the config file so that you could strt the server #port = handler.readServerPort() # Define parameters for thrift server processor = BlockServerService.Processor(handler) transport = TSocket.TServerSocket(port=handler.port) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() # Create a server object server = TServer.TThreadedServer(processor, transport, tfactory, pfactory) print "Starting server on port : ", handler.port 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