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