TTEthernet Model for INET Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
TTEthernetModel::TTEScheduler Class Reference

Scheduler module. More...

#include <TTEScheduler.h>

Inheritance diagram for TTEthernetModel::TTEScheduler:

Public Member Functions

virtual int numInitStages () const
 Returns the number of initialization stages this module needs. More...
 
virtual uint32_t getTicks ()
 Returns the current number of ticks. More...
 
virtual uint64_t getTotalTicks ()
 Returns the absolute number of ticks. More...
 
virtual uint32_t getCycles ()
 Returns the current number of cycles. More...
 
virtual void clockCorrection (int32_t ticks)
 Corrects the clock by the number of ticks. More...
 
virtual bool registerEvent (SchedulerEvent *event)
 Register a new event in the scheduler. More...
 
virtual bool registerEvent (SchedulerActionTimeEvent *actionTimeEvent, bool forceNextCycle)
 Register a new event in the scheduler. More...
 
virtual void unregisterEvent (SchedulerEvent *event)
 Unregister an event that was previously registered in the scheduler. More...
 
virtual void correctEvents ()
 Helper function to correct Events after tick length changes. More...
 
virtual void changeDrift ()
 Helper function to change the drift. More...
 

Protected Member Functions

virtual void initialize (int stage)
 Initialization of the module. More...
 
virtual void handleMessage (cMessage *msg)
 Handles all scheduler messages. More...
 
virtual void handleParameterChange (const char *parname)
 Indicates a parameter has changed. More...
 

Static Protected Attributes

static simsignal_t currentDrift = SIMSIGNAL_NULL
 Signal that is emitted every time the drift (Difference of configured and actual tick length) changes. More...
 
static simsignal_t newCycle = SIMSIGNAL_NULL
 Signal that is emitted at the beginning of a new cycle. More...
 

Private Attributes

simtime_t lastCycleStart
 Simulation time when the last cycle started. More...
 
simtime_t lastNewCycleMessage
 Simulation time when the last newcycle message was seen. More...
 
uint64_t lastCycleTicks
 Number of ticks since the simulation started when the last cycle started. More...
 
std::list< SchedulerEvent * > registredEvents
 List of registered SchedulerEvents. More...
 
cMessage * newCyclemsg
 
simtime_t maxDriftChange
 caches max_drift_change parameter More...
 
simtime_t maxDrift
 caches max_drift parameter More...
 
simtime_t currentTick
 caches current_tick parameter More...
 
simtime_t tick
 caches tick parameter More...
 
uint32_t cycleTicks
 caches cycle_ticks parameter More...
 
uint32_t cycles
 Number of cycles since the simulation started. More...
 

Detailed Description

Scheduler module.

Implements a scheduler with a dedicated local clock with adjustable precision that may be synchronized by a synchronization module

Definition at line 34 of file TTEScheduler.h.

Member Function Documentation

void TTEthernetModel::TTEScheduler::changeDrift ( )
virtual

Helper function to change the drift.

Can be overwritten to implement special clock drift behavior.

Definition at line 136 of file TTEScheduler.cc.

Referenced by handleMessage().

136  {
137 
138  simtime_t newDriftChange = uniform(-maxDriftChange,maxDriftChange);
139  simtime_t newTick = currentTick+newDriftChange;
140  if((newTick-tick)>maxDrift)
141  par("current_tick").setDoubleValue((tick+maxDrift).dbl());
142  else if((newTick-tick)<-maxDrift)
143  par("current_tick").setDoubleValue((tick-maxDrift).dbl());
144  else
145  par("current_tick").setDoubleValue(newTick.dbl());
146  emit(currentDrift, par("current_tick").doubleValue()-tick);
147 }
void TTEthernetModel::TTEScheduler::clockCorrection ( int32_t  ticks)
virtual

Corrects the clock by the number of ticks.

Parameters
numberof ticks the clock must be corrected

Definition at line 191 of file TTEScheduler.cc.

Referenced by TTEthernetModel::DummySync::handleMessage().

191  {
192  Enter_Method("clock correction %d ticks",ticks);
193 
195 
196  //Now correct the new cylce time
197  cancelEvent(newCyclemsg);
198  scheduleAt(simTime()-(simTime()-lastCycleStart) + currentTick * cycleTicks, newCyclemsg);
199  //Now correct the events
200  correctEvents();
201 }
void TTEthernetModel::TTEScheduler::correctEvents ( )
virtual

