src/core/model/realtime-simulator-impl.h
author Mathieu Lacage <mathieu.lacage@gmail.com>
Wed, 03 Aug 2011 13:58:10 -0400
changeset 7399 520706f801e8
parent 7383 c5e131450339
child 7780 69c3c67b1b97
permissions -rw-r--r--
introduce Config::Reset
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     2
/*
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     3
 * Copyright (c) 2008 University of Washington
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     4
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License version 2 as
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     7
 * published by the Free Software Foundation;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     8
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    12
 * GNU General Public License for more details.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    13
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    17
 */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    18
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    19
#ifndef REALTIME_SIMULATOR_IMPL_H
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    20
#define REALTIME_SIMULATOR_IMPL_H
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    21
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    22
#include "simulator-impl.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    24
#include "scheduler.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
#include "synchronizer.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    26
#include "event-impl.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    27
7383
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 6821
diff changeset
    28
#include "ptr.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 6821
diff changeset
    29
#include "assert.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 6821
diff changeset
    30
#include "log.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 6821
diff changeset
    31
#include "system-mutex.h"
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    32
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    33
#include <list>
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    35
namespace ns3 {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
class RealtimeSimulatorImpl : public SimulatorImpl
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
public:
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
  static TypeId GetTypeId (void);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
  /**
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
   * Enumeration of the types of packets supported in the class.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
   *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
   */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
  enum SynchronizationMode {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
    SYNC_BEST_EFFORT, /** Make a best effort to keep synced to real-time */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
    SYNC_HARD_LIMIT, /** Keep to real-time within a tolerance or die trying */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    49
  };
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
  RealtimeSimulatorImpl ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
  ~RealtimeSimulatorImpl ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    54
  virtual void Destroy ();
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    55
  virtual bool IsFinished (void) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    56
  virtual Time Next (void) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    57
  virtual void Stop (void);
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
    58
  virtual void Stop (Time const &time);
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    59
  virtual EventId Schedule (Time const &time, EventImpl *event);
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
    60
  virtual void ScheduleWithContext (uint32_t context, Time const &time, EventImpl *event);
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    61
  virtual EventId ScheduleNow (EventImpl *event);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    62
  virtual EventId ScheduleDestroy (EventImpl *event);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    63
  virtual void Remove (const EventId &ev);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    64
  virtual void Cancel (const EventId &ev);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    65
  virtual bool IsExpired (const EventId &ev) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    66
  virtual void Run (void);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    67
  virtual void RunOneEvent (void);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    68
  virtual Time Now (void) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    69
  virtual Time GetDelayLeft (const EventId &id) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    70
  virtual Time GetMaximumSimulationTime (void) const;
5507
915abd2b907b Simulator::SetScheduler now takes an ObjectFactory
Guillaume Seguin <guillaume@segu.in>
parents: 4552
diff changeset
    71
  virtual void SetScheduler (ObjectFactory schedulerFactory);
6113
0ce37bf4f1c1 Merge distributed simulation code
Josh Pelkey <jpelkey@gatech.edu>
parents: 5872
diff changeset
    72
  virtual uint32_t GetSystemId (void) const; 
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
    73
  virtual uint32_t GetContext (void) const;
3809
5e007004402e move MakeEvent out of Simulator
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3808
diff changeset
    74
5822
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
    75
  void ScheduleRealtimeWithContext (uint32_t context, Time const &time, EventImpl *event);
3816
edd4928b2046 remove realtime public API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3812
diff changeset
    76
  void ScheduleRealtime (Time const &time, EventImpl *event);
5822
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
    77
  void ScheduleRealtimeNowWithContext (uint32_t context, EventImpl *event);
3816
edd4928b2046 remove realtime public API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3812
diff changeset
    78
  void ScheduleRealtimeNow (EventImpl *event);
3801
dc1f5e534e56 slight reorganization and naming change for added realtime methods
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
    79
  Time RealtimeNow (void) const;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
  void SetSynchronizationMode (RealtimeSimulatorImpl::SynchronizationMode mode);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
  RealtimeSimulatorImpl::SynchronizationMode GetSynchronizationMode (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
  void SetHardLimit (Time limit);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
  Time GetHardLimit (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
private:
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
  bool Running (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
  bool Realtime (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
  void ProcessOneEvent (void);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
  uint64_t NextTs (void) const;
5872
188ecd74fcb4 It is not legal to call Dispose from within a destructor
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 5822
diff changeset
    93
  virtual void DoDispose (void);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
  typedef std::list<EventId> DestroyEvents;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
  DestroyEvents m_destroyEvents;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
  bool m_stop;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    98
  bool m_running;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    99
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   100
  // The following variables are protected using the m_mutex
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
  Ptr<Scheduler> m_events;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
  int m_unscheduledEvents;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
  uint32_t m_uid;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
  uint32_t m_currentUid;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   105
  uint64_t m_currentTs;
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   106
  uint32_t m_currentContext;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
  mutable SystemMutex m_mutex;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
  Ptr<Synchronizer> m_synchronizer;
3561
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   111
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
  /**
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
   * The policy to use if the simulation cannot keep synchronized to real-time.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   114
   */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   115
  SynchronizationMode m_synchronizationMode;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
  /**
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   118
   * The maximum allowable drift from real-time in SYNC_HARD_LIMIT mode.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
   */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
  Time m_hardLimit;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
};
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   122
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   123
} // namespace ns3
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
#endif /* REALTIME_SIMULATOR_IMPL_H */