1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/wns3-lowlevel.cc Thu Feb 26 15:35:57 2009 +0100
1.3 @@ -0,0 +1,213 @@
1.4 +#include <iostream>
1.5 +#include <fstream>
1.6 +#include "ns3/simulator-module.h"
1.7 +#include "ns3/node-module.h"
1.8 +#include "ns3/core-module.h"
1.9 +#include "ns3/csma-module.h"
1.10 +#include "ns3/wifi-module.h"
1.11 +#include "ns3/internet-stack-module.h"
1.12 +#include "ns3/onoff-module.h"
1.13 +#include "ns3/packet-sink-module.h"
1.14 +#include "ns3/mobility-module.h"
1.15 +#include "ns3/global-route-manager.h"
1.16 +#include "ns3/contrib-module.h"
1.17 +#include "ns3/common-module.h"
1.18 +
1.19 +using namespace ns3;
1.20 +
1.21 +static Ptr<CsmaNetDevice>
1.22 +AddCsma (Ptr<Node> node, Ptr<CsmaChannel> channel)
1.23 +{
1.24 + Ptr<CsmaNetDevice> dev = CreateObject<CsmaNetDevice> ();
1.25 + dev->SetAddress (Mac48Address::Allocate ());
1.26 + node->AddDevice (dev);
1.27 + Ptr<Queue> queue = CreateObject<DropTailQueue> ();
1.28 + dev->SetQueue (queue);
1.29 + dev->Attach (channel);
1.30 + return dev;
1.31 +}
1.32 +
1.33 +static Ptr<WifiNetDevice>
1.34 +AddWifi (Ptr<Node> node, Ptr<YansWifiChannel> channel)
1.35 +{
1.36 + Ptr<WifiNetDevice> device = CreateObject<WifiNetDevice> ();
1.37 +
1.38 + Ptr<YansWifiPhy> phy = CreateObject<YansWifiPhy> ();
1.39 + Ptr<ErrorRateModel> error = CreateObject<YansErrorRateModel> ();
1.40 + phy->SetErrorRateModel (error);
1.41 + phy->SetChannel (channel);
1.42 + phy->SetMobility (node);
1.43 + phy->SetDevice (device);
1.44 +
1.45 + Ptr<WifiRemoteStationManager> manager = CreateObject<ArfWifiManager> ();
1.46 + Ptr<WifiMac> mac = CreateObject<AdhocWifiMac> ();
1.47 + mac->SetAddress (Mac48Address::Allocate ());
1.48 +
1.49 + device->SetMac (mac);
1.50 + device->SetPhy (phy);
1.51 + device->SetRemoteStationManager (manager);
1.52 + node->AddDevice (device);
1.53 +
1.54 + return device;
1.55 +}
1.56 +
1.57 +static Ptr<PositionAllocator>
1.58 +GetPositionAllocator (void)
1.59 +{
1.60 + static Ptr<PositionAllocator> allocator = 0;
1.61 + if (allocator == 0)
1.62 + {
1.63 + allocator = CreateObject<RandomDiscPositionAllocator> ();
1.64 + allocator->SetAttribute ("X", StringValue ("100.0"));
1.65 + allocator->SetAttribute ("Y", StringValue ("100.0"));
1.66 + allocator->SetAttribute ("Rho", StringValue ("Uniform:0:30"));
1.67 + }
1.68 + return allocator;
1.69 +}
1.70 +
1.71 +static void
1.72 +AddMobility (Ptr<Node> node)
1.73 +{
1.74 + Ptr<StaticMobilityModel> mobility = CreateObject<StaticMobilityModel> ();
1.75 + mobility->SetPosition (GetPositionAllocator ()->GetNext ());
1.76 + node->AggregateObject (mobility);
1.77 +}
1.78 +
1.79 +static void
1.80 +AddIpv4Interface (Ptr<NetDevice> device,
1.81 + const char * ip,
1.82 + const char * mask)
1.83 +{
1.84 + Ptr<Node> node = device->GetNode ();
1.85 + Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
1.86 + int32_t ifIndex = ipv4->AddInterface (device);
1.87 + ipv4->SetAddress (ifIndex, Ipv4Address (ip));
1.88 + ipv4->SetNetworkMask (ifIndex, Ipv4Mask (mask));
1.89 + ipv4->SetMetric (ifIndex, 1);
1.90 + ipv4->SetUp (ifIndex);
1.91 +}
1.92 +
1.93 +static Ptr<Application>
1.94 +AddUdpSource (Ptr<Node> node,
1.95 + const char *ip, uint16_t port)
1.96 +{
1.97 + Ptr<OnOffApplication> application = CreateObject<OnOffApplication> ();
1.98 + application->SetAttribute ("Protocol", StringValue ("ns3::UdpSocketFactory"));
1.99 + application->SetAttribute ("Remote", AddressValue (InetSocketAddress (Ipv4Address (ip), port)));
1.100 + application->SetAttribute ("OnTime", StringValue ("Constant:1.0"));
1.101 + application->SetAttribute ("OffTime", StringValue ("Constant:0.0"));
1.102 + node->AddApplication (application);
1.103 + return application;
1.104 +}
1.105 +
1.106 +static Ptr<Application>
1.107 +AddUdpSink (Ptr<Node> node, const char *ip, uint16_t port)
1.108 +{
1.109 + Ptr<PacketSink> sink = CreateObject<PacketSink> ();
1.110 + sink->SetAttribute ("Protocol", StringValue ("ns3::UdpSocketFactory"));
1.111 + sink->SetAttribute ("Local", AddressValue (InetSocketAddress (Ipv4Address (ip), port)));
1.112 + node->AddApplication (sink);
1.113 + return sink;
1.114 +}
1.115 +
1.116 +static void PcapPhyRxEvent (Ptr<PcapWriter> writer,
1.117 + Ptr<const Packet> packet, double snr, WifiMode mode,
1.118 + enum WifiPreamble preamble)
1.119 +{
1.120 + writer->WritePacket (packet);
1.121 +}
1.122 +static void PcapPhyTxEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet,
1.123 + WifiMode mode, WifiPreamble preamble,
1.124 + uint8_t txLevel)
1.125 +{
1.126 + writer->WritePacket (packet);
1.127 +}
1.128 +
1.129 +
1.130 +static void
1.131 +WifiEnablePcap (Ptr<WifiNetDevice> wifi, const char *filename)
1.132 +{
1.133 + uint32_t nodeId = wifi->GetNode ()->GetId ();
1.134 + uint32_t deviceId = wifi->GetIfIndex ();
1.135 + std::ostringstream oss;
1.136 + oss << filename << "-" << nodeId << "-" << deviceId << ".pcap";
1.137 + Ptr<PcapWriter> pcap = ::ns3::Create<PcapWriter> ();
1.138 + pcap->Open (oss.str ());
1.139 + pcap->WriteWifiHeader ();
1.140 + oss.str ("");
1.141 + oss << "/NodeList/" << nodeId << "/DeviceList/" << deviceId
1.142 + << "/$ns3::WifiNetDevice/Phy/State/Tx";
1.143 + Config::ConnectWithoutContext (oss.str (),
1.144 + MakeBoundCallback (&PcapPhyTxEvent, pcap));
1.145 + oss.str ("");
1.146 + oss << "/NodeList/" << nodeId << "/DeviceList/" << deviceId
1.147 + << "/$ns3::WifiNetDevice/Phy/State/RxOk";
1.148 + Config::ConnectWithoutContext (oss.str (),
1.149 + MakeBoundCallback (&PcapPhyRxEvent, pcap));
1.150 +}
1.151 +
1.152 +int main (int argc, char *argv[])
1.153 +{
1.154 + Ptr<Node> node0 = CreateObject<Node> ();
1.155 + Ptr<Node> node1 = CreateObject<Node> ();
1.156 + Ptr<Node> node2 = CreateObject<Node> ();
1.157 + Ptr<Node> node3 = CreateObject<Node> ();
1.158 + Ptr<Node> node4 = CreateObject<Node> ();
1.159 +
1.160 + Ptr<CsmaChannel> csmaChannel = CreateObject<CsmaChannel> ();
1.161 + csmaChannel->SetAttribute ("DataRate", StringValue ("5Mbps"));
1.162 + csmaChannel->SetAttribute ("Delay", StringValue ("2ms"));
1.163 + Ptr<CsmaNetDevice> csmaDev0 = AddCsma (node0, csmaChannel);
1.164 + Ptr<CsmaNetDevice> csmaDev1 = AddCsma (node1, csmaChannel);
1.165 +
1.166 + Ptr<YansWifiChannel> wifiChannel = CreateObject<YansWifiChannel> ();
1.167 + wifiChannel->SetPropagationDelayModel (CreateObject<ConstantSpeedPropagationDelayModel> ());
1.168 + Ptr<PropagationLossModel> loss = CreateObject<LogDistancePropagationLossModel> ();
1.169 + wifiChannel->SetPropagationLossModel (loss);
1.170 +
1.171 + Ptr<WifiNetDevice> wifiDev0 = AddWifi (node1, wifiChannel);
1.172 + Ptr<WifiNetDevice> wifiDev1 = AddWifi (node2, wifiChannel);
1.173 + Ptr<WifiNetDevice> wifiDev2 = AddWifi (node3, wifiChannel);
1.174 + Ptr<WifiNetDevice> wifiDev3 = AddWifi (node4, wifiChannel);
1.175 +
1.176 + AddMobility (node1);
1.177 + AddMobility (node2);
1.178 + AddMobility (node3);
1.179 + AddMobility (node4);
1.180 +
1.181 + AddInternetStack (node0);
1.182 + AddInternetStack (node1);
1.183 + AddInternetStack (node2);
1.184 + AddInternetStack (node3);
1.185 + AddInternetStack (node4);
1.186 +
1.187 + AddIpv4Interface (csmaDev0, "10.1.1.1", "255.255.255.0");
1.188 + AddIpv4Interface (csmaDev1, "10.1.1.2", "255.255.255.0");
1.189 + AddIpv4Interface (wifiDev0, "10.1.2.1", "255.255.255.0");
1.190 + AddIpv4Interface (wifiDev1, "10.1.2.2", "255.255.255.0");
1.191 + AddIpv4Interface (wifiDev2, "10.1.2.3", "255.255.255.0");
1.192 + AddIpv4Interface (wifiDev3, "10.1.2.4", "255.255.255.0");
1.193 +
1.194 + GlobalRouteManager::PopulateRoutingTables ();
1.195 +
1.196 + Ptr<Application> app;
1.197 + app = AddUdpSource (node0, "10.1.2.2", 1025);
1.198 + app->Start (Seconds (1.0));
1.199 + app->Stop (Seconds (4.0));
1.200 + app = AddUdpSink (node2, "10.1.2.2", 1025);
1.201 + app->Start (Seconds (0.0));
1.202 + app->Stop (Seconds (4.0));
1.203 +
1.204 + WifiEnablePcap (wifiDev2, "wns3-lowlevel");
1.205 +
1.206 + GtkConfigStore config;
1.207 + config.Configure ();
1.208 +
1.209 + Simulator::Stop (Seconds (4.5));
1.210 +
1.211 + Simulator::Run ();
1.212 + Simulator::Destroy ();
1.213 +
1.214 +
1.215 + return 0;
1.216 +}