def handle_msg(self, command):
        ex = Exchange()
        try:
            ex.ParseFromString(command)
        except Exception as e:
            print 'Exception received ', e
            logging.error(e)
            self.transport.loseConnection()
            return
        exr = Exchange()
        if not self.registered:
            if not ex.HasField('hb'):
                exr.mid = ex.mid
                ex.sm.sv = StatusMsg.FAIL
                resp = exr.SerializeToString()
                outlen = len(resp)
                outbuf = pack('i', outlen)
                self.sendData(outbuf + resp)
                return
            else:
                self.registered = True
                hb = ex.hb
                self.clientId = hb.clientId
                statusdata = utility.get_StatusMsg_bin(ex.mid, StatusMsg.SUCCESS)
                self.sendData(statusdata)
                self.allclients.add_client(self.clientId, self)
        else:
            if ex.HasField('ur'):
                logging.debug('Unregistering client ' + self.clientId)
                self.allclients.unRegisterClient(self.clientId)
                self.transport.loseConnection()
                return
            self.allclients.add_client(self.clientId)
            if ex.HasField('clocks'):
                # details about locks is wanted
                c_locks = ex.clocks
                clientId = c_locks.clientId 
                if not get_client().is_registered(clientId):
                    exr.sm = CLIENT_NOT_REGISTERED
                    resp = exr.SerializeToString()
                    outlen = len(resp)
                    outbuf(pack('i', outlen))
                    self.sendData(outbuf + resp)
                else:
                    clocks = get_lc().getClientLocks(clientId) 
            elif ex.HasField('ld'):
                logging.debug('Getting lock dettails  ' + ex.ld.lockName) 
                ld = get_lc().getLockDetails(ex.ld.lockName)
                lddata = utility.get_lockDetail_resp_msg(ex.mid, ld) 
                self.sendData(lddata)

            elif ex.HasField('lc'):
                lcl = ex.lc 
                logging.debug('Received lock request ' + ' ' +
                               lcl.clientId + ' ' + lcl.cmd.lockId + ' ' +
                               str(lcl.cmd.op.opval))
                eobj = event(common.LOCKOP_TOPIC, ex, str(lcl.cmd.lockId))
                self.ebus.post(eobj)
Пример #2
0
def get_unRegister_msg(msgid, clientId):
    ex = Exchange()
    ex.mid = msgid 
    ur = ex.ur
    ur.clientId = clientId
    out = ex.SerializeToString()
    outlen = len(out)
    outbuf = pack('i', outlen)
    return outbuf + out
Пример #3
0
def get_StatusMsg_bin(msgId, status):
    ex = Exchange()
    ex.mid = msgId
    ex.sm.sv = status
    out = ex.SerializeToString()
    outlen = len(out)
    outbuf = pack('i', outlen)
    
    return outbuf + out
Пример #4
0
def get_lockDetail_msg(msgid, lockName):
    ex = Exchange()
    ex.mid = msgid 
    ld = ex.ld
    ld.sm.sv = StatusMsg.SUCCESS
    ld.lockName = lockName
    out = ex.SerializeToString()
    outlen = len(out)
    outbuf = pack('i', outlen)
    return outbuf + out
Пример #5
0
def unpack_protocol_msg(data):
    datalen = unpack('i', data[0:4])
    command = data[4:]
    ex = Exchange()
    try:
        ex.ParseFromString(command)
    except Exception as e:
        print 'Failure in unpacking data'
        return None
    return ex
 def connectionMade(self):
     self.connected = True
     self.peerhost, self.peerport = self.transport.socket.getpeername()
     # Register the client
     ex = Exchange()
     ex.mid = self.register_msg_id 
     hb = ex.hb
     hb.clientId = self.clientId
     out = ex.SerializeToString()
     outlen = len(out)
     outbuf = pack('i', outlen)
     self.sendData(outbuf + out)
     lc.add_server_connection(self.peerhost, self.peerport, self)
