Skip to content
Snippets Groups Projects
Messages.py 1.34 KiB
Newer Older
Abe Zukor's avatar
Abe Zukor committed

azukor2's avatar
azukor2 committed
from __future__ import annotations
Abe Zukor's avatar
Abe Zukor committed
from dataclasses import dataclass
from Transaction import Transaction
azukor2's avatar
azukor2 committed
from typing import TYPE_CHECKING
from uuid import uuid4

if TYPE_CHECKING:
    from TransactionDeliverer import TransactionDeliverer
Abe Zukor's avatar
Abe Zukor committed

@dataclass
class Message:
    sender: str
    receiver: str

azukor2's avatar
azukor2 committed
    def receive_action(self):
        raise NotImplementedError("This is an abstract method that should never be called")

Abe Zukor's avatar
Abe Zukor committed
@dataclass
class TransactionMessage(Message):
    transaction: Transaction
azukor2's avatar
azukor2 committed
    transaction_ID: str
Abe Zukor's avatar
Abe Zukor committed
    deliverable: bool = False
azukor2's avatar
azukor2 committed
    message_id: str = None
    
    def receive_action(self, td: TransactionDeliverer):
        td.process_transaction_message(self)

    def __post_init__(self):
        self.message_id = str(uuid4())
Abe Zukor's avatar
Abe Zukor committed

@dataclass
class ProposedPriorityMessage(Message):
    transaction_ID: str
    proposed_priority: int
azukor2's avatar
azukor2 committed
    message_id: str = None

    def receive_action(self, td: TransactionDeliverer):
        td.process_proposed_priority_message(self)
    
    def __post_init__(self):
        self.message_id = str(uuid4())
Abe Zukor's avatar
Abe Zukor committed

@dataclass
class AgreedPriorityMessage(Message):
    transaction_ID: str
azukor2's avatar
azukor2 committed
    agreed_priority: int
    message_id: str = None

    def receive_action(self, td: TransactionDeliverer):
        td.process_agreed_priority_message(self)
    
    def __post_init__(self):
        self.message_id = str(uuid4())