9982-getdata 1 import time
9982-getdata 2
9982-getdata 3 from lib.broadcast import Broadcast
9982-getdata 4 from lib.commands import BROADCAST, DIRECT
9982-getdata 5 from lib.direct import Direct
9982-getdata 6 from lib.message import EMPTY_CHAIN
9982-getdata 7
9982-getdata 8
9982-getdata 9 class LongBuffer(object):
9982-getdata 10 def __init__(self, state):
9982-getdata 11 self.state = state
9982-getdata 12 self.buffer = {}
9982-getdata 13
9982-getdata 14 def exhume(self, message_hash):
9982-getdata 15 message = self.buffer.get(message_hash)
9982-getdata 16 if message:
9982-getdata 17 return message
9982-getdata 18
9982-getdata 19 command, message_bytes = self.state.get_message(message_hash)
9982-getdata 20 if message_bytes:
9982-getdata 21 if command == DIRECT:
9982-getdata 22 return Direct({message_bytes: message_bytes}, self.state)
9982-getdata 23 elif command == BROADCAST:
9982-getdata 24 return Broadcast({'message_bytes': message_bytes}, self.state)
9982-getdata 25
9982-getdata 26 def expunge_expired(self):
9982-getdata 27 for message in self.buffer.values():
9982-getdata 28 if message.timestamp < time.time() - 3600:
9982-getdata 29 del self.buffer[message.message_hash]
9982-getdata 30
9982-getdata 31 def intern(self, message):
9982-getdata 32
9982-getdata 33 if self.has(message.message_hash):
9982-getdata 34 return
9982-getdata 35
9982-getdata 36 self.expunge_expired()
9982-getdata 37 self.buffer[message.message_hash] = message
9982-getdata 38 self.state.log_message(message)
9982-getdata 39
9982-getdata 40 def has(self, message_hash):
9982-getdata 41 if EMPTY_CHAIN == message_hash:
9982-getdata 42 return True
9982-getdata 43
9982-getdata 44 if self.buffer.get(message_hash) is None:
9982-getdata 45 if self.state.log_has_message(message_hash):
9982-getdata 46 return True
9982-getdata 47 return False
9982-getdata 48 else:
9982-getdata 49 return True