def receive(self, timeout): ''' receive the message for the current queue, return the message to caller. note that the caller should deep copy the message to new place, since the message space in the queue will be override currently no protection to protect the message will not be override before the data was copied to new place, caller should do it when it get the message immediately. later will consider to let caller provide a place, and the receive function in charge to do the copy in rlock protection after receive the message, this function will split the sender name and message body, return a tuple data whose first item is the sender's name, and second one is message body ''' self.rlock.acquire() arr = self.array if self.recv_pos.value == -1: self.recv_pos.value = 0 start_ts = time.time() * 1000 while self.send_pos.value == -1 or \ self.recv_pos.value == self.send_pos.value: self.rlock.release() if timeout == 0: return None elif timeout < 0: time.sleep(0.01) else: now_ts = time.time() * 1000 delta = now_ts - start_ts if delta > timeout: return None time.sleep(0.001) self.rlock.acquire() msg = arr[self.recv_pos.value].value if self.recv_pos.value + 1 >= MyDef.QUEUE_SIZE: self.recv_pos.value = 0 else: self.recv_pos.value += 1 self.rlock.release() rcvmsg = MyMessage() if len(msg) < rcvmsg.header.fixed_header_len: print "message length should be more than %s"%\ rcvmsg.header.fixed_header_len return None ret = rcvmsg.decode_header(msg[:rcvmsg.header.fixed_header_len]) if ret is False: print "fail to decode message" return None rcvmsg.set_body(msg[rcvmsg.header.fixed_header_len:]) return rcvmsg
def __init__(self, msgtype=MsgType.NORMAL_TEXT): MyMessage.__init__(self, evtype=EVENTYPE.TIMEREXPIRE, msgtype=msgtype)
def __init__(self): MyMessage.__init__(self, evtype=EVENTYPE.TIMEREXPIRE, msgtype=MsgTypeBase.TIMEREXP) self.tag = 0