equal
deleted
inserted
replaced
1 /* -*- Mode:C++; c-basic-offset:4; tab-width:4; indent-tabs-mode:nil -*- */ |
1 /* -*- Mode:NS3; -*- */ |
2 /* |
2 /* |
3 * Copyright (c) 2005,2006 INRIA |
3 * Copyright (c) 2005,2006 INRIA |
4 * All rights reserved. |
4 * All rights reserved. |
5 * |
5 * |
6 * This program is free software; you can redistribute it and/or modify |
6 * This program is free software; you can redistribute it and/or modify |
94 m_currentNs = 0; |
94 m_currentNs = 0; |
95 } |
95 } |
96 |
96 |
97 SimulatorPrivate::~SimulatorPrivate () |
97 SimulatorPrivate::~SimulatorPrivate () |
98 { |
98 { |
99 while (!m_destroy.empty ()) { |
99 while (!m_destroy.empty ()) |
|
100 { |
100 EventImpl *ev = m_destroy.front ().first; |
101 EventImpl *ev = m_destroy.front ().first; |
101 m_destroy.pop_front (); |
102 m_destroy.pop_front (); |
102 TRACE ("handle destroy " << ev); |
103 TRACE ("handle destroy " << ev); |
103 ev->Invoke (); |
104 ev->Invoke (); |
104 delete ev; |
105 delete ev; |
105 } |
106 } |
106 delete m_events; |
107 delete m_events; |
107 m_events = (Scheduler *)0xdeadbeaf; |
108 m_events = (Scheduler *)0xdeadbeaf; |
108 } |
109 } |
109 |
110 |
110 |
111 |
122 Scheduler::EventKey nextKey = m_events->PeekNextKey (); |
123 Scheduler::EventKey nextKey = m_events->PeekNextKey (); |
123 m_events->RemoveNext (); |
124 m_events->RemoveNext (); |
124 TRACE ("handle " << nextEv); |
125 TRACE ("handle " << nextEv); |
125 m_currentNs = nextKey.m_ns; |
126 m_currentNs = nextKey.m_ns; |
126 m_currentUid = nextKey.m_uid; |
127 m_currentUid = nextKey.m_uid; |
127 if (m_logEnable) { |
128 if (m_logEnable) |
|
129 { |
128 m_log << "e "<<nextKey.m_uid << " " << nextKey.m_ns << std::endl; |
130 m_log << "e "<<nextKey.m_uid << " " << nextKey.m_ns << std::endl; |
129 } |
131 } |
130 nextEv->Invoke (); |
132 nextEv->Invoke (); |
131 delete nextEv; |
133 delete nextEv; |
132 } |
134 } |
133 |
135 |
134 bool |
136 bool |
147 |
149 |
148 void |
150 void |
149 SimulatorPrivate::Run (void) |
151 SimulatorPrivate::Run (void) |
150 { |
152 { |
151 while (!m_events->IsEmpty () && !m_stop && |
153 while (!m_events->IsEmpty () && !m_stop && |
152 (m_stopAt == 0 || m_stopAt > Next ().Ns ())) { |
154 (m_stopAt == 0 || m_stopAt > Next ().Ns ())) |
|
155 { |
153 ProcessOneEvent (); |
156 ProcessOneEvent (); |
154 } |
157 } |
155 m_log.close (); |
158 m_log.close (); |
156 } |
159 } |
157 |
160 |
158 |
161 |
159 void |
162 void |
167 m_stopAt = at.Ns (); |
170 m_stopAt = at.Ns (); |
168 } |
171 } |
169 EventId |
172 EventId |
170 SimulatorPrivate::Schedule (Time const &time, EventImpl *event) |
173 SimulatorPrivate::Schedule (Time const &time, EventImpl *event) |
171 { |
174 { |
172 if (time.IsDestroy ()) { |
175 if (time.IsDestroy ()) |
|
176 { |
173 m_destroy.push_back (std::make_pair (event, m_uid)); |
177 m_destroy.push_back (std::make_pair (event, m_uid)); |
174 if (m_logEnable) { |
178 if (m_logEnable) |
|
179 { |
175 m_log << "id " << m_currentUid << " " << Now ().Ns () << " " |
180 m_log << "id " << m_currentUid << " " << Now ().Ns () << " " |
176 << m_uid << std::endl; |
181 << m_uid << std::endl; |
177 } |
182 } |
178 m_uid++; |
183 m_uid++; |
179 //XXX |
184 //XXX |
180 return EventId (); |
185 return EventId (); |
181 } |
186 } |
182 assert (time.Ns () >= Now ().Ns ()); |
187 assert (time.Ns () >= Now ().Ns ()); |
183 Scheduler::EventKey key = {time.Ns (), m_uid}; |
188 Scheduler::EventKey key = {time.Ns (), m_uid}; |
184 if (m_logEnable) { |
189 if (m_logEnable) |
|
190 { |
185 m_log << "i "<<m_currentUid<<" "<<Now ().Ns ()<<" " |
191 m_log << "i "<<m_currentUid<<" "<<Now ().Ns ()<<" " |
186 <<m_uid<<" "<<time.Ns () << std::endl; |
192 <<m_uid<<" "<<time.Ns () << std::endl; |
187 } |
193 } |
188 m_uid++; |
194 m_uid++; |
189 return m_events->Insert (event, key); |
195 return m_events->Insert (event, key); |
190 } |
196 } |
191 Time |
197 Time |
192 SimulatorPrivate::Now (void) const |
198 SimulatorPrivate::Now (void) const |
198 SimulatorPrivate::Remove (EventId ev) |
204 SimulatorPrivate::Remove (EventId ev) |
199 { |
205 { |
200 Scheduler::EventKey key; |
206 Scheduler::EventKey key; |
201 EventImpl *impl = m_events->Remove (ev, &key); |
207 EventImpl *impl = m_events->Remove (ev, &key); |
202 delete impl; |
208 delete impl; |
203 if (m_logEnable) { |
209 if (m_logEnable) |
|
210 { |
204 m_log << "r " << m_currentUid << " " << Now ().Ns () << " " |
211 m_log << "r " << m_currentUid << " " << Now ().Ns () << " " |
205 << key.m_uid << " " << key.m_ns << std::endl; |
212 << key.m_uid << " " << key.m_ns << std::endl; |
206 } |
213 } |
207 } |
214 } |
208 |
215 |
209 void |
216 void |
210 SimulatorPrivate::Cancel (EventId id) |
217 SimulatorPrivate::Cancel (EventId id) |
211 { |
218 { |
217 bool |
224 bool |
218 SimulatorPrivate::IsExpired (EventId ev) |
225 SimulatorPrivate::IsExpired (EventId ev) |
219 { |
226 { |
220 if (ev.GetEventImpl () != 0 && |
227 if (ev.GetEventImpl () != 0 && |
221 ev.GetNs () <= Now ().Ns () && |
228 ev.GetNs () <= Now ().Ns () && |
222 ev.GetUid () < m_currentUid) { |
229 ev.GetUid () < m_currentUid) |
|
230 { |
223 return false; |
231 return false; |
224 } |
232 } |
225 return true; |
233 return true; |
226 } |
234 } |
227 |
235 |
228 |
236 |
229 }; // namespace ns3 |
237 }; // namespace ns3 |
267 |
275 |
268 |
276 |
269 SimulatorPrivate * |
277 SimulatorPrivate * |
270 Simulator::GetPriv (void) |
278 Simulator::GetPriv (void) |
271 { |
279 { |
272 if (m_priv == 0) { |
280 if (m_priv == 0) |
|
281 { |
273 Scheduler *events; |
282 Scheduler *events; |
274 switch (m_listType) { |
283 switch (m_listType) { |
275 case LINKED_LIST: |
284 case LINKED_LIST: |
276 events = new SchedulerList (); |
285 events = new SchedulerList (); |
277 break; |
286 break; |
287 events = 0; |
296 events = 0; |
288 assert (false); |
297 assert (false); |
289 break; |
298 break; |
290 } |
299 } |
291 m_priv = new SimulatorPrivate (events); |
300 m_priv = new SimulatorPrivate (events); |
292 } |
301 } |
293 TRACE_S ("priv " << m_priv); |
302 TRACE_S ("priv " << m_priv); |
294 return m_priv; |
303 return m_priv; |
295 } |
304 } |
296 |
305 |
297 void |
306 void |
419 m_a = false; |
428 m_a = false; |
420 } |
429 } |
421 void |
430 void |
422 SimulatorTests::B (int b) |
431 SimulatorTests::B (int b) |
423 { |
432 { |
424 if (b != 2 || Simulator::Now ().Us () != 11) { |
433 if (b != 2 || Simulator::Now ().Us () != 11) |
|
434 { |
425 m_b = false; |
435 m_b = false; |
426 } else { |
436 } |
|
437 else |
|
438 { |
427 m_b = true; |
439 m_b = true; |
428 } |
440 } |
429 Simulator::Remove (m_idC); |
441 Simulator::Remove (m_idC); |
430 Simulator::Schedule (Time::RelUs (10), &SimulatorTests::D, this, 4); |
442 Simulator::Schedule (Time::RelUs (10), &SimulatorTests::D, this, 4); |
431 } |
443 } |
432 void |
444 void |
433 SimulatorTests::C (int c) |
445 SimulatorTests::C (int c) |
435 m_c = false; |
447 m_c = false; |
436 } |
448 } |
437 void |
449 void |
438 SimulatorTests::D (int d) |
450 SimulatorTests::D (int d) |
439 { |
451 { |
440 if (d != 4 || Simulator::Now ().Us () != (11+10)) { |
452 if (d != 4 || Simulator::Now ().Us () != (11+10)) |
|
453 { |
441 m_d = false; |
454 m_d = false; |
442 } else { |
455 } |
|
456 else |
|
457 { |
443 m_d = true; |
458 m_d = true; |
444 } |
459 } |
445 } |
460 } |
446 bool |
461 bool |
447 SimulatorTests::RunOneTest (void) |
462 SimulatorTests::RunOneTest (void) |
448 { |
463 { |
449 bool ok = true; |
464 bool ok = true; |
457 m_idC = Simulator::Schedule (Time::AbsUs (12), &SimulatorTests::C, this, 3); |
472 m_idC = Simulator::Schedule (Time::AbsUs (12), &SimulatorTests::C, this, 3); |
458 |
473 |
459 Simulator::Cancel (a); |
474 Simulator::Cancel (a); |
460 Simulator::Run (); |
475 Simulator::Run (); |
461 |
476 |
462 if (!m_a || !m_b || !m_c || !m_d) { |
477 if (!m_a || !m_b || !m_c || !m_d) |
|
478 { |
463 ok = false; |
479 ok = false; |
464 } |
480 } |
465 return ok; |
481 return ok; |
466 } |
482 } |
467 bool |
483 bool |
468 SimulatorTests::RunTests (void) |
484 SimulatorTests::RunTests (void) |
469 { |
485 { |
470 bool ok = true; |
486 bool ok = true; |
471 |
487 |
472 Simulator::SetLinkedList (); |
488 Simulator::SetLinkedList (); |
473 if (!RunOneTest ()) { |
489 if (!RunOneTest ()) |
|
490 { |
474 ok = false; |
491 ok = false; |
475 } |
492 } |
476 Simulator::Destroy (); |
493 Simulator::Destroy (); |
477 Simulator::SetBinaryHeap (); |
494 Simulator::SetBinaryHeap (); |
478 if (!RunOneTest ()) { |
495 if (!RunOneTest ()) |
|
496 { |
479 ok = false; |
497 ok = false; |
480 } |
498 } |
481 Simulator::Destroy (); |
499 Simulator::Destroy (); |
482 Simulator::SetStdMap (); |
500 Simulator::SetStdMap (); |
483 if (!RunOneTest ()) { |
501 if (!RunOneTest ()) |
|
502 { |
484 ok = false; |
503 ok = false; |
485 } |
504 } |
486 Simulator::Destroy (); |
505 Simulator::Destroy (); |
487 |
506 |
488 return ok; |
507 return ok; |
489 } |
508 } |
490 |
509 |