src/simulator/realtime-simulator-impl.h
author Guillaume Seguin <guillaume@segu.in>
Thu, 12 Nov 2009 13:19:35 +0100
changeset 5507 915abd2b907b
parent 4552 8d3801089629
child 5521 37c6c83d4252
permissions -rw-r--r--
Simulator::SetScheduler now takes an ObjectFactory
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
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    28
#include "ns3/ptr.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    29
#include "ns3/assert.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    30
#include "ns3/log.h"
3650
516c4f22d251 Missing include
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3566
diff changeset
    31
#include "ns3/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);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    58
  virtual EventId Schedule (Time const &time, EventImpl *event);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    59
  virtual EventId ScheduleNow (EventImpl *event);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    60
  virtual EventId ScheduleDestroy (EventImpl *event);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    61
  virtual void Remove (const EventId &ev);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    62
  virtual void Cancel (const EventId &ev);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    63
  virtual bool IsExpired (const EventId &ev) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    64
  virtual void Run (void);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    65
  virtual void RunOneEvent (void);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    66
  virtual Time Now (void) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    67
  virtual Time GetDelayLeft (const EventId &id) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    68
  virtual Time GetMaximumSimulationTime (void) const;
5507
915abd2b907b Simulator::SetScheduler now takes an ObjectFactory
Guillaume Seguin <guillaume@segu.in>
parents: 4552
diff changeset
    69
  virtual void SetScheduler (ObjectFactory schedulerFactory);
3809
5e007004402e move MakeEvent out of Simulator
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3808
diff changeset
    70
3816
edd4928b2046 remove realtime public API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3812
diff changeset
    71
  void ScheduleRealtime (Time const &time, EventImpl *event);
edd4928b2046 remove realtime public API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3812
diff changeset
    72
  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
    73
  Time RealtimeNow (void) const;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    75
  void SetSynchronizationMode (RealtimeSimulatorImpl::SynchronizationMode mode);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    76
  RealtimeSimulatorImpl::SynchronizationMode GetSynchronizationMode (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
  void SetHardLimit (Time limit);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
  Time GetHardLimit (void) const;
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
private:
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
  bool Running (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
  bool Realtime (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
  void ProcessOneEvent (void);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
  uint64_t NextTs (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
  typedef std::list<EventId> DestroyEvents;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
  DestroyEvents m_destroyEvents;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
  bool m_stop;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
  bool m_running;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
  // 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
    94
  Ptr<Scheduler> m_events;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
  int m_unscheduledEvents;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
  uint32_t m_uid;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
  uint32_t m_currentUid;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    98
  uint64_t m_currentTs;
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
  mutable SystemMutex m_mutex;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
  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
   103
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
  /**
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   105
   * 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
   106
   */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
  SynchronizationMode m_synchronizationMode;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
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
   * 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
   111
   */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
  Time m_hardLimit;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
};
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
} // namespace ns3
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
#endif /* REALTIME_SIMULATOR_IMPL_H */