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

#include <OscillatorBasedClock.h>

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

Public Member Functions

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 ()
 

Protected Member Functions

virtual void initialize (int stage) override
 
- 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

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")
 

Constructor & Destructor Documentation

◆ ~OscillatorBasedClock()

inet::OscillatorBasedClock::~OscillatorBasedClock ( )
virtual
39 {
40  for (auto event : events)
41  event->setClock(nullptr);
42 }

Member Function Documentation

◆ cancelClockEvent()

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

Cancels a previously scheduled clock event.

The clock event ownership is transferred to the caller.

Reimplemented from inet::ClockBase.

102 {
103  events.erase(std::remove(events.begin(), events.end(), event), events.end());
104  return ClockBase::cancelClockEvent(event);
105 }

Referenced by inet::SettableClock::handleOverdueClockEvent().

◆ computeClockTimeFromSimTime()

clocktime_t inet::OscillatorBasedClock::computeClockTimeFromSimTime ( simtime_t  time) const
overridevirtual

Returns the clock time for the specified future simulation time according to the current state of the clock.

This method implements a monotonic function with respect to the simulation time argument. It's allowed to return a different value for the same argument value if the clock is set between calls. The time argument must be greater than or equal to the current simulation time, otherwise an error is raised. See SIMTIME_AS_CLOCKTIME macro for simple type conversion.

Implements inet::IClock.

75 {
76  ASSERT(t >= simTime());
78 }

◆ computeSimTimeFromClockTime()

simtime_t inet::OscillatorBasedClock::computeSimTimeFromClockTime ( clocktime_t  time) const
overridevirtual

Returns the simulation time (first moment) for the specified future clock time according to the current state of the clock.

This method implements a monotonic function with respect to the clock time argument. It's allowed to return a different value for the same argument value if the clock is set between calls. The time argument must be greater or than equal to the current clock time, otherwise an error is raised. See CLOCKTIME_AS_SIMTIME macro for simple type conversion.

Implements inet::IClock.

81 {
82  ASSERT(t >= getClockTime());
83  int64_t numTicks = t.raw() / oscillator->getNominalTickLength().raw();
85 }

Referenced by receiveSignal(), and inet::SettableClock::setClockTime().

◆ handleClockEvent()

void inet::OscillatorBasedClock::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.

Reimplemented from inet::ClockBase.

108 {
109  events.erase(std::remove(events.begin(), events.end(), event), events.end());
111 }

◆ initialize()

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

Reimplemented from inet::ClockBase.

Reimplemented in inet::SettableClock.

45 {
46  ClockBase::initialize(stage);
47  if (stage == INITSTAGE_LOCAL) {
48  auto oscillatorModule = getSubmodule("oscillator");
49  oscillator = check_and_cast<IOscillator *>(oscillatorModule);
50  oscillatorModule->subscribe(IOscillator::preOscillatorStateChangedSignal, this);
51  oscillatorModule->subscribe(IOscillator::postOscillatorStateChangedSignal, this);
52  const char *roundingMode = par("roundingMode");
53  if (!strcmp(roundingMode, "up"))
54  roundingFunction = roundUp;
55  else if (!strcmp(roundingMode, "down"))
56  roundingFunction = roundDown;
57  else if (!strcmp(roundingMode, "closer"))
58  roundingFunction = roundCloser;
59  else if (!strcmp(roundingMode, "none"))
60  roundingFunction = roundNone;
61  else
62  throw cRuntimeError("Unknown rounding mode");
63  WATCH(originClockTick);
64  WATCH_PTRVECTOR(events);
65  }
66  else if (stage == INITSTAGE_CLOCK) {
67  simtime_t initialClockTime = par("initialClockTime");
68  if (initialClockTime.raw() % oscillator->getNominalTickLength().raw() != 0)
69  throw cRuntimeError("Initial clock time must be a multiple of the oscillator nominal tick length");
70  originClockTick = initialClockTime.raw() / oscillator->getNominalTickLength().raw();
71  }
72 }

Referenced by inet::SettableClock::initialize().

◆ receiveSignal()

void inet::OscillatorBasedClock::receiveSignal ( cComponent *  source,
int  signal,
cObject *  obj,
cObject *  details 
)
overridevirtual
130 {
131  Enter_Method("%s", cComponent::getSignalName(signal));
132 
136  simtime_t currentSimTime = simTime();
137  for (auto event : events) {
138  if (event->getRelative()) {
139  simtime_t simTimeDelay = computeSimTimeFromClockTime(event->getArrivalClockTime()) - currentSimTime;
140  ASSERT(simTimeDelay >= 0);
141  cSimpleModule *targetModule = check_and_cast<cSimpleModule *>(event->getArrivalModule());
142  cContextSwitcher contextSwitcher(targetModule);
143  targetModule->rescheduleAfter(simTimeDelay, event);
144  }
145  else {
146  clocktime_t arrivalClockTime = event->getArrivalClockTime();
147  simtime_t arrivalSimTime = computeSimTimeFromClockTime(arrivalClockTime);
148  ASSERT(arrivalSimTime >= currentSimTime);
149  cSimpleModule *targetModule = check_and_cast<cSimpleModule *>(event->getArrivalModule());
150  cContextSwitcher contextSwitcher(targetModule);
151  targetModule->rescheduleAt(arrivalSimTime, event);
152  }
153  }
155  }
156 }