Helper function to correct Events after tick length changes.

Definition at line 149 of file TTEScheduler.cc.

Referenced by clockCorrection(), and handleMessage().

149  {
150  for(std::list<SchedulerEvent*>::const_iterator registredEvent = registredEvents.begin(); registredEvent != registredEvents.end(); registredEvent++){
151  if((*registredEvent)->getKind() == ACTION_TIME_EVENT){
152  SchedulerActionTimeEvent *actionTimeEvent = (SchedulerActionTimeEvent*)*registredEvent;
153  cancelEvent(actionTimeEvent);
154  //TODO lots of improvements!
155  if (actionTimeEvent->getAction_time() > getTicks())
156  {
157  scheduleAt(lastCycleStart + currentTick * actionTimeEvent->getAction_time(),
158  actionTimeEvent);
159  }
160  else if (actionTimeEvent->getAction_time() == getTicks())
161  {
162  scheduleAt(simTime(), actionTimeEvent);
163  }
164  else
165  {
166  if(lastCycleStart>simTime()){
167  scheduleAt(lastCycleStart
168  + currentTick
169  * (actionTimeEvent->getAction_time()),
170  actionTimeEvent);
171  }
172  else{
173  scheduleAt(lastCycleStart
174  + currentTick
175  * (actionTimeEvent->getAction_time() + cycleTicks),
176  actionTimeEvent);
177  }
178  }
179  }
180  }
181 }
uint32_t TTEthernetModel::TTEScheduler::getCycles ( )
virtual

Returns the current number of cycles.

Returns
Number of cycles since simulation start

Definition at line 218 of file TTEScheduler.cc.

Referenced by TTEthernetModel::DummySync::handleMessage().

219 {
220  return cycles;
221 }
uint32_t TTEthernetModel::TTEScheduler::getTicks ( )
virtual

Returns the current number of ticks.

Returns
Number of ticks since last cycle start

Definition at line 203 of file TTEScheduler.cc.

Referenced by correctEvents(), TTEthernetModel::TTIncoming::handleMessage(), TTEthernetModel::TTEOutput::isTransmissionAllowed(), and registerEvent().

204 {
205  if(simTime() >= lastCycleStart){
206  return floor((simTime() - lastCycleStart) / currentTick);
207  }
208  else{
209  return cycleTicks - floor((lastCycleStart - simTime()) / currentTick);
210  }
211 }
uint64_t TTEthernetModel::TTEScheduler::getTotalTicks ( )
virtual

Returns the absolute number of ticks.

Returns
Number of ticks since simulation start

Definition at line 213 of file TTEScheduler.cc.

Referenced by TTEthernetModel::RCIncoming::handleMessage(), and TTEthernetModel::TTEOutput::setTransparentClock().

214 {
215  return lastCycleTicks + floor((simTime() - lastNewCycleMessage) / currentTick);
216 }
void TTEthernetModel::TTEScheduler::handleMessage ( cMessage *  msg)
protectedvirtual

Handles all scheduler messages.

Registers self-messages for events sends messages to trigger events in remote modules.

Parameters
msgthe incoming message.

Definition at line 111 of file TTEScheduler.cc.

112 {
113  if (msg->isSelfMessage() && (msg->getKind() == ACTION_TIME_EVENT || msg->getKind() == TIMER_EVENT))
114  {
115  SchedulerEvent *event = (SchedulerEvent*) msg;
116  registredEvents.remove(event);
117  sendDirect(event, event->getDestinationGate());
118  }
119  else if (msg->isSelfMessage() && msg->getKind() == NEW_CYCLE)
120  {
121  //First the precision is changed for the next cycle
122  changeDrift();
123 
124  emit(newCycle, 1L);
125  cycles++;
126  lastCycleStart = simTime();
127  lastNewCycleMessage = simTime();
129  scheduleAt(lastCycleStart + currentTick * cycleTicks, msg);
130  newCyclemsg = msg;
131 
132  correctEvents();
133  }
134 }
void TTEthernetModel::TTEScheduler::handleParameterChange ( const char *  parname)
protectedvirtual

Indicates a parameter has changed.

Parameters
parnameName of the changed parameter or NULL if multiple parameter changed.

Definition at line 183 of file TTEScheduler.cc.

