From 3d2e372663a0dd799259c28820e8c299fd62eca4 Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Mon, 25 Nov 2019 12:59:32 -0800 Subject: [PATCH] simplified next node id logic to avoid trying to over optimize --- coretk/coretk/coreclient.py | 39 ++++++++++--------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/coretk/coretk/coreclient.py b/coretk/coretk/coreclient.py index 8c370a91..c4e58795 100644 --- a/coretk/coretk/coreclient.py +++ b/coretk/coretk/coreclient.py @@ -58,13 +58,11 @@ class CoreClient: # helpers self.interface_to_edge = {} - self.deleted_nodes = [] self.interfaces_manager = InterfaceManager(self.app) self.created_nodes = set() self.created_links = set() # session data - self.id = 1 self.state = None self.canvas_nodes = {} self.location = None @@ -78,11 +76,9 @@ class CoreClient: self.file_configs = {} def reset(self): - self.id = 1 # helpers self.created_nodes.clear() self.created_links.clear() - self.deleted_nodes.clear() self.interfaces_manager.reset() self.interface_to_edge.clear() # session data @@ -190,18 +186,6 @@ class CoreClient: # get emane model config - # determine next node id and reusable nodes - max_id = 1 - existing_nodes = set() - for node in session.nodes: - if node.id > max_id: - max_id = node.id - existing_nodes.add(node.id) - self.id = max_id - for i in range(1, self.id): - if i not in existing_nodes: - self.deleted_nodes.append(i) - # draw session self.app.canvas.reset_and_redraw(session) @@ -417,19 +401,19 @@ class CoreClient: logging.debug("Close grpc") self.client.close() - def get_id(self): + def next_node_id(self): """ - Get the next node id as well as update id status and reusable ids + Get the next usable node id. - :rtype: int :return: the next id to be used + :rtype: int """ - if self.deleted_nodes: - return self.deleted_nodes.pop(0) - else: - new_id = self.id - self.id = self.id + 1 - return new_id + i = 1 + while True: + if i not in self.canvas_nodes: + break + i += 1 + return i def create_node(self, x, y, node_type, model): """ @@ -441,7 +425,7 @@ class CoreClient: :param str model: node model :return: nothing """ - node_id = self.get_id() + node_id = self.next_node_id() position = core_pb2.Position(x=x, y=y) image = None if NodeUtils.is_image_node(node_type): @@ -482,7 +466,6 @@ class CoreClient: logging.error("unknown node: %s", node_id) continue del self.canvas_nodes[node_id] - self.deleted_nodes.append(node_id) if node_id in self.mobility_configs: del self.mobility_configs[node_id] if node_id in self.wlan_configs: @@ -500,8 +483,6 @@ class CoreClient: logging.error("unknown edge: %s", edge.token) del self.links[edge.token] - self.deleted_nodes.sort() - def create_interface(self, canvas_node): node = canvas_node.core_node ip4, ip6, prefix = self.interfaces_manager.get_ips(node.id)