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/system-thread.h"
25 #include "ns3/string.h"
26 #include "ns3/config.h"
27 #include "ns3/global-value.h"
28 #include "ns3/ptr.h"
29 
30 #include <unistd.h>
31 #include <sys/time.h>
32 
43 using namespace ns3;
44 
45 
46 NS_LOG_COMPONENT_DEFINE ("TestSync");
47 
48 namespace {
49 
51 bool gFirstRun = false;
52 
54 void
55 inserted_function (void)
56 {
57  NS_ASSERT (gFirstRun);
58  NS_LOG_UNCOND ("inserted_function() called at " <<
59  Simulator::Now ().GetSeconds () << " s");
60 }
61 
63 void
64 background_function (void)
65 {
66  NS_ASSERT (gFirstRun);
67  NS_LOG_UNCOND ("background_function() called at " <<
68  Simulator::Now ().GetSeconds () << " s");
69 }
70 
72 void
73 first_function (void)
74 {
75  NS_LOG_UNCOND ("first_function() called at " <<
76  Simulator::Now ().GetSeconds () << " s");
77  gFirstRun = true;
78 }
79 
81 class FakeNetDevice
82 {
83 public:
85  FakeNetDevice ();
87  void Doit3 (void);
88 };
89 
90 FakeNetDevice::FakeNetDevice ()
91 {
93 }
94 
95 void
96 FakeNetDevice::Doit3 (void)
97 {
99  sleep (1);
100  for (uint32_t i = 0; i < 10000; ++i)
101  {
102  //
103  // Exercise the realtime relative now path
104  //
106  usleep (1000);
107  }
108 }
109 
119 void
120 test (void)
121 {
122  GlobalValue::Bind ("SimulatorImplementationType",
123  StringValue ("ns3::RealtimeSimulatorImpl"));
124 
125  FakeNetDevice fnd;
126 
127  //
128  // Make sure ScheduleNow works when the system isn't running
129  //
130  Simulator::ScheduleWithContext(0xffffffff, Seconds(0.0), MakeEvent (&first_function));
131 
132  //
133  // drive the progression of m_currentTs at a ten millisecond rate from the main thread
134  //
135  for (double d = 0.; d < 14.999; d += 0.01)
136  {
137  Simulator::Schedule (Seconds (d), &background_function);
138  }
139 
140  Ptr<SystemThread> st3 = Create<SystemThread> (
141  MakeCallback (&FakeNetDevice::Doit3, &fnd));
142  st3->Start ();
143 
144  Simulator::Stop (Seconds (15.0));
145  Simulator::Run ();
146  st3->Join ();
148 }
149 
150 } // unnamed namespace
151 
152 
153 int
154 main (int argc, char *argv[])
155 {
157  cmd.Parse (argc, argv);
158 
159  while (true)
160  {
161  test ();
162  }
163 }
164 
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
Hold variables of type string.
Definition: string.h:41
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:67
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
tuple cmd
Definition: second.py:35
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1375
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
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...
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionaly.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
Flag for events not associated with any particular context.
Definition: simulator.h:192
static void ScheduleWithContext(uint32_t context, Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:1469
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
void Join(void)
Suspend the caller until the thread of execution, running the provided callback, finishes.
void Parse(int argc, char *argv[])
Parse the program arguments.
EventImpl * MakeEvent(void(*f)(void))
Make an EventImpl from a function pointer taking varying numbers of arguments.
Definition: make-event.cc:34