INET Framework for OMNeT++/OMNEST
inet::SettableClock Class Reference

#include <SettableClock.h>

Inheritance diagram for inet::SettableClock:
inet::OscillatorBasedClock inet::IScriptable inet::ClockBase inet::IClock inet::StringFormat::IDirectiveResolver

Public Member Functions

virtual void setClockTime (clocktime_t time, bool resetOscillator=true)
 
- Public Member Functions inherited from inet::OscillatorBasedClock
virtual ~OscillatorBasedClock ()
 
virtual clocktime_t computeClockTimeFromSimTime (simtime_t t) const override
 Returns the clock time for the specified future simulation time according to the current state of the clock. More...
 
virtual simtime_t computeSimTimeFromClockTime (clocktime_t t) const override
 Returns the simulation time (first moment) for the specified future clock time according to the current state of the clock. More...
 
virtual void scheduleClockEventAt (clocktime_t t, ClockEvent *event) override
 Schedules an event to be delivered to the caller module (i.e. More...
 
virtual void scheduleClockEventAfter (clocktime_t delay, ClockEvent *event) override
 Schedules an event to be delivered to the caller module (i.e. More...
 
virtual ClockEventcancelClockEvent (ClockEvent *event) override
 Cancels a previously scheduled clock event. More...
 
virtual void handleClockEvent (ClockEvent *event) override
 Called by the clock event to be executed in the context of this clock. More...
 
virtual const char * resolveDirective (char directive) const override
 
virtual void receiveSignal (cComponent *source, int signal, cObject *obj, cObject *details) override
 
- Public Member Functions inherited from inet::ClockBase
virtual clocktime_t getClockTime () const override
 Returns the current clock time. More...
 
- Public Member Functions inherited from inet::IClock
virtual ~IClock ()
 
- Public Member Functions inherited from inet::IScriptable
virtual ~IScriptable ()
 

Protected Member Functions

virtual void initialize (int stage) override
 
virtual OverdueClockEventHandlingMode getOverdueClockEventHandlingMode (ClockEvent *event) const
 
virtual simtime_t handleOverdueClockEvent (ClockEvent *event, simtime_t t)
 
virtual void processCommand (const cXMLElement &node) override
 Called by ScenarioManager whenever a script command needs to be carried out by the module. More...
 
- Protected Member Functions inherited from inet::ClockBase
virtual int numInitStages () const override
 
virtual void finish () override
 
virtual void refreshDisplay () const override
 
virtual void updateDisplayString () const
 
cSimpleModule * getTargetModule () const
 

Protected Attributes

OverdueClockEventHandlingMode defaultOverdueClockEventHandlingMode = UNSPECIFIED
 
- Protected Attributes inherited from inet::OscillatorBasedClock
IOscillatoroscillator = nullptr
 
int64_t(* roundingFunction )(int64_t, int64_t) = nullptr
 
int64_t originClockTick = -1
 
std::vector< ClockEvent * > events
 
- Protected Attributes inherited from inet::ClockBase
clocktime_t clockEventTime = -1
 
const char * displayStringTextFormat = nullptr
 

Additional Inherited Members

- Static Public Attributes inherited from inet::ClockBase
static simsignal_t timeChangedSignal = cComponent::registerSignal("timeChanged")
 

Member Function Documentation

◆ getOverdueClockEventHandlingMode()

OverdueClockEventHandlingMode inet::SettableClock::getOverdueClockEventHandlingMode ( ClockEvent event) const
protectedvirtual
34 {
35  auto mode = event->getOverdueClockEventHandlingMode();
36  if (mode == UNSPECIFIED)
38  else
39  return mode;
40 }

Referenced by handleOverdueClockEvent().

◆ handleOverdueClockEvent()

simtime_t inet::SettableClock::handleOverdueClockEvent ( ClockEvent event,
simtime_t  t 
)
protectedvirtual
43 {
44  switch (getOverdueClockEventHandlingMode(event)) {
45  case EXECUTE:
46  EV_WARN << "Scheduling overdue clock event " << event->getName() << " to current simulation time.\n";
47  return t;
48  case SKIP:
49  EV_WARN << "Skipping overdue clock event " << event->getName() << ".\n";
50  cancelClockEvent(event);
51  return -1;
52  case ERROR:
53  throw cRuntimeError("Clock event is overdue");
54  default:
55  throw cRuntimeError("Unknown overdue clock event handling mode");
56  }
57 }

Referenced by setClockTime().

◆ initialize()

void inet::SettableClock::initialize ( int  stage)
overrideprotectedvirtual

Reimplemented from inet::OscillatorBasedClock.

18 {
20  if (stage == INITSTAGE_LOCAL) {
21  const char *text = par("defaultOverdueClockEventHandlingMode");
22  if (!strcmp(text, "execute"))
24  else if (!strcmp(text, "skip"))
26  else if (!strcmp(text, "error"))
28  else
29  throw cRuntimeError("Unknown defaultOverdueClockEventHandlingMode parameter value");
30  }
31 }

◆ processCommand()

void inet::SettableClock::processCommand ( const cXMLElement &  node)
overrideprotectedvirtual

Called by ScenarioManager whenever a script command needs to be carried out by the module.

The command is represented by the XML element or element tree. The command name can be obtained as:

const char *command = node->getTagName()

Parameters are XML attributes, e.g. a "neighbour" parameter can be retrieved as:

const char *attr = node->getAttribute("neighbour")

More complex input can be passed in child elements.

See also
cXMLElement

Implements inet::IScriptable.

97 {
98  Enter_Method("processCommand");
99  if (!strcmp(node.getTagName(), "set-clock")) {
101  bool resetOscillator = xmlutils::getAttributeBoolValue(&node, "reset-oscillator", true);
102  setClockTime(time, resetOscillator);
103  }
104  else
105  throw cRuntimeError("Invalid command: %s", node.getTagName());
106 }

◆ setClockTime()

void inet::SettableClock::setClockTime ( clocktime_t  time,
bool  resetOscillator = true 
)
virtual
60 {
61  Enter_Method("setClockTime");
62  clocktime_t oldClockTime = getClockTime();
63  if (newClockTime != oldClockTime) {
64  emit(timeChangedSignal, oldClockTime.asSimTime());
65  if (resetOscillator) {
66  if (auto constantDriftOscillator = dynamic_cast<ConstantDriftOscillator *>(oscillator))
67  constantDriftOscillator->setTickOffset(0);
68  }
69  simtime_t currentSimTime = simTime();
70  EV_DEBUG << "Setting clock time from " << oldClockTime << " to " << newClockTime << " at simtime " << currentSimTime << ".\n";
71  int64_t numTicks = oscillator->computeTicksForInterval(currentSimTime - oscillator->getComputationOrigin());
72  originClockTick = newClockTime.raw() / oscillator->getNominalTickLength().raw() - numTicks;
73  ASSERT(newClockTime == getClockTime());
74  clocktime_t clockDelta = newClockTime - oldClockTime;
75  for (auto event : events) {
76  if (event->getRelative())
77  // NOTE: the simulation time of event execution is not affected
78  event->setArrivalClockTime(event->getArrivalClockTime() + clockDelta);
79  else {
80  clocktime_t arrivalClockTime = event->getArrivalClockTime();
81  bool isOverdue = arrivalClockTime < newClockTime;
82  simtime_t arrivalSimTime = isOverdue ? -1 : computeSimTimeFromClockTime(arrivalClockTime);
83  if (isOverdue || arrivalSimTime < currentSimTime)
84  arrivalSimTime = handleOverdueClockEvent(event, currentSimTime);
85  if (event->isScheduled()) {
86  cSimpleModule *targetModule = check_and_cast<cSimpleModule *>(event->getArrivalModule());
87  cContextSwitcher contextSwitcher(targetModule);
88  targetModule->rescheduleAt(arrivalSimTime, event);
89  }
90  }
91  }
92  emit(timeChangedSignal, newClockTime.asSimTime());
93  }
94 }

Referenced by processCommand().

Member Data Documentation

◆ defaultOverdueClockEventHandlingMode

OverdueClockEventHandlingMode inet::SettableClock::defaultOverdueClockEventHandlingMode = UNSPECIFIED
protected

The documentation for this class was generated from the following files:
inet::OscillatorBasedClock::computeSimTimeFromClockTime
virtual simtime_t computeSimTimeFromClockTime(clocktime_t t) const override
Returns the simulation time (first moment) for the specified future clock time according to the curre...
Definition: OscillatorBasedClock.cc:80
inet::OscillatorBasedClock::initialize
virtual void initialize(int stage) override
Definition: OscillatorBasedClock.cc:44
inet::clocktime_t
ClockTime clocktime_t
Definition: contract/ClockTime.h:25
inet::OscillatorBasedClock::oscillator
IOscillator * oscillator
Definition: OscillatorBasedClock.h:19
inet::ClockBase::timeChangedSignal
static simsignal_t timeChangedSignal
Definition: ClockBase.h:21
inet::EXECUTE
@ EXECUTE
Definition: ClockEvent_m.h:60
inet::ClockTime::parse
static const ClockTime parse(const char *s)
Converts the given string to simulation time.
Definition: common/ClockTime.h:315
inet::OscillatorBasedClock::events
std::vector< ClockEvent * > events
Definition: OscillatorBasedClock.h:23
inet::SettableClock::handleOverdueClockEvent
virtual simtime_t handleOverdueClockEvent(ClockEvent *event, simtime_t t)
Definition: SettableClock.cc:42
inet::ClockBase::getClockTime
virtual clocktime_t getClockTime() const override
Returns the current clock time.
Definition: ClockBase.cc:42
inet::UNSPECIFIED
@ UNSPECIFIED
Definition: ClockEvent_m.h:59
inet::SettableClock::getOverdueClockEventHandlingMode
virtual OverdueClockEventHandlingMode getOverdueClockEventHandlingMode(ClockEvent *event) const
Definition: SettableClock.cc:33
inet::xmlutils::getAttributeBoolValue
bool getAttributeBoolValue(const cXMLElement *node, const char *attrName, bool defVal)
Definition: XMLUtils.cc:169
inet::INITSTAGE_LOCAL
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
inet::SKIP
@ SKIP
Definition: ClockEvent_m.h:61
inet::OscillatorBasedClock::originClockTick
int64_t originClockTick
Definition: OscillatorBasedClock.h:22
inet::SettableClock::defaultOverdueClockEventHandlingMode
OverdueClockEventHandlingMode defaultOverdueClockEventHandlingMode
Definition: SettableClock.h:19
inet::ERROR
@ ERROR
Definition: ClockEvent_m.h:62
Enter_Method
#define Enter_Method(...)
Definition: SelfDoc.h:71
inet::IOscillator::getComputationOrigin
virtual simtime_t getComputationOrigin() const =0
Returns the oscillator computation origin from which the ticks are measured.
inet::IOscillator::getNominalTickLength
virtual simtime_t getNominalTickLength() const =0
Returns the nominal time interval between subsequent ticks.
inet::SettableClock::setClockTime
virtual void setClockTime(clocktime_t time, bool resetOscillator=true)
Definition: SettableClock.cc:59
inet::OscillatorBasedClock::cancelClockEvent
virtual ClockEvent * cancelClockEvent(ClockEvent *event) override
Cancels a previously scheduled clock event.
Definition: OscillatorBasedClock.cc:101
inet::IOscillator::computeTicksForInterval
virtual int64_t computeTicksForInterval(simtime_t timeInterval) const =0
Returns the number of ticks in the specified time interval measured from the oscillator computation o...
inet::xmlutils::getMandatoryFilledAttribute
const char * getMandatoryFilledAttribute(const cXMLElement &node, const char *attr)
Definition: XMLUtils.cc:160