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