samples/main-simulator.cc
changeset 150 663120712cd9
parent 143 552ff26b4224
child 162 5b398ac221c7
equal deleted inserted replaced
149:d5b12472c5e2 150:663120712cd9
     1 /* -*- Mode:NS3; -*- */
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 #include "ns3/simulator.h"
     2 #include "ns3/simulator.h"
     3 #include "ns3/nstime.h"
     3 #include "ns3/nstime.h"
     4 #include <iostream>
     4 #include <iostream>
     5 
     5 
     6 using namespace ns3;
     6 using namespace ns3;
     7 
     7 
     8 class MyModel {
     8 class MyModel {
     9 public:
     9 public:
    10     void Start (void);
    10   void Start (void);
    11 private:
    11 private:
    12     void DealWithEvent (double eventValue);
    12   void DealWithEvent (double eventValue);
    13 };
    13 };
    14 
    14 
    15 void 
    15 void 
    16 MyModel::Start (void)
    16 MyModel::Start (void)
    17 {
    17 {
    18     Simulator::Schedule (Now () + Seconds (10.0), 
    18   Simulator::Schedule (Now () + Seconds (10.0), 
    19                          &MyModel::DealWithEvent, 
    19                        &MyModel::DealWithEvent, 
    20                          this, Simulator::Now ().ApproximateToSeconds ());
    20                        this, Simulator::Now ().ApproximateToSeconds ());
    21 }
    21 }
    22 void
    22 void
    23 MyModel::DealWithEvent (double value)
    23 MyModel::DealWithEvent (double value)
    24 {
    24 {
    25     std::cout << "Member method received event at " << Simulator::Now ().ApproximateToSeconds () 
    25   std::cout << "Member method received event at " << Simulator::Now ().ApproximateToSeconds () 
    26               << "s started at " << value << "s" << std::endl;
    26             << "s started at " << value << "s" << std::endl;
    27 }
    27 }
    28 
    28 
    29 static void 
    29 static void 
    30 random_function (MyModel *model)
    30 random_function (MyModel *model)
    31 {
    31 {
    32     std::cout << "random function received event at " << 
    32   std::cout << "random function received event at " << 
    33         Simulator::Now ().ApproximateToSeconds () << "s" << std::endl;
    33       Simulator::Now ().ApproximateToSeconds () << "s" << std::endl;
    34     model->Start ();
    34   model->Start ();
    35 }
    35 }
    36 
    36 
    37 
    37 
    38 int main (int argc, char *argv[])
    38 int main (int argc, char *argv[])
    39 {
    39 {
    40     MyModel model;
    40   MyModel model;
    41 
    41 
    42     Simulator::Schedule (Now () + Seconds (10.0), &random_function, &model);
    42   Simulator::Schedule (Now () + Seconds (10.0), &random_function, &model);
    43 
    43 
    44     Simulator::Run ();
    44   Simulator::Run ();
    45 
    45 
    46     Simulator::Destroy ();
    46   Simulator::Destroy ();
    47 }
    47 }