Exemple #1
0
    def KVServerRegisterMaster(self, sockObj):
        reqmsg = KVMessage()
        reqmsg.msgType = ErrorCodes.KVMessageType["REGISTER"]
        reqmsg.key = self.hostname
        reqmsg.value = str(self.port)
        reqmsg.KVMessageSend(sockObj)

        respmsg = KVMessage()
        respmsg.KVMessageParse(sockObj)

        if (respmsg.message == None
                or respmsg.message != ErrorCodes.Successmsg):
            return -1
        else:
            self.state = ErrorCodes.TPCStates["TPC_READY"]
            return 0
Exemple #2
0
    def TPCMasterHandle(self, sockObj):
        reqmsg = KVMessage()
        respmsg = KVMessage()
        reqmsg.KVMessageParse(sockObj)
        respmsg.msgType = ErrorCodes.KVMessageType["RESP"]

        if reqmsg.key != None or reqmsg.key == "":
            respmsg.key = reqmsg.key

        #Copy the message?
        if (reqmsg == None or reqmsg.key == None):
            respmsg.message = ErrorCodes.getErrorMessage(
                ErrorCodes.InvalidRequest)
        elif (reqmsg.msgType == ErrorCodes.KVMessageType["INFO"]):
            respmsg = self.TPCMasterInfo(reqmsg)
        elif (reqmsg.msgType == ErrorCodes.KVMessageType["REGISTER"]):
            respmsg = self.TPCMasterRegister(reqmsg)
        elif (reqmsg.msgType == ErrorCodes.KVMessageType["GETREQ"]):
            respmsg = self.TPCMasterHandleGet(reqmsg)
        else:

            respmsg = self.TPCMasterHandleTPC(reqmsg)

        respmsg.KVMessageSend(sockObj)
Exemple #3
0
    def TPCMasterHandleGet(self, reqmsg):

        respmsg = KVMessage()
        respmsg.msgType = ErrorCodes.KVMessageType["RESP"]

        if (reqmsg.value == None or reqmsg.key == None):
            respmsg.message = ErrorCodes.getErrorMessage(
                ErrorCodes.InvalidRequest)
            # print("here in error")
            return respmsg

        value = ""
        receivedResponse = KVMessage()
        lock = self.cache.KVCacheGetLock(reqmsg.key)
        lock.acquire()
        cacheGet = self.cache.KVCacheGet(reqmsg.key)
        if (cacheGet[0] == 1):
            lock.release()
            respmsg.key = reqmsg.key
            respmsg.value = cacheGet[1]
            respmsg.msgType = ErrorCodes.KVMessageType["GETRESP"]

        else:
            lock.release()
            slave = self.TPCMasterGetPrimary(reqmsg.key)
            successfulConnection = False

            for i in range(0, self.redundancy):
                ret = connectTo(slave.host, slave.port,
                                TPCMaster.timeoutSeconds)
                fd = ret[1]
                if (ret[0] < 0):
                    slave = self.tpcMasterGetSuccessor(slave)
                else:

                    successfulConnection = True
                    break

            if (successfulConnection == False):

                respmsg.msgType = ErrorCodes.KVMessageType["RESP"]
                respmsg.message = ErrorCodes.getErrorMessage(-1)
                return respmsg

            respmsg.msgType = ErrorCodes.KVMessageType["GETREQ"]
            respmsg.key = reqmsg.key
            respmsg.value = ""
            respmsg.KVMessageSend(fd)
            receivedResponse.KVMessageParse(fd)
            fd.close()

            if (receivedResponse.msgType == None):
                respmsg.msgType = ErrorCodes.KVMessageType["RESP"]
                respmsg.message = ErrorCodes.getErrorMessage(-1)
                return respmsg

            if (receivedResponse.msgType !=
                    ErrorCodes.KVMessageType["GETRESP"]):
                respmsg.msgType = ErrorCodes.KVMessageType["RESP"]
                respmsg.message = receivedResponse.message
            else:
                respmsg.key = receivedResponse.key
                respmsg.value = receivedResponse.value
                respmsg.msgType = receivedResponse.msgType
                respmsg.message = ErrorCodes.Successmsg
                lock.acquire()
                self.cache.KVCachePut(respmsg.key, respmsg.value)
                lock.release()

        return respmsg