samples/ns-2/simple.tcl.cc
changeset 320 2fa7caae08ea
parent 319 89bcaf124f51
child 322 45c63d88ce45
equal deleted inserted replaced
319:89bcaf124f51 320:2fa7caae08ea
    78 
    78 
    79 
    79 
    80 static void
    80 static void
    81 GenerateTraffic (UdpSocket *socket, uint32_t size)
    81 GenerateTraffic (UdpSocket *socket, uint32_t size)
    82 {
    82 {
    83   std::cout << "Node: " << socket->GetNode()->GetId () << " at=" << Simulator::Now ().GetSeconds () << "s, tx bytes=" << size << std::endl;
    83   std::cout << "Node: " << socket->GetNode()->GetId () 
       
    84             << " at=" << Simulator::Now ().GetSeconds () << "s,"
       
    85             << " tx bytes=" << size << std::endl;
    84   socket->SendDummy (size);
    86   socket->SendDummy (size);
    85   if (size > 50)
    87   if (size > 50)
    86     {
    88     {
    87       Simulator::Schedule (Seconds (0.5), &GenerateTraffic, socket, size - 50);
    89       Simulator::Schedule (Seconds (0.5), &GenerateTraffic, socket, size - 50);
    88     }
    90     }
    89 }
    91 }
    90 
    92 
    91 static void
    93 static void
    92 UdpSocketPrinter (UdpSocket *socket, uint32_t size, Ipv4Address from, uint16_t fromPort)
    94 UdpSocketPrinter (UdpSocket *socket, uint32_t size, Ipv4Address from, uint16_t fromPort)
    93 {
    95 {
    94   std::cout << "Node: " << socket->GetNode()->GetId () << " at=" << Simulator::Now ().GetSeconds () << "s, rx bytes=" << size << std::endl;
    96   std::cout << "Node: " << socket->GetNode()->GetId () 
       
    97             << " at=" << Simulator::Now ().GetSeconds () << "s,"
       
    98             << " rx bytes=" << size << std::endl;
    95 }
    99 }
    96 
   100 
    97 static void
   101 static void
    98 PrintTraffic (UdpSocket *socket)
   102 PrintTraffic (UdpSocket *socket)
    99 {
   103 {
   136   // const Rate& rate, 
   140   // const Rate& rate, 
   137   // const Time& delay,
   141   // const Time& delay,
   138   TraceContainer &traceContainer,
   142   TraceContainer &traceContainer,
   139   std::string &name) 
   143   std::string &name) 
   140 {
   144 {
   141     std::string qName;
   145   std::string qName;
   142     SerialChannel* channel = new SerialChannel();
   146   SerialChannel* channel = new SerialChannel();
   143 
   147 
   144     qName = name + "::Queue A";
   148   qName = name + "::Queue A";
   145     DropTailQueue* dtqa = new DropTailQueue(qName);
   149   DropTailQueue* dtqa = new DropTailQueue(qName);
   146     dtqa->RegisterTraces (traceContainer);
   150   dtqa->RegisterTraces (traceContainer);
   147 
   151 
   148     SerialNetDevice* neta = new SerialNetDevice(a, macaddra);
   152   SerialNetDevice* neta = new SerialNetDevice(a, macaddra);
   149     neta->AddQueue(dtqa);
   153   neta->AddQueue(dtqa);
   150     Ipv4Interface *interfA = new ArpIpv4Interface (a, neta);
   154   Ipv4Interface *interfA = new ArpIpv4Interface (a, neta);
   151     uint32_t indexA = a->GetIpv4 ()->AddInterface (interfA);
   155   uint32_t indexA = a->GetIpv4 ()->AddInterface (interfA);
   152     channel->Attach (neta);
   156   channel->Attach (neta);
   153     neta->Attach (channel);
   157   neta->Attach (channel);
   154 
   158   
   155     interfA->SetAddress (addra);
   159   interfA->SetAddress (addra);
   156     interfA->SetNetworkMask (netmask);
   160   interfA->SetNetworkMask (netmask);
   157     interfA->SetUp ();
   161   interfA->SetUp ();
   158 
   162 
   159     qName = name + "::Queue B";
   163   qName = name + "::Queue B";
   160     DropTailQueue* dtqb = new DropTailQueue(qName);
   164   DropTailQueue* dtqb = new DropTailQueue(qName);
   161     dtqb->RegisterTraces (traceContainer);
   165   dtqb->RegisterTraces (traceContainer);
   162 
   166 
   163     SerialNetDevice* netb = new SerialNetDevice(b, macaddrb);
   167   SerialNetDevice* netb = new SerialNetDevice(b, macaddrb);
   164     netb->AddQueue(dtqb);
   168   netb->AddQueue(dtqb);
   165     Ipv4Interface *interfB = new ArpIpv4Interface (b, netb);
   169   Ipv4Interface *interfB = new ArpIpv4Interface (b, netb);
   166     uint32_t indexB = b->GetIpv4 ()->AddInterface (interfB);
   170   uint32_t indexB = b->GetIpv4 ()->AddInterface (interfB);
   167     channel->Attach (netb);
   171   channel->Attach (netb);
   168     netb->Attach (channel);
   172   netb->Attach (channel);
   169 
   173 
   170     interfB->SetAddress (addrb);
   174   interfB->SetAddress (addrb);
   171     interfB->SetNetworkMask (netmask);
   175   interfB->SetNetworkMask (netmask);
   172     interfB->SetUp ();
   176   interfB->SetUp ();
   173 
   177 
   174     a->GetIpv4 ()->AddHostRouteTo (addrb, indexA);
   178   a->GetIpv4 ()->AddHostRouteTo (addrb, indexA);
   175     b->GetIpv4 ()->AddHostRouteTo (addra, indexB);
   179   b->GetIpv4 ()->AddHostRouteTo (addra, indexB);
   176 
   180 
   177     NS_DEBUG_UNCOND("Adding interface " << indexA << " to node " << a->GetId())
   181   NS_DEBUG_UNCOND("Adding interface " << indexA << " to node " << a->GetId());
   178     NS_DEBUG_UNCOND("Adding interface " << indexB << " to node " << b->GetId())
   182   NS_DEBUG_UNCOND("Adding interface " << indexB << " to node " << b->GetId());
   179 
   183 
   180     //PrintRoutingTable (a, "a");
   184   //PrintRoutingTable (a, "a");
   181     //PrintRoutingTable (b, "b");
   185   //PrintRoutingTable (b, "b");
   182 
   186 
   183     return channel;
   187   return channel;
   184 }
   188 }
   185 
   189 
   186 static void
   190 static void
   187 SetupTrace (TraceContainer &container, Tracer &tracer)
   191 SetupTrace (TraceContainer &container, Tracer &tracer)
   188 {
   192 {
   197 
   201 
   198 }
   202 }
   199 
   203 
   200 int main (int argc, char *argv[])
   204 int main (int argc, char *argv[])
   201 {
   205 {
   202     // set ns [new Simulator]
   206   // set ns [new Simulator]
   203     // ** not needed, Simulator is static object 
   207   // ** not needed, Simulator is static object 
   204 
   208 
   205     // set n0 [$ns node]
   209   // set n0 [$ns node]
   206     // set n1 [$ns node]
   210   // set n1 [$ns node]
   207     // set n2 [$ns node]
   211   // set n2 [$ns node]
   208     // set n3 [$ns node]
   212   // set n3 [$ns node]
   209 
   213 
   210     // ** Here, some kind of factory or topology object will instantiates 
   214   // ** Here, some kind of factory or topology object will instantiates 
   211     // ** four identical nodes; for now, we just explicitly create them
   215   // ** four identical nodes; for now, we just explicitly create them
   212     InternetNode *n0 = new InternetNode();
   216   InternetNode *n0 = new InternetNode();
   213     InternetNode *n1 = new InternetNode();
   217   InternetNode *n1 = new InternetNode();
   214     InternetNode *n2 = new InternetNode();
   218   InternetNode *n2 = new InternetNode();
   215     InternetNode *n3 = new InternetNode();
   219   InternetNode *n3 = new InternetNode();
   216 
   220 
   217     TraceContainer traceContainer;
   221   TraceContainer traceContainer;
   218 
   222 
   219     n0->SetName(std::string("Node 0"));
   223   n0->SetName(std::string("Node 0"));
   220     n1->SetName(std::string("Node 1"));
   224   n1->SetName(std::string("Node 1"));
   221     n2->SetName(std::string("Node 2"));
   225   n2->SetName(std::string("Node 2"));
   222     n3->SetName(std::string("Node 3"));
   226   n3->SetName(std::string("Node 3"));
   223  
   227   
   224     // set f [open out.tr w]
   228   // set f [open out.tr w]
   225     // $ns trace-all $f 
   229   // $ns trace-all $f 
   226     // set nf [open out.nam w]
   230   // set nf [open out.nam w]
   227     // $ns namtrace-all $nf
   231   // $ns namtrace-all $nf
   228     // ** tracing configuration occurs later **
   232   // ** tracing configuration occurs later **
   229 
   233 
   230     Tracer tracer("serial-net-test.log");
   234   Tracer tracer("serial-net-test.log");
   231     
   235     
   232     // $ns duplex-link $n0 $n2 5Mb 2ms DropTail
   236   // $ns duplex-link $n0 $n2 5Mb 2ms DropTail
   233     // $ns duplex-link $n1 $n2 5Mb 2ms DropTail
   237   // $ns duplex-link $n1 $n2 5Mb 2ms DropTail
   234     // $ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
   238   // $ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
   235     // ** part of topology creation object? **
   239   // ** part of topology creation object? **
   236     std::string channelName;
   240   std::string channelName;
   237 
   241     
   238     channelName = "Channel 1";
   242   channelName = "Channel 1";
   239     SerialChannel* ch1 = AddDuplexLink (
   243   SerialChannel* ch1 = AddDuplexLink (
   240       n0, Ipv4Address("10.1.1.1"), MacAddress("00:00:00:00:00:01"), 
   244       n0, Ipv4Address("10.1.1.1"), MacAddress("00:00:00:00:00:01"), 
   241       n2, Ipv4Address("10.1.1.2"), MacAddress("00:00:00:00:00:02"), 
   245       n2, Ipv4Address("10.1.1.2"), MacAddress("00:00:00:00:00:02"), 
   242       Ipv4Mask("255.255.255.0"), 
   246       Ipv4Mask("255.255.255.0"), 
   243       traceContainer, channelName);
   247       traceContainer, channelName);
   244     SetupTrace (traceContainer, tracer);
   248   SetupTrace (traceContainer, tracer);
   245 
   249 
   246     channelName = "Channel 2";
   250   channelName = "Channel 2";
   247     SerialChannel* ch2 = AddDuplexLink (
   251   SerialChannel* ch2 = AddDuplexLink (
   248       n1, Ipv4Address("10.1.2.1"), MacAddress("00:00:00:00:00:03"), 
   252       n1, Ipv4Address("10.1.2.1"), MacAddress("00:00:00:00:00:03"), 
   249       n2, Ipv4Address("10.1.2.2"), MacAddress("00:00:00:00:00:04"), 
   253       n2, Ipv4Address("10.1.2.2"), MacAddress("00:00:00:00:00:04"), 
   250       Ipv4Mask("255.255.255.0"), 
   254       Ipv4Mask("255.255.255.0"), 
   251       traceContainer, channelName);
   255       traceContainer, channelName);
   252     SetupTrace (traceContainer, tracer);
   256   SetupTrace (traceContainer, tracer);
   253 
   257 
   254     channelName = "Channel 3";
   258   channelName = "Channel 3";
   255     SerialChannel* ch3 = AddDuplexLink (
   259   SerialChannel* ch3 = AddDuplexLink (
   256       n2, Ipv4Address("10.1.3.1"), MacAddress("00:00:00:00:00:05"), 
   260       n2, Ipv4Address("10.1.3.1"), MacAddress("00:00:00:00:00:05"), 
   257       n3, Ipv4Address("10.1.3.2"), MacAddress("00:00:00:00:00:06"), 
   261       n3, Ipv4Address("10.1.3.2"), MacAddress("00:00:00:00:00:06"), 
   258       Ipv4Mask("255.255.255.0"), 
   262       Ipv4Mask("255.255.255.0"), 
   259       traceContainer, channelName);
   263       traceContainer, channelName);
   260     SetupTrace (traceContainer, tracer);
   264   SetupTrace (traceContainer, tracer);
   261 
   265   
   262     // $ns duplex-link-op $n0 $n2 orient right-up
   266   // $ns duplex-link-op $n0 $n2 orient right-up
   263     // $ns duplex-link-op $n1 $n2 orient right-down
   267   // $ns duplex-link-op $n1 $n2 orient right-down
   264     // $ns duplex-link-op $n2 $n3 orient right
   268   // $ns duplex-link-op $n2 $n3 orient right
   265     // $ns duplex-link-op $n2 $n3 queuePos 0.5
   269   // $ns duplex-link-op $n2 $n3 queuePos 0.5
   266     // ** nam commands-- not supported **
   270   // ** nam commands-- not supported **
   267 
   271   
   268     // set udp0 [new Agent/UDP]
   272   // set udp0 [new Agent/UDP]
   269     // $ns attach-agent $n0 $udp0
   273   // $ns attach-agent $n0 $udp0
   270     UdpSocket *source0 = new UdpSocket (n0);
   274   UdpSocket *source0 = new UdpSocket (n0);
   271 
   275 
   272     // set cbr0 [new Application/Traffic/CBR]
   276   // set cbr0 [new Application/Traffic/CBR]
   273     // $cbr0 attach-agent $udp0
   277   // $cbr0 attach-agent $udp0
   274 
   278   
   275     // set udp1 [new Agent/UDP]
   279   // set udp1 [new Agent/UDP]
   276     // $ns attach-agent $n3 $udp1
   280   // $ns attach-agent $n3 $udp1
   277     UdpSocket *source3 = new UdpSocket (n3);
   281   UdpSocket *source3 = new UdpSocket (n3);
   278 
   282 
   279     // $udp1 set class_ 1
   283   // $udp1 set class_ 1
   280     // ** class_ is for nam **
   284   // ** class_ is for nam **
   281 
   285 
   282     // set cbr1 [new Application/Traffic/CBR]
   286   // set cbr1 [new Application/Traffic/CBR]
   283     // $cbr1 attach-agent $udp1
   287   // $cbr1 attach-agent $udp1
   284     // 
   288   // 
   285     // set null0 [new Agent/Null]
   289   // set null0 [new Agent/Null]
   286     // $ns attach-agent $n3 $null0
   290   // $ns attach-agent $n3 $null0
   287     UdpSocket *sink3 = new UdpSocket(n3);
   291   UdpSocket *sink3 = new UdpSocket(n3);
   288     sink3->Bind (80);
   292   sink3->Bind (80);
   289    
   293    
   290     // set null1 [new Agent/Null]
   294   // set null1 [new Agent/Null]
   291     // $ns attach-agent $n1 $null1
   295   // $ns attach-agent $n1 $null1
   292     UdpSocket *sink1 = new UdpSocket(n1);
   296   UdpSocket *sink1 = new UdpSocket(n1);
   293     sink1->Bind (80);
   297   sink1->Bind (80);
   294 
   298 
   295     // $ns connect $udp0 $null0
   299   // $ns connect $udp0 $null0
   296     source0->SetDefaultDestination (Ipv4Address ("10.1.3.2"), 80);
   300   source0->SetDefaultDestination (Ipv4Address ("10.1.3.2"), 80);
   297     // $ns connect $udp1 $null1
   301   // $ns connect $udp1 $null1
   298     source3->SetDefaultDestination (Ipv4Address ("10.1.2.1"), 80);
   302   source3->SetDefaultDestination (Ipv4Address ("10.1.2.1"), 80);
   299 
   303 
   300     // Here, finish off packet routing configuration
   304   // Here, finish off packet routing configuration
   301     n0->GetIpv4()->SetDefaultRoute (Ipv4Address ("10.1.1.2"), 1);
   305   n0->GetIpv4()->SetDefaultRoute (Ipv4Address ("10.1.1.2"), 1);
   302     n3->GetIpv4()->SetDefaultRoute (Ipv4Address ("10.1.3.1"), 1);
   306   n3->GetIpv4()->SetDefaultRoute (Ipv4Address ("10.1.3.1"), 1);
   303 
   307 
   304     //PrintRoutingTable (n0, "n0");
   308   //PrintRoutingTable (n0, "n0");
   305     //PrintRoutingTable (n1, "n1");
   309   //PrintRoutingTable (n1, "n1");
   306     //PrintRoutingTable (n2, "n2");
   310   //PrintRoutingTable (n2, "n2");
   307     //PrintRoutingTable (n3, "n3");
   311   //PrintRoutingTable (n3, "n3");
   308 
   312   
   309     // $ns at 1.0 "$cbr0 start"
   313   // $ns at 1.0 "$cbr0 start"
   310     // $ns at 1.1 "$cbr1 start"
   314   // $ns at 1.1 "$cbr1 start"
   311 
   315 
   312 
   316 
   313     GenerateTraffic (source0, 100);
   317   GenerateTraffic (source0, 100);
   314     PrintTraffic (sink3);
   318   PrintTraffic (sink3);
   315 
   319 
   316     GenerateTraffic (source3, 100);
   320   GenerateTraffic (source3, 100);
   317     PrintTraffic (sink1);
   321   PrintTraffic (sink1);
   318     // 
   322   // 
   319     // set tcp [new Agent/TCP]
   323   // set tcp [new Agent/TCP]
   320     // $tcp set class_ 2
   324   // $tcp set class_ 2
   321     // set sink [new Agent/TCPSink]
   325   // set sink [new Agent/TCPSink]
   322     // $ns attach-agent $n0 $tcp
   326   // $ns attach-agent $n0 $tcp
   323     // $ns attach-agent $n3 $sink
   327   // $ns attach-agent $n3 $sink
   324     // $ns connect $tcp $sink
   328   // $ns connect $tcp $sink
   325     // set ftp [new Application/FTP]
   329   // set ftp [new Application/FTP]
   326     // $ftp attach-agent $tcp
   330   // $ftp attach-agent $tcp
   327     // $ns at 1.2 "$ftp start"
   331   // $ns at 1.2 "$ftp start"
   328     // 
   332   // 
   329     // $ns at 1.35 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
   333   // $ns at 1.35 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
   330     // ** TCP may not be in this snapshot **
   334   // ** TCP may not be in this snapshot **
   331     // 
   335   // 
   332     // puts [$cbr0 set packetSize_]
   336   // puts [$cbr0 set packetSize_]
   333     // puts [$cbr0 set interval_]
   337   // puts [$cbr0 set interval_]
   334     // 
   338   // 
   335     // $ns at 3.0 "finish"
   339   // $ns at 3.0 "finish"
   336     Simulator::StopAt (Seconds(3.0));
   340   Simulator::StopAt (Seconds(3.0));
   337     // 
   341   // 
   338     // proc finish {} {
   342   // proc finish {} {
   339     //         global ns f nf
   343   //         global ns f nf
   340     //         $ns flush-trace
   344   //         $ns flush-trace
   341     //         close $f
   345   //         close $f
   342     //         close $nf
   346   //         close $nf
   343     //         
   347   //         
   344     //         puts "running nam..."
   348   //         puts "running nam..."
   345     //         exec nam out.nam &
   349   //         exec nam out.nam &
   346     //         exit 0
   350   //         exit 0
   347     // }
   351   // }
   348     // ** this finish proc not needed? **
   352   // ** this finish proc not needed? **
   349     // 
   353   // 
   350     // $ns run
   354   // $ns run
   351     Simulator::Run ();
   355   Simulator::Run ();
   352     
   356     
   353     // The below deletes will be managed by future topology objects
   357   // The below deletes will be managed by future topology objects
   354     // or containers or smart pointers
   358   // or containers or smart pointers
   355     delete n0;
   359   delete n0;
   356     delete n1;
   360   delete n1;
   357     delete n2;
   361   delete n2;
   358     delete n3;
   362   delete n3;
   359     delete ch1;
   363   delete ch1;
   360     delete ch2;
   364   delete ch2;
   361     delete ch3;
   365   delete ch3;
   362     delete source3;
   366   delete source3;
   363     delete source0;
   367   delete source0;
   364     delete sink3;
   368   delete sink3;
   365     delete sink1;
   369   delete sink1;
   366 
   370 
   367     Simulator::Destroy ();
   371   Simulator::Destroy ();
   368 }
   372 }