2019-11-15 02:38:40 +00:00
|
|
|
from actions.action import Action
|
|
|
|
|
2020-05-17 10:15:16 -04:00
|
|
|
|
2019-11-15 02:38:40 +00:00
|
|
|
class SleepAction(Action):
|
2020-05-17 10:15:16 -04:00
|
|
|
"""
|
|
|
|
Defines the SleepAction - causes the engine to pause before sending a packet.
|
|
|
|
"""
|
|
|
|
# Do not select the sleep action during evolutions
|
|
|
|
frequency = 0
|
2019-11-15 02:38:40 +00:00
|
|
|
def __init__(self, time=1, environment_id=None):
|
2020-05-17 10:15:16 -04:00
|
|
|
"""
|
|
|
|
Initializes the sleep action.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
time (float): How much time the packet should delay before sending
|
|
|
|
environment_id (str, optional): Environment ID of the strategy this action is a part of
|
|
|
|
"""
|
|
|
|
Action.__init__(self, "sleep", "out")
|
2019-11-15 02:38:40 +00:00
|
|
|
self.terminal = False
|
|
|
|
self.branching = False
|
|
|
|
self.time = time
|
|
|
|
|
|
|
|
def run(self, packet, logger):
|
|
|
|
"""
|
|
|
|
The sleep action simply passes along the packet it was given with an instruction for how long the engine should sleep before sending it.
|
|
|
|
"""
|
2020-02-13 20:51:00 +00:00
|
|
|
logger.debug(" - Adding %g sleep to given packet." % self.time)
|
2019-11-15 02:38:40 +00:00
|
|
|
packet.sleep = self.time
|
|
|
|
return packet, None
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""
|
|
|
|
Returns a string representation.
|
|
|
|
"""
|
|
|
|
s = Action.__str__(self)
|
2020-02-13 20:51:00 +00:00
|
|
|
s += "{%g}" % self.time
|
2019-11-15 02:38:40 +00:00
|
|
|
return s
|
|
|
|
|
|
|
|
def parse(self, string, logger):
|
|
|
|
"""
|
|
|
|
Parses a string representation for this object.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if string:
|
|
|
|
self.time = float(string)
|
|
|
|
except ValueError:
|
|
|
|
logger.exception("Cannot parse time %s" % string)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|