-
Notifications
You must be signed in to change notification settings - Fork 1
/
MessageClass.py
78 lines (64 loc) · 1.69 KB
/
MessageClass.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from dataclasses import dataclass
from collections import deque
from multiprocessing import Lock
from threading import Event
# Message Structure
## Connect Request: (CONNECT_REQEUST, Node ID, node.message_queue)
## Connect Reply: (CONNECT_REPLY, Node ID, blockchain) # no need to send message queue
## New Incoming TXN: (NEW_TXN, Transaction obj) # for now this is not coming from any node.
## Request Blockchain: (REQ_BlockChain, NodeID)
## Reply to BC REQ: (REPLY_BlockChain, NodeID, Blockchain)
## Broadcast newly mined Block: (NEW_BLOCK, NodeID, Block)
@dataclass
class Message:
id: str
args: []
class Queue_Event_Lock:
def __init__(self):
self.__q = deque()
self.__e = Event()
self.__lock = Lock()
def acquire(self):
return self.__lock.acquire()
def release(self):
return self.__lock.release()
def pop_message(self):
return self.__q.popleft()
def add_message(self, m):
self.__q.append(m)
def empty(self):
if len(self.__q) == 0:
return True
return False
def isSet(self):
return self.__e.isSet()
def wait(self):
return self.__e.wait()
def set(self):
return self.__e.set()
def clear(self):
return self.__e.clear()
def __repr__(self):
return str(self.q)
# class Message_Queue:
# def __init__(self):
# self.q = deque()
# def pop_message(self):
# return self.q.popleft()
# def add_message(self, m):
# self.q.append(m)
# def empty(self):
# if len(self.q) == 0:
# return True
# return False
# def __repr__(self):
# return str(self.q)
# class Manager_Message_Queue:
# def __init__(self, Q):
# self.q = Q
# def pop_message(self):
# return self.q.get()
# def add_message(self, m):
# self.q.put(m)
# def empty(self):
# return self.q.empty()