def readFile(self, fileC): ipAddress = socket.gethostbyname(socket.gethostname()) file = ipAddress + ':' + str(port) sha256key = hashlib.sha256(file.encode()) sHash = sha256key.hexdigest() sha256val = hashlib.sha256(fileC.encode()) contentHash = sha256val.hexdigest() checkNode = NodeID() checkNode = self.findSucc(contentHash) if self.sha256ID == checkNode.id: if sHash in self.log: #sha256val = hashlib.sha256(fileC.encode()) #sHashfile = sha256val.hexdigest() sFile = RFile() sFileMeta = RFileMetadata() sFileMeta.filename = fileC sFileMeta.version = self.version sFileMeta.contentHash = contentHash file = open(fileC, "r") sFile.content = file.read() print(sFile.content) sFile.meta = sFileMeta else: x = SystemException() x.message = "File Is not Present in server" raise x return sFile else: z = SystemException() z.message = "server is not the file’s successor" raise z
def writeFile(self, rFile): key = hashlib.sha256(rFile.meta.owner + ":" + rFile.meta.filename).hexdigest() node = self.findSucc(key) if not node.id == self.NodeID: raise SystemException("Write File Error - Server does not own fil`s id") new_file = True for my_file in self.files: if rFile.meta.filename == my_file.meta.filename: my_file.meta.version += 1 my_file.meta.owner = rFile.meta.owner my_file.meta.contentHash = hashlib.sha256(rFile.meta.contentHash).hexdigest() my_file.content = rFile.content new_file = False if new_file: f = RFile() m = RFileMetadata() m.filename = rFile.meta.filename m.version = 0 m.owner = rFile.meta.owner m.contentHash = hashlib.sha256(rFile.meta.contentHash).hexdigest() f.meta = m f.content = rFile.content self.files.append(f)
def readFile(self, filenameIn): rFile = RFile() try: hash_object = hashlib.sha256(filenameIn.encode()).hexdigest() # Find the successor of file fileServerNode = self.findSucc(hash_object) # If successor of file is not same server to which client has requested file - SystemException is thrown if (fileServerNode.id != self.serverNode.id): systemException = SystemException() systemException.message = ("File not owned by Server " + self.serverNode.ip + ":" + str(self.serverNode.port) + "\n") raise systemException # If successor of file is same server to which client has requested file - Request served rFileMetadata = RFileMetadata() if (self.file_info.get(hash_object) != None): rFileMetadata.filename = filenameIn rFileMetadata.version = self.file_info.get(hash_object)[1] + 1 rFileMetadata.contentHash = self.file_info.get(hash_object)[3] rFile.meta = rFileMetadata rFile.content = self.file_info.get(hash_object)[2] else: systemException = SystemException() systemException.message = ("\nFile " + filenameIn + " Not Found") raise systemException except IOError as e: systemException = SystemException() systemException.message = "IO Exception in read file" raise systemException return rFile
def readFile(self, filename): key = sha_256(filename) node = self.findSucc(key) if node != self.currentNode: raise SystemException("ReadFile, file does not exist") exists = False index = 0 for x in range(0, len(self.files)): if self.files[x].meta.filename == filename: exists = True index = x temp = self.files[index] rFile1 = RFile() if exists: print("exist") meta = RFileMetadata() meta.filename = filename meta.version = temp.meta.version meta.contentHash = temp.meta.contentHash rFile1.meta = meta rFile1.content = temp.content return rFile1 raise SystemException("ReadFile, file not found")
def main(): ip = sys.argv[1] port = int(sys.argv[2]) (transport, client) = openConnection(ip, port) ''' Call FileStore methods on PythonServer.py ''' h = getID(ip, port) p = client.findPred(h) print('The predecessor of %i is %i' % (port, p.port)) f = RFile() f.content = "Tomato! Wahoo." f.meta = RFileMetadata() f.meta.filename = "tomato.txt" s = client.findSucc(getHash(f.meta.filename)) print('The successor of %s is %i' % (f.meta.filename, s.port)) if s.port != port: #print('Will write to incorrect server...') #try: #client.writeFile(f) #except SystemException as se: # print('Wrong server: %s' % se.message) # return transport.close() print('Connecting with correct server at port %i' % s.port) (transport, client) = openConnection(ip, s.port) # Attempt to read non-existent file #client.readFile('asparagus.txt') client.writeFile(f) r = client.readFile(f.meta.filename) print('contents of %s is %s' % (f.meta.filename, f.content)) #client.readFile('Hello') #client.setFingertable(None) #client.findSucc('key') #client.findPred('key') #n = client.getNodeSucc() #print('client: getNodeSucc() returned', n) #sum_ = client.add(1, 1) #print('1+1=%d' % sum_) #work = Work() #work.op = Operation.DIVIDE #work.num1 = 1 #work.num2 = 0 #try: # quotient = client.calculate(1, work) # print('Whoa? You know how to divide by zero?') # print('FYI the answer is %d' % quotient) #except InvalidOperation as e: # print('InvalidOperation: %r' % e) #work.op = Operation.SUBTRACT #work.num1 = 15 #work.num2 = 10 #diff = client.calculate(1, work) #print('15-10=%d' % diff) #log = client.getStruct(1) #print('Check log: %s' % log.value) # Close! print('Client closing connection') transport.close()
def main(): if len(sys.argv) < 3: print "Arguments missing.\nUsage: ./client.sh [server_ip] [server_port]" sys.exit(1) # Make socket transport = TSocket.TSocket(sys.argv[1], int(sys.argv[2])) transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) # Connect transport.open() try: rfile = RFile() rfile.meta = RFileMetadata() rfile.meta.filename = "dream.txt" rfile.meta.owner = "Bob" rfile.content = "XYZWER" client.writeFile(rfile) print "\nFile %s written on server.." % rfile.meta.filename except SystemException as e: print e.message try: rfile1 = RFile() rfile1.meta = RFileMetadata() rfile1.meta.filename = "example123.txt" rfile1.meta.owner = "Alex" rfile1.content = "Hello" client.writeFile(rfile1) print "\nFile %s written on server.." % rfile1.meta.filename except SystemException as e: print e.message try: rfile1 = RFile() rfile1.meta = RFileMetadata() rfile1.meta.filename = "example.txt" rfile1.meta.owner = "guest" rfile1.content = "ABCDEF" client.writeFile(rfile1) print "\nFile %s written on server.." % rfile1.meta.filename except SystemException as e: print e.message try: rfile2 = RFile() rfile2 = client.readFile("example.txt", "guest") print "\nContent: ", rfile2.content print "Meta Information:\nFileName: %s , Owner: %s, ContentHash: %s, Version: %s" % ( rfile2.meta.filename, rfile2.meta.owner, rfile2.meta.contentHash, rfile2.meta.version) except SystemException as e: print e.message try: rfile2 = RFile() rfile2 = client.readFile("demo.txt", "guest") print "\nContent: ", rfile2.content print "Meta Information:\nFileName: %s , Owner: %s, ContentHash: %s, Version: %s" % ( rfile2.meta.filename, rfile2.meta.owner, rfile2.meta.contentHash, rfile2.meta.version) except SystemException as e: print e.message # Close transport.close()
def main(): # Make socket port = sys.argv[1] 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 = FileStore.Client(protocol) # Connect! transport.open() rFile = RFile() #print(rFile) rFile.meta = RFileMetadata() rFile.meta.owner = 'mandy' rFile.meta.filename = 'abc.txt' rFile.content = 'My Name is manideep' client.writeFile(rFile) print('ping()') s = client.readFile('abc.txt', 'mandy') print(s.content) print(s.meta.version) print(s) #work = Work() #work.op = Operation.DIVIDE #work.num1 = 1 #work.num2 = 0 '''try: quotient = client.calculate(1, work) print('Whoa? You know how to divide by zero?') print('FYI the answer is %d' % quotient) except InvalidOperation as e: print('InvalidOperation: %r' % e) work.op = Operation.SUBTRACT work.num1 = 15 work.num2 = 10 ''' k = client.getNodeSucc() print('getNodeSucc', k) s = client.findSucc( '8f019af015bab34f148262235d11a541d607adad3e1db9d2283cbf283a4e991b' ) # 91-92 print('findSucc91-92-', s) s = client.findSucc( '82b9e6b1548ad0837abe6ff674d1d3e982a2038442a1059f595d95962627f827' ) # 91 print('findSucc91-', s) s = client.findSucc( 'fef33d8355d29002c802820e9d371b9d8c3c7a14eb072ac4a5090556a4285013' ) # 93 print('findSucc93-', s) s = client.findSucc( 'e318e4cd5089903a714f2d5585fb1c70bf1ed0af4fc8fea5b3775a5a687fd040' ) # 90 print('findSucc90-', s) s = client.findSucc( 'b2bd42182ac6a8872c76db866738d47e04f66a3d2cf5c359962008baa052dbc4' ) # 92 print('findSucc92-', s) s = client.findSucc( 'b74191dd2d9480d6e30125527eaaf23d4334a8941e4ea81872bd4ab35a7137f2' ) # 92 print('findSucc94-', s) s = client.findSucc( 'f5c1043bec54c8869db4ad0c1aa5e8835d9ebe87ae2deeb95916a5e54ffcbfc3' ) # 92 print('findSucc-mani-', s) l = client.findPred( '8f019af015bab34f148262235d11a541d607adad3e1db9d2283cbf283a4e991b' ) #91-92 print('findPred91-92-', l) l = client.findPred( '82b9e6b1548ad0837abe6ff674d1d3e982a2038442a1059f595d95962627f827' ) #91 print('findPred91-', l) l = client.findPred( 'fef33d8355d29002c802820e9d371b9d8c3c7a14eb072ac4a5090556a4285013' ) # 93 print('findPred93-', l) l = client.findPred( 'e318e4cd5089903a714f2d5585fb1c70bf1ed0af4fc8fea5b3775a5a687fd040' ) # 90 print('findPred90-', l) l = client.findPred( 'b2bd42182ac6a8872c76db866738d47e04f66a3d2cf5c359962008baa052dbc4' ) # 92 print('findPred92-', l) l = client.findPred( 'b74191dd2d9480d6e30125527eaaf23d4334a8941e4ea81872bd4ab35a7137f2' ) # 94 print('findPred94-', l) l = client.findPred( 'f5c1043bec54c8869db4ad0c1aa5e8835d9ebe87ae2deeb95916a5e54ffcbfc3' ) # 94 print('findPred-mani-', l) #l = client.findPred('72b9e6b1548ad0837abe6ff674d1d3e982a2038442a1059f595d95962627f827') #l = client.findPred('305ba2b525c2e7b344d851cf1ec247f2ba28667222d33b4c1d57519fc82cf418') #print(client.getNodeSucc()) # Close! transport.close()
def test_readFile(list_clients): rFile_a = RFile() rFile_a.content = 'rFile_a content!' rFile_a.meta = RFileMetadata() rFile_a.meta.filename = 'rFile_a.txt' rFile_a.meta.owner = 'a' rFile_a.meta.contentHash = hashlib.sha256(rFile_a.meta.owner + ':' + \ rFile_a.meta.filename).hexdigest() rFile_b = RFile() rFile_b.content = 'rFile_b content!' rFile_b.meta = RFileMetadata() rFile_b.meta.filename = 'rFile_b.txt' rFile_b.meta.owner = 'b' rFile_b.meta.contentHash = hashlib.sha256(rFile_b.meta.owner + ':' + \ rFile_b.meta.filename).hexdigest() # rFile_a test owner_node = list_clients[0][0].findSucc(rFile_a.meta.contentHash) count = 0 for client in list_clients: if client[2] == owner_node.id: count = count + 1 client[0].writeFile(rFile_a) assert count == 1 for client in list_clients: if owner_node.id == client[2]: count = count + 1 returned_file = client[0].readFile(rFile_a.meta.filename, rFile_a.meta.owner) assert returned_file.content == rFile_a.content assert returned_file.meta.filename == rFile_a.meta.filename assert returned_file.meta.owner == rFile_a.meta.owner assert returned_file.meta.contentHash == rFile_a.meta.contentHash assert returned_file.meta.version == 1 else: try: returned_file = client[0].readFile(rFile_a.meta.filename, rFile_a.meta.owner) assert False except SystemException as e: print(e) assert count == 2 rFile_a.content = 'Updated rFile_a content!' for client in list_clients: if client[2] == owner_node.id: client[0].writeFile(rFile_a) for client in list_clients: if owner_node.id == client[2]: count = count + 1 returned_file = client[0].readFile(rFile_a.meta.filename, rFile_a.meta.owner) assert returned_file.content == 'Updated rFile_a content!' assert returned_file.meta.filename == rFile_a.meta.filename assert returned_file.meta.owner == rFile_a.meta.owner assert returned_file.meta.contentHash == rFile_a.meta.contentHash assert returned_file.meta.version == 2 else: try: returned_file = client[0].readFile(rFile_a.meta.filename, rFile_a.meta.owner) assert False except SystemException as e: print(e) assert count == 3 # rFile_b test owner_node = list_clients[0][0].findSucc(rFile_b.meta.contentHash) count = 0 for client in list_clients: if client[2] == owner_node.id: count = count + 1 client[0].writeFile(rFile_b) assert count == 1 for client in list_clients: if owner_node.id == client[2]: count = count + 1 returned_file = client[0].readFile(rFile_b.meta.filename, rFile_b.meta.owner) assert returned_file.content == rFile_b.content assert returned_file.meta.filename == rFile_b.meta.filename assert returned_file.meta.owner == rFile_b.meta.owner assert returned_file.meta.contentHash == rFile_b.meta.contentHash assert returned_file.meta.version == 1 else: try: returned_file = client[0].readFile(rFile_b.meta.filename, rFile_b.meta.owner) assert False except SystemException as e: print(e) assert count == 2 rFile_b.content = 'Updated rFile_b content!' for client in list_clients: if client[2] == owner_node.id: client[0].writeFile(rFile_b) for client in list_clients: if owner_node.id == client[2]: count = count + 1 returned_file = client[0].readFile(rFile_b.meta.filename, rFile_b.meta.owner) assert returned_file.content == 'Updated rFile_b content!' assert returned_file.meta.filename == rFile_b.meta.filename assert returned_file.meta.owner == rFile_b.meta.owner assert returned_file.meta.contentHash == rFile_b.meta.contentHash assert returned_file.meta.version == 2 else: try: returned_file = client[0].readFile(rFile_b.meta.filename, rFile_b.meta.owner) assert False except SystemException as e: print(e) assert count == 3
def run_client(parser): args = parser.parse_args() if args.operation != 5 and args.file_name == "": print("Operation {0} requires file_name argument".format( args.operation)) parser.print_help() sys.exit(os.EX_USAGE) else: transport = TSocket.TSocket(args.ip, args.port) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = FileStore.Client(protocol) try: transport.open() if args.operation == 1: # To WriteFile if DEBUG: print("Client: {0}:{1} ".format(args.ip, args.port)) print("WriteFile: {0} ".format(args.file_name)) rFile = RFile() rFile.content = "Any fool can write code that a computer can understand.\nGood programmers write code that humans can understand." meta = RFileMetadata(args.file_name, 0) rFile.meta = meta client.writeFile(rFile) elif args.operation == 2: # To ReadFile if DEBUG: print("Client: {0}:{1} ".format(args.ip, args.port)) print("ReadFile: {0} ".format(args.file_name)) rFile = client.readFile(args.file_name) print("File Name: {0} ".format(rFile.meta.filename)) print("File Version: {0} ".format(rFile.meta.version)) print("File Content: {0} ".format(rFile.content)) elif args.operation == 3: # To FindSucc if DEBUG: print("Client: {0}:{1} ".format(args.ip, args.port)) print("FindSucc: {0} ".format(args.file_name)) file_key = calculate_hash(args.file_name) succ_node = client.findSucc(file_key) print("Succ Node id: {0}".format(succ_node.id)) print("Succ Node ip: {0}".format(succ_node.ip)) print("Succ Node port: {0}".format(succ_node.port)) elif args.operation == 4: # To FindPred if DEBUG: print("Client: {0}:{1} ".format(args.ip, args.port)) print("FindPred: {0} ".format(args.file_name)) file_key = calculate_hash(args.file_name) pred_node = client.findPred(file_key) print("Pred Node id: {0}".format(pred_node.id)) print("Pred Node ip: {0}".format(pred_node.ip)) print("Pred Node port: {0}".format(pred_node.port)) elif args.operation == 5: # To GetNodeSucc if DEBUG: print("Client: {0}:{1} ".format(args.ip, args.port)) print("GetNodeSucc:") first_node = client.getNodeSucc() print("First Node id: {0}".format(first_node.id)) print("First Node ip: {0}".format(first_node.ip)) print("First Node port: {0}".format(first_node.port)) transport.close() except SystemException as sx: print("SystemException: {0}".format(sx)) except TException as tx: print("TException: {0}".format(tx)) return 0
assert err.message == "Fingertable not exist for the current node", "Test_Find_Pred - message content mismatch" print "Test case passed 16 -- ", err.message try: filestore.getNodeSucc() raise AssertionError( "Test_Get_Node_Succ - No Exception on empty finger table") except SystemException as err: assert err.message == "Fingertable not exist for the current node", "Test_Node_Succ - message content mismatch" print "Test case passed 17 -- ", err.message if __name__ == '__main__': rf_metadata = RFileMetadata() rf_metadata.filename = "file_name" rf_metadata.version = 0 rf_metadata.owner = "owner_name" rf_metadata.contentHash = sha256(rf_metadata.owner + ":" + rf_metadata.filename).hexdigest() rfile = RFile() rfile.meta = rf_metadata rfile.content = "This is my first apache thrift programming experience" try: t = TestChord() t.test_write_file(rfile) t.test_read_file(rf_metadata.filename, rf_metadata.owner) #t.test_negative_cases(make_socket('128.226.180.166', 9095), rfile.meta.contentHash) except Thrift.TException as tx: print('%s' % tx.message)
def writeFile(self, inputF): #print('--------------------------------------------------') ipAddress = socket.gethostbyname(socket.gethostname()) self.file = ipAddress + ':' + str(port) sha256file = hashlib.sha256(self.file.encode()) self.sha256ID = sha256file.hexdigest() #print ('IP and port ', file) filename = inputF.meta.filename contentHash = inputF.meta.contentHash checkNode = NodeID() checkNode = self.findSucc(contentHash) #print('checknode id:', checkNode.id, 'self: ',self.sha256ID) if self.sha256ID == checkNode.id: if self.sha256ID in self.log: #x = self.log[self.sha256ID].meta.version #self.log[self.sha256ID].meta.version = x+1 #self.log[self.sha256ID].content = inputF.content #x = RFile() #x1 = RFileMetadata() #x1 = self.log[self.sha256ID].meta #x1.version = x1.version + 1 #self.log[self.sha256ID].meta = x1 self.version = self.version + 1 self.content = inputF.content #x = self.log[self.sha256ID] #x.meta = RFileMetadata() #x.meta.version = x.meta.version + 1 #x1 = x.meta #x1.version = x.meta.version + 1 #x.RFileMetadata = x1 #x.meta.version = x.meta.version+1 #x.content = inputF.content #self.log[self.sha256ID] = x #self.log[sha256ID].meta.version = x+1 #self.log[sha256ID].content = inputF.content #x = serverFile.meta.version #serverFile.meta.version = x+1 #serverFile.meta.contentHash = inputF.meta.contentHash #serverFile.content = inputF.content file = open(inputF.meta.filename, "w+") file.write(inputF.content) else: serverFile = RFile() sMeta = RFileMetadata() file = open(filename, "w+") file.write(inputF.content) sMeta.filename = filename sMeta.version = 0 self.version = sMeta.version sMeta.contentHash = contentHash serverFile.content = inputF.content self.content = serverFile.content serverFile.RFileMetadata = sMeta self.log[self.sha256ID] = serverFile file.close() else: y = SystemException() y.message = "Rserver is not the file’s successor" raise y
def testOverwrite(host, port_list): rand_idx = random.randint(0, len(port_list)-1) rand_port = port_list[rand_idx] # Make socket transport = TSocket.TSocket(host, rand_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 = FileStore.Client(protocol) # Connect! transport.open() meta_obj = RFileMetadata() meta_obj.filename = "book.txt" meta_obj.version = 0 meta_obj.owner = "Brad" meta_obj.contentHash = \ hashlib.sha256(("%s:%s" % (meta_obj.filename,\ meta_obj.owner)).encode("utf-8")).hexdigest() content_str = "Test String" file_obj = RFile() file_obj.meta = meta_obj file_obj.content = content_str succ = client.findSucc(file_obj.meta.contentHash) transport.close() # Make socket transport = TSocket.TSocket(host, succ.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 = FileStore.Client(protocol) # Connect! transport.open() client.writeFile(file_obj) # Make socket transport = TSocket.TSocket(host, succ.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 = FileStore.Client(protocol) # Connect! transport.open() file_obj.meta.version = file_obj.meta.version + 1 file_obj.content = "New Test String" client.writeFile(file_obj) try: rand_idx = random.randint(0, len(port_list)-1) rand_port = port_list[rand_idx] # Make socket transport = TSocket.TSocket(host, rand_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 = FileStore.Client(protocol) # Connect! transport.open() res = client.readFile("book.txt", "Brad") assert res.content == "New Test String" assert res.meta.version == 1 print("File Overwrite Successful") except SystemException: print("Success: Incorrect owner") transport.close()
def testReadAfterWriteError(host, port_list): rand_idx = random.randint(0, len(port_list)-1) rand_port = port_list[rand_idx] # Make socket transport = TSocket.TSocket(host, rand_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 = FileStore.Client(protocol) # Connect! transport.open() meta_obj = RFileMetadata() meta_obj.filename = "book.txt" meta_obj.version = 0 meta_obj.owner = "Brad" meta_obj.contentHash = \ hashlib.sha256(("%s:%s" % (meta_obj.filename,\ meta_obj.owner)).encode("utf-8")).hexdigest() content_str = "Knowledge Bitch!" file_obj = RFile() file_obj.meta = meta_obj file_obj.content = content_str succ = client.findSucc(file_obj.meta.contentHash) transport.close() # Make socket transport = TSocket.TSocket(host, succ.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 = FileStore.Client(protocol) # Connect! transport.open() client.writeFile(file_obj) transport.close() other_port = [port for port in port_list if port != succ.port][0] # Make socket transport = TSocket.TSocket(host, other_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 = FileStore.Client(protocol) # Connect! transport.open() try: res = client.readFile("book.txt", "Brad") print("Read after write error NOT succesful") except SystemException: print("Read after write error successful") transport.close()
def writeFile(self, rfile_input): # Find the SHA256 hash value for input rfile rfile_write_hash_id = hashlib.sha256( rfile_input.meta.filename.encode(ENCODE_TYPE_UTF_8)).hexdigest() # Find successor of input file key identifier key_succ_node_id = self.findSucc(rfile_write_hash_id) if DEBUG_FLAG: print("Inside Write ==> Input filename =============== %s" % rfile_input.meta.filename) print("Inside Write ==> Input file content =========== %s" % rfile_input.content) print("Inside Write ==> Key Identifier of Input File = %s" % rfile_write_hash_id) print("Inside Write ==> Successor of Key Identifier == %s" % key_succ_node_id.id) print("Inside Write ==> Current Node's Hash Value Id = %s\n" % self.current_node_id.id) # To serve the request successor node of key should be the current node if key_succ_node_id.ip == self.current_node_id.ip and \ key_succ_node_id.port == self.current_node_id.port and \ key_succ_node_id.id == self.current_node_id.id: # Check whether rfile exists in current file system repository # if yes, then update the version of the file in repository # otherwise, insert new entry for this file in repository if rfile_write_hash_id in ChordServerFileStoreHandler.file_system_repository_dict: existing_rfile = ChordServerFileStoreHandler.file_system_repository_dict[ rfile_write_hash_id] existing_rfile.meta.version += 1 existing_rfile.content = rfile_input.content existing_rfile.meta.contentHash = hashlib.sha256( rfile_input.content.encode(ENCODE_TYPE_UTF_8)).hexdigest() else: # Local variables rfile_write_meta = RFileMetadata() rfile_write = RFile() rfile_write.meta = rfile_write_meta # initialize new rfile using input rfile sent by Client rfile_write.meta.version = 0 rfile_write.content = rfile_input.content rfile_write.meta.filename = rfile_input.meta.filename # Set content hash as SHA256 hash value using "file content" as string rfile_write.meta.contentHash = hashlib.sha256( rfile_write.content.encode(ENCODE_TYPE_UTF_8)).hexdigest() # Insert new rfile into global file system repository with version 0 ChordServerFileStoreHandler.file_system_repository_dict[ rfile_write_hash_id] = rfile_write if DEBUG_FLAG: print( "\n---------------------------------------- FILE SYSTEM REPO ----------------------------------------" ) for hash_k, rfile_v in ChordServerFileStoreHandler.file_system_repository_dict.items( ): rfile_desc = rfile_v.meta.filename + SPACE_STRING + \ str(rfile_v.meta.version) + SPACE_STRING + rfile_v.content print(hash_k, " --> %s" % rfile_desc) print( "--------------------------------------------------------------------------------------------------\n" ) else: writeFile_system_ex = SystemException() writeFile_system_ex.message = ERROR_WRITE_SYSTEM_EXCP_MSG + \ self.current_node_id.ip + COLON_STRING + \ str(self.current_node_id.port) raise writeFile_system_ex
def testOverwrite(): # Make socket transport = TSocket.TSocket('alpha.cs.binghamton.edu', 9000) # 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 = FileStore.Client(protocol) # Connect! transport.open() meta_obj = RFileMetadata() meta_obj.filename = "book.txt" meta_obj.version = 0 meta_obj.owner = "Brad" meta_obj.contentHash = hashlib.sha256(meta_obj.filename +\ ":" + meta_obj.owner).hexdigest() content_str = "Test String" file_obj = RFile() file_obj.meta = meta_obj file_obj.content = content_str client.writeFile(file_obj) transport.close() # Make socket transport = TSocket.TSocket('alpha.cs.binghamton.edu', 9000) # 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 = FileStore.Client(protocol) # Connect! transport.open() file_obj.meta.version = file_obj.meta.version + 1 file_obj.content = "New Test String" client.writeFile(file_obj) try: # Make socket transport = TSocket.TSocket('alpha.cs.binghamton.edu', 9000) # 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 = FileStore.Client(protocol) # Connect! transport.open() res = client.readFile("book.txt", "Brad") assert res.content == "New Test String" assert res.meta.version == 1 print "File Overwrite Successful" except SystemException: print "Success: Incorrect owner" transport.close()