src/simulator/realtime-simulator-impl.h
author Guillaume Seguin <guillaume@segu.in>
Thu Nov 12 13:19:35 2009 +0100 (2009-11-12)
changeset 5507 915abd2b907b
parent 4552 8d3801089629
child 5521 37c6c83d4252
permissions -rw-r--r--
Simulator::SetScheduler now takes an ObjectFactory
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2008 University of Washington
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License version 2 as
     7  * published by the Free Software Foundation;
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17  */
    18 
    19 #ifndef REALTIME_SIMULATOR_IMPL_H
    20 #define REALTIME_SIMULATOR_IMPL_H
    21 
    22 #include "simulator-impl.h"
    23 
    24 #include "scheduler.h"
    25 #include "synchronizer.h"
    26 #include "event-impl.h"
    27 
    28 #include "ns3/ptr.h"
    29 #include "ns3/assert.h"
    30 #include "ns3/log.h"
    31 #include "ns3/system-mutex.h"
    32 
    33 #include <list>
    34 
    35 namespace ns3 {
    36 
    37 class RealtimeSimulatorImpl : public SimulatorImpl
    38 {
    39 public:
    40   static TypeId GetTypeId (void);
    41 
    42   /**
    43    * Enumeration of the types of packets supported in the class.
    44    *
    45    */
    46   enum SynchronizationMode {
    47     SYNC_BEST_EFFORT, /** Make a best effort to keep synced to real-time */
    48     SYNC_HARD_LIMIT, /** Keep to real-time within a tolerance or die trying */
    49   };
    50 
    51   RealtimeSimulatorImpl ();
    52   ~RealtimeSimulatorImpl ();
    53 
    54   virtual void Destroy ();
    55   virtual bool IsFinished (void) const;
    56   virtual Time Next (void) const;
    57   virtual void Stop (void);
    58   virtual EventId Schedule (Time const &time, EventImpl *event);
    59   virtual EventId ScheduleNow (EventImpl *event);
    60   virtual EventId ScheduleDestroy (EventImpl *event);
    61   virtual void Remove (const EventId &ev);
    62   virtual void Cancel (const EventId &ev);
    63   virtual bool IsExpired (const EventId &ev) const;
    64   virtual void Run (void);
    65   virtual void RunOneEvent (void);
    66   virtual Time Now (void) const;
    67   virtual Time GetDelayLeft (const EventId &id) const;
    68   virtual Time GetMaximumSimulationTime (void) const;
    69   virtual void SetScheduler (ObjectFactory schedulerFactory);
    70 
    71   void ScheduleRealtime (Time const &time, EventImpl *event);
    72   void ScheduleRealtimeNow (EventImpl *event);
    73   Time RealtimeNow (void) const;
    74 
    75   void SetSynchronizationMode (RealtimeSimulatorImpl::SynchronizationMode mode);
    76   RealtimeSimulatorImpl::SynchronizationMode GetSynchronizationMode (void) const;
    77 
    78   void SetHardLimit (Time limit);
    79   Time GetHardLimit (void) const;
    80 
    81 private:
    82   bool Running (void) const;
    83   bool Realtime (void) const;
    84 
    85   void ProcessOneEvent (void);
    86   uint64_t NextTs (void) const;
    87 
    88   typedef std::list<EventId> DestroyEvents;
    89   DestroyEvents m_destroyEvents;
    90   bool m_stop;
    91   bool m_running;
    92 
    93   // The following variables are protected using the m_mutex
    94   Ptr<Scheduler> m_events;
    95   int m_unscheduledEvents;
    96   uint32_t m_uid;
    97   uint32_t m_currentUid;
    98   uint64_t m_currentTs;
    99 
   100   mutable SystemMutex m_mutex;
   101 
   102   Ptr<Synchronizer> m_synchronizer;
   103 
   104   /**
   105    * The policy to use if the simulation cannot keep synchronized to real-time.
   106    */
   107   SynchronizationMode m_synchronizationMode;
   108 
   109   /**
   110    * The maximum allowable drift from real-time in SYNC_HARD_LIMIT mode.
   111    */
   112   Time m_hardLimit;
   113 };
   114 
   115 } // namespace ns3
   116 
   117 #endif /* REALTIME_SIMULATOR_IMPL_H */