Skip to content

Commit

Permalink
feat(roles): requester role
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianLusina committed Mar 14, 2023
1 parent 12c8a0f commit e765ce7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions konsensus/models/roles/requester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import Callable
from itertools import count
from . import Role
from ..node import Node
from konsensus.entities.messages_types import Invoke
from konsensus.constants import INVOKE_RETRANSMIT


class Requester(Role):
"""
The requester role manages a request to the distributed state machine.
The role class simply sends Invoke messages to the local replica until it receives a corresponding Invoked.
"""

client_ids = count(start=100000)

def __init__(self, node: Node, n, callback: Callable) -> None:
super().__init__(node)
self.client_id = self.client_ids.next()
self.n = n
self.output = None
self.callback = callback

def start(self):
self.node.send([self.node.address], Invoke(
caller=self.node.address, client_id=self.client_id, input_value=self.n))
self.invoke_timer = self.set_timer(INVOKE_RETRANSMIT, self.start)

def do_invoked(self, sender, client_id, output):
if client_id != self.client_id:
return
self.logger.debug(f"received output {output}")
self.invoke_timer.cancel()
self.callback(output)
self.stop()
2 changes: 1 addition & 1 deletion konsensus/models/roles/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from . import Role
from ..node import Node
from .bootstrap import Bootstrap
from konsensus.entities.messages_types import Join, Welcome
from konsensus.entities.messages_types import Welcome
from konsensus.constants import JOIN_RETRANSMIT


Expand Down

0 comments on commit e765ce7

Please sign in to comment.