### Run a single C++ example with --example=udp-echo Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Use this command to run a single C++ example. The relative path for the example does not need to be included, and the example must be registered in the relevant examples-to-run.py file. ```bash $ ./test.py --example=udp-echo ``` ```text PASS: Example examples/udp/udp-echo ``` -------------------------------- ### Emitter setup with Names::Add and Simulator::Schedule Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Creates an Emitter object, adds it to the Names system, and schedules its Start method. The Emitter is not associated with an ns-3 node, so it must be started manually. ```cpp Ptr emitter = CreateObject (); Names::Add ("/Names/Emitter", emitter); // The Emitter object is not associated with an ns-3 node, so // it won't get started automatically, so we need to do this ourselves Simulator::Schedule (Seconds (0.0), &Emitter::Start, emitter); ``` -------------------------------- ### Declare multi-file C++ example Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Shows how to split an example implementation among several source files. Include the additional files as explicit sources of the example. ```python obj = bld.create_ns3_program('new-module-example', [new-module]) obj.source = ['new-module-example.cc', 'new-module-example-part.cc'] ``` -------------------------------- ### Run a single Python example with --pyexample Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Use this command to run a single Python example. The relative path for the example must be included, and Python examples need their extensions. ```bash $ ./test.py --pyexample=examples/tutorial/first.py ``` ```text PASS: Example examples/tutorial/first.py ``` -------------------------------- ### Build with build.py enabling examples and tests Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Run build.py with --enable-examples and --enable-tests to build with examples and tests. ```bash $ ./build.py --enable-examples --enable-tests ``` ```text 170 of 170 tests passed (170 passed, 0 skipped, 0 failed, 0 crashed, 0 valgrind errors) ``` -------------------------------- ### Build with waf enabling examples and tests Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Run waf configure with --enable-examples and --enable-tests, then build to enable examples and tests. ```bash $ ./waf configure --enable-examples --enable-tests $ ./waf build ``` ```text 170 of 170 tests passed (170 passed, 0 skipped, 0 failed, 0 crashed, 0 valgrind errors) ``` -------------------------------- ### Build with build.py without examples or tests Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Run build.py from the ns-3-allinone directory to build without examples or tests. ```bash $ ./build.py ``` ```text 0 of 0 tests passed (0 passed, 0 skipped, 0 failed, 0 crashed, 0 valgrind errors) ``` -------------------------------- ### ConfigureFile() usage example from seventh.cc Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Example call to ConfigureFile() as used in the seventh.cc code, configuring a formatted output file. ```cpp fileHelper.ConfigureFile ("seventh-packet-byte-count", FileAggregator::FORMATTED); ``` -------------------------------- ### Run realtime-udp-echo example Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Command to run the real-time UDP echo example script, which demonstrates how to configure real-time behavior. ```bash $ ./waf --run realtime-udp-echo ``` -------------------------------- ### Run test.py with examples and tests Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Output of running test.py when examples and tests are enabled, showing that all tests pass. ```text 170 of 170 tests passed (170 passed, 0 skipped, 0 failed, 0 crashed, 0 valgrind errors) ``` -------------------------------- ### Configure ns-3 with tests and examples enabled Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Run this command during the configuration stage to enable tests and examples so that test.py can run them. This is required before building and running tests. ```bash $ ./waf --configure --enable-examples --enable-tests ``` -------------------------------- ### File helper example output file (first 10 lines) Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html First 10 lines of the output file 'file-helper-example.txt' created by the file-helper-example.cc example. Shows the formatted output with time and count columns. ```text Time (Seconds) = 0.203 Count = 1 Time (Seconds) = 0.702 Count = 2 Time (Seconds) = 1.404 Count = 3 Time (Seconds) = 2.368 Count = 4 Time (Seconds) = 3.364 Count = 5 Time (Seconds) = 3.579 Count = 6 Time (Seconds) = 5.873 Count = 7 Time (Seconds) = 6.410 Count = 8 Time (Seconds) = 6.472 Count = 9 ... ``` -------------------------------- ### Declare Python example in examples/wscript Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Specify a Python example using the 'register_ns3_script' function. The second argument is the list of modules the Python example depends on. ```python bld.register_ns3_script('new-module-example.py', ['new-module']) ``` -------------------------------- ### Configure .ns3rc to enable examples and tests Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Modified .ns3rc file with examples_enabled and tests_enabled set to True, enabling examples and tests to be built and run. ```python #! /usr/bin/env python # A list of the modules that will be enabled when ns-3 is run. # Modules that depend on the listed modules will be enabled also. # # All modules can be enabled by choosing 'all_modules'. modules_enabled = ['all_modules'] # Set this equal to true if you want examples to be run. examples_enabled = True # Set this equal to true if you want tests to be run. tests_enabled = True ``` -------------------------------- ### Build with waf without examples or tests Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Run waf configure and build from the top level ns-3 directory to build without examples or tests. ```bash $ ./waf configure $ ./waf build ``` ```text 0 of 0 tests passed (0 passed, 0 skipped, 0 failed, 0 crashed, 0 valgrind errors) ``` -------------------------------- ### Formatted output file example Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Example output file created by the FileAggregator example, showing formatted values with 'Time = ... Value = ...'. ```text Time = -5.000e+00 Value = 25 Time = -4.000e+00 Value = 16 Time = -3.000e+00 Value = 9 Time = -2.000e+00 Value = 4 Time = -1.000e+00 Value = 1 Time = 0.000e+00 Value = 0 Time = 1.000e+00 Value = 1 Time = 2.000e+00 Value = 4 Time = 3.000e+00 Value = 9 Time = 4.000e+00 Value = 16 Time = 5.000e+00 Value = 25 ``` -------------------------------- ### Reconfigure and build after installing GTK libraries Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html After installing a suitable GTK version, rerun waf configure and waf to enable GtkConfigStore. ```bash $ ./waf configure --enable-examples --enable-tests $ ./waf ``` -------------------------------- ### C++ and Python example lists in src/spectrum/test/examples-to-run.py Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Defines the cpp_examples and python_examples lists that control which spectrum model examples are run by test.py. Each C++ tuple contains the example name, do_run, and do_valgrind_run flags; each Python tuple contains the example name and do_run flag. ```python # A list of C++ examples to run in order to ensure that they remain # buildable and runnable over time. Each tuple in the list contains # # (example_name, do_run, do_valgrind_run). # # See test.py for more information. cpp_examples = [ ("adhoc-aloha-ideal-phy", "True", "True"), ("adhoc-aloha-ideal-phy-with-microwave-oven", "True", "True"), ("adhoc-aloha-ideal-phy-matrix-propagation-loss-model", "True", "True"), ] # A list of Python examples to run in order to ensure that they remain # runnable over time. Each tuple in the list contains # # (example_name, do_run). # # See test.py for more information. python_examples = [ ( ``` -------------------------------- ### Specify build path with --buildpath Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Use this command to specify the directory where ns-3 was built when running an example. The --buildpath option is used in conjunction with --example. ```bash $ ./test.py --buildpath=/home/craigdo/repos/ns-3-allinone-test/ns-3-dev/build/debug --example=wifi-simple-adhoc ``` -------------------------------- ### Install GTK libraries for ConfigStore GUI Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Ubuntu command to install libgtk and libgtk-dev, required to use the GTK-based ConfigStore front end. ```bash $ sudo apt-get install libgtk2.0-0 libgtk2.0-dev ``` -------------------------------- ### Comma-separated output file example Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Example output file created by the FileAggregator example, showing 2 columns of values separated by commas. ```text -5,25 -4,16 -3,9 -2,4 -1,1 0,0 1,1 2,4 3,9 4,16 5,25 ``` -------------------------------- ### Build ns-3 without examples or tests Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Commands to configure and build ns-3 from the top-level directory when examples and tests are disabled. ```bash $ ./waf configure $ ./waf build ``` -------------------------------- ### test.py --help command output Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Shows the usage and options for the test.py script. Run `test.py --help` to see this command summary. Options include -b/--buildpath to specify the ns-3 build directory, -c/--constrain to filter tests by kind, -e/--example to run a single example, -d/--duration to print test durations, -u/--update-data to regenerate reference data files, and -f/--fullness to choose the duration of tests to run (QUICK, EXTENSIVE, or TAKES_FOREVER). ```text Usage: test.py [options] Options: -h, --help show this help message and exit -b BUILDPATH, --buildpath=BUILDPATH specify the path where ns-3 was built (defaults to the build directory for the current variant) -c KIND, --constrain=KIND constrain the test-runner by kind of test -e EXAMPLE, --example=EXAMPLE specify a single example to run (no relative path is needed) -d, --duration print the duration of each test suite and example -e EXAMPLE, --example=EXAMPLE specify a single example to run (no relative path is needed) -u, --update-data If examples use reference data files, get them to re- generate them -f FULLNESS, --fullness=FULLNESS choose the duration of tests to run: QUICK, EXTENSIVE, or TAKES_FOREVER, where EXTENSIVE includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK and TAKES_FOREVER includes QUICK ``` -------------------------------- ### Declare C++ example in examples/wscript Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Define a C++ example program in the examples/wscript file. The 'create_ns3_program' function creates a program with the given name and module dependencies. The second argument is the list of modules the program depends on; include 'new-module' in the list. Only list direct module dependencies. ```python def build(bld): obj = bld.create_ns3_program('adhoc-aloha-ideal-phy', ['spectrum', 'mobility']) obj.source = 'adhoc-aloha-ideal-phy.cc' ``` -------------------------------- ### WriteProbe() usage from seventh.cc Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Example call to WriteProbe() from the seventh.cc example. It creates an Ipv4PacketProbe and writes the OutputBytes trace source for all nodes' Ipv4L3Protocol Tx trace. ```cpp fileHelper.WriteProbe ("ns3::Ipv4PacketProbe", "/NodeList/*/$ns3::Ipv4L3Protocol/Tx", "OutputBytes"); ``` -------------------------------- ### Read and write ConfigStore attributes with input-defaults.xml and output-attributes.xml Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Complete example that loads defaults from input-defaults.xml, allows command-line overrides, and saves resulting attributes to output-attributes.xml before Simulator::Run(). The ConfigStore attributes Filename, Mode, and FileFormat must be set either via command-line or program statements. ```cpp #include "ns3/config-store-module.h" ... int main (...) { Config::SetDefault ("ns3::ConfigStore::Filename", StringValue ("input-defaults.xml")); Config::SetDefault ("ns3::ConfigStore::Mode", StringValue ("Load")); Config::SetDefault ("ns3::ConfigStore::FileFormat", StringValue ("Xml")); ConfigStore inputConfig; inputConfig.ConfigureDefaults (); // // Allow the user to override any of the defaults and the above Bind () at // run-time, viacommand-line arguments // CommandLine cmd; cmd.Parse (argc, argv); // setup topology ... // Invoke just before entering Simulator::Run () Config::SetDefault ("ns3::ConfigStore::Filename", StringValue ("output-attributes.xml")); Config::SetDefault ("ns3::ConfigStore::Mode", StringValue ("Save")); ConfigStore outputConfig; outputConfig.ConfigureAttributes (); Simulator::Run (); } ``` -------------------------------- ### CreateObject example Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Creates a Node instance using the CreateObject function, which initializes attributes. ```cpp Ptr n = CreateObject (); ``` -------------------------------- ### Include public header example Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Shows how users include public headers of the module in their code. The header must be declared in the wscript as shown in the previous example. ```cpp #include "ns3/spectrum-model.h" ``` -------------------------------- ### GnuplotHelper usage example from seventh.cc Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Fully worked example showing how to create a GnuplotHelper, configure the plot, and plot a probe with a key placement option. The probeType and tracePath are defined separately for IPv4. ```cpp // Create the gnuplot helper. GnuplotHelper plotHelper; // Configure the plot. // Configure the plot. The first argument is the file name prefix // for the output files generated. The second, third, and fourth // arguments are, respectively, the plot title, x-axis, and y-axis labels plotHelper.ConfigurePlot ("seventh-packet-byte-count", "Packet Byte Count vs. Time", "Time (Seconds)", "Packet Byte Count", "png"); // Specify the probe type, trace source path (in configuration namespace), and // probe output trace source ("OutputBytes") to plot. The fourth argument // specifies the name of the data series label on the plot. The last // argument formats the plot by specifying where the key should be placed. plotHelper.PlotProbe (probeType, tracePath, "OutputBytes", "Packet Byte Count", GnuplotAggregator::KEY_BELOW); ``` ```cpp probeType = "ns3::Ipv4PacketProbe"; tracePath = "/NodeList/*/$ns3::Ipv4L3Protocol/Tx"; ``` -------------------------------- ### Usage example of SpecificFunctor Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Shows a complete example: a class A with a method Hello, and main() that creates an instance and calls the method via a SpecificFunctor. Note: this is illustrative, not real ns-3 code. ```cpp class A { public: A (int a0) : a (a0) {} int Hello (int b0) { std::cout << "Hello from A, a = " << a << " b0 = " << b0 << std::endl; } int a; }; int main() { A a(10); SpecificFunctor sf(&a, &A::Hello); sf(5); } ``` -------------------------------- ### Run gnuplot-example with waf shell Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Commands to run the gnuplot-example program from the ns-3 build directory. The example is located at src/stats/examples/gnuplot-example.cc. ```bash $ ./waf shell $ cd build/debug/src/stats/examples $ ./gnuplot-example ``` -------------------------------- ### Create nodes and network stacks with ns3::NodeContainer, WiFiHelper, and InternetStackHelper Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Creates two wireless nodes, configures the WiFi PHY and MAC layers for ad hoc mode, installs the internet stack, and assigns IP addresses to the nodes. ```cpp NodeContainer nodes; nodes.Create(2); WifiHelper wifi; wifi.SetMac("ns3::AdhocWifiMac"); wifi.SetPhy("ns3::WifiPhy"); NetDeviceContainer nodeDevices = wifi.Install(nodes); InternetStackHelper internet; internet.Install(nodes); Ipv4AddressHelper ipAddrs; ipAddrs.SetBase("192.168.0.0", "255.255.255.0"); ipAddrs.Assign(nodeDevices); ``` -------------------------------- ### Example trace helper method calls Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Shows typical calls to enable pcap and ascii tracing on point-to-point and CSMA devices, including file naming conventions. ```cpp pointToPoint.EnablePcapAll ("second"); pointToPoint.EnablePcap ("second", p2pNodes.Get (0)->GetId (), 0); csma.EnablePcap ("third", csmaDevices.Get (0), true); pointToPoint.EnableAsciiAll (ascii.CreateFileStream ("myfirst.tr")); ``` -------------------------------- ### Modified .ns3rc to enable core module with examples and tests Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Modify the .ns3rc file to enable only the core module and enable examples and tests. ```python #! /usr/bin/env python # A list of the modules that will be enabled when ns-3 is run. # Modules that depend on the listed modules will be enabled also. # # All modules can be enabled by choosing 'all_modules'. modules_enabled = ['core'] # Set this equal to true if you want examples to be run. examples_enabled = True # Set this equal to true if you want tests to be run. tests_enabled = True ``` -------------------------------- ### Set up bindings toolchain with bake build system Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Commands to configure, download, and build the ns-allinone-3.26 target using the bake build system, which provides an automated setup for the Python bindings toolchain. This toolchain is only supported for gcc version 4; gcc-5, gcc-6, and clang are not supported. ```bash ./bake.py configure -e ns-allinone-3.26 ./bake.py download ./bake.py build ``` -------------------------------- ### NS_LOG with severity prefix example Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Enables logging for all components and all severity classes, with the severity class prefixed. Shows the command and its output. ```bash $ NS_LOG="*=all|prefix_level" ./waf --run scratch-simulator Scratch Simulator [ERROR] error message [WARN] warn message [DEBUG] debug message [INFO] info message [FUNCT] function message [LOGIC] logic message ``` -------------------------------- ### Raw text output of configuration store Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Example content of the output-attributes.txt file after running the configuration save. It lists default attribute values, global values, and the value of the ConfigExample instance. ```text default ns3::RealtimeSimulatorImpl::SynchronizationMode "BestEffort" default ns3::RealtimeSimulatorImpl::HardLimit "+100000000.0ns" default ns3::PcapFileWrapper::CaptureSize "65535" default ns3::PacketSocket::RcvBufSize "131072" default ns3::ErrorModel::IsEnabled "true" default ns3::RateErrorModel::ErrorUnit "EU_BYTE" default ns3::RateErrorModel::ErrorRate "0" default ns3::RateErrorModel::RanVar "Uniform:0:1" default ns3::DropTailQueue::Mode "Packets" default ns3::DropTailQueue::MaxPackets "100" default ns3::DropTailQueue::MaxBytes "6553500" default ns3::Application::StartTime "+0.0ns" default ns3::Application::StopTime "+0.0ns" default ns3::ConfigStore::Mode "Save" default ns3::ConfigStore::Filename "output-attributes.txt" default ns3::ConfigStore::FileFormat "RawText" default ns3::ConfigExample::TestInt16 "-5" global RngSeed "1" global RngRun "1" global SimulatorImplementationType "ns3::DefaultSimulatorImpl" global SchedulerType "ns3::MapScheduler" global ChecksumEnabled "false" value /$ns3::ConfigExample/TestInt16 "-3" ``` -------------------------------- ### Run tcp-point-to-point and observe crash Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Command to run the tcp-point-to-point example, which exits with code -11, indicating a crash. ```bash $ ./waf --run tcp-point-to-point Entering directory '/home/tomh/ns-3-nsc/build' Compilation finished successfully Command ['/home/tomh/ns-3-nsc/build/debug/examples/tcp-point-to-point'] exited with code -11 ``` -------------------------------- ### Enable core module with waf --enable-modules Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Commands to clean, configure, build, and list built libraries when enabling only the core module with examples and tests. The clean step is optional but makes the built modules obvious. ```bash $ ./waf clean $ ./waf configure --enable-examples --enable-tests --enable-modules=core $ ./waf build $ cd build/debug/ $ ls ``` -------------------------------- ### Include internal header example Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Shows how to include headers used strictly internally in your implementation. These headers are not declared in the wscript and are only accessible to your implementation. ```cpp #include "my-module-implementation.h" ``` -------------------------------- ### Formatted output file: seventh-packet-byte-count-0.txt (first 10 lines) Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Example of a formatted text file created by the FileHelper with two columns. Only the first 10 lines are shown. ```text Time (Seconds) = 1.000e+00 Packet Byte Count = 40 Time (Seconds) = 1.004e+00 Packet Byte Count = 40 Time (Seconds) = 1.004e+00 Packet Byte Count = 576 Time (Seconds) = 1.009e+00 Packet Byte Count = 576 Time (Seconds) = 1.009e+00 Packet Byte Count = 576 Time (Seconds) = 1.015e+00 Packet Byte Count = 512 Time (Seconds) = 1.017e+00 Packet Byte Count = 576 Time (Seconds) = 1.017e+00 Packet Byte Count = 544 Time (Seconds) = 1.025e+00 Packet Byte Count = 576 Time (Seconds) = 1.025e+00 Packet Byte Count = 544 ... ``` -------------------------------- ### Direct standard output printing in C++ Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Shows the most straightforward way to get information out of a program by printing to standard output with std::cout. Useful in small environments, but becomes harder to parse and control as simulations grow. The ellipsis lines are part of the original example. ```cpp #include ... int main () { ... std::cout << "The value of x is " << x << std::endl; ... } ``` -------------------------------- ### XML output of configuration store Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Example content of the output-attributes.xml file after running the configuration save. It contains the same information as the raw text version but in XML format. ```xml ``` -------------------------------- ### Formatted output file: seventh-packet-byte-count-1.txt (first 10 lines) Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Another example of a formatted text file created by the FileHelper, corresponding to the second wildcard match. Only the first 10 lines are shown. ```text Time (Seconds) = 1.002e+00 Packet Byte Count = 40 Time (Seconds) = 1.007e+00 Packet Byte Count = 40 Time (Seconds) = 1.013e+00 Packet Byte Count = 40 Time (Seconds) = 1.020e+00 Packet Byte Count = 40 Time (Seconds) = 1.028e+00 Packet Byte Count = 40 Time (Seconds) = 1.036e+00 Packet Byte Count = 40 Time (Seconds) = 1.045e+00 Packet Byte Count = 40 Time (Seconds) = 1.053e+00 Packet Byte Count = 40 Time (Seconds) = 1.061e+00 Packet Byte Count = 40 Time (Seconds) = 1.069e+00 Packet Byte Count = 40 ... ``` -------------------------------- ### Declare source files in wscript Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Example from the spectrum module showing how to list source files in the module.source list. The compiled objects are assembled into a link library. ```python def build(bld): module = bld.create_ns3_module('spectrum', ['internet', 'propagation', 'antenna', 'applications']) module.source = [ 'model/spectrum-model.cc', 'model/spectrum-value.cc', . . . 'model/microwave-oven-spectrum-value-helper.cc', 'helper/spectrum-helper.cc', 'helper/adhoc-aloha-noack-ideal-phy-helper.cc', 'helper/waveform-generator-helper.cc', 'helper/spectrum-analyzer-helper.cc', ] ``` -------------------------------- ### Run test.py with no examples or tests Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Output of running test.py when examples and tests are disabled, showing that no tests are run. ```text 0 of 0 tests passed (0 passed, 0 skipped, 0 failed, 0 crashed, 0 valgrind errors) ``` -------------------------------- ### List legal test kinds with --kinds Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Use this command to display the list of legal test kinds that can be used with --constraint. The output shows the available kinds: bvt, core, example, performance, system, and unit. ```bash $ ./test.py --kinds ``` ```text Waf: Entering directory `/home/craigdo/repos/ns-3-allinone-test/ns-3-dev/build' Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone-test/ns-3-dev/build' 'build' finished successfully (0.939s)Waf: Entering directory `/home/craigdo/repos/ns-3-allinone-test/ns-3-dev/build' bvt: Build Verification Tests (to see if build completed successfully) core: Run all TestSuite-based tests (exclude examples) example: Examples (to see if example programs run successfully) performance: Performance Tests (check to see if the system is as fast as expected) system: System Tests (spans modules to check integration of modules) unit: Unit Tests (within modules to check basic functionality) ``` -------------------------------- ### Prototypical module directory structure Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Shows the standard directory layout for an ns-3 module, including bindings, doc, examples, helper, model, test, and wscript files. Not all directories are present in every module. ```text src/ module-name/ bindings/ doc/ examples/ wscript helper/ model/ test/ examples-to-run.py wscript ``` -------------------------------- ### Enable all logging with NS_LOG environment variable Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Runs the 'first' tutorial program with all logging output enabled by setting NS_LOG to the wildcard '*'. ```bash $ NS_LOG="*" ./waf --run first ``` -------------------------------- ### Start ns-3 documents with include and highlight directives Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Recommended first two lines for ns-3 Sphinx documents. The include enables simple replacements (e.g., |ns3| renders as ns-3). The highlight sets the default source code highlighting language explicitly for the file. ```rst .. include:: replace.txt .. highlight:: cpp ``` -------------------------------- ### Run test-runner with --help Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Use this command to display the available options for the test-runner executable. It is run via waf and shows the usage and all command-line flags. ```bash $ ./waf --run "test-runner --help" ``` -------------------------------- ### Enable logging for a specific component with NS_LOG Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Runs the 'first' tutorial program with logging enabled only for the Ipv4L3Protocol component. ```bash $ NS_LOG="Ipv4L3Protocol" ./waf --run first ``` -------------------------------- ### Run an ns-3 simulation from Python (examples/tutorial/first.py) Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Python script that uses ns-3 Python bindings to create a point-to-point network with two nodes and run a UDP echo application. It enables logging for UDP echo client and server, sets up the network, installs the internet stack, assigns IP addresses, and schedules start/stop times for the applications. The simulation runs for 10 seconds and then destroys the simulator. ```python import ns.applications import ns.core import ns.internet import ns.network import ns.point_to_point ns.core.LogComponentEnable("UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO) ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO) nodes = ns.network.NodeContainer() nodes.Create(2) pointToPoint = ns.point_to_point.PointToPointHelper() pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps")) pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("2ms")) devices = pointToPoint.Install(nodes) stack = ns.internet.InternetStackHelper() stack.Install(nodes) address = ns.internet.Ipv4AddressHelper() address.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0")) interfaces = address.Assign (devices); echoServer = ns.applications.UdpEchoServerHelper(9) serverApps = echoServer.Install(nodes.Get(1)) serverApps.Start(ns.core.Seconds(1.0)) serverApps.Stop(ns.core.Seconds(10.0)) echoClient = ns.applications.UdpEchoClientHelper(interfaces.GetAddress(1), 9) echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1)) echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds (1.0))) echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(1024)) clientApps = echoClient.Install(nodes.Get(0)) clientApps.Start(ns.core.Seconds(2.0)) clientApps.Stop(ns.core.Seconds(10.0)) ns.core.Simulator.Run() ns.core.Simulator.Destroy() ``` -------------------------------- ### Build all Sphinx documentation with ./waf sphinx Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Command to build all Sphinx documentation. Run from the ns-3 root directory. ```bash $ ./waf sphinx ``` -------------------------------- ### IPv4 packet plot example network topology diagram Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html ASCII diagram showing the network topology for the IPv4 packet plot example, with two nodes connected by a point-to-point link. ```text node 0 node 1 +----------------+ +----------------+ | ns-3 TCP | | ns-3 TCP | +----------------+ +----------------+ | 10.1.1.1 | | 10.1.1.2 | +----------------+ +----------------+ | point-to-point | | point-to-point | +----------------+ +----------------+ | | +---------------------+ ``` -------------------------------- ### Enable logging via LogComponentEnable in main() Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Shows how to enable logging for UdpEchoClientApplication and UdpEchoServerApplication at LOG_LEVEL_INFO using explicit program statements. ```cpp int main (int argc, char *argv[]) { LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); ... ``` -------------------------------- ### Install traffic generator and sink applications Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Installs custom Sender and Receiver applications on nodes 0 and 1. The commented-out Config::Set line changes the destination from broadcast to a specific IP address. ```cpp Ptr appSource = NodeList::GetNode(0); Ptr sender = CreateObject(); appSource->AddApplication(sender); sender->Start(Seconds(1)); Ptr appSink = NodeList::GetNode(1); Ptr receiver = CreateObject(); appSink->AddApplication(receiver); receiver->Start(Seconds(0)); // Config::Set("/NodeList/*/ApplicationList/*/$Sender/Destination", // Ipv4AddressValue("192.168.0.2")); ``` -------------------------------- ### List new-module directory contents Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html After creating the module, cd into new-module and list its contents to see the generated directories and wscript file. ```bash $ cd new-module $ ls doc examples helper model test wscript ``` -------------------------------- ### Create DoubleProbe with attributes using CreateObjectWithAttributes Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Creates a DoubleProbe with custom attributes: Name, Enabled, Start, and Stop. Start and Stop define the active interval; Stop of 0 seconds disables the stop time. ```cpp Ptr myprobe = CreateObjectWithAttributes ( "Name", StringValue ("myprobe"), "Enabled", BooleanValue (false), "Start", TimeValue (Seconds (100.0)), "Stop", TimeValue (Seconds (1000.0))); ``` -------------------------------- ### Check GtkConfigStore configuration with waf configure Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Run waf configure with --enable-examples --enable-tests to check whether GtkConfigStore is enabled. The output shows 'not enabled' when the required library 'gtk+-2.0 >= 2.12' is not found. ```bash $ ./waf configure --enable-examples --enable-tests ---- Summary of optional NS-3 features: Python Bindings : enabled Python API Scanning Support : enabled NS-3 Click Integration : enabled GtkConfigStore : not enabled (library 'gtk+-2.0 >= 2.12' not found) ``` -------------------------------- ### GnuplotHelper ConfigurePlot usage example in seventh.cc Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Configures a plot with the GnuplotHelper in the seventh.cc example. Creates a space separated gnuplot data file named outputFileNameWithoutExtension + ".dat", a gnuplot control file named outputFileNameWithoutExtension + ".plt", and a shell script to generate the gnuplot named outputFileNameWithoutExtension + ".sh". ```cpp plotHelper.ConfigurePlot ("seventh-packet-byte-count", "Packet Byte Count vs. Time", "Time (Seconds)", "Packet Byte Count"); ``` -------------------------------- ### Default .ns3rc configuration file Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html The default .ns3rc file enables all modules and disables examples and tests. ```python # A list of the modules that will be enabled when ns-3 is run. # Modules that depend on the listed modules will be enabled also. # # All modules can be enabled by choosing 'all_modules'. modules_enabled = ['all_modules'] # Set this equal to true if you want examples to be run. examples_enabled = False # Set this equal to true if you want tests to be run. tests_enabled = False ``` -------------------------------- ### Configure and use FileHelper to write probe output Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Configures the FileHelper to write formatted output to 'file-helper-example.txt', sets the 2D format, and writes the values from the Uinteger32Probe at '/Names/Emitter/Counter'. ```cpp // Create the file helper. FileHelper fileHelper; // Configure the file to be written. fileHelper.ConfigureFile ("file-helper-example", FileAggregator::FORMATTED); // Set the labels for this formatted output file. fileHelper.Set2dFormat ("Time (Seconds) = %.3e\tCount = %.0f"); // Write the values generated by the probe. The path that we // provide helps to disambiguate the source of the trace. fileHelper.WriteProbe ("ns3::Uinteger32Probe", "/Names/Emitter/Counter", "Output"); ``` -------------------------------- ### Use GtkConfigStore to configure defaults and attributes Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Usage is almost the same as the non-GTK version, but there are no ConfigStore attributes involved. Invoke just before entering Simulator::Run(). A GUI pops up to open menus of attributes and launch simulation execution. ```cpp // Invoke just before entering Simulator::Run () GtkConfigStore config; config.ConfigureDefaults (); config.ConfigureAttributes (); ``` -------------------------------- ### Binding three arguments with MakeBoundCallback Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Example of creating a bound callback that binds all three arguments of NotifyEvent. ```cpp MakeBoundCallback (&NotifyEvent, a1, b1, c1); ``` -------------------------------- ### FileAggregator::Enable() Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Enables the FileAggregator to start writing data to the file. Must be called before writing values. ```APIDOC ## FileAggregator::Enable() ### Description Turns on the FileAggregator so that it will write values to the file. This method must be called before any Write2d calls. ### Method Enable ### Parameters None ### Request Example ```cpp aggregator->Enable(); ``` ### Response #### Success Response No return value. ``` -------------------------------- ### Pass a specific functor to LibraryFunction Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Shows how to build a SpecificFunctor and pass it to LibraryFunction, completing the callback setup. ```cpp MyClass myClass; SpecificFunctor functor (&myclass, MyClass::MyMethod); ``` -------------------------------- ### Get MaxPackets attribute from DropTailQueue Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Retrieves the 'MaxPackets' attribute from the DropTailQueue using UintegerValue and logs the value. ```cpp UintegerValue limit; dtq->GetAttribute ("MaxPackets", limit); NS_LOG_INFO ("1. dtq limit: " << limit.Get () << " packets"); ``` -------------------------------- ### Complete module skeleton file layout Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Shows the full file tree created by create-module.py, including skeleton .cc, .h, .rst, and wscript files. The bindings directory is created automatically during build if required. ```text src/ new-module/ doc/ new-module.rst examples/ new-module-example.cc wscript helper/ new-module-helper.cc new-module-helper.h model/ new-module.cc new-module.h test/ new-module-test-suite.cc wscript ``` -------------------------------- ### ObjectFactory example Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Creates a Node object without knowing the concrete type, using ObjectFactory and a TypeId string. ```cpp ObjectFactory factory; const std::string typeId = "ns3::Node''; factory.SetTypeId (typeId); Ptr node = factory.Create (); ``` -------------------------------- ### Enable ascii tracing for all devices (helper.EnableAsciiAll) Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Enables ascii tracing for all devices of the type managed by the helper. Creates one trace file per device, following the --.tr convention. ```cpp helper.EnableAsciiAll ("prefix"); ``` -------------------------------- ### Binding two arguments with MakeBoundCallback Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Example of creating a bound callback that binds the first two arguments of NotifyEvent. ```cpp MakeBoundCallback (&NotifyEvent, a1, b1); ``` -------------------------------- ### Calling the promiscuous sniffer trace with a packet Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Example of invoking the callback with only the packet parameter, as desired in the calling code. ```cpp m_promiscSnifferTrace (m_currentPkt); ``` -------------------------------- ### Get MaxPackets attribute directly from Object Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Shows that the downcast is not needed; the attribute can be retrieved directly from the Object pointer. ```cpp txQueue->GetAttribute ("MaxPackets", limit); NS_LOG_INFO ("2. txQueue limit: " << limit.Get () << " packets"); ``` -------------------------------- ### Get a random integer from a UniformRandomVariable Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Call GetInteger() on the random variable object to obtain a random integer value. ```cpp myRandomNo = x->GetInteger (); ``` -------------------------------- ### CreateObject to instantiate Object Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Typical way to create an ns-3 Object on the heap using the templated CreateObject function. Objects deriving from Object must be allocated this way. ```cpp Ptr nd = CreateObject (); ``` -------------------------------- ### C++ conversion constructor example Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Shows C++ code that uses implicit conversion constructors, which are not fully supported in Python bindings. ```cpp Ipv4AddressHelper ipAddrs; ipAddrs.SetBase ("192.168.0.0", "255.255.255.0"); ipAddrs.Assign (backboneDevices); ``` -------------------------------- ### Build Doxygen documentation with waf Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Command to build the Doxygen documentation for the project. ```bash $ ./waf doxygen ``` -------------------------------- ### Enable logging for all wifi components Source: https://www.nsnam.org/docs/release/3.26/manual/singlehtml/index.html Shows how to enable logging for all components in the wifi module using the WifiHelper's EnableLogComponents method. ```cpp WifiHelper wifiHelper; wifiHelper.EnableLogComponents (); ```