def main(): transport = TSocket.TSocket(sys.argv[1], int(sys.argv[2])) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = FileStore.Client(protocol) transport.open() wFile = RFile() wFileMeta = RFileMetadata() wFileMeta.filename = "sample.txt" sha256file = hashlib.sha256(wFileMeta.filename.encode()) wFileMeta.contentHash = sha256file.hexdigest() wFile.meta = wFileMeta wFile.content = "what now?" #print('dddddddddddddddd : ',sha256file.hexdigest() ) client.writeFile(wFile) rFile = RFile() rFile = client.readFile('aaa.txt') print('sha256: ', rFile.meta.contentHash) print('content: ', rFile.content) nodeID = NodeID() #nodeID = client.getNodeSucc() #keey = '69c9b6900f19eeddc1c9c99928410ddf045adb877f565487546c9cf05c9e2fd2' #nodeID = client.findPred(sha256file.hexdigest()) #print('node', nodeID) transport.close()
def writeFile(self, rFile): self.curr_node = getCurrNode() node = self.findSucc(rFile.meta.contentHash) if node.port == self.node.port and node.ip == self.node.ip: if not rFile in rfile_list: temp = RFile() tempMeta = RFileMetadata() tempMeta.filename = rFile.meta.filename tempMeta.version = 0 tempMeta.contentHash = rFile.meta.contentHash temp.content = rFile.content temp.meta = tempMeta rfile_list.append(temp) else: for i in rfile_list: if i.meta.filename == rFile.meta.filename: ver = i.meta.version rfile_list.remove(i) break temp = RFile() tempMeta = RFileMetadata() tempMeta.filename = rFile.meta.filename tempMeta.version = ver + 1 tempMeta.contentHash = rFile.meta.contentHash temp.content = rFile.content temp.meta = tempMeta rfile_list.append(temp) else: s = SystemException() s.message = "Key not associated" raise s
def main(): transport = TSocket.TSocket(sys.argv[1], int(sys.argv[2])) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = FileStore.Client(protocol) transport.open() ## writeFile rfile = RFile() rfileMetadata = RFileMetadata( '', 0, '' ) # NOTE: structure needs to be initialised with default values. otherwise, nonetype error. rfileMetadata.filename = sys.argv[3] rfile.meta = rfileMetadata rfile.content = 'Winter Is Coming. Be Prepared.' print() try: client.writeFile(rfile) print("writeFile() called..") print() except SystemException as e: print(e) transport.close()
def main(): # Make socket transport = TSocket.TSocket(sys.argv[1], int(sys.argv[2])) # 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() # writing to file - need object of RFileMetadata and RFile rFileMetadata = RFileMetadata() rFileMetadata.filename = 'testdp.txt' rFile = RFile() rFile.meta = rFileMetadata rFile.content = "Hi I am client" client.writeFile(rFile) # read file rFile2 = client.readFile("testdp.txt") print("\nOn Client Side received \nFile -- " + rFile2.meta.filename + "\nVersion -- " + str(rFile2.meta.version) + "\nContent -- " + str(rFile2.content) + "\nContentHash -- " + str(rFile2.meta.contentHash) + "\n") # Close! transport.close()
def test_writeFile(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() owner_node = list_clients[0][0].findSucc(rFile_a.meta.contentHash) print('File hash: {}'.format(rFile_a.meta.contentHash)) print('Owner node:') print(owner_node) for client in list_clients: print('Client hash:') print(client[2]) if owner_node.id == client[2]: client[0].writeFile(rFile_a) else: try: client[0].writeFile(rFile_a) assert False except SystemException as e: print(e) 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() owner_node = list_clients[0][0].findSucc(rFile_b.meta.contentHash) print('File hash: {}'.format(rFile_b.meta.contentHash)) print('Owner node:') print(owner_node) for client in list_clients: print('Client hash:') print(client[2]) if owner_node.id == client[2]: client[0].writeFile(rFile_b) else: try: client[0].writeFile(rFile_b) assert False except SystemException as e: print(e)
def _test_write_file(self): # Create rfile to write at server side rfile_meta_data = RFileMetadata() rfile = RFile() rfile.meta = rfile_meta_data rfile.meta.filename = "file1.txt" rfile.content = "Nitesh_File_Content" self.client_filestore.writeFile(rfile)
def _test_write_file(self): # Create rfile to write at server side rfile_meta_data = RFileMetadata() rfile = RFile() rfile.meta = rfile_meta_data # Write first file self._createFileStoreClient(sys.argv[1], int(sys.argv[2])) rfile.meta.filename = "File.txt" rfile.content = "This is File_Content." rfile_write_hash_id = hashlib.sha256( rfile.meta.filename.encode(ENCODE_TYPE_UTF_8)).hexdigest() succ_node_id = self.client_filestore.findSucc(rfile_write_hash_id) if succ_node_id.id != self.user_id: self.client_transport.close() self._createFileStoreClient(succ_node_id.ip, succ_node_id.port) self.client_filestore.writeFile(rfile) self.client_transport.close() else: self.client_filestore.writeFile(rfile) self.client_transport.close() # Write second file self._createFileStoreClient(sys.argv[1], int(sys.argv[2])) rfile.meta.filename = "Test_File.txt" rfile.content = "This is Test_File_Content." rfile_write_hash_id = hashlib.sha256( rfile.meta.filename.encode(ENCODE_TYPE_UTF_8)).hexdigest() succ_node_id = self.client_filestore.findSucc(rfile_write_hash_id) if succ_node_id.id != self.user_id: self.client_transport.close() self._createFileStoreClient(succ_node_id.ip, succ_node_id.port) self.client_filestore.writeFile(rfile) self.client_transport.close() else: self.client_filestore.writeFile(rfile) self.client_transport.close() # Write third file self._createFileStoreClient(sys.argv[1], int(sys.argv[2])) rfile.meta.filename = "Nitesh_File.txt" rfile.content = "This is Nitesh_File_Content." rfile_write_hash_id = hashlib.sha256( rfile.meta.filename.encode(ENCODE_TYPE_UTF_8)).hexdigest() succ_node_id = self.client_filestore.findSucc(rfile_write_hash_id) if succ_node_id.id != self.user_id: self.client_transport.close() self._createFileStoreClient(succ_node_id.ip, succ_node_id.port) self.client_filestore.writeFile(rfile) self.client_transport.close() else: self.client_filestore.writeFile(rfile) self.client_transport.close()
def readFile(self, filename, owner): logger.info("Request to Read file %s by owner %s" % (filename, owner)) self.curr_node = getCurrentNode() node = self.findSucc(sha256(owner + ":" + filename).hexdigest()) if node.port == self.node.port and node.ip == self.node.ip: if os.path.isfile(os.getcwd() + "/" + filename): with open(filename, 'r') as f: data = f.readlines() if str(owner) == str(data[2].strip().split('=')[1]): rf_metadata = RFileMetadata() rf_metadata.filename = data[0].strip().split('=')[1] rf_metadata.version = int( data[1].strip().split('=')[1]) rf_metadata.owner = data[2].strip().split('=')[1] rf_metadata.contentHash = data[3].strip().split('=')[1] r_file = RFile() r_file.meta = rf_metadata r_file.content = data[4].strip().split('=')[1] return r_file else: logger.error( "File %s does not exist for the given owner %s" % (filename, owner)) raise system_exception( "File does not exist with the given owner name") else: logger.error("No file exist with the name %s" % filename) raise system_exception("No file exist with the name%s" % filename) else: logger.error("Key is not associated with the node") raise system_exception("Key is not associated with the node")
def createFile(self, filename, content): """ Parameters: - filename - content """ if DEBUG: print("Inside createFile ...") rFile = RFile() rFile_meta = RFileMetadata("", 0) rFile.meta = rFile_meta file_key = calculate_hash(filename) if file_key in self.file_id: if DEBUG: print("File {0} exits ...".format(filename)) # write to the file and pass to func caller with open(filename, "w") as fp: fp.write(content) # store info about file in memory rFile_meta = self.file_id[file_key] rFile_meta.version += 1 rFile.content = content rFile.meta = rFile_meta else: if DEBUG: print("File {0} does't exits ...".format(filename)) # create the file and pass to func caller with open(filename, "w") as fp: fp.write(content) # store info about file in memory rFile_meta.filename = filename rFile.content = content rFile.meta = rFile_meta # only keep meta info of file in dict self.file_id[file_key] = rFile.meta return 0
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 readFile(self, filename_input): # Find the SHA256 hash value for input rfile rfile_read_hash_id = hashlib.sha256( filename_input.encode(ENCODE_TYPE_UTF_8)).hexdigest() # Find successor of input file key identifier key_succ_node_id = self.findSucc(rfile_read_hash_id) if DEBUG_FLAG: print("Inside Read ==> Input filename =============== %s" % filename_input) print("Inside Read ==> Key Identifier of Input File = %s" % rfile_read_hash_id) print("Inside Read ==> Successor of Key Identifier == %s" % key_succ_node_id.id) print("Inside Read ==> 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 for File Not Found Exception if rfile_read_hash_id in ChordServerFileStoreHandler.file_system_repository_dict: # Local variables rfile_read_meta = RFileMetadata() rfile_read = RFile() rfile_read.meta = rfile_read_meta existing_rfile = ChordServerFileStoreHandler.file_system_repository_dict[ rfile_read_hash_id] rfile_read.meta = existing_rfile.meta rfile_read.content = existing_rfile.content return rfile_read else: readFile_system_ex = SystemException() readFile_system_ex.message = ERROR_READ_FILE_NOT_FOUND + \ self.current_node_id.ip + COLON_STRING + \ str(self.current_node_id.port) raise readFile_system_ex else: readFile_system_ex = SystemException() readFile_system_ex.message = ERROR_READ_SYSTEM_EXCP_MSG + \ self.current_node_id.ip + COLON_STRING + \ str(self.current_node_id.port) raise readFile_system_ex
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 writeFile(self, rFile): if not os.path.isdir(self.serverDir): raise retException("Directory " + self.serverDir + " does not exist") print('%i: writeFile()' % port) #print('rFile', rFile) filename = rFile.meta.filename # Check if this server is the file's successor h = getHash(filename) s = self.findSucc(h) if s.id != nodeID.id: print('%i: The successor of %s should be %i, not %i' % (nodeID.port, filename, s.port, nodeID.port)) raise retException("The successor of " + filename + " is not node " + str(nodeID.port) + ". Write failed.") # Add or update this file to the RFiles dictionary if filename not in self.files.keys(): print('%i: %s was not found. Creating.' % (nodeID.port, rFile.meta.filename)) self.files[filename] = RFile() f = self.files[filename] f.content = rFile.content f.meta = RFileMetadata() f.meta.filename = filename f.meta.version = 0 f.meta.contentHash = getHash(rFile.content) else: f = self.files[filename] f.content = rFile.content # overwrite content f.meta.version += 1 f.meta.contentHash = getHash(rFile.content) # update content hash path = self.serverDir + '/' + filename print('path=%s' % path) try: f = open(path, 'w') f.write(rFile.content) f.close() except: raise retException("Failed to open " + filename) print('%i: Wrote to file %s' % (nodeID.port, filename))
def incorrectOwnerTest(host, port_list): if len(port_list) < 2: return # Make socket transport = TSocket.TSocket(host, port_list[0]) # 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 client.writeFile(file_obj) transport.close() # Make socket transport = TSocket.TSocket(host, port_list[1]) # 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", "Drake") except SystemException: print("Success: Incorrect owner") transport.close()
def writeFile(self, rFile): """ Parameters: - rFile """ # print ("\n\nwriteFile({})".format(rFile)) fileSHA = decode_sha256(rFile.meta.owner + ":" + rFile.meta.filename) fileServer = self.findSucc(key=fileSHA) # print ("fileServer = {} ".format(fileServer.port)) if self.nodeId == fileServer: self.rFilesLock.acquire() print("Writing to this server....") if rFile.meta.filename in self.rFiles: existingFile = self.rFiles[rFile.meta.filename] existingSHA = decode_sha256(existingFile.meta.owner + ":" + existingFile.meta.filename) if fileSHA == existingSHA: # Edit existingFile.content = rFile.content existingFile.meta.version = existingFile.meta.version + 1 existingFile.meta.contentHash = decode_sha256( rFile.content) else: self.rFilesLock.release() raise SystemException( message='Write Failed: File name already exists ') else: # create meta = RFileMetadata(filename=rFile.meta.filename, version=0, owner=rFile.meta.owner, contentHash=decode_sha256(rFile.content)) self.rFiles[rFile.meta.filename] = RFile(meta=meta, content=rFile.content) self.rFilesLock.release() else: raise SystemException( message='Write Failed: Server does not own the file')
def testReadAfterWriteError(): # 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 = "Knowledge Bitch!" 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', 9001) # 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 main(): # Make socket transport = TSocket.TSocket(sys.argv[1], int(sys.argv[2])) # 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 = RFile() file_obj.meta = RFileMetadata() file_obj.string = "please work, i dont want to do this anymore" file_obj.meta.filename = "sample.txt" file_obj.meta.contentHash = hashlib.sha256( file_obj.meta.filename).hexdigest() print("hash value of the file") print(hashlib.sha256(file_obj.meta.filename).hexdigest()) print("Writing sample.txt to a server") client.writeFile(file_obj) print("reading sample.txt") node = client.readFile("sample.txt") print(node) print("trying to read a file that doesn't exist") node = client.readFile("not_found.txt") print(node) # Close! transport.close()
def incorrectOwnerTest(): # 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() try: res = client.readFile("book.txt", "Drake") except SystemException: print "Success: Incorrect owner" transport.close()
def readFile(self, filename, owner): key = hashlib.sha256(owner + ":" + filename).hexdigest() node = self.findSucc(key) if not node.id == self.NodeID: raise SystemException("Write File Error - Server does not own fil`s id") found = False for my_file in self.files: if filename == my_file.meta.filename and owner == my_file.meta.owner: found = True f = RFile() m = RFileMetadata() m.filename = filename m.version = my_file.meta.version m.owner = owner m.contentHash = my_file.meta.contentHash f.meta = m f.content = my_file.content return f if not found: raise SystemException("Read File Error - File not found")
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 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 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
"Test_Find_Pred - No Exception on empty finger table") except SystemException as err: 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:
def main(): ip = sys.argv[1] port = sys.argv[2] # Make socket transport = TSocket.TSocket(ip, int(port, 10)) # 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() # # node = client.findSucc('1d7e6801e8f1784f36b8ac4dc996047a8a3975af6f30857193415ad5007aa3ef') try: meta = RFileMetadata( filename='abc.txt', version=1, owner='sonali', contentHash=decode_sha256('file .....................')) rFile = RFile(meta=meta, content='file .....................') client.writeFile(rFile) except Thrift.TException as tx: print('%s' % tx.message) try: meta = RFileMetadata( filename='abc.txt', version=1, owner='sonali W', contentHash=decode_sha256('file .....................')) rFile = RFile(meta=meta, content='file .....................') client.writeFile(rFile) except Thrift.TException as tx: print('%s' % tx.message) try: meta = RFileMetadata( filename='abc.txt', version=1, owner='sonali P', contentHash=decode_sha256('file .....................')) rFile = RFile(meta=meta, content='file .....................') client.writeFile(rFile) except Thrift.TException as tx: print('%s' % tx.message) try: meta = RFileMetadata( filename='abc.txt', version=1, owner='sonali p W', contentHash=decode_sha256('file .....................')) rFile = RFile(meta=meta, content='file .....................') client.writeFile(rFile) except Thrift.TException as tx: print('%s' % tx.message) try: rfile = client.readFile(filename='abc.txt', owner='sonali') print(rfile) except Thrift.TException as tx: print('%s' % tx.message) try: client.readFile(filename='abc.txt', owner='sonali W') print(rfile) except Thrift.TException as tx: print('%s' % tx.message) try: client.readFile(filename='abc.txt', owner='sonali P') print(rfile) except Thrift.TException as tx: print('%s' % tx.message) try: client.readFile(filename='abc.txt', owner='sonali p W') print(rfile) except Thrift.TException as tx: print('%s' % tx.message) # # # print (node) # Close! transport.close()
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
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()