def findSucc(self, key):
     if not self.node_list:
         s = SystemException()
         s.message = "Finger Table does not exist"
         raise s
     self.curr_node = getCurrNode()
     self.curr_node = self.findPred(key)
     if self.node.ip == self.curr_node.ip and self.node.port == self.curr_node.port:
         return self.getNodeSucc()
     transport = TSocket.TSocket(self.curr_node.ip, self.curr_node.port)
     transport = TTransport.TBufferedTransport(transport)
     protocol = TBinaryProtocol.TBinaryProtocol(transport)
     tNode = FileStore.Client(protocol)
     transport.open()
     targetNode = tNode.getNodeSucc()
     transport.close()
     return targetNode
    def setFingertable(self, node_list_input):

        # Raise exception in case of empty input node list, otherwise set finger table node list
        if len(node_list_input) != 0:
            self.finger_table_node_list = node_list_input
        else:
            setFingertable_system_ex = SystemException()
            setFingertable_system_ex.message = ERROR_FINGERTABLE_SYSTEM_EXCP_MSG + \
                                               self.current_node_id.ip + COLON_STRING + \
                                               str(self.current_node_id.port)
            raise setFingertable_system_ex

        print(SUCCESS_SET_FINGER_TABLE_MSG)

        if DEBUG_FLAG:
            for node_id in self.finger_table_node_list:
                print("Node Info --> " + node_id.id + " : " + node_id.ip +
                      " : " + str(node_id.port))
            print(
                "------------------------------------------------------------------------------------------\n"
            )
예제 #3
0
 def findPred(self, key):
     print('findPred')
     next_node = None
     key_num = int(key, 16)
     if self.finger_table:
         print('Checking if current node is predeccesor')
         if is_between(int(self.node_id.id, 16),
                       int(self.finger_table[0].id, 16), key_num):
             # Means current node is predecessor
             # Base case
             return self.node_id
         found_predecessor = False
         print('Checking if key is between nodes in fingertable')
         for node in self.finger_table:
             if next_node is not None and is_between(int(next_node.id, 16), \
                                                     int(node.id, 16), key_num):
                 found_predecessor = True
                 break
             next_node = node
         if found_predecessor is False:
             # Node was not in between any two nodes in finger table. Need to check last node then
             print('Using last node in finger table')
             next_node = self.finger_table[-1]
         assert next_node.id != self.node_id.id
         print('Next node {}:{}'.format(next_node.ip, next_node.port))
         transport = TSocket.TSocket(next_node.ip, next_node.port)
         transport = TTransport.TBufferedTransport(transport)
         protocol = TBinaryProtocol.TBinaryProtocol(transport)
         client = FileStore.Client(protocol)
         transport.open()
         predecessor_node = client.findPred(key)
         print('Node returned fron next node:')
         print(predecessor_node)
         transport.close()
         return predecessor_node
     else:
         exception = SystemException()
         exception.message = 'Node {}:{} does not have a fingertable!'.format(self.node_id.ip, \
                                                                              self.node_id.port)
         raise exception
예제 #4
0
    def writeFile(self, rFileNodeIn):
        hash_object = hashlib.sha256(
            (rFileNodeIn.meta.filename).encode()).hexdigest()
        # print("\nWrite - " + rFileNodeIn.meta.filename + ", hash code = " + hash_object)
        fileServerNodeSucc = self.findSucc(hash_object)
        # print("\nWrite - fileServerNodeSucc = " + fileServerNodeSucc.id)

        if (fileServerNodeSucc.id != self.serverNode.id):
            systemException = SystemException()
            systemException.message = ("File not owned by Server " +
                                       self.serverNode.ip + ":" +
                                       str(self.serverNode.port) + "\n")
            raise systemException

        if hash_object not in self.file_info:
            self.file_info[hash_object] = (
                rFileNodeIn.meta.filename, 0, rFileNodeIn.content,
                hashlib.sha256((rFileNodeIn.content).encode()).hexdigest())
        else:
            version = self.file_info[hash_object][1]
            self.file_info[hash_object] = (
                rFileNodeIn.meta.filename, version + 1, rFileNodeIn.content,
                hashlib.sha256((rFileNodeIn.content).encode()).hexdigest())
    def findPred(self, key):
        cNode = NodeID()
        sha256id = hashlib.sha256(self.id.encode())
        cNode.id = sha256id.hexdigest()
        cNode.ip = self.ip
        cNode.port = self.port
        tempNode = NodeID()

        if not self.fTable:
            z = SystemException()
            z.message = "no fTable"
            raise z

        tempNode = self.fTable[0]
        x = self.checkInRange(key, cNode.id, tempNode.id)
        #print('x:', x)
        while not x:
            for i in range(len(self.fTable) - 1, 0, -1):
                temp = NodeID()
                temp = self.fTable[i]
                y = self.checkInRange(temp.id, cNode.id, key)
                #print('temp.id: ', temp.id, '\n', 'cNode.id:', cNode.id, '\n', 'key:', key )
                #print('y',y)
                if y:
                    ttransport = TSocket.TSocket(temp.ip, temp.port)
                    #ttransport = TTransport.TBufferedTransport(ttransport)
                    ttransport.open()
                    pprotocol = TBinaryProtocol.TBinaryProtocol(ttransport)
                    client = FileStore.Client(pprotocol)
                    #ttransport.open()
                    sNode = NodeID()
                    sNode = client.findPred(key)

                    ttransport.close()
                    return sNode
            break
        return cNode
    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 getNodeSucc(self):
     if not self.fTable:
         z = SystemException()
         z.message = "no fTable"
         raise z
     return self.fTable[0]
예제 #8
0
def retException(msg):
    se = SystemException()
    se.message = msg
    print('%s' % msg)
    return se
    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
예제 #10
0
def system_exception(msg):
    sx = SystemException()
    sx.message = msg
    return sx