◆ resolveDirective()

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

Reimplemented from inet::ClockBase.

114 {
115  static std::string result;
116  switch (directive) {
117  case 'o':
118  result = std::to_string(originClockTick);
119  break;
120  case 'c':
121  result = std::to_string(originClockTick + oscillator->computeTicksForInterval(simTime() - oscillator->getComputationOrigin()));
122  break;
123  default:
124  return ClockBase::resolveDirective(directive);
125  }
126  return result.c_str();
127 }

◆ scheduleClockEventAfter()

void inet::OscillatorBasedClock::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.

Reimplemented from inet::ClockBase.

95 {
96  int64_t roundedDelay = roundingFunction(delay.raw(), oscillator->getNominalTickLength().raw());
97  ClockBase::scheduleClockEventAfter(ClockTime().setRaw(roundedDelay), event);
98  events.push_back(event);
99 }

◆ scheduleClockEventAt()

void inet::OscillatorBasedClock::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.

Reimplemented from inet::ClockBase.

88 {
89  int64_t roundedTime = roundingFunction(time.raw(), oscillator->getNominalTickLength().raw());
90  ClockBase::scheduleClockEventAt(ClockTime().setRaw(roundedTime), event);
91  events.push_back(event);
92 }

Member Data Documentation

◆ events

◆ originClockTick

int64_t inet::OscillatorBasedClock::originClockTick = -1
protected

◆ oscillator

◆ roundingFunction

int64_t(* inet::OscillatorBasedClock::roundingFunction) (int64_t, int64_t) = nullptr
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::clocktime_t
ClockTime clocktime_t
Definition: contract/ClockTime.h:25
inet::OscillatorBasedClock::oscillator
IOscillator * oscillator
Definition: OscillatorBasedClock.h:19
inet::ClockTime::from
static ClockTime from(SimTime t)
Convert from SimTime.
Definition: common/ClockTime.h:185
inet::ClockBase::cancelClockEvent
virtual ClockEvent * cancelClockEvent(ClockEvent *event) override
Cancels a previously scheduled clock event.
Definition: ClockBase.cc:72
CLOCKTIME_AS_SIMTIME
#define CLOCKTIME_AS_SIMTIME(x)
Definition: contract/ClockTime.h:19
inet::ClockBase::timeChangedSignal
static simsignal_t timeChangedSignal
Definition: ClockBase.h:21
inet::remove
void remove(std::vector< T > &v, const Tk &a)
Definition: stlutils.h:107
inet::ClockBase::resolveDirective
virtual const char * resolveDirective(char directive) const override
Definition: ClockBase.cc:87
ClockTime
#define ClockTime
Definition: contract/ClockTime.h:18
inet::ClockBase::scheduleClockEventAt
virtual void scheduleClockEventAt(clocktime_t time, ClockEvent *event) override
Schedules an event to be delivered to the caller module (i.e.
Definition: ClockBase.cc:47
inet::OscillatorBasedClock::events
std::vector< ClockEvent * > events
Definition: OscillatorBasedClock.h:23
inet::ClockBase::getClockTime
virtual clocktime_t getClockTime() const override
Returns the current clock time.
Definition: ClockBase.cc:42
inet::ClockBase::initialize
virtual void initialize(int stage) override
Definition: ClockBase.cc:14
inet::IOscillator::postOscillatorStateChangedSignal
static simsignal_t postOscillatorStateChangedSignal
Definition: IOscillator.h:22
inet::INITSTAGE_LOCAL
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
inet::ClockBase::scheduleClockEventAfter
virtual void scheduleClockEventAfter(clocktime_t time, ClockEvent *event) override
Schedules an event to be delivered to the caller module (i.e.
Definition: ClockBase.cc:58
inet::OscillatorBasedClock::originClockTick
int64_t originClockTick
Definition: OscillatorBasedClock.h:22
inet::OscillatorBasedClock::roundingFunction
int64_t(* roundingFunction)(int64_t, int64_t)
Definition: OscillatorBasedClock.h:20
inet::ClockBase::handleClockEvent
virtual void handleClockEvent(ClockEvent *event) override
Called by the clock event to be executed in the context of this clock.
Definition: ClockBase.cc:79
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::IOscillator::preOscillatorStateChangedSignal
static simsignal_t preOscillatorStateChangedSignal
Definition: IOscillator.h:21
inet::INITSTAGE_CLOCK
INET_API InitStage INITSTAGE_CLOCK
Initialization of clocks.
inet::IOscillator::computeIntervalForTicks
virtual simtime_t computeIntervalForTicks(int64_t numTicks) const =0
Returns the smallest simulation time interval for the specified number of ticks measured from the osc...
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...