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
config = request.wlan_config.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.updatemodel(config)
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.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.nodes.base import CoreNetworkBase, CoreNode, NodeOptions
from core.nodes.interface import CoreInterface
@ -167,7 +167,7 @@ class EmaneNet(CoreNetworkBase):
self.mobility: Optional[WayPointMobility] = None
model_class = self.session.emane.get_model(options.emane_model)
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)
@classmethod
@ -280,7 +280,7 @@ class EmaneNet(CoreNetworkBase):
self.attach(iface)
if self.up:
iface.startup()
if self.session.state == EventTypes.RUNTIME_STATE:
if self.session.is_running():
self.session.emane.start_iface(self, iface)
return iface

View file

@ -520,8 +520,7 @@ class Session:
if isinstance(node, WlanNode):
self.mobility.set_model_config(node.id, BasicRangeModel.name)
# boot core nodes after runtime
is_runtime = self.state == EventTypes.RUNTIME_STATE
if is_runtime and isinstance(node, CoreNode):
if self.is_running() and isinstance(node, CoreNode):
self.add_remove_control_iface(node, remove=False)
self.boot_node(node)
self.sdt.add_node(node)
@ -1011,7 +1010,7 @@ class Session:
: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")
return []
# 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
if not in runtime.
"""
if self.state == EventTypes.RUNTIME_STATE:
if self.is_running():
return time.monotonic() - self.state_time
else:
return 0.0
@ -1442,3 +1441,11 @@ class Session:
color = LINK_COLORS[index]
self.link_colors[network_id] = 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