simplify the implementation

Mathieu Lacage 2007-05-14 10:25:50 +02:00
parent 791a45c3da
commit a6d24b73bd
2 changed files with 7 additions and 23 deletions

View File

@ -61,11 +61,8 @@ uint32_t OnOffApplication::g_defaultSize = 512;
m_residualBits(0), m_residualBits(0),
m_lastStartTime((HighPrecision)0), m_lastStartTime((HighPrecision)0),
m_maxBytes(0xffffffff), m_maxBytes(0xffffffff),
m_totBytes(0), m_totBytes(0)
m_startStopScheduled(false), {}
m_sendScheduled(false)
{
}
OnOffApplication::~OnOffApplication() OnOffApplication::~OnOffApplication()
{} {}
@ -112,32 +109,25 @@ void OnOffApplication::StartApplication() // Called at time specified by Star
void OnOffApplication::StopApplication() // Called at time specified by Stop void OnOffApplication::StopApplication() // Called at time specified by Stop
{ {
if (m_startStopScheduled) if (m_sendEvent.IsRunning ())
{ // Cancel the startStop event
Simulator::Cancel(m_startStopEvent);
m_startStopScheduled = false;
}
if (m_sendScheduled)
{ // Cancel the pending send packet event { // Cancel the pending send packet event
Simulator::Cancel(m_sendEvent);
m_sendScheduled = false;
// Calculate residual bits since last packet sent // Calculate residual bits since last packet sent
Time delta(Simulator::Now() - m_lastStartTime); Time delta(Simulator::Now() - m_lastStartTime);
m_residualBits += (uint32_t)(m_cbrRate.GetBitRate() * delta.GetSeconds()); m_residualBits += (uint32_t)(m_cbrRate.GetBitRate() * delta.GetSeconds());
} }
Simulator::Cancel(m_sendEvent);
Simulator::Cancel(m_startStopEvent);
} }
// Event handlers // Event handlers
void OnOffApplication::StartSending() void OnOffApplication::StartSending()
{ {
m_startStopScheduled = true;
ScheduleNextTx(); // Schedule the send packet event ScheduleNextTx(); // Schedule the send packet event
} }
void OnOffApplication::StopSending() void OnOffApplication::StopSending()
{ {
m_startStopScheduled = true; Simulator::Cancel(m_sendEvent);
if (m_sendScheduled) Simulator::Cancel(m_sendEvent);
} }
// Private helpers // Private helpers
@ -148,7 +138,6 @@ void OnOffApplication::ScheduleNextTx()
uint32_t bits = m_pktSize * 8 - m_residualBits; uint32_t bits = m_pktSize * 8 - m_residualBits;
Time nextTime(Seconds (bits / Time nextTime(Seconds (bits /
static_cast<double>(m_cbrRate.GetBitRate()))); // Time till next packet static_cast<double>(m_cbrRate.GetBitRate()))); // Time till next packet
m_sendScheduled = true;
m_sendEvent = Simulator::Schedule(nextTime, &OnOffApplication::SendPacket, this); m_sendEvent = Simulator::Schedule(nextTime, &OnOffApplication::SendPacket, this);
} }
else else
@ -161,21 +150,18 @@ void OnOffApplication::ScheduleStartEvent()
{ // Schedules the event to start sending data (switch to the "On" state) { // Schedules the event to start sending data (switch to the "On" state)
Time offInterval = Seconds(m_offTime->GetValue()); Time offInterval = Seconds(m_offTime->GetValue());
m_startStopEvent = Simulator::Schedule(offInterval, &OnOffApplication::StartSending, this); m_startStopEvent = Simulator::Schedule(offInterval, &OnOffApplication::StartSending, this);
m_startStopScheduled = true;
} }
void OnOffApplication::ScheduleStopEvent() void OnOffApplication::ScheduleStopEvent()
{ // Schedules the event to stop sending data (switch to "Off" state) { // Schedules the event to stop sending data (switch to "Off" state)
Time onInterval = Seconds(m_onTime->GetValue()); Time onInterval = Seconds(m_onTime->GetValue());
Simulator::Schedule(onInterval, &OnOffApplication::StopSending, this); Simulator::Schedule(onInterval, &OnOffApplication::StopSending, this);
m_startStopScheduled = true;
} }
void OnOffApplication::SendPacket() void OnOffApplication::SendPacket()
{ {
NS_ASSERT (m_sendScheduled); NS_ASSERT (m_sendEvent.IsExpired ());
m_sendScheduled = false;
m_socket->Send(0, m_pktSize); m_socket->Send(0, m_pktSize);
m_totBytes += m_pktSize; m_totBytes += m_pktSize;
m_lastStartTime = Simulator::Now(); m_lastStartTime = Simulator::Now();

View File

@ -81,9 +81,7 @@ private:
Time m_lastStartTime;// Time last packet sent Time m_lastStartTime;// Time last packet sent
uint32_t m_maxBytes; // Limit total number of bytes sent uint32_t m_maxBytes; // Limit total number of bytes sent
uint32_t m_totBytes; // Total bytes sent so far uint32_t m_totBytes; // Total bytes sent so far
bool m_startStopScheduled; // True if start or stop event scheduled
EventId m_startStopEvent; // Event id for next start or stop event EventId m_startStopEvent; // Event id for next start or stop event
bool m_sendScheduled;// True of send event scheduled
EventId m_sendEvent; // Eventid of pending "send packet" event EventId m_sendEvent; // Eventid of pending "send packet" event
bool m_sending; // True if currently in sending state bool m_sending; // True if currently in sending state