Merge remote-tracking branch 'origin/dev' into dev-game-layer

This commit is contained in:
Marek Wolan
2023-11-06 16:23:29 +00:00
9 changed files with 389 additions and 36 deletions

View File

@@ -926,6 +926,18 @@ class Node(SimComponent):
shut_down_countdown: int = 0
"Time steps needed until node is shut down."
is_resetting: bool = False
"If true, the node will try turning itself off then back on again."
node_scan_duration: int = 10
"How many timesteps until the whole node is scanned. Default 10 time steps."
node_scan_countdown: int = 0
"Time steps until scan is complete"
red_scan_countdown: int = 0
"Time steps until reveal to red scan is complete."
def __init__(self, **kwargs):
"""
Initialize the Node with various components and managers.
@@ -979,14 +991,18 @@ class Node(SimComponent):
self._application_request_manager = RequestManager()
rm.add_request("application", RequestType(func=self._application_request_manager))
rm.add_request("scan", RequestType(func=lambda request, context: ...)) # TODO implement OS scan
rm.add_request("scan", RequestType(func=lambda request, context: self.reveal_to_red()))
rm.add_request("shutdown", RequestType(func=lambda request, context: self.power_off()))
rm.add_request("startup", RequestType(func=lambda request, context: self.power_on()))
rm.add_request("reset", RequestType(func=lambda request, context: ...)) # TODO implement node reset
rm.add_request("reset", RequestType(func=lambda request, context: self.reset())) # TODO implement node reset
rm.add_request("logon", RequestType(func=lambda request, context: ...)) # TODO implement logon request
rm.add_request("logoff", RequestType(func=lambda request, context: ...)) # TODO implement logoff request
self._os_request_manager = RequestManager()
self._os_request_manager.add_request("scan", RequestType(func=lambda request, context: self.scan()))
rm.add_request("os", RequestType(func=self._os_request_manager))
return rm
def _install_system_software(self):
@@ -1057,7 +1073,7 @@ class Node(SimComponent):
def apply_timestep(self, timestep: int):
"""
Apply a single timestep of simulation dynamics to this service.
Apply a single timestep of simulation dynamics to this node.
In this instance, if any multi-timestep processes are currently occurring
(such as starting up or shutting down), then they are brought one step closer to
@@ -1087,6 +1103,93 @@ class Node(SimComponent):
self.operating_state = NodeOperatingState.OFF
self.sys_log.info("Turned off")
# if resetting turn back on
if self.is_resetting:
self.is_resetting = False
self.power_on()
# time steps which require the node to be on
if self.operating_state == NodeOperatingState.ON:
# node scanning
if self.node_scan_countdown > 0:
self.node_scan_countdown -= 1
if self.node_scan_countdown == 0:
# scan everything!
for process_id in self.processes:
self.processes[process_id].scan()
# scan services
for service_id in self.services:
self.services[service_id].scan()
# scan applications
for application_id in self.applications:
self.applications[application_id].scan()
# scan file system
self.file_system.scan(instant_scan=True)
if self.red_scan_countdown > 0:
self.red_scan_countdown -= 1
if self.red_scan_countdown == 0:
# scan processes
for process_id in self.processes:
self.processes[process_id].reveal_to_red()
# scan services
for service_id in self.services:
self.services[service_id].reveal_to_red()
# scan applications
for application_id in self.applications:
self.applications[application_id].reveal_to_red()
# scan file system
self.file_system.reveal_to_red(instant_scan=True)
for process_id in self.processes:
self.processes[process_id].apply_timestep(timestep=timestep)
for service_id in self.services:
self.services[service_id].apply_timestep(timestep=timestep)
for application_id in self.applications:
self.applications[application_id].apply_timestep(timestep=timestep)
self.file_system.apply_timestep(timestep=timestep)
def scan(self) -> None:
"""
Scan the node and all the items within it.
Scans the:
- Processes
- Services
- Applications
- Folders
- Files
to the red agent.
"""
self.node_scan_countdown = self.node_scan_duration
def reveal_to_red(self) -> None:
"""
Reveals the node and all the items within it to the red agent.
Set all the:
- Processes
- Services
- Applications
- Folders
- Files
`revealed_to_red` to `True`.
"""
self.red_scan_countdown = self.node_scan_duration
def power_on(self):
"""Power on the Node, enabling its NICs if it is in the OFF state."""
if self.operating_state == NodeOperatingState.OFF:
@@ -1112,6 +1215,20 @@ class Node(SimComponent):
self.operating_state = NodeOperatingState.OFF
self.sys_log.info("Turned off")
def reset(self):
"""
Resets the node.
Powers off the node and sets is_resetting to True.
Applying more timesteps will eventually turn the node back on.
"""
if not self.operating_state.ON:
self.sys_log.error(f"Cannot reset {self.hostname} - node is not turned on.")
else:
self.is_resetting = True
self.sys_log.info(f"Resetting {self.hostname}...")
self.power_off()
def connect_nic(self, nic: NIC):
"""
Connect a NIC (Network Interface Card) to the node.