Пример #7
0
def get_lockOp_msg(msgId, clientId, lockName, op):
    ex = Exchange()
    ex.mid = msgId
    lc = ex.lc
    lc.clientId  = clientId
    lc.cmd.op.opval = op
    lc.cmd.lockId = lockName
    lc.cmd.expireTime = 0 
    out = ex.SerializeToString()
    outlen = len(out)
    outbuf = pack('i', outlen)
    
    return outbuf + out
Пример #8
0
def get_lockDetail_resp_msg(msgid, ldin):
    ex = Exchange()
    ex.mid = msgid 
    ld = ex.ld
    ld.sm.sv = ldin.sm.sv
    ld.currentWriter = ldin.currentWriter
    ld.lockName = ldin.lockName
    ld.lockType = ldin.lockType
    ld.currentWriteWaits.extend(ldin.currentWriteWaits)
    ld.currentReaders.extend(ldin.currentReaders)
    out = ex.SerializeToString()
    outlen = len(out)
    outbuf = pack('i', outlen)
    return outbuf + out
Пример #9
0
def unpack_protocol_msg(data):
    datalen = unpack('i', data[0:4])
    command = data[4:]
    ex = Exchange()
    try:
        ex.ParseFromString(command)
    except Exception as e:
        print 'Failure in unpacking data'
        return None
    return ex


def get_hash_index(key, max_index, bitwiseand):
    if max_index <= 0:
        return 0
    return abs((crc32(str(key))) & bitwiseand) % max_index
    

if __name__ == '__main__':
    data = get_unLock_msg(1, 'abcdefgh' , 'MyLock2')
    datalen = unpack('i', data[0:4])
    print datalen
    command = data[4:]
    ex = Exchange()
    try:
        ex.ParseFromString(command)
    except Exception as e:
        print e
    print ex

Пример #10
0
    def handle_msg(self, command):
        ex = Exchange()
        try:
            ex.ParseFromString(command)
        except Exception as e:
            print 'Exception received ', e
            logging.error(e)
            self.transport.loseConnection()
            return
        exr = Exchange()
        if not self.registered:
            if not ex.HasField('hb'):
                exr.mid = ex.mid
                ex.sm.sv = StatusMsg.FAIL
                resp = exr.SerializeToString()
                outlen = len(resp)
                outbuf = pack('i', outlen)
                self.sendData(outbuf + resp)
                return
            else:
                self.registered = True
                hb = ex.hb
                self.clientId = hb.clientId
                statusdata = utility.get_StatusMsg_bin(ex.mid,
                                                       StatusMsg.SUCCESS)
                self.sendData(statusdata)
                self.allclients.add_client(self.clientId, self)
        else:
            if ex.HasField('ur'):
                logging.debug('Unregistering client ' + self.clientId)
                self.allclients.unRegisterClient(self.clientId)
                self.transport.loseConnection()
                return
            self.allclients.add_client(self.clientId)
            if ex.HasField('clocks'):
                # details about locks is wanted
                c_locks = ex.clocks
                clientId = c_locks.clientId
                if not get_client().is_registered(clientId):
                    exr.sm = CLIENT_NOT_REGISTERED
                    resp = exr.SerializeToString()
                    outlen = len(resp)
                    outbuf(pack('i', outlen))
                    self.sendData(outbuf + resp)
                else:
                    clocks = get_lc().getClientLocks(clientId)
            elif ex.HasField('ld'):
                logging.debug('Getting lock dettails  ' + ex.ld.lockName)
                ld = get_lc().getLockDetails(ex.ld.lockName)
                lddata = utility.get_lockDetail_resp_msg(ex.mid, ld)
                self.sendData(lddata)

            elif ex.HasField('lc'):
                lcl = ex.lc
                logging.debug('Received lock request ' + ' ' + lcl.clientId +
                              ' ' + lcl.cmd.lockId + ' ' +
                              str(lcl.cmd.op.opval))
                eobj = event(common.LOCKOP_TOPIC, ex, str(lcl.cmd.lockId))
                self.ebus.post(eobj)