-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathdash_client.py
78 lines (52 loc) · 2.27 KB
/
dash_client.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
# -*- coding: utf-8 -*-
"""
@author: Marcos F. Caetano ([email protected]) 11/03/2020
@description: PyDash Project
Dash client representation. It knows all entities in our
layer model (Player, R2A and Connection_Handler).
There isn't a strong relation among the entities
(they don't know each other). The Dash_client is
responsible to make the communication among them happens.
"""
import importlib
from base.configuration_parser import ConfigurationParser
from base.scheduler import Scheduler
from connection.connection_handler import ConnectionHandler
from player.player import Player
class DashClient:
def __init__(self):
config_parser = ConfigurationParser.get_instance()
r2a_algorithm = str(config_parser.get_parameter('r2a_algorithm'))
self.scheduler = Scheduler()
self.modules = []
# adding modules to manage
self.player = Player(0)
# automatic loading class by the name
r2a_class = getattr(importlib.import_module('r2a.' + r2a_algorithm.lower()), r2a_algorithm)
self.r2a = r2a_class(1)
self.connection_handler = ConnectionHandler(2)
self.modules.append(self.player)
self.modules.append(self.r2a)
self.modules.append(self.connection_handler)
def run_application(self):
self.modules_initialization()
while not self.scheduler.is_empty():
event = self.scheduler.get_event()
self.handle_scheduler_event(event)
self.modules_finalization()
def handle_scheduler_event(self, event):
#checking if the event is inside of the modules position range limits
if event.get_dst() < 0 or event.get_dst() >= len(self.modules):
print(f'It is no possible to route a {event.get_msg()} message from {event.get_src()} to {event.get_dst()} module list position.')
exit(0)
self.modules[event.get_dst()].handle_message(event.get_msg())
def modules_initialization(self):
print('Initialization modules phase.')
for m in self.modules:
super(type(m), m).initialize()
m.initialize()
def modules_finalization(self):
print('Finalization modules phase.')
for m in self.modules:
super(type(m), m).finalization()
m.finalization()