183  {
184  maxDriftChange = SimTime(par("max_drift_change").doubleValue());
185  maxDrift = SimTime(par("max_drift").doubleValue());
186  currentTick = SimTime(par("current_tick").doubleValue());
187  tick = SimTime(par("tick").doubleValue());
188  cycleTicks = par("cycle_ticks").longValue();
189 }
void TTEthernetModel::TTEScheduler::initialize ( int  stage)
protectedvirtual

Initialization of the module.

Parameters
stagethe stages. Module initializes when stage==1

Definition at line 32 of file TTEScheduler.cc.

33 {
34  if(stage==0){
35  currentDrift = registerSignal("currentDrift");
36  newCycle = registerSignal("newCycle");
37  cycles = 0;
38 
39  //Start Timer
40  scheduleAt(simTime(), new SchedulerEvent("NEW_CYCLE", NEW_CYCLE));
41 
42  lastCycleStart = simTime();
43  lastNewCycleMessage = simTime();
44  lastCycleTicks = 0;
45  }
46 }
int TTEthernetModel::TTEScheduler::numInitStages ( ) const
virtual

Returns the number of initialization stages this module needs.

Returns
1

Definition at line 27 of file TTEScheduler.cc.

28 {
29  return 1;
30 }
bool TTEthernetModel::TTEScheduler::registerEvent ( SchedulerEvent event)
virtual

Register a new event in the scheduler.

May fail if ActionTimeEvent is out of schedule

Parameters
eventPointer to the Event to be scheduled. The scheduler will send the event according to the event type
Returns
returns true on success, else false
See Also
SchedulerEvent_Base, SchedulerEvent, SchedulerActionTimeEvent, SchedulerTimerEvent

Definition at line 48 of file TTEScheduler.cc.

Referenced by TTEthernetModel::RCTrafficSourceApp::handleMessage(), TTEthernetModel::TTTrafficSourceApp::handleMessage(), TTEthernetModel::DummySync::handleMessage(), TTEthernetModel::TTIncoming::handleMessage(), TTEthernetModel::TTBuffer::handleMessage(), TTEthernetModel::TTEAPIApplicationBase::handleMessage(), TTEthernetModel::RCTrafficSourceApp::initialize(), TTEthernetModel::TTTrafficSourceApp::initialize(), TTEthernetModel::DummySync::initialize(), TTEthernetModel::TTBuffer::initialize(), TTEthernetModel::TTEAPIApplicationBase::registerTask(), and TTEthernetModel::RCBuffer::resetBag().

48  {
49 #ifdef DEBUG
50  Enter_Method("registerEvent(SchedulerEvent %s)",event->getName());
51 #else
52  Enter_Method_Silent();
53 #endif
54 
55  if (event->getKind() == ACTION_TIME_EVENT)
56  {
57  return registerEvent(dynamic_cast<SchedulerActionTimeEvent*>(event), false);
58  }
59  else if (event->getKind() == TIMER_EVENT)
60  {
61  take(event);
62  registredEvents.push_back(event);
63  SchedulerTimerEvent *timerEvent = (SchedulerTimerEvent*) event;
64  scheduleAt(simTime() + currentTick * timerEvent->getTimer(), event);
65  }
66  return true;
67 }
bool TTEthernetModel::TTEScheduler::registerEvent ( SchedulerActionTimeEvent actionTimeEvent,
bool  forceNextCycle 
)
virtual

Register a new event in the scheduler.

May fail if ActionTimeEvent is out of schedule

Parameters
actionTimeEventPointer to the Event to be scheduled. The scheduler will send the event according to the event type
forceNextCycleshould the event be forced in the next cycle even if it could be executed in this cycle
Returns
returns true on success, else false
See Also
SchedulerEvent_Base, SchedulerEvent, SchedulerActionTimeEvent, SchedulerTimerEvent

Definition at line 70 of file TTEScheduler.cc.

71 {
72 #ifdef DEBUG
73  Enter_Method("registerEvent(SchedulerEvent %s, forceNextCycle %d)",event->getName(), forceNextCycle);
74 #else
75  Enter_Method_Silent();
76 #endif
77 
78  take(actionTimeEvent);
79  registredEvents.push_back(actionTimeEvent);
80 
81  //Check whether event is in cycle
82  if(actionTimeEvent->getAction_time()>cycleTicks){
83  bubble("Schedule contains out of cycle events!");
84  return false;
85  }
86 
87  if ((actionTimeEvent->getAction_time() <= getTicks()) || forceNextCycle)
88  {
89  scheduleAt(lastCycleStart
90  + currentTick
91  * (actionTimeEvent->getAction_time() + cycleTicks),
92  actionTimeEvent);
93  }
94  else
95  {
96 
97  scheduleAt(lastCycleStart + currentTick * (actionTimeEvent->getAction_time()),
98  actionTimeEvent);
99  }
100  return true;
101 }
void TTEthernetModel::TTEScheduler::unregisterEvent ( SchedulerEvent event)
virtual

