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

#include <ClockBase.h>

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

Public Member Functions

virtual clocktime_t getClockTime () const override
 Returns the current clock time. More...
 
virtual void scheduleClockEventAt (clocktime_t time, ClockEvent *event) override
 Schedules an event to be delivered to the caller module (i.e. More...
 
virtual void scheduleClockEventAfter (clocktime_t time, 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
 
- Public Member Functions inherited from inet::IClock
virtual ~IClock ()
 
virtual clocktime_t computeClockTimeFromSimTime (simtime_t time) const =0
 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 time) const =0
 Returns the simulation time (first moment) for the specified future clock time according to the current state of the clock. More...
 

Static Public Attributes

static simsignal_t timeChangedSignal = cComponent::registerSignal("timeChanged")
 

Protected Member Functions

virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void finish () override
 
virtual void refreshDisplay () const override
 
virtual void updateDisplayString () const
 
cSimpleModule * getTargetModule () const
 

Protected Attributes

clocktime_t clockEventTime = -1
 
const char * displayStringTextFormat = nullptr
 

Member Function Documentation

◆ cancelClockEvent()

ClockEvent * inet::ClockBase::cancelClockEvent ( ClockEvent event)
overridevirtual

Cancels a previously scheduled clock event.

The clock event ownership is transferred to the caller.

Implements inet::IClock.

Reimplemented in inet::OscillatorBasedClock.

73 {
74  getTargetModule()->cancelEvent(msg);
75  msg->setClock(nullptr);
76  return msg;
77 }

Referenced by inet::OscillatorBasedClock::cancelClockEvent().

◆ finish()

void inet::ClockBase::finish ( )
overrideprotectedvirtual

Reimplemented in inet::IdealClock.

25 {
26  emit(timeChangedSignal, getClockTime().asSimTime());
27 }

Referenced by inet::IdealClock::finish().

◆ getClockTime()

clocktime_t inet::ClockBase::getClockTime ( ) const
overridevirtual

Returns the current clock time.

Note that the clock time is not necessarily monotonous in execution order. For example, the clock time may decrease even at the same simulation time.

Implements inet::IClock.

43 {
44  return clockEventTime != -1 ? clockEventTime : computeClockTimeFromSimTime(simTime());
45 }

Referenced by inet::OscillatorBasedClock::computeSimTimeFromClockTime(), finish(), initialize(), inet::OscillatorBasedClock::receiveSignal(), resolveDirective(), scheduleClockEventAfter(), scheduleClockEventAt(), and inet::SettableClock::setClockTime().

◆ getTargetModule()

cSimpleModule* inet::ClockBase::getTargetModule ( ) const
inlineprotected
34  {
35  cSimpleModule *target = getSimulation()->getContextSimpleModule();
36  if (target == nullptr)
37  throw cRuntimeError("scheduleAt()/cancelEvent() must be called with a simple module in context");
38  return target;
39  }

Referenced by cancelClockEvent(), scheduleClockEventAfter(), and scheduleClockEventAt().

◆ handleClockEvent()

void inet::ClockBase::handleClockEvent ( ClockEvent event)
overridevirtual

Called by the clock event to be executed in the context of this clock.

This method is primarily useful for clock implementations to update their internal data structures related to individual clock events.

Implements inet::IClock.

Reimplemented in inet::OscillatorBasedClock.

80 {
81  clockEventTime = msg->getArrivalClockTime();
82  msg->setClock(nullptr);
83  msg->callBaseExecute();
84  clockEventTime = -1;
85 }

Referenced by inet::OscillatorBasedClock::handleClockEvent().

◆ initialize()

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

Reimplemented in inet::OscillatorBasedClock, inet::SettableClock, and inet::IdealClock.

15 {
16  if (stage == INITSTAGE_LOCAL)
17  displayStringTextFormat = par("displayStringTextFormat");
18  else if (stage == INITSTAGE_LAST) {
20  emit(timeChangedSignal, getClockTime().asSimTime());
21  }
22 }

Referenced by inet::IdealClock::initialize(), and inet::OscillatorBasedClock::initialize().

◆ numInitStages()

virtual int inet::ClockBase::numInitStages ( ) const
inlineoverrideprotectedvirtual
28 { return NUM_INIT_STAGES; }

◆ refreshDisplay()

void inet::ClockBase::refreshDisplay ( ) const
overrideprotectedvirtual
30 {
32 }

◆ resolveDirective()

const char * inet::ClockBase::resolveDirective ( char  directive) const
overridevirtual

Implements inet::StringFormat::IDirectiveResolver.

Reimplemented in inet::OscillatorBasedClock.

88 {
89  static std::string result;
90  switch (directive) {
91  case 't':
92  result = getClockTime().str() + " s";
93  break;
94  default:
95  throw cRuntimeError("Unknown directive: %c", directive);
96  }
97  return result.c_str();
98 }

Referenced by inet::OscillatorBasedClock::resolveDirective().

◆ scheduleClockEventAfter()

void inet::ClockBase::scheduleClockEventAfter ( clocktime_t  delay,
ClockEvent event 
)
overridevirtual

Schedules an event to be delivered to the caller module (i.e.

the context module) after the given clock time delay has elapsed. The event is anchored to a specific clock time duration, so the actual simulation time when this event is executed is not affected if the clock time is set later. On the other hand, setting the clock drift still affects the simulation time of the event execution.

Implements inet::IClock.

Reimplemented in inet::OscillatorBasedClock.

59 {
60  if (clockTimeDelay < 0)
61  throw cRuntimeError("Cannot schedule clock event with negative delay");
62  cSimpleModule *targetModule = getTargetModule();
63  msg->setClock(this);
64  msg->setRelative(true);
65  clocktime_t nowClock = getClockTime();
66  clocktime_t arrivalClockTime = nowClock + clockTimeDelay;
67  msg->setArrivalClockTime(arrivalClockTime);
68  simtime_t simTimeDelay = clockTimeDelay.isZero() ? SIMTIME_ZERO : computeSimTimeFromClockTime(arrivalClockTime) - simTime();
69  targetModule->scheduleAfter(simTimeDelay, msg);
70 }

Referenced by inet::OscillatorBasedClock::scheduleClockEventAfter().

◆ scheduleClockEventAt()

void inet::ClockBase::scheduleClockEventAt ( clocktime_t  time,
ClockEvent event 
)
overridevirtual

Schedules an event to be delivered to the caller module (i.e.

the context module) at the specified clock time. The event is anchored to a specific clock time value, so the actual simulation time when this event is executed will be affected if the clock time is set later.

Implements inet::IClock.

Reimplemented in inet::OscillatorBasedClock.

48 {
49  if (t < getClockTime())
50  throw cRuntimeError("Cannot schedule clock event in the past");
51  cSimpleModule *targetModule = getTargetModule();
52  msg->setClock(this);
53  msg->setRelative(false);
54  msg->setArrivalClockTime(t);
55  targetModule->scheduleAt(computeSimTimeFromClockTime(t), msg);
56 }

Referenced by inet::OscillatorBasedClock::scheduleClockEventAt().

◆ updateDisplayString()

void inet::ClockBase::updateDisplayString ( ) const
protectedvirtual
35 {
36  if (getEnvir()->isGUI()) {
38  getDisplayString().setTagArg("t", 0, text);
39  }
40 }

Referenced by initialize(), and refreshDisplay().

Member Data Documentation

◆ clockEventTime

clocktime_t inet::ClockBase::clockEventTime = -1
protected

Referenced by getClockTime(), and handleClockEvent().

◆ displayStringTextFormat

const char* inet::ClockBase::displayStringTextFormat = nullptr
protected

Referenced by initialize(), and updateDisplayString().

◆ timeChangedSignal


The documentation for this class was generated from the following files:
inet::INITSTAGE_LAST
INET_API InitStage INITSTAGE_LAST
Operations that no other initializations can depend on, e.g.
inet::ClockBase::updateDisplayString
virtual void updateDisplayString() const
Definition: ClockBase.cc:34
inet::clocktime_t
ClockTime clocktime_t
Definition: contract/ClockTime.h:25
inet::ClockBase::timeChangedSignal
static simsignal_t timeChangedSignal
Definition: ClockBase.h:21
inet::ClockBase::getTargetModule
cSimpleModule * getTargetModule() const
Definition: ClockBase.h:34
inet::ClockBase::getClockTime
virtual clocktime_t getClockTime() const override
Returns the current clock time.
Definition: ClockBase.cc:42
inet::ClockBase::displayStringTextFormat
const char * displayStringTextFormat
Definition: ClockBase.h:25
inet::ClockBase::clockEventTime
clocktime_t clockEventTime
Definition: ClockBase.h:24
inet::IClock::computeSimTimeFromClockTime
virtual simtime_t computeSimTimeFromClockTime(clocktime_t time) const =0
Returns the simulation time (first moment) for the specified future clock time according to the curre...
inet::ClockTime::str
std::string str() const
Converts the time to a numeric string.
Definition: common/ClockTime.h:255
inet::INITSTAGE_LOCAL
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
NUM_INIT_STAGES
#define NUM_INIT_STAGES
Definition: InitStageRegistry.h:73
inet::IClock::computeClockTimeFromSimTime
virtual clocktime_t computeClockTimeFromSimTime(simtime_t time) const =0
Returns the clock time for the specified future simulation time according to the current state of the...
inet::StringFormat::formatString
const char * formatString(IDirectiveResolver *resolver) const
Definition: StringFormat.cc:17
inet::ClockTime::isZero
bool isZero() const
Returns true if this simulation time is zero, false otherwise.
Definition: common/ClockTime.h:197