A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
main-test-sync.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 
3 #include "ns3/simulator.h"
4 #include "ns3/realtime-simulator-impl.h"
5 #include "ns3/nstime.h"
6 #include "ns3/log.h"
7 #include "ns3/system-thread.h"
8 #include "ns3/string.h"
9 #include "ns3/config.h"
10 #include "ns3/global-value.h"
11 #include "ns3/ptr.h"
12 
13 #include <unistd.h>
14 #include <sys/time.h>
15 
16 using namespace ns3;
17 
18 NS_LOG_COMPONENT_DEFINE ("TestSync");
19 
20 bool gFirstRun = false;
21 
22 void
24 {
26  NS_LOG_UNCOND ("inserted_function() called at " <<
27  Simulator::Now ().GetSeconds () << " s");
28 }
29 
30 void
32 {
34  NS_LOG_UNCOND ("background_function() called at " <<
35  Simulator::Now ().GetSeconds () << " s");
36 }
37 
38 void
40 {
41  NS_LOG_UNCOND ("first_function() called at " <<
42  Simulator::Now ().GetSeconds () << " s");
43  gFirstRun = true;
44 }
45 
47 {
48 public:
49  FakeNetDevice ();
50  void Doit3 (void);
51 };
52 
54 {
56 }
57 
58 void
60 {
62  sleep (1);
63  for (uint32_t i = 0; i < 10000; ++i)
64  {
65  //
66  // Exercise the realtime relative now path
67  //
68  Simulator::ScheduleWithContext(0xffffffff, Seconds(0.0), MakeEvent (&inserted_function));
69  usleep (1000);
70  }
71 }
72 
73 
74 void
75 test (void)
76 {
77  GlobalValue::Bind ("SimulatorImplementationType",
78  StringValue ("ns3::RealtimeSimulatorImpl"));
79 
80  FakeNetDevice fnd;
81 
82  //
83  // Make sure ScheduleNow works when the system isn't running
84  //
85  Simulator::ScheduleWithContext(0xffffffff, Seconds(0.0), MakeEvent (&first_function));
86 
87  //
88  // drive the progression of m_currentTs at a ten millisecond rate from the main thread
89  //
90  for (double d = 0.; d < 14.999; d += 0.01)
91  {
93  }
94 
95  Ptr<SystemThread> st3 = Create<SystemThread> (
97  st3->Start ();
98 
99  Simulator::Stop (Seconds (15.0));
100  Simulator::Run ();
101  st3->Join ();
103 }
104 
105 int
106 main (int argc, char *argv[])
107 {
108  while (true)
109  {
110  test ();
111  }
112 }
113 
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
hold variables of type string
Definition: string.h:18
int main(int argc, char *argv[])
void Start(void)
Start a thread of execution, running the provided callback.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
static void Bind(std::string name, const AttributeValue &value)
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:905
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void first_function(void)
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionaly.
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
void Doit3(void)
void background_function(void)
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
bool gFirstRun
void Join(void)
Suspend the caller until the thread of execution, running the provided callback, finishes.
EventImpl * MakeEvent(void(*f)(void))
Definition: make-event.cc:8
void inserted_function(void)
void test(void)