Commit b2bbc8d8 authored by Swann Perarnau's avatar Swann Perarnau
Browse files

Remove useless safedict class from erebor

SafeDict was used at some point to deal with multithreading issues.

1) we don't need it in the current code.
2) it has a slightly weird interface not matching typical dicts, which
makes the code less readable.

This commit removes the class, replacing the few variables using it with
a regular dict, and changes the corresponding code points use the
regular dict interface.
parent 11d4dce5
......@@ -36,30 +36,6 @@ class Neighbour :
return "{}@{} target {}".format(
self.rank, self.networkId, self.target)
class SafeDict :
def __init__(self):
self.dict = dict()
def add(self, key, value):
self.dict[key] = value
def remove(self, key):
value = self.get(key)
del self.dict[key]
return value
def get(self, key):
toreturn = None
if key in self.dict.keys() :
toreturn = self.dict[key]
return toreturn
def get_copy(self):
return self.dict
def length(self):
l = len(self.dict.values())
return l
# An Erebor is a network controler
# it can handle several networks
......@@ -80,13 +56,13 @@ class Erebor:
self.isRoot = root
self.run = True
# map networkId network
self.networks = SafeDict()
self.networks = dict()
# holder for master network
self.master_network = None
self.master_neighbr = None
# rank@networkId Neighbour map.
# Is populated when a child is connected on one of our master networks
self.erebor_children=SafeDict()
self.erebor_children = dict()
# dict string - callback
self.ntwk_up_cllbks =dict()
# dict string - callback
......@@ -329,7 +305,7 @@ class Erebor:
if route in self.routing_table.keys() :
old_gateway = self.routing_table[route]
# If the destination is not me, and we need to update it
if old_gateway != new_gateway and self.networks.get(route) == None :
if old_gateway != new_gateway and route not in self.networks:
updates = True
self.routing_table[route] = new_gateway
logger.debug("routing table {} ".format(self.routing_table.keys()))
......@@ -340,7 +316,7 @@ class Erebor:
if self.master_network != None and self.master_network.ID != gateway.ID :
self.send_routing_table_to_father()
# send my tables to my children, avoiding the sender
for key, value in self.networks.get_copy().items():
for key, value in self.networks.items():
if self.master_network == None or key != self.master_network.ID :
self.send_routing_table_to_all_children(value, [from_+"@"+gateway.ID])
......@@ -484,7 +460,7 @@ class Erebor:
logger.info("TRY TO delete children of network "+ID)
if network != None :
to_quit= []
for children in self.erebor_children.get_copy().values() :
for children in self.erebor_children.values():
if children.networkId == ID :
to_quit.append(children)
self.network_terminaison[ID] = len(to_quit)
......@@ -631,7 +607,7 @@ class Erebor:
logger.debug("try to find {}".format(rank+"@"+snetid))
children = self.erebor_children.get(rank+"@"+snetid)
if children != None :
children = self.erebor_children.remove(rank+"@"+snetid)
children = self.erebor_children.pop(rank+"@"+snetid)
logger.debug("children dead !! {}".format(children.to_string()))
self.children_is_dead(children, snetid)
else :
......@@ -810,7 +786,7 @@ class Erebor:
def child_connect_callback(self, rank, networkId) :
logger.info("a child has come "+rank+" "+networkId)
child = Neighbour(rank, networkId)
self.erebor_children.add(rank+"@"+networkId, child)
self.erebor_children[rank+"@"+networkId] = child
# notify the registered that a child has come
network = self.networks.get(networkId)
if network != None :
......@@ -841,7 +817,7 @@ class Erebor:
else :
# a children has died need to update routing table
# first, pick the neighbour and neighbour from children list
neighbour = self.erebor_children.remove(rank_from+"@"+networkId)
neighbour = self.erebor_children.pop(rank_from+"@"+networkId)
# kill child
network = self.networks.get(networkId)
self.kill_it(network,neighbour)
......@@ -879,7 +855,7 @@ class Erebor:
network.connector.terminate()
# propagate the information that the network ID is down
self.del_network_callback("0", networkId)
self.networks.remove(networkId)
del self.networks[networkId]
# Propagation ##############################################################
......@@ -996,7 +972,7 @@ class Erebor:
self.loglv,
self.logf,
self.taktuk_path)
self.networks.add(networkId, self.master_network)
self.networks[networkId] = self.master_network
# register callbacks to the network
self.register_callback_on_network(self.master_network)
# for routing purposes
......@@ -1009,7 +985,7 @@ class Erebor:
def start_network(self, networkId, options=""):
network = Network(self.add_to_queue, True, networkId, self.debug_list,
self.loglv, self.logf, self.taktuk_path, options, self.flog_time)
self.networks.add(networkId, network)
self.networks[networkId] = network
# register callbacks to the network
self.register_callback_on_network(network)
# holder for routing
......@@ -1153,7 +1129,7 @@ class Erebor:
logger.debug("self terminating")
self.state = consts.TERMINAISON_PENDING
# terminate all children and wait for their completion
self.to_wait_terminaison = self.erebor_children.length()
self.to_wait_terminaison = len(self.erebor_children)
# If there is children to wait, kill them before
logger.info(" to wait {}".format(self.to_wait_terminaison))
if self.to_wait_terminaison > 0 :
......@@ -1163,7 +1139,7 @@ class Erebor:
def terminate_children(self):
logger.info("terminate children")
for children in self.erebor_children.get_copy().values() :
for children in self.erebor_children.values():
# send order to terminate to children
network = self.networks.get(children.networkId)
self.send_quit_order(network, children)
......@@ -1172,7 +1148,7 @@ class Erebor:
network.connector.send_message_to(children.rank, "all", consts.QUIT_ORDER, True)
def kill_them(self):
for child in self.erebor_children.get_copy().values() :
for child in self.erebor_children.values():
network = self.networks.get(child.networkId)
self.kill_it(network, child)
......@@ -1202,7 +1178,7 @@ class Erebor:
def terminate_non_master_networks(self):
logger.info("terminate non master networks")
# terminate the networks
for key, value in self.networks.get_copy().items():
for key, value in self.networks.items():
if self.master_network == None or key != self.master_network.ID :
logger.debug("terminate network "+value.ID)
value.connector.terminate()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment