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/simulator.h"
20 #include "ns3/realtime-simulator-impl.h"
21 #include "ns3/nstime.h"
22 #include "ns3/log.h"
23 #include "ns3/system-thread.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 <unistd.h>
30 #include <sys/time.h>
31 
41 using namespace ns3;
42 
43 NS_LOG_COMPONENT_DEFINE ("TestSync");
44 
46 bool gFirstRun = false;
47 
49 void
51 {
53  NS_LOG_UNCOND ("inserted_function() called at " <<
54  Simulator::Now ().GetSeconds () << " s");
55 }
56 
58 void
60 {
62  NS_LOG_UNCOND ("background_function() called at " <<
63  Simulator::Now ().GetSeconds () << " s");
64 }
65 
67 void
69 {
70  NS_LOG_UNCOND ("first_function() called at " <<
71  Simulator::Now ().GetSeconds () << " s");
72  gFirstRun = true;
73 }
74 
77 {
78 public:
80  FakeNetDevice ();
82  void Doit3 (void);
83 };
84 
86 {
88 }
89 
90 void
92 {
94  sleep (1);
95  for (uint32_t i = 0; i < 10000; ++i)
96  {
97  //
98  // Exercise the realtime relative now path
99  //
101  usleep (1000);
102  }
103 }
104 
114 void
115 test (void)
116 {
117  GlobalValue::Bind ("SimulatorImplementationType",
118  StringValue ("ns3::RealtimeSimulatorImpl"));
119 
120  FakeNetDevice fnd;
121 
122  //
123  // Make sure ScheduleNow works when the system isn't running
124  //
126 
127  //
128  // drive the progression of m_currentTs at a ten millisecond rate from the main thread
129  //
130  for (double d = 0.; d < 14.999; d += 0.01)
131  {
133  }
134 
135  Ptr<SystemThread> st3 = Create<SystemThread> (
137  st3->Start ();
138 
139  Simulator::Stop (Seconds (15.0));
140  Simulator::Run ();
141  st3->Join ();
143 }
144 
145 int
146 main (int argc, char *argv[])
147 {
148  while (true)
149  {
150  test ();
151  }
152 }
153 
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:200
#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.
FakeNetDevice()
Constructor.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1216
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1480
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 Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:164
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void first_function(void)
An event method called once from the main thread.
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionaly.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
void Doit3(void)
The thread entry point.
static void ScheduleWithContext(uint32_t context, Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:1297
void background_function(void)
An event method called many times from the main thread.
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:208
bool gFirstRun
Check that the event functions run in the intended order.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void Join(void)
Suspend the caller until the thread of execution, running the provided callback, finishes.
Example class with a method for the background task.
void inserted_function(void)
An event method called many times from the background thread.
void test(void)
Example use of ns3::SystemThread.
EventImpl * MakeEvent(void(*f)(void))
Make an EventImpl from a function pointer taking varying numbers of arguments.
Definition: make-event.cc:34