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()

    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()
Esempio n. 3
0
    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")
Esempio n. 4
0
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()
Esempio n. 5
0
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()
Esempio n. 6
0
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 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)
Esempio n. 10
0
    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
Esempio n. 11
0
    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")
Esempio n. 12
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
Esempio n. 14
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()
Esempio n. 15
0
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()