daemon: added convenience method to check if a session is running, which is used in multiple places, providing a cleaner and easier to read experience

This commit is contained in:
Blake Harnden 2023-04-12 14:44:51 -07:00
parent cdc8c7360d
commit b6b300207b
3 changed files with 15 additions and 8 deletions

View file

@ -1104,7 +1104,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
node_id = request.wlan_config.node_id node_id = request.wlan_config.node_id
config = request.wlan_config.config config = request.wlan_config.config
session.mobility.set_model_config(node_id, BasicRangeModel.name, config) session.mobility.set_model_config(node_id, BasicRangeModel.name, config)
if session.state == EventTypes.RUNTIME_STATE: if session.is_running():
node = self.get_node(session, node_id, context, WlanNode) node = self.get_node(session, node_id, context, WlanNode)
node.updatemodel(config) node.updatemodel(config)
return SetWlanConfigResponse(result=True) return SetWlanConfigResponse(result=True)

View file

@ -10,7 +10,7 @@ from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Type, Union
from core.emulator.data import InterfaceData, LinkData, LinkOptions from core.emulator.data import InterfaceData, LinkData, LinkOptions
from core.emulator.distributed import DistributedServer from core.emulator.distributed import DistributedServer
from core.emulator.enumerations import EventTypes, MessageFlags, RegisterTlvs from core.emulator.enumerations import MessageFlags, RegisterTlvs
from core.errors import CoreCommandError, CoreError from core.errors import CoreCommandError, CoreError
from core.nodes.base import CoreNetworkBase, CoreNode, NodeOptions from core.nodes.base import CoreNetworkBase, CoreNode, NodeOptions
from core.nodes.interface import CoreInterface from core.nodes.interface import CoreInterface
@ -167,7 +167,7 @@ class EmaneNet(CoreNetworkBase):
self.mobility: Optional[WayPointMobility] = None self.mobility: Optional[WayPointMobility] = None
model_class = self.session.emane.get_model(options.emane_model) model_class = self.session.emane.get_model(options.emane_model)
self.wireless_model: Optional["EmaneModel"] = model_class(self.session, self.id) self.wireless_model: Optional["EmaneModel"] = model_class(self.session, self.id)
if self.session.state == EventTypes.RUNTIME_STATE: if self.session.is_running():
self.session.emane.add_node(self) self.session.emane.add_node(self)
@classmethod @classmethod
@ -280,7 +280,7 @@ class EmaneNet(CoreNetworkBase):
self.attach(iface) self.attach(iface)
if self.up: if self.up:
iface.startup() iface.startup()
if self.session.state == EventTypes.RUNTIME_STATE: if self.session.is_running():
self.session.emane.start_iface(self, iface) self.session.emane.start_iface(self, iface)
return iface return iface

View file

@ -520,8 +520,7 @@ class Session:
if isinstance(node, WlanNode): if isinstance(node, WlanNode):
self.mobility.set_model_config(node.id, BasicRangeModel.name) self.mobility.set_model_config(node.id, BasicRangeModel.name)
# boot core nodes after runtime # boot core nodes after runtime
is_runtime = self.state == EventTypes.RUNTIME_STATE if self.is_running() and isinstance(node, CoreNode):
if is_runtime and isinstance(node, CoreNode):
self.add_remove_control_iface(node, remove=False) self.add_remove_control_iface(node, remove=False)
self.boot_node(node) self.boot_node(node)
self.sdt.add_node(node) self.sdt.add_node(node)
@ -1011,7 +1010,7 @@ class Session:
:return: list of service boot errors during startup :return: list of service boot errors during startup
""" """
if self.state == EventTypes.RUNTIME_STATE: if self.is_running():
logger.warning("ignoring instantiate, already in runtime state") logger.warning("ignoring instantiate, already in runtime state")
return [] return []
# create control net interfaces and network tunnels # create control net interfaces and network tunnels
@ -1365,7 +1364,7 @@ class Session:
Return the current time we have been in the runtime state, or zero Return the current time we have been in the runtime state, or zero
if not in runtime. if not in runtime.
""" """
if self.state == EventTypes.RUNTIME_STATE: if self.is_running():
return time.monotonic() - self.state_time return time.monotonic() - self.state_time
else: else:
return 0.0 return 0.0
@ -1442,3 +1441,11 @@ class Session:
color = LINK_COLORS[index] color = LINK_COLORS[index]
self.link_colors[network_id] = color self.link_colors[network_id] = color
return color return color
def is_running(self) -> bool:
"""
Convenience for checking if this session is in the runtime state.
:return: True if in the runtime state, False otherwise
"""
return self.state == EventTypes.RUNTIME_STATE