(0 . 0)(1 . 49)
1427 import time
1428
1429 from lib.broadcast import Broadcast
1430 from lib.commands import BROADCAST, DIRECT
1431 from lib.direct import Direct
1432 from lib.message import EMPTY_CHAIN
1433
1434
1435 class LongBuffer(object):
1436 def __init__(self, state):
1437 self.state = state
1438 self.buffer = {}
1439
1440 def exhume(self, message_hash):
1441 message = self.buffer.get(message_hash)
1442 if message:
1443 return message
1444
1445 command, message_bytes = self.state.get_message(message_hash)
1446 if message_bytes:
1447 if command == DIRECT:
1448 return Direct({message_bytes: message_bytes}, self.state)
1449 elif command == BROADCAST:
1450 return Broadcast({'message_bytes': message_bytes}, self.state)
1451
1452 def expunge_expired(self):
1453 for message in self.buffer.values():
1454 if message.timestamp < time.time() - 3600:
1455 del self.buffer[message.message_hash]
1456
1457 def intern(self, message):
1458 # somehow we are trying to intern a message we've already got
1459 if self.has(message.message_hash):
1460 return
1461
1462 self.expunge_expired()
1463 self.buffer[message.message_hash] = message
1464 self.state.log_message(message)
1465
1466 def has(self, message_hash):
1467 if EMPTY_CHAIN == message_hash:
1468 return True
1469
1470 if self.buffer.get(message_hash) is None:
1471 if self.state.log_has_message(message_hash):
1472 return True
1473 return False
1474 else:
1475 return True