-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgq.py
44 lines (36 loc) · 1.44 KB
/
msgq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import time
import logging
from block import *
from block_type import *
import random
from config import *
logger = logging.getLogger()
class MessageQueue():
def __init__(self, blockchain, sender, user_id):
self.queue = []
self.blockchain = blockchain
self.sender = sender
self.user_id = user_id
def add_message(self, msg):
self.queue.append(msg)
def consume(self):
while True:
if not self.queue:
time.sleep(EMPTY_QUEUE_TIMEOUT)
elif self.blockchain.proposal_allowed():
msg = self.queue.pop(0)
while True:
last_hash = self.blockchain.peek().hash()
# if self.blockchain.peek().block_type == BlockType.empty:
# last_hash = ""
block = Block(BlockType.message, self.user_id, last_hash, msg)
if self.blockchain.proposal_allowed():
self.sender.send(block)
time.sleep(RESEND_TIMEOUT + (random.random() * RESEND_RANDOM_FACTOR))
if self.blockchain.contains(block.hash()):
break
else:
time.sleep(RESEND_TIMEOUT)
else: # we have a message we want to send but htere is a proposal pending
logger.info("not allowed")
time.sleep(PROPOSAL_PENDING_TIMEOUT)