src/simulator/simulator.h
changeset 25 9b3bb088c560
parent 24 706b1d903da9
child 26 011c8d27b674
equal deleted inserted replaced
24:706b1d903da9 25:9b3bb088c560
    21 
    21 
    22 #ifndef SIMULATOR_H
    22 #ifndef SIMULATOR_H
    23 #define SIMULATOR_H
    23 #define SIMULATOR_H
    24 
    24 
    25 #include <stdint.h>
    25 #include <stdint.h>
    26 #include "event.h"
    26 #include "event-id.h"
       
    27 #include "event-impl.h"
       
    28 #include "time.h"
    27 
    29 
    28 namespace ns3 {
    30 namespace ns3 {
       
    31 
    29 
    32 
    30 class SimulatorPrivate;
    33 class SimulatorPrivate;
    31 
    34 
    32 /**
    35 /**
    33  * \brief Control the scheduling of simulation events. 
    36  * \brief Control the scheduling of simulation events. 
   102 	/**
   105 	/**
   103 	 * If Simulator::is_finished returns true, the behavior of this
   106 	 * If Simulator::is_finished returns true, the behavior of this
   104 	 * method is undefined. Otherwise, it returns the microsecond-based
   107 	 * method is undefined. Otherwise, it returns the microsecond-based
   105 	 * time of the next event expected to be scheduled.
   108 	 * time of the next event expected to be scheduled.
   106 	 */
   109 	 */
   107 	static uint64_t next_us (void);
   110 	static Time next (void);
   108 
   111 
   109 	/**
   112 	/**
   110 	 * Run the simulation until one of:
   113 	 * Run the simulation until one of:
   111 	 *   - no events are present anymore
   114 	 *   - no events are present anymore
   112 	 *   - the user called Simulator::stop
   115 	 *   - the user called Simulator::stop
   125 	 * Force the Simulator::run method to return to the caller
   128 	 * Force the Simulator::run method to return to the caller
   126 	 * when the expiration time of the next event to be processed 
   129 	 * when the expiration time of the next event to be processed 
   127 	 * is greater than or equal to the stop time.
   130 	 * is greater than or equal to the stop time.
   128 	 * @param at the stop time.
   131 	 * @param at the stop time.
   129 	 */
   132 	 */
   130 	static void stop_at_us (uint64_t at);
   133 	static void stop_at (Time time);
   131 
   134 
   132 	/**
   135 	/**
   133 	 * Schedule an event to expire at delta, relative to the
   136 	 * Schedule an event to expire at time.
   134 	 * current time.
   137 	 *
   135 	 * @param delta the expiration time relative to the current
   138 	 * @param delta the expiration time of the event.
   136 	 *        time. Expressed in microsecond units.
       
   137 	 * @param event the event to schedule.
   139 	 * @param event the event to schedule.
   138 	 */
   140 	 * @returns an id for the scheduled event.
   139 	static Event schedule_rel_us (uint64_t delta, Event event);
   141 	 */
   140 	/**
   142 	template <typename T>
   141 	 * Schedule an event to expire at delta, relative to the
   143 	static EventId schedule (Time time, void (T::*mem_ptr) (void), T *obj) {
   142 	 * current time.
   144 		class EventMemberImpl0 : public EventImpl {
   143 	 * @param delta the expiration time, relative to the current
   145 		public:
   144 	 *        time. Expressed in second units.
   146 			typedef void (T::*F)(void);
   145 	 * @param event the event to schedule.
   147 			EventMemberImpl0 (T *obj, F function) 
   146 	 */
   148 				: m_obj (obj), 
   147 	static Event schedule_rel_s (double delta, Event event);
   149 				  m_function (function)
   148 	/**
   150 			{}
   149 	 * Schedule an event to expire at an absolute time.
   151 			virtual ~EventMemberImpl0 () {}
   150 	 * @param time the expiration time. Expressed in 
   152 		private:
   151 	 *             microsecond units.
   153 			virtual void notify (void) { 
   152 	 * @param event the event to schedule.
   154 				(m_obj->*m_function) (); 
   153 	 */
   155 			}
   154 	static Event schedule_abs_us (uint64_t time, Event event);
   156 			T* m_obj;
   155 	/**
   157 			F m_function;
   156 	 * Schedule an event to expire at an absolute time.
   158 		} *ev = new EventMemberImpl0 (obj, mem_ptr);
   157 	 * @param time the expiration time. Expressed in 
   159 		return schedule (time, ev);
   158 	 *             second units.
   160 	}
   159 	 * @param event the event to schedule.
   161 	static EventId schedule (Time time, void (*f) (void)) {
   160 	 */
   162 		class EventFunctionImpl0 : public EventImpl {
   161 	static Event schedule_abs_s (double time, Event event);
   163 		public:
       
   164 			typedef void (*F)(void);
       
   165 			
       
   166 			EventFunctionImpl0 (F function) 
       
   167 				: m_function (function)
       
   168 			{}
       
   169 		protected:
       
   170 			virtual void notify (void) { 
       
   171 				(*m_function) (); 
       
   172 			}
       
   173 		private:
       
   174 			virtual ~EventFunctionImpl0 () {}
       
   175 			F m_function;
       
   176 		} *ev = new EventFunctionImpl0 (f);
       
   177 		return schedule (time, ev);
       
   178 	}
       
   179 	template <typename T1>
       
   180 	static EventId schedule (Time time, void (*f) (T1), T1 a1) {
       
   181 		class EventFunctionImpl1 : public EventImpl {
       
   182 		public:
       
   183 			typedef void (*F)(T1);
       
   184 			
       
   185 			EventFunctionImpl1 (F function, T1 a1) 
       
   186 				: m_function (function),
       
   187 				  m_a1 (a1)
       
   188 				{ }
       
   189 		protected:
       
   190 			virtual ~EventFunctionImpl1 () {}
       
   191 		private:
       
   192 			virtual void notify (void) { 
       
   193 				(*m_function) (m_a1);
       
   194 			}
       
   195 			F m_function;
       
   196 			T1 m_a1;
       
   197 		} *ev = new EventFunctionImpl1(f, a1);
       
   198 		return schedule (time, ev);
       
   199 	}
   162 	/**
   200 	/**
   163 	 * Unschedule the event. i.e.: the removed event never expires.
   201 	 * Unschedule the event. i.e.: the removed event never expires.
   164 	 * @param id the event to remove from the list of scheduled events.
   202 	 * @param id the event to remove from the list of scheduled events.
   165 	 */
   203 	 */
   166 	static Event remove (Event const id);
   204 	static void remove (EventId id);
   167 	/**
   205 	/*
   168 	 * Return the "current time" in microsecond units.
   206 	  XXX
   169 	 */
   207 	 */
   170 	static uint64_t now_us (void);
   208 	static void cancel (EventId id);
   171 	/**
   209 	/*
   172 	 * Return the "current time" in second units.
   210 	  XXX
   173 	 */
   211 	 */
   174 	static double now_s (void);
   212 	static bool is_expired (EventId id);
   175 	/**
   213 	/**
   176 	 * Schedule an event to expire right now. i.e., it will
   214 	 * Return the "current time".
   177 	 * expire after the currently-executing event is executed.
   215 	 */
   178 	 * If multiple events are scheduled with this method, 
   216 	static Time now (void);
   179 	 * they are executed in FIFO order: the events scheduled first
       
   180 	 * are executed first.
       
   181 	 * @param event the event to schedule now.
       
   182 	 */
       
   183 	static void schedule_now (Event event);
       
   184 	/**
       
   185 	 * Schedule an event to expire when the Simulator::destroy method
       
   186 	 * is invoked. Events are executed in FIFO order: the events
       
   187 	 * scheduled first are executed first.
       
   188 	 * @param event the event to schedule.
       
   189 	 */
       
   190 	static void schedule_destroy (Event event);
       
   191 private:
   217 private:
   192 	Simulator ();
   218 	Simulator ();
   193 	~Simulator ();
   219 	~Simulator ();
   194 	static SimulatorPrivate *get_priv (void);
   220 	static SimulatorPrivate *get_priv (void);
       
   221 	static EventId schedule (Time time, EventImpl *event);
   195 	static SimulatorPrivate *m_priv;
   222 	static SimulatorPrivate *m_priv;
   196 	static enum ListType {
   223 	static enum ListType {
   197 		LINKED_LIST,
   224 		LINKED_LIST,
   198 		BINARY_HEAP,
   225 		BINARY_HEAP,
       
   226 
   199 		STD_MAP
   227 		STD_MAP
   200 	} m_list_type;
   228 	} m_list_type;
   201 };
   229 };
   202 
   230 
   203 }; // namespace ns3
   231 }; // namespace ns3