00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2008 University of Washington 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 */ 00018 00019 #ifndef REALTIME_SIMULATOR_IMPL_H 00020 #define REALTIME_SIMULATOR_IMPL_H 00021 00022 #include "simulator-impl.h" 00023 00024 #include "scheduler.h" 00025 #include "synchronizer.h" 00026 #include "event-impl.h" 00027 00028 #include "ns3/ptr.h" 00029 #include "ns3/assert.h" 00030 #include "ns3/log.h" 00031 #include "ns3/system-mutex.h" 00032 00033 #include <list> 00034 00035 namespace ns3 { 00036 00037 class RealtimeSimulatorImpl : public SimulatorImpl 00038 { 00039 public: 00040 static TypeId GetTypeId (void); 00041 00042 /** 00043 * Enumeration of the types of packets supported in the class. 00044 * 00045 */ 00046 enum SynchronizationMode { 00047 SYNC_BEST_EFFORT, /** Make a best effort to keep synced to real-time */ 00048 SYNC_HARD_LIMIT, /** Keep to real-time within a tolerance or die trying */ 00049 }; 00050 00051 RealtimeSimulatorImpl (); 00052 ~RealtimeSimulatorImpl (); 00053 00054 virtual void Destroy (); 00055 virtual bool IsFinished (void) const; 00056 virtual Time Next (void) const; 00057 virtual void Stop (void); 00058 virtual EventId Schedule (Time const &time, EventImpl *event); 00059 virtual EventId ScheduleNow (EventImpl *event); 00060 virtual EventId ScheduleDestroy (EventImpl *event); 00061 virtual void Remove (const EventId &ev); 00062 virtual void Cancel (const EventId &ev); 00063 virtual bool IsExpired (const EventId &ev) const; 00064 virtual void Run (void); 00065 virtual void RunOneEvent (void); 00066 virtual Time Now (void) const; 00067 virtual Time GetDelayLeft (const EventId &id) const; 00068 virtual Time GetMaximumSimulationTime (void) const; 00069 virtual void SetScheduler (Ptr<Scheduler> scheduler); 00070 virtual Ptr<Scheduler> GetScheduler (void) const; 00071 00072 void ScheduleRealtime (Time const &time, EventImpl *event); 00073 void ScheduleRealtimeNow (EventImpl *event); 00074 Time RealtimeNow (void) const; 00075 00076 void SetSynchronizationMode (RealtimeSimulatorImpl::SynchronizationMode mode); 00077 RealtimeSimulatorImpl::SynchronizationMode GetSynchronizationMode (void) const; 00078 00079 void SetHardLimit (Time limit); 00080 Time GetHardLimit (void) const; 00081 00082 private: 00083 bool Running (void) const; 00084 bool Realtime (void) const; 00085 00086 void ProcessOneEvent (void); 00087 uint64_t NextTs (void) const; 00088 00089 typedef std::list<EventId> DestroyEvents; 00090 DestroyEvents m_destroyEvents; 00091 bool m_stop; 00092 bool m_running; 00093 00094 // The following variables are protected using the m_mutex 00095 Ptr<Scheduler> m_events; 00096 int m_unscheduledEvents; 00097 uint32_t m_uid; 00098 uint32_t m_currentUid; 00099 uint64_t m_currentTs; 00100 00101 mutable SystemMutex m_mutex; 00102 00103 Ptr<Synchronizer> m_synchronizer; 00104 00105 /** 00106 * The policy to use if the simulation cannot keep synchronized to real-time. 00107 */ 00108 SynchronizationMode m_synchronizationMode; 00109 00110 /** 00111 * The maximum allowable drift from real-time in SYNC_HARD_LIMIT mode. 00112 */ 00113 Time m_hardLimit; 00114 }; 00115 00116 } // namespace ns3 00117 00118 #endif /* REALTIME_SIMULATOR_IMPL_H */