A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
main-test-sync.cc
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
23 inserted_function (void)
24 {
25  NS_ASSERT (gFirstRun);
26  NS_LOG_UNCOND ("inserted_function() called at " <<
27  Simulator::Now ().GetSeconds () << " s");
28 }
29 
30 void
31 background_function (void)
32 {
33  NS_ASSERT (gFirstRun);
34  NS_LOG_UNCOND ("background_function() called at " <<
35  Simulator::Now ().GetSeconds () << " s");
36 }
37 
38 void
39 first_function (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 
53 FakeNetDevice::FakeNetDevice ()
54 {
56 }
57 
58 void
59 FakeNetDevice::Doit3 (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  {
92  Simulator::Schedule (Seconds (d), &background_function);
93  }
94 
95  Ptr<SystemThread> st3 = Create<SystemThread> (
96  MakeCallback (&FakeNetDevice::Doit3, &fnd));
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:59
hold variables of type string
Definition: string.h:19
void Start(void)
Start a thread of execution, running the provided callback.
#define NS_ASSERT(condition)
Definition: assert.h:64
static void Run(void)
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:502
static void Bind(std::string name, const AttributeValue &value)
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:900
static void Destroy(void)
Definition: simulator.cc:121
#define NS_LOG_UNCOND(msg)
Definition: log.h:343
static Time Now(void)
Definition: simulator.cc:179
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
static void Stop(void)
Definition: simulator.cc:164
void Join(void)
Suspend the caller until the thread of execution, running the provided callback, finishes.