Skip to content
Snippets Groups Projects
Commit 9c1cac3c authored by NGUYEN Do Duc Anh's avatar NGUYEN Do Duc Anh
Browse files

update show time converge

parent 7f3a5080
No related branches found
No related tags found
No related merge requests found
import socket, struct import socket, struct
import time
import threading import threading
import time
# Configuration # Configuration
LISTEN_PORT = 12346 # Specify the port number to listen on LISTEN_PORT = 12346 # Specify the port number to listen on
node_index_id = 0 node_index_id = 0
map_security_node = {} map_security_node = {}
time_converge = 0
class State: class State:
...@@ -30,18 +31,17 @@ def listen_for_node(id_node, client_socket): ...@@ -30,18 +31,17 @@ def listen_for_node(id_node, client_socket):
def print_mapping_state(): def print_mapping_state():
global map_security_node global map_security_node, time_converge
old_map_security = {} old_map_security = {}
convergence_count = 0
change_count = 0 change_count = 0
is_converged = False
while True: while True:
print("--- New record ---") contents = ["--- New record ---"]
convergence_count += 1
is_changed = False is_changed = False
for id_node in map_security_node.keys(): for id_node in map_security_node.keys():
node = map_security_node[id_node] node = map_security_node[id_node]
print(f"Node {id_node:<5} " contents.append(f"Node {id_node:<5} "
f"| State: {'IN VC' if node['state'] == State.DECIDED_IN_VC else 'NOT in VC' if node['state'] == State.DECIDED_NOT_VC else 'None':<10} " f"| State: {'IN VC' if node['state'] == State.DECIDED_IN_VC else 'NOT in VC' if node['state'] == State.DECIDED_NOT_VC else 'None':<10} "
f"| Pointer: {node['pointer']}") f"| Pointer: {node['pointer']}")
if id_node not in old_map_security: if id_node not in old_map_security:
...@@ -49,14 +49,20 @@ def print_mapping_state(): ...@@ -49,14 +49,20 @@ def print_mapping_state():
elif old_map_security[id_node]['state'] != map_security_node[id_node]['state'] or old_map_security[id_node]['pointer'] != map_security_node[id_node]['pointer']: elif old_map_security[id_node]['state'] != map_security_node[id_node]['state'] or old_map_security[id_node]['pointer'] != map_security_node[id_node]['pointer']:
old_map_security[id_node]['state'] = node['state'] old_map_security[id_node]['state'] = node['state']
old_map_security[id_node]['pointer'] = node['pointer'] old_map_security[id_node]['pointer'] = node['pointer']
convergence_count = 0
is_changed = True is_changed = True
if is_changed: if is_changed:
old_time_converge = time_converge
time_converge = time.time()
is_converged = False
for cont in contents:
print(cont)
change_count += 1 change_count += 1
print("Convergence count: ", convergence_count)
print("Round (Num of change): ", change_count) print("Round (Num of change): ", change_count)
time.sleep(2) if old_time_converge != 0:
print("Round time: ", time_converge - old_time_converge)
elif time_converge != 0 and not is_converged and (time.time() - time_converge) > 10:
is_converged = True
print("Converged")
def main(): def main():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment