@@ -38,43 +38,97 @@ def __init__(self,
38
38
@return None
39
39
"""
40
40
super ().__init__ ()
41
- # Initializer node hostname.
42
- self .HOSTNAME : str = HOSTNAME
43
- # Initializer port
44
- self .PORT : int = PORT
45
- # Maximum buffer size
46
- self .BUFFER_SIZE :int = 4096
47
- # Graph
48
- self .G : nx .Graph = G
49
- # Number of nodes in the graph
50
- self .N : int = G .number_of_nodes ()
51
- # Available ports, one for each node of the graph
52
- self .ports : list = [65432 + x for x in range (self .N )] # one port for each node
53
- # DNS server holding tuples <node:port>
54
- self .DNS : dict = {node :port for node ,port in zip (G .nodes (), self .ports )}
55
- # path of the client file
56
- self .client : str = client
57
- # Boolean
58
- self .shell : bool = shell
59
- if not log_path :
60
- self .log_path = os .path .join (os .path .split (self .client )[0 ], "logs" )
61
- self .s = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
62
- self .s .bind (("" , self .PORT ))
63
- self .start_listener (self .s , self .message_queue )
64
- self .visualizer_port = None
41
+ self ._HOSTNAME : str = HOSTNAME
42
+ self ._PORT : int = PORT
43
+ self ._G : nx .Graph = G
44
+ self ._ports : list = [65432 + x for x in range (self .number_of_nodes ())] # one port for each node
45
+ self ._DNS : dict = {node :port for node ,port in zip (G .nodes (), self .ports )}
46
+ self ._client : str = client
47
+ self ._shell : bool = shell
48
+
49
+ if not log_path : self ._log_path = os .path .join (os .path .split (self .client )[0 ], "logs" )
50
+ else : self ._log_path = None
51
+
52
+ self ._s = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
53
+ self ._s .bind (("" , self .PORT ))
54
+ self .start_listener (self ._s , self .message_queue )
55
+
56
+ self ._visualizer_port = None
57
+ self ._visualizer = None
65
58
if visualizer :
66
- self .visualizer_port = self .ports [- 1 ]+ 1
67
- self .visualizer = Visualizer (self .visualizer_port , self .G )
59
+ self ._visualizer_port = self .ports [- 1 ]+ 1
60
+ self ._visualizer = Visualizer (self .visualizer_port , self .G )
61
+
68
62
self .initialize_clients ()
69
63
self .setup_clients ()
70
64
65
+ @property
66
+ def HOSTNAME (self ):
67
+ """!Return server hostname."""
68
+ return self ._HOSTNAME
69
+
70
+ @property
71
+ def PORT (self ):
72
+ """!Return server listener port."""
73
+ return self ._PORT
74
+
75
+ @property
76
+ def G (self ):
77
+ """!Return network structure."""
78
+ return self ._G
79
+
80
+ @property
81
+ def ports (self ):
82
+ """!Return ports used by the client nodes."""
83
+ return self ._ports
84
+
85
+ @property
86
+ def DNS (self ):
87
+ """!Return DNS (nodeID : Port)"""
88
+ return self ._DNS
89
+
90
+ @property
91
+ def client (self ):
92
+ """!Return path to client file."""
93
+ return self ._client
94
+
95
+ @property
96
+ def shell (self ):
97
+ """!Return True if each node should get a separate terminal."""
98
+ return self ._shell
99
+
100
+ @property
101
+ def log_path (self ):
102
+ """!Return root path to log files."""
103
+ return self ._log_path
104
+
105
+ @property
106
+ def s (self ):
107
+ """!Return socket used by the server to send messages."""
108
+ return self ._s
109
+
110
+ @property
111
+ def visualizer_port (self ):
112
+ """!Return visualizer port if visualizer is active."""
113
+ return self ._visualizer_port
114
+
115
+ @property
116
+ def visualizer (self ):
117
+ """!Return visualizer if activated."""
118
+ return self ._visualizer
119
+
120
+
71
121
def __str__ (self ):
72
122
"""!Convert node to string."""
73
123
table = PrettyTable ()
74
124
table .field_names = ["Node" , "Port" ]
75
125
for key , val in self .DNS .items ():
76
126
table .add_row ([key , val ])
77
127
return table .__str__ ()
128
+
129
+ def number_of_nodes (self ) -> int :
130
+ """Return number of nodes in the network."""
131
+ return self .G .number_of_nodes ()
78
132
79
133
def initialize_clients (self ):
80
134
"""!Initialize all of the nodes of the graph.
@@ -120,7 +174,7 @@ def wait_for_termination(self):
120
174
message = TerminationMessage .deserialize (data )
121
175
if message .command == Command .END_PROTOCOL :
122
176
EOP_received += 1
123
- if EOP_received == self .N :
177
+ if EOP_received == self .number_of_nodes () :
124
178
print ("Received EOP from all nodes in the network." )
125
179
break
126
180
elif message .command == Command .ERROR :
@@ -141,7 +195,7 @@ def wait_for_number_of_messages(self):
141
195
message = CountMessage .deserialize (data )
142
196
total_count += message .counter
143
197
counts_received += 1
144
- if counts_received == self .N :
198
+ if counts_received == self .number_of_nodes () :
145
199
print ("Received message count from all nodes." )
146
200
print (f"Total number of messages: { total_count } " )
147
201
break
@@ -176,7 +230,7 @@ def setup_clients(self):
176
230
data = self .receive_message ()
177
231
if not data : continue
178
232
ans_message = Message .deserialize (data )
179
- if ans_message .command != "SOP" :
233
+ if ans_message .command != Command . START_PROTOCOL :
180
234
print ("Something went wrong during clients setup." )
181
235
break
182
236
else :
@@ -231,7 +285,8 @@ def send_termination(self):
231
285
for node , port in self .DNS .items ():
232
286
s .sendto (termination_message .serialize (), ("localhost" , port ))
233
287
234
- def start_visualization (self ):
288
+ def start_visualization (self ):
289
+ """!Communicate to the visualizer to start the visualization loop."""
235
290
assert self .visualizer , "Specify visualizer=True in the constructor to use this method."
236
291
state = self .visualizer .start_visualization ()
237
292
if state == VisualizerState .INTERNAL_ERROR :
0 commit comments