Download Model-Driven Development and Simulation of - Hu

Transcript
5.2 Code Generation
the first one was directly removed from the queue, the timer is not removed from the
list. Instead, only its expiration time is checked against the current time retrieved with
GetTime. The timer is removed from the list and pushed into the input queue only if
the current time is less then its expiration. In this case, the message loop M is called
again to handle the new timer expire message. On the other hand, if the expiration
time has not been reached yet, then the platform dependent code part is executed. This
is the shaded part in Figure 5.23. The actual implementation of this part is shown in
Listing 5.5.
1: M: // message handling code
2: if (Scheduler::GetTime() < message->timerExpire) {
3: #ifdef SIMULATION
4:
ns3::Simulator::Schedule(ns3::NanoSeconds(message->timerExpire - Scheduler::GetTime()), &
5:
return;
Scheduler::Run, this);
6: #else
7:
struct timespec wake;
8:
wake.tv_sec = (time_t) (message->timerExpire / 1000000000ULL);
9:
wake.tv_nsec = (long) (message->timerExpire % 1000000000ULL);
10:
while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &wake, NULL) != 0);
11:
goto M;
12: #endif
13: } else {
14:
timerList->PopFront();
15:
inputQueue->Push(message);
16:
goto M;
17: }
Listing 5.5: Implementation of the timer handling part of the Run operation for ns-3
and Linux.
The simulation part consists of a call to the Schedule operation of the ns-3 scheduler
(Line 4-5). This call simply creates an ns-3 Event which will be consumed after the
specified time (the difference between expiration and current time). The simulation
event is just another call to the Run operation. After rescheduling itself, there is nothing
left for the Run operation to do then return. When Run is called again as a result of the
scheduled event, and the input queue is still empty, then the else clause (Line 13) will be
executed. This will cause the timer to be removed from the list, pushed into the input
queue, and handled as a normal message.
The Linux part is similar but with a key difference (Line 7-11). In this case execution
will be stopped (put to sleep to release operating system resources) for the specified
amount of time. When the Run operation resumes after the sleep, its execution continues in the same way as in the simulation part after rescheduling.
The self-contained and minimal pieces of platform dependent code allow for ease of
extensibility for other platforms (simulation or deployment). These extensions must
91