A Discrete-Event Network Simulator
API
main-test-sync.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17 
18 #include "ns3/command-line.h"
19 #include "ns3/config.h"
20 #include "ns3/global-value.h"
21 #include "ns3/log.h"
22 #include "ns3/nstime.h"
23 #include "ns3/ptr.h"
24 #include "ns3/realtime-simulator-impl.h"
25 #include "ns3/simulator.h"
26 #include "ns3/string.h"
27 
28 #include <chrono> // seconds, milliseconds
29 #include <thread> // sleep_for
30 
40 using namespace ns3;
41 
42 NS_LOG_COMPONENT_DEFINE("TestSync");
43 
44 namespace
45 {
46 
48 bool gFirstRun = false;
49 
51 void
52 inserted_function()
53 {
54  NS_ASSERT(gFirstRun);
55  NS_LOG_UNCOND("inserted_function() called at " << Simulator::Now().GetSeconds() << " s");
56 }
57 
59 void
60 background_function()
61 {
62  NS_ASSERT(gFirstRun);
63  NS_LOG_UNCOND("background_function() called at " << Simulator::Now().GetSeconds() << " s");
64 }
65 
67 void
68 first_function()
69 {
70  NS_LOG_UNCOND("first_function() called at " << Simulator::Now().GetSeconds() << " s");
71  gFirstRun = true;
72 }
73 
75 class FakeNetDevice
76 {
77  public:
79  FakeNetDevice();
81  void Doit3();
82 };
83 
84 FakeNetDevice::FakeNetDevice()
85 {
87 }
88 
89 void
90 FakeNetDevice::Doit3()
91 {
93  std::this_thread::sleep_for(std::chrono::seconds(1));
94 
95  for (uint32_t i = 0; i < 10000; ++i)
96  {
97  //
98  // Exercise the realtime relative now path
99  //
101  Seconds(0.0),
102  MakeEvent(&inserted_function));
103  std::this_thread::sleep_for(std::chrono::milliseconds(1));
104  }
105 }
106 
116 void
117 test()
118 {
119  GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
120 
121  FakeNetDevice fnd;
122 
123  //
124  // Make sure ScheduleNow works when the system isn't running
125  //
126  Simulator::ScheduleWithContext(0xffffffff, Seconds(0.0), MakeEvent(&first_function));
127 
128  //
129  // drive the progression of m_currentTs at a ten millisecond rate from the main thread
130  //
131  for (double d = 0.; d < 14.999; d += 0.01)
132  {
133  Simulator::Schedule(Seconds(d), &background_function);
134  }
135 
136  std::thread st3 = std::thread(&FakeNetDevice::Doit3, &fnd);
137 
138  Simulator::Stop(Seconds(15.0));
139  Simulator::Run();
140 
141  if (st3.joinable())
142  {
143  st3.join();
144  }
145 
147 }
148 
149 } // unnamed namespace
150 
151 int
152 main(int argc, char* argv[])
153 {
154  CommandLine cmd(__FILE__);
155  cmd.Parse(argc, argv);
156 
157  while (true)
158  {
159  test();
160  }
161 
162  return 0;
163 }
Parse command-line arguments.
Definition: command-line.h:232
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition: simulator.h:210
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
EventImpl * MakeEvent(void(*f)())
Make an EventImpl from a function pointer taking varying numbers of arguments.
Definition: make-event.cc:36
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40