src/simulator/default-simulator-impl.cc
author Guillaume Seguin <guillaume@segu.in>
Sat, 14 Nov 2009 17:47:05 +0100
changeset 5521 37c6c83d4252
parent 5507 915abd2b907b
child 5872 188ecd74fcb4
permissions -rw-r--r--
Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     2
/*
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     3
 * Copyright (c) 2005,2006 INRIA
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     4
 *
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License version 2 as
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     7
 * published by the Free Software Foundation;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     8
 *
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    12
 * GNU General Public License for more details.
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    13
 *
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    17
 *
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    19
 */
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    20
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    21
#include "simulator.h"
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    22
#include "default-simulator-impl.h"
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
#include "scheduler.h"
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    24
#include "event-impl.h"
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    26
#include "ns3/ptr.h"
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    27
#include "ns3/pointer.h"
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    28
#include "ns3/assert.h"
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    29
#include "ns3/log.h"
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    30
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    31
#include <math.h>
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    32
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    33
NS_LOG_COMPONENT_DEFINE ("DefaultSimulatorImpl");
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    35
namespace ns3 {
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
NS_OBJECT_ENSURE_REGISTERED (DefaultSimulatorImpl);
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
TypeId
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
DefaultSimulatorImpl::GetTypeId (void)
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
{
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
  static TypeId tid = TypeId ("ns3::DefaultSimulatorImpl")
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
    .SetParent<Object> ()
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
    .AddConstructor<DefaultSimulatorImpl> ()
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
    ;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
  return tid;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    49
DefaultSimulatorImpl::DefaultSimulatorImpl ()
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
{
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
  m_stop = false;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
  // uids are allocated from 4.
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
  // uid 0 is "invalid" events
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    54
  // uid 1 is "now" events
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
  // uid 2 is "destroy" events
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
    56
  m_uid = 4;
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
  // before ::Run is entered, the m_currentUid will be zero
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    58
  m_currentUid = 0;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    59
  m_currentTs = 0;
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
    60
  m_currentContext = 0xffffffff;
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    61
  m_unscheduledEvents = 0;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    62
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    63
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    64
DefaultSimulatorImpl::~DefaultSimulatorImpl ()
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    65
{
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    66
  while (!m_events->IsEmpty ())
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
    {
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
    68
      Scheduler::Event next = m_events->RemoveNext ();
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
    69
      next.impl->Unref ();
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    70
    }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    71
  m_events = 0;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    72
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    73
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
void
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    75
DefaultSimulatorImpl::Destroy ()
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    76
{
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
  while (!m_destroyEvents.empty ()) 
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
    {
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
      Ptr<EventImpl> ev = m_destroyEvents.front ().PeekEventImpl ();
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
      m_destroyEvents.pop_front ();
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
      NS_LOG_LOGIC ("handle destroy " << ev);
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
      if (!ev->IsCancelled ())
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
        {
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
          ev->Invoke ();
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
        }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
    }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
void
5507
915abd2b907b Simulator::SetScheduler now takes an ObjectFactory
Guillaume Seguin <guillaume@segu.in>
parents: 5302
diff changeset
    90
DefaultSimulatorImpl::SetScheduler (ObjectFactory schedulerFactory)
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
{
5507
915abd2b907b Simulator::SetScheduler now takes an ObjectFactory
Guillaume Seguin <guillaume@segu.in>
parents: 5302
diff changeset
    92
  Ptr<Scheduler> scheduler = schedulerFactory.Create<Scheduler> ();
915abd2b907b Simulator::SetScheduler now takes an ObjectFactory
Guillaume Seguin <guillaume@segu.in>
parents: 5302
diff changeset
    93
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
  if (m_events != 0)
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
    {
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
      while (!m_events->IsEmpty ())
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
        {
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
    98
          Scheduler::Event next = m_events->RemoveNext ();
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    99
          scheduler->Insert (next);
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   100
        }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
    }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
  m_events = scheduler;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   105
void
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   106
DefaultSimulatorImpl::ProcessOneEvent (void)
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
{
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   108
  Scheduler::Event next = m_events->RemoveNext ();
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   110
  NS_ASSERT (next.key.m_ts >= m_currentTs);
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   111
  m_unscheduledEvents--;
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   113
  NS_LOG_LOGIC ("handle " << next.key.m_ts);
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   114
  m_currentTs = next.key.m_ts;
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   115
  m_currentContext = next.key.m_context;
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   116
  m_currentUid = next.key.m_uid;
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   117
  next.impl->Invoke ();
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   118
  next.impl->Unref ();
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
bool 
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   122
DefaultSimulatorImpl::IsFinished (void) const
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   123
{
4085
31945ecb17e2 Make Simulator::IsFinished () take into account the stop time. Needed to run custom simulation loops (see bug 375)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4057
diff changeset
   124
  return m_events->IsEmpty () || m_stop;
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
uint64_t
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   128
DefaultSimulatorImpl::NextTs (void) const
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   129
{
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   130
  NS_ASSERT (!m_events->IsEmpty ());
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   131
  Scheduler::Event ev = m_events->PeekNext ();
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   132
  return ev.key.m_ts;
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   133
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   134
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   135
Time
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   136
DefaultSimulatorImpl::Next (void) const
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   137
{
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   138
  return TimeStep (NextTs ());
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   139
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   140
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   141
void
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   142
DefaultSimulatorImpl::Run (void)
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   143
{
4057
a68a42e667f6 bug 459: Simulator::Run does nothing after Simulator::Stop is called
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4009
diff changeset
   144
  m_stop = false;
4009
76cb8ed8fe98 fix bug 230, rescan bindings
mathieu.lacage@sophia.inria.fr
parents: 3816
diff changeset
   145
  while (!m_events->IsEmpty () && !m_stop) 
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   146
    {
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   147
      ProcessOneEvent ();
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   148
    }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   149
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   150
  // If the simulator stopped naturally by lack of events, make a
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   151
  // consistency test to check that we didn't lose any events along the way.
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   152
  NS_ASSERT(!m_events->IsEmpty () || m_unscheduledEvents == 0);
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   153
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   154
3515
88e9cee20461 Bug #270: Simulator::RunOneEvent
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3494
diff changeset
   155
void
88e9cee20461 Bug #270: Simulator::RunOneEvent
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3494
diff changeset
   156
DefaultSimulatorImpl::RunOneEvent (void)
88e9cee20461 Bug #270: Simulator::RunOneEvent
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3494
diff changeset
   157
{
88e9cee20461 Bug #270: Simulator::RunOneEvent
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3494
diff changeset
   158
  ProcessOneEvent ();
88e9cee20461 Bug #270: Simulator::RunOneEvent
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3494
diff changeset
   159
}
88e9cee20461 Bug #270: Simulator::RunOneEvent
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3494
diff changeset
   160
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   161
void 
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   162
DefaultSimulatorImpl::Stop (void)
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   163
{
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   164
  m_stop = true;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   165
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   166
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   167
void 
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   168
DefaultSimulatorImpl::Stop (Time const &time)
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   169
{
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   170
  Simulator::Schedule (time, &Simulator::Stop);
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   171
}
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   172
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   173
//
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   174
// Schedule an event for a _relative_ time in the future.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   175
//
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   176
EventId
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3807
diff changeset
   177
DefaultSimulatorImpl::Schedule (Time const &time, EventImpl *event)
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   178
{
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   179
  Time tAbsolute = time + TimeStep (m_currentTs);
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   180
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   181
  NS_ASSERT (tAbsolute.IsPositive ());
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   182
  NS_ASSERT (tAbsolute >= TimeStep (m_currentTs));
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   183
  Scheduler::Event ev;
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3807
diff changeset
   184
  ev.impl = event;
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   185
  ev.key.m_ts = (uint64_t) tAbsolute.GetTimeStep ();
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   186
  ev.key.m_context = GetContext ();
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   187
  ev.key.m_uid = m_uid;
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   188
  m_uid++;
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   189
  m_unscheduledEvents++;
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   190
  m_events->Insert (ev);
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   191
  return EventId (event, ev.key.m_ts, ev.key.m_context, ev.key.m_uid);
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   192
}
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   193
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   194
void
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   195
DefaultSimulatorImpl::ScheduleWithContext (uint32_t context, Time const &time, EventImpl *event)
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   196
{
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   197
  NS_LOG_FUNCTION (this << context << time.GetTimeStep () << m_currentTs << event);
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   198
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   199
  Scheduler::Event ev;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   200
  ev.impl = event;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   201
  ev.key.m_ts = m_currentTs + time.GetTimeStep ();
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   202
  ev.key.m_context = context;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   203
  ev.key.m_uid = m_uid;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   204
  m_uid++;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   205
  m_unscheduledEvents++;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   206
  m_events->Insert (ev);
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   207
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   208
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   209
EventId
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3807
diff changeset
   210
DefaultSimulatorImpl::ScheduleNow (EventImpl *event)
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   211
{
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   212
  Scheduler::Event ev;
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3807
diff changeset
   213
  ev.impl = event;
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   214
  ev.key.m_ts = m_currentTs;
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   215
  ev.key.m_context = GetContext ();
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   216
  ev.key.m_uid = m_uid;
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   217
  m_uid++;
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   218
  m_unscheduledEvents++;
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   219
  m_events->Insert (ev);
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   220
  return EventId (event, ev.key.m_ts, ev.key.m_context, ev.key.m_uid);
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   221
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   222
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   223
EventId
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3807
diff changeset
   224
DefaultSimulatorImpl::ScheduleDestroy (EventImpl *event)
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   225
{
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   226
  EventId id (Ptr<EventImpl> (event, false), m_currentTs, 0xffffffff, 2);
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   227
  m_destroyEvents.push_back (id);
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   228
  m_uid++;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   229
  return id;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   230
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   231
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   232
Time
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   233
DefaultSimulatorImpl::Now (void) const
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   234
{
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   235
  return TimeStep (m_currentTs);
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   236
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   237
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   238
Time 
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   239
DefaultSimulatorImpl::GetDelayLeft (const EventId &id) const
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   240
{
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   241
  if (IsExpired (id))
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   242
    {
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   243
      return TimeStep (0);
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   244
    }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   245
  else
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   246
    {
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   247
      return TimeStep (id.GetTs () - m_currentTs);
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   248
    }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   249
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   250
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   251
void
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   252
DefaultSimulatorImpl::Remove (const EventId &id)
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   253
{
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   254
  if (id.GetUid () == 2)
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   255
    {
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   256
      // destroy events.
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   257
      for (DestroyEvents::iterator i = m_destroyEvents.begin (); i != m_destroyEvents.end (); i++)
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   258
        {
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   259
          if (*i == id)
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   260
            {
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   261
              m_destroyEvents.erase (i);
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   262
              break;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   263
            }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   264
         }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   265
      return;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   266
    }
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   267
  if (IsExpired (id))
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   268
    {
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   269
      return;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   270
    }
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   271
  Scheduler::Event event;
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   272
  event.impl = id.PeekEventImpl ();
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   273
  event.key.m_ts = id.GetTs ();
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   274
  event.key.m_context = id.GetContext ();
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   275
  event.key.m_uid = id.GetUid ();
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   276
  m_events->Remove (event);
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   277
  event.impl->Cancel ();
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   278
  // whenever we remove an event from the event list, we have to unref it.
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   279
  event.impl->Unref ();
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   280
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   281
  m_unscheduledEvents--;
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   282
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   283
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   284
void
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   285
DefaultSimulatorImpl::Cancel (const EventId &id)
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   286
{
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   287
  if (!IsExpired (id))
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   288
    {
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   289
      id.PeekEventImpl ()->Cancel ();
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   290
    }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   291
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   292
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   293
bool
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   294
DefaultSimulatorImpl::IsExpired (const EventId &ev) const
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   295
{
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   296
  if (ev.GetUid () == 2)
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   297
    {
5302
5ed72d440db4 fix old bug: canceled destroy events are expired
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4552
diff changeset
   298
      if (ev.PeekEventImpl () == 0 ||
5ed72d440db4 fix old bug: canceled destroy events are expired
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4552
diff changeset
   299
          ev.PeekEventImpl ()->IsCancelled ())
5ed72d440db4 fix old bug: canceled destroy events are expired
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4552
diff changeset
   300
        {
5ed72d440db4 fix old bug: canceled destroy events are expired
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4552
diff changeset
   301
          return true;
5ed72d440db4 fix old bug: canceled destroy events are expired
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4552
diff changeset
   302
        }
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   303
      // destroy events.
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   304
      for (DestroyEvents::const_iterator i = m_destroyEvents.begin (); i != m_destroyEvents.end (); i++)
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   305
        {
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   306
          if (*i == ev)
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   307
            {
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   308
              return false;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   309
            }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   310
         }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   311
      return true;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   312
    }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   313
  if (ev.PeekEventImpl () == 0 ||
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   314
      ev.GetTs () < m_currentTs ||
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   315
      (ev.GetTs () == m_currentTs &&
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   316
       ev.GetUid () <= m_currentUid) ||
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   317
      ev.PeekEventImpl ()->IsCancelled ()) 
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   318
    {
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   319
      return true;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   320
    }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   321
  else
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   322
    {
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   323
      return false;
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   324
    }
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   325
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   326
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   327
Time 
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   328
DefaultSimulatorImpl::GetMaximumSimulationTime (void) const
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   329
{
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   330
  // XXX: I am fairly certain other compilers use other non-standard
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   331
  // post-fixes to indicate 64 bit constants.
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   332
  return TimeStep (0x7fffffffffffffffLL);
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   333
}
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   334
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   335
uint32_t
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   336
DefaultSimulatorImpl::GetContext (void) const
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   337
{
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   338
  return m_currentContext;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   339
}
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   340
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3796
diff changeset
   341
} // namespace ns3
3470
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   342
21022872009d make simulator implementation replaceable via global default value
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   343