Unregister an event that was previously registered in the scheduler.

You must delete the event afterwards manually!

Parameters
eventPointer to the Event to be unregistered. The scheduler will not send the event anymore
See Also
SchedulerEvent_Base, SchedulerEvent, SchedulerActionTimeEvent, SchedulerTimerEvent

Definition at line 103 of file TTEScheduler.cc.

103  {
104  if(event->isScheduled()){
105  cancelEvent(event);
106  registredEvents.remove(event);
107  }
108 
109 }

Member Data Documentation

simsignal_t TTEthernetModel::TTEScheduler::currentDrift = SIMSIGNAL_NULL
staticprotected

Signal that is emitted every time the drift (Difference of configured and actual tick length) changes.

Definition at line 202 of file TTEScheduler.h.

Referenced by changeDrift(), and initialize().

simtime_t TTEthernetModel::TTEScheduler::currentTick
private

caches current_tick parameter

Definition at line 73 of file TTEScheduler.h.

Referenced by changeDrift(), clockCorrection(), correctEvents(), getTicks(), getTotalTicks(), handleMessage(), handleParameterChange(), and registerEvent().

uint32_t TTEthernetModel::TTEScheduler::cycles
private

Number of cycles since the simulation started.

Definition at line 86 of file TTEScheduler.h.

Referenced by getCycles(), handleMessage(), and initialize().

uint32_t TTEthernetModel::TTEScheduler::cycleTicks
private

caches cycle_ticks parameter

Definition at line 81 of file TTEScheduler.h.

Referenced by clockCorrection(), correctEvents(), getTicks(), handleMessage(), handleParameterChange(), and registerEvent().

simtime_t TTEthernetModel::TTEScheduler::lastCycleStart
private

Simulation time when the last cycle started.

Definition at line 40 of file TTEScheduler.h.

Referenced by clockCorrection(), correctEvents(), getTicks(), handleMessage(), initialize(), and registerEvent().

uint64_t TTEthernetModel::TTEScheduler::lastCycleTicks
private

Number of ticks since the simulation started when the last cycle started.

Definition at line 50 of file TTEScheduler.h.

Referenced by getTotalTicks(), handleMessage(), and initialize().

simtime_t TTEthernetModel::TTEScheduler::lastNewCycleMessage
private

Simulation time when the last newcycle message was seen.

Definition at line 44 of file TTEScheduler.h.

Referenced by getTotalTicks(), handleMessage(), and initialize().

simtime_t TTEthernetModel::TTEScheduler::maxDrift
private

caches max_drift parameter

Definition at line 68 of file TTEScheduler.h.

Referenced by changeDrift(), and handleParameterChange().

simtime_t TTEthernetModel::TTEScheduler::maxDriftChange
private

caches max_drift_change parameter

Definition at line 64 of file TTEScheduler.h.

Referenced by changeDrift(), and handleParameterChange().

simsignal_t TTEthernetModel::TTEScheduler::newCycle = SIMSIGNAL_NULL
staticprotected

Signal that is emitted at the beginning of a new cycle.

Definition at line 207 of file TTEScheduler.h.

Referenced by handleMessage(), and initialize().

cMessage* TTEthernetModel::TTEScheduler::newCyclemsg
private

Definition at line 59 of file TTEScheduler.h.

Referenced by clockCorrection(), and handleMessage().

std::list<SchedulerEvent*> TTEthernetModel::TTEScheduler::registredEvents
private

List of registered SchedulerEvents.

Used to reregister after clock tick changes.

Definition at line 57 of file TTEScheduler.h.

Referenced by correctEvents(), handleMessage(), registerEvent(), and unregisterEvent().

simtime_t TTEthernetModel::TTEScheduler::tick
private

caches tick parameter

Definition at line 77 of file TTEScheduler.h.

Referenced by changeDrift(), and handleParameterChange().


The documentation for this class was generated from the following files: