A Discrete-Event Network Simulator
API
main-test-sync.cc
Go to the documentation of this file.
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 #include "ns3/command-line.h"
20 #include "ns3/simulator.h"
21 #include "ns3/realtime-simulator-impl.h"
22 #include "ns3/nstime.h"
23 #include "ns3/log.h"
24 #include "ns3/string.h"
25 #include "ns3/config.h"
26 #include "ns3/global-value.h"
27 #include "ns3/ptr.h"
28 
29 #include <chrono> // seconds, milliseconds
30 #include <thread> // sleep_for
31 
41 using namespace ns3;
42 
43 
44 NS_LOG_COMPONENT_DEFINE ("TestSync");
45 
46 namespace {
47 
49 bool gFirstRun = false;
50 
52 void
53 inserted_function (void)
54 {
55  NS_ASSERT (gFirstRun);
56  NS_LOG_UNCOND ("inserted_function() called at " <<
57  Simulator::Now ().GetSeconds () << " s");
58 }
59 
61 void
62 background_function (void)
63 {
64  NS_ASSERT (gFirstRun);
65  NS_LOG_UNCOND ("background_function() called at " <<
66  Simulator::Now ().GetSeconds () << " s");
67 }
68 
70 void
71 first_function (void)
72 {
73  NS_LOG_UNCOND ("first_function() called at " <<
74  Simulator::Now ().GetSeconds () << " s");
75  gFirstRun = true;
76 }
77 
79 class FakeNetDevice
80 {
81 public:
83  FakeNetDevice ();
85  void Doit3 (void);
86 };
87 
88 FakeNetDevice::FakeNetDevice ()
89 {
91 }
92 
93 void
94 FakeNetDevice::Doit3 (void)
95 {
97  std::this_thread::sleep_for (std::chrono::seconds (1));
98 
99  for (uint32_t i = 0; i < 10000; ++i)
100  {
101  //
102  // Exercise the realtime relative now path
103  //
105  std::this_thread::sleep_for (std::chrono::milliseconds (1));
106  }
107 }
108 
118 void
119 test (void)
120 {
121  GlobalValue::Bind ("SimulatorImplementationType",
122  StringValue ("ns3::RealtimeSimulatorImpl"));
123 
124  FakeNetDevice fnd;
125 
126  //
127  // Make sure ScheduleNow works when the system isn't running
128  //
129  Simulator::ScheduleWithContext (0xffffffff, Seconds (0.0), MakeEvent (&first_function));
130 
131  //
132  // drive the progression of m_currentTs at a ten millisecond rate from the main thread
133  //
134  for (double d = 0.; d < 14.999; d += 0.01)
135  {
136  Simulator::Schedule (Seconds (d), &background_function);
137  }
138 
139  std::thread st3 = std::thread (&FakeNetDevice::Doit3, &fnd);
140 
141  Simulator::Stop (Seconds (15.0));
142  Simulator::Run ();
143 
144  if (st3.joinable ())
145  {
146  st3.join ();
147  }
148 
150 }
151 
152 } // unnamed namespace
153 
154 
155 int
156 main (int argc, char *argv[])
157 {
158  CommandLine cmd (__FILE__);
159  cmd.Parse (argc, argv);
160 
161  while (true)
162  {
163  test ();
164  }
165 }
166 
Parse command-line arguments.
Definition: command-line.h:229
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 void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:571
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition: simulator.h:199
Hold variables of type string.
Definition: string.h:41
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
#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:205
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
EventImpl * MakeEvent(void(*f)(void))
Make an EventImpl from a function pointer taking varying numbers of arguments.
Definition: make-event.cc:34
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:35