├── README.md ├── project_1 ├── third_edited.cc └── thrpt_rtt.cpp ├── project_2 ├── manet-28.cc ├── manet100.csv └── number_of_neighbors.cpp ├── project_3 ├── compare.cc ├── dsdv-manet-08.cc ├── dsr-08.cc ├── manet-28.cc ├── manet100.csv └── olsr-08.cc ├── project_4 ├── aodv-proj-4-broadcast.cc ├── aodv-proj-4-p2p.cc ├── dsdv-proj-4-broadcast.cc ├── dsdv-proj-4-p2p.cc ├── olsr-proj-4-broadcast.cc └── olsr-proj-4-p2p.cc └── project_5 ├── aodv_cases ├── aodv-test-edit-rect-center.cc ├── aodv-test-edit-rect-void-2-servers.cc ├── aodv-test-edit-rect-void.cc ├── aodv-test-edit-sq-center.cc └── aodv-test-edit-sq-corner.cc ├── gpsr-edited └── gpsr │ ├── examples │ ├── gpsr-test1.cc │ ├── gpsr-test2.cc │ ├── gpsr-test3.cc │ ├── gpsr-test4.cc │ ├── gpsr-test5.cc │ ├── gpsr-test6.cc │ ├── gpsr-test7.cc │ └── wscript │ ├── helper │ ├── gpsr-helper.cc │ └── gpsr-helper.h │ ├── model │ ├── TODO │ ├── gpsr-packet.cc │ ├── gpsr-packet.h │ ├── gpsr-ptable.cc │ ├── gpsr-ptable.h │ ├── gpsr-rqueue.cc │ ├── gpsr-rqueue.h │ ├── gpsr.cc │ └── gpsr.h │ └── wscript └── gpsr_bird_flock_cases ├── gpsr-test-edit-rect-center.cc ├── gpsr-test-edit-rect-void-2-servers.cc ├── gpsr-test-edit-rect-void.cc ├── gpsr-test-edit-sq-center.cc └── gpsr-test-edit-sq-corner.cc /README.md: -------------------------------------------------------------------------------- 1 | # NS-3 MANET Projects 2 | 3 | All of the programs written for implementation of various Mobile Ad-Hoc Networks in Network Simulator-3 for ECE 69500 Mobile Wireless Networking at IUPUI (Fall 2016). 4 | 5 | - **Project 1:** This project focused on understanding how NS-3 works and basic Wi-Fi and LAN networks, and how they communicate among one another. 6 | 7 | 1. *third_edited.cc:* Modifications were to be made to the *third.cc* example file of NS-3 to change the number of clients and how many packets each one of them sends to the server. Additional command line variables like simulation time and output PCAP file name were added. 8 | 2. *thrpt_rtt.cpp:* This was used to read and analyze the PCAP files generated to measure the throughput and round trip time. The variations were plotted against the number of Wi-Fi client stations to observe the effect of increase in number of Wi-Fi stations on throughput and round trip time of each UDP Echo packet. 9 | 10 | - **Project 2:** This project was to understand the working of AODV as a protocol, and how the network reacts to varying transmission ranges of each packet. 11 | 12 | 1. *manet-28.cc:* This file is edited from the AODV example source file for a network of 100 nodes, whose X-Y positions are determined by the *manet100.csv* file. The packet delivery ratio and throughput are measured by using the ns3::FlowMonitor class. 13 | 2. *number_of_neighbors.cpp:* This file is used to calculate the mean neighbors for each Ad-Hoc Wi-Fi node. 14 | 15 | - **Project 3:** This project focuses on comparing the performances of various Ad-Hoc Routing Protocols like Ad Hoc On-Demand Distance Vector (AODV), Destination-Sequenced Distance-Vector (DSDV), Optimized Link State Routing (OLSR) and Dynamic Source Routing (DSR) based on their throughput performance. 16 | 17 | 1. *manet-28.cc:* This file is the same as the one from Project 2. 18 | 2. *dsdv-manet-08.cc:* Here, we implement the DSDV protocol on the same network topology as defined in *manet100.csv*, with the same parameters as used for the *manet-28.cc*. 19 | 3. *olsr-08.cc:* Here, we implement the OLSR protocol on the same network topology as defined in *manet100.csv*, with the same parameters as used for the *manet-28.cc*. 20 | 4. *dsr-08.cc:* Here, we implement the DSR protocol on the same network topology as defined in *manet100.csv*, with the same parameters as used for the *manet-28.cc*. 21 | 5. *compare.cc:* At the end, we use the *manet-routing-compare.cc* from the examples and modify it to our requirement. We use it to verify the results obtained. 22 | 23 | - **Project 4:** This project focuses on comparing AODV, DSDV and OLSR in an entirely different topology that consists of one LAN server, connected to a Gateway, which relays messages to and from the 100 clients distributed over a space. These mobile nodes communicate via Ad-Hoc Wi-Fi protocols. Comparison between point-to-point communication to the clients from the server, and broadcast communication from the server to all the clients is also done. (File names for this project are self-explanatory.) 24 | 25 | - **Project 5:** 26 | This project focuses on combining Greedy Perimeter Stateless Routing (GPSR) and the Bird Flocking Algorithm (BFA) to create a new bio-inspired routing algorithm that performs better than conventional routing algorithms. The routing algorithm deployed is tested for various network topologies and the performance of AODV, simple GPSR and GPSR with BFA are compared based on the throughput, delay and jitter. The GPSR implementation for NS-3 was obtained from http://www.mehic.info/2016/04/greedy-perimeter-stateless-routing-gpsr-in-ns3-25/. 27 | 28 | The edits made to the GPSR files included changes made to route perimeter nodes through both left and right most nodes alternately, instead of GPSR's right-most node selection, the time between 'HELLO' packets, the addition of the mobility model (*ns3::RandomDirection2dMobilityModel*) that would provide the direction and velocity of the nodes, and update the neighbor nodes each time either of them changed, and the addition of a energy module to simulate a real-life application. 29 | 30 | The network topologies compared were simulated from real world probably Wireless Sensor Network topologies. The topologies tested were: 31 | 1. *...-sq-corner:* The server is in the left top corner, with nodes distributed in a square. 32 | 2. *...-sq-center:* The server is in the center, with nodes distributed in a square. 33 | 3. *...-rect-center:* The server is in the center, with nodes distributed in a rectangle. 34 | 4. *...-rect-void:* The nodes are distributed in a rectangle with a void in the center. The server placed at the center of the void. 35 | 5. *...-rect-void-2-servers:* The nodes are distributed in a rectangle with a void in the center. Two servers are placed at the center of the edges of the void. 36 | -------------------------------------------------------------------------------- /project_1/third_edited.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Project 1 3 | * Subject: ECE695 - Mobile Wireless Networking (Fall 2016) 4 | * Professor: Dr. Stephen Kim 5 | * Group members: Abhijit Katkar, Lena Ara, Mariya Ali, Setu Shah 6 | */ 7 | 8 | #include "ns3/core-module.h" 9 | #include "ns3/point-to-point-module.h" 10 | #include "ns3/network-module.h" 11 | #include "ns3/applications-module.h" 12 | #include "ns3/wifi-module.h" 13 | #include "ns3/mobility-module.h" 14 | #include "ns3/csma-module.h" 15 | #include "ns3/internet-module.h" 16 | 17 | /* Network Topology 18 | Number of wifi or csma nodes can be increased up to 250 19 | | 20 | Rank 0 | Rank 1 21 | -------------------------|---------------------------- 22 | Wifi 10.1.3.0 23 | AP 24 | * * * * 25 | | | | | 10.1.1.0 26 | n5 n6 n7 n0 -------------- n1 n2 n3 n4 27 | point-to-point | | | | 28 | ================ 29 | LAN 10.1.2.0 30 | */ 31 | 32 | using namespace ns3; 33 | 34 | NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample"); 35 | 36 | int 37 | main (int argc, char *argv[]) 38 | { 39 | bool verbose = true; 40 | uint32_t nCsma = 3; 41 | uint32_t nWifi = 3; 42 | uint32_t simTime = 10; 43 | bool tracing = true; 44 | char* outputFilename = (char *)"thrpt"; 45 | uint32_t i = 0; 46 | 47 | CommandLine cmd; 48 | cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma); 49 | cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi); 50 | cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose); 51 | cmd.AddValue ("tracing", "Enable pcap tracing", tracing); 52 | cmd.AddValue ("simTime", "Simulation time", simTime); 53 | cmd.AddValue ("outputFilename", "Output filename", outputFilename); 54 | 55 | cmd.Parse (argc,argv); 56 | 57 | if (nWifi > 250 || nCsma > 250) 58 | { 59 | std::cout << "Too many wifi or csma nodes, no more than 250 each." << std::endl; 60 | return 1; 61 | } 62 | 63 | if (verbose) 64 | { 65 | LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); 66 | LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); 67 | } 68 | 69 | NodeContainer p2pNodes; 70 | p2pNodes.Create (2); 71 | 72 | PointToPointHelper pointToPoint; 73 | pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); 74 | pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); 75 | 76 | NetDeviceContainer p2pDevices; 77 | p2pDevices = pointToPoint.Install (p2pNodes); 78 | 79 | NodeContainer csmaNodes; 80 | csmaNodes.Add (p2pNodes.Get (1)); 81 | csmaNodes.Create (nCsma); 82 | 83 | CsmaHelper csma; 84 | csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); 85 | csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); 86 | 87 | NetDeviceContainer csmaDevices; 88 | csmaDevices = csma.Install (csmaNodes); 89 | 90 | NodeContainer wifiStaNodes; 91 | wifiStaNodes.Create (nWifi); 92 | NodeContainer wifiApNode = p2pNodes.Get (0); 93 | 94 | YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); 95 | YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); 96 | phy.SetChannel (channel.Create ()); 97 | 98 | WifiHelper wifi; 99 | wifi.SetRemoteStationManager ("ns3::AarfWifiManager"); 100 | 101 | WifiMacHelper mac; 102 | Ssid ssid = Ssid ("ns-3-ssid"); 103 | mac.SetType ("ns3::StaWifiMac", 104 | "Ssid", SsidValue (ssid), 105 | "ActiveProbing", BooleanValue (false)); 106 | 107 | NetDeviceContainer staDevices; 108 | staDevices = wifi.Install (phy, mac, wifiStaNodes); 109 | 110 | mac.SetType ("ns3::ApWifiMac", 111 | "Ssid", SsidValue (ssid)); 112 | 113 | NetDeviceContainer apDevices; 114 | apDevices = wifi.Install (phy, mac, wifiApNode); 115 | 116 | MobilityHelper mobility; 117 | 118 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 119 | "MinX", DoubleValue (0.0), 120 | "MinY", DoubleValue (0.0), 121 | "DeltaX", DoubleValue (5.0), 122 | "DeltaY", DoubleValue (10.0), 123 | "GridWidth", UintegerValue (3), 124 | "LayoutType", StringValue ("RowFirst")); 125 | 126 | mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", 127 | "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50))); 128 | mobility.Install (wifiStaNodes); 129 | 130 | mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 131 | mobility.Install (wifiApNode); 132 | 133 | InternetStackHelper stack; 134 | stack.Install (csmaNodes); 135 | stack.Install (wifiApNode); 136 | stack.Install (wifiStaNodes); 137 | 138 | Ipv4AddressHelper address; 139 | 140 | address.SetBase ("10.1.1.0", "255.255.255.0"); 141 | Ipv4InterfaceContainer p2pInterfaces; 142 | p2pInterfaces = address.Assign (p2pDevices); 143 | 144 | address.SetBase ("10.1.2.0", "255.255.255.0"); 145 | Ipv4InterfaceContainer csmaInterfaces; 146 | csmaInterfaces = address.Assign (csmaDevices); 147 | 148 | address.SetBase ("10.1.3.0", "255.255.255.0"); 149 | address.Assign (staDevices); 150 | address.Assign (apDevices); 151 | 152 | UdpEchoServerHelper echoServer (9); 153 | 154 | ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma)); 155 | serverApps.Start (Seconds (1.0)); 156 | serverApps.Stop (Seconds (simTime + 1)); 157 | 158 | UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9); 159 | echoClient.SetAttribute ("MaxPackets", UintegerValue (4294967295u)); 160 | echoClient.SetAttribute ("Interval", TimeValue (Seconds (0.0005))); 161 | echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); 162 | 163 | for(i=0; i < nWifi; i++) 164 | { 165 | ApplicationContainer clientApps = echoClient.Install (wifiStaNodes.Get (i)); 166 | clientApps.Start (Seconds (2.0)); 167 | clientApps.Stop (Seconds (simTime + 1)); 168 | } 169 | 170 | Ipv4GlobalRoutingHelper::PopulateRoutingTables (); 171 | 172 | Simulator::Stop (Seconds (simTime)); 173 | 174 | if (tracing == true) 175 | { 176 | pointToPoint.EnablePcapAll (outputFilename); 177 | phy.EnablePcap (outputFilename, apDevices.Get (0)); 178 | csma.EnablePcap (outputFilename, csmaDevices.Get (0), true); 179 | } 180 | 181 | Simulator::Run (); 182 | Simulator::Destroy (); 183 | return 0; 184 | } 185 | -------------------------------------------------------------------------------- /project_1/thrpt_rtt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void my_packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char *packet); 8 | void calc_packet_info(const u_char *packet, struct pcap_pkthdr packet_header); 9 | 10 | 11 | //Global variables for data and time calculations 12 | int i = 0; 13 | double tme1 = 0; 14 | double tme2 = 0; 15 | double rtm1 = 0; 16 | double rtm2 = 0; 17 | double ftm1 = 0; 18 | double ftm2 = 0; 19 | double tdata = 0; 20 | double rtt = 0; 21 | 22 | int main(int argc, char *argv[]) 23 | { 24 | char error_buffer[PCAP_ERRBUF_SIZE]; 25 | pcap_t *handle = pcap_open_offline("/home/setu/Documents/ece695/ns-3-allinone/ns-3-dev/thrpt-0-0.pcap", error_buffer); 26 | int timeout_limit = 10000; /* In milliseconds */ 27 | double thrpt; 28 | 29 | if (handle == NULL) 30 | { 31 | return 2; 32 | } 33 | 34 | pcap_loop(handle, 0, my_packet_handler, NULL); 35 | pcap_close(handle); 36 | 37 | 38 | //Calculations of time and throughput 39 | ftm1 = (ftm1 + ftm2) - (rtm1 + rtm2); 40 | thrpt = tdata * 8 / (ftm1 * 1000000); 41 | rtt = (((tme1 + tme2)/i) - (rtm1 + rtm2)); 42 | 43 | 44 | //Output 45 | printf("\nTotal number of packets sent / received: %d", i); 46 | printf("\n\nTotal data transmitted: %f", tdata); 47 | printf("\n\nAverage round trip time of packets: %f", rtt); 48 | printf("\n\nThroughput: %f Mbit/s\n\n", thrpt); 49 | return 0; 50 | } 51 | 52 | void my_packet_handler(u_char *args, const struct pcap_pkthdr *packet_header, const u_char *packet_body) 53 | { 54 | calc_packet_info(packet_body, *packet_header); 55 | return; 56 | } 57 | 58 | void calc_packet_info(const u_char *packet, struct pcap_pkthdr packet_header) 59 | { 60 | //Recording data of each packet 61 | tdata = tdata + packet_header.len; 62 | i++; 63 | if(i == 1) 64 | { 65 | rtm1 = packet_header.ts.tv_sec; 66 | rtm2 = packet_header.ts.tv_usec*0.000001; 67 | tme1 = rtm1; 68 | tme2 = rtm2; 69 | } 70 | else 71 | { 72 | ftm1 = packet_header.ts.tv_sec; 73 | ftm2 = packet_header.ts.tv_usec*0.000001; 74 | tme1 = tme1 + ftm1; 75 | tme2 = tme2 + ftm2; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /project_2/manet-28.cc: -------------------------------------------------------------------------------- 1 | #include "ns3/aodv-module.h" 2 | #include "ns3/core-module.h" 3 | #include "ns3/network-module.h" 4 | #include "ns3/internet-module.h" 5 | #include "ns3/mobility-module.h" 6 | #include "ns3/point-to-point-module.h" 7 | #include "ns3/wifi-module.h" 8 | #include "ns3/v4ping-helper.h" 9 | #include "ns3/position-allocator.h" 10 | #include "ns3/propagation-loss-model.h" 11 | #include "ns3/applications-module.h" 12 | #include "ns3/v4ping.h" 13 | #include "ns3/flow-monitor-module.h" 14 | #include "ns3/udp-client-server-helper.h" 15 | #include "ns3/udp-server.h" 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | using namespace ns3; 22 | using namespace std; 23 | 24 | /** 25 | * \brief Test script. 26 | * 27 | * This script creates 1-dimensional grid topology and then ping last node from the first one: 28 | * 29 | * [10.0.0.1] <-- step --> [10.0.0.2] <-- step --> [10.0.0.3] <-- step --> [10.0.0.4] 30 | * 31 | * ping 10.0.0.4 32 | */ 33 | class AodvExample 34 | { 35 | public: 36 | AodvExample (); 37 | /// Configure script parameters, \return true on successful configuration 38 | bool Configure (int argc, char **argv); 39 | /// Run simulation 40 | void Run (); 41 | /// Report results 42 | void Report (std::ostream & os); 43 | 44 | private: 45 | 46 | // parameters 47 | /// Number of nodes 48 | uint32_t size; 49 | /// Distance between nodes, meters 50 | double step; 51 | /// Simulation time, seconds 52 | double simTime; 53 | /// Write per-device PCAP traces if true 54 | bool pcap; 55 | /// Print routes if true 56 | bool printRoutes; 57 | 58 | string topology = "scratch/manet100.csv"; 59 | 60 | double txrange = 50; 61 | 62 | uint32_t interval = 10; 63 | 64 | bool verbose = false; 65 | 66 | bool tracing = true; 67 | 68 | char* outputFilename = (char *)"manet"; 69 | 70 | // network 71 | NodeContainer nodes; 72 | NetDeviceContainer devices; 73 | Ipv4InterfaceContainer interfaces; 74 | Address serverAddress[50]; 75 | UdpServer sermon; 76 | YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); 77 | WifiMacHelper wifiMac; 78 | 79 | private: 80 | void CreateNodes (); 81 | void CreateDevices (); 82 | void InstallInternetStack (); 83 | void InstallApplications (); 84 | }; 85 | 86 | NS_LOG_COMPONENT_DEFINE ("ManetTest"); 87 | 88 | int main (int argc, char **argv) 89 | { 90 | AodvExample test; 91 | if (!test.Configure (argc, argv)) 92 | NS_FATAL_ERROR ("Configuration failed. Aborted."); 93 | 94 | test.Run (); 95 | test.Report (std::cout); 96 | return 0; 97 | } 98 | 99 | //----------------------------------------------------------------------------- 100 | AodvExample::AodvExample () : 101 | size (10), 102 | step (5), 103 | simTime (50), 104 | pcap (true), 105 | printRoutes (false) 106 | { 107 | } 108 | 109 | bool 110 | AodvExample::Configure (int argc, char **argv) 111 | { 112 | // Enable AODV logs by default. Comment this if too noisy 113 | // LogComponentEnable("AodvRoutingProtocol", LOG_LEVEL_ALL); 114 | 115 | SeedManager::SetSeed (12345); 116 | CommandLine cmd; 117 | 118 | cmd.AddValue ("pcap", "Write PCAP traces.", pcap); 119 | cmd.AddValue ("printRoutes", "Print routing table dumps.", printRoutes); 120 | cmd.AddValue ("size", "Number of nodes.", size); 121 | cmd.AddValue ("simTime", "Simulation time, in seconds.", simTime); 122 | cmd.AddValue ("outputFilename", "Output filename", outputFilename); 123 | cmd.AddValue ("topology", "Topology file.", topology); 124 | cmd.AddValue ("txrange", "Transmission range per node, in meters.", txrange); 125 | cmd.AddValue ("interval", "Interval between each iteration.", interval); 126 | cmd.AddValue ("verbose", "Verbose tracking.", verbose); 127 | cmd.AddValue ("tracing", "Enable pcap tracing", tracing); 128 | cmd.AddValue ("outputFilename", "Output filename", outputFilename); 129 | 130 | cmd.Parse (argc, argv); 131 | 132 | if (verbose) 133 | { 134 | LogComponentEnable ("UdpSocket", LOG_LEVEL_INFO); 135 | LogComponentEnable ("UdpServer", LOG_LEVEL_INFO); 136 | } 137 | 138 | return true; 139 | } 140 | 141 | void 142 | AodvExample::Run () 143 | { 144 | // Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time. 145 | CreateNodes (); 146 | CreateDevices (); 147 | InstallInternetStack (); 148 | InstallApplications (); 149 | 150 | //std::cout << "Starting simulation for " << simTime << " s ...\n"; 151 | 152 | Simulator::Stop (Seconds (simTime)); 153 | Simulator::Run (); 154 | Simulator::Destroy (); 155 | } 156 | 157 | void 158 | AodvExample::Report (std::ostream &) 159 | { 160 | } 161 | 162 | void 163 | AodvExample::CreateNodes () 164 | { 165 | std::cout << "Creating " << (unsigned)size << " nodes with transmission range " << txrange << "m.\n"; 166 | nodes.Create (size); 167 | // Name nodes 168 | for (uint32_t i = 0; i < size; ++i) 169 | { 170 | std::ostringstream os; 171 | os << "node-" << i; 172 | Names::Add (os.str (), nodes.Get (i)); 173 | } 174 | 175 | Ptr positionAllocS = CreateObject (); 176 | 177 | 178 | std::string line; 179 | ifstream file(topology); 180 | 181 | uint16_t i = 0 ; 182 | double vec[3]; 183 | 184 | if(file.is_open()) 185 | { 186 | while(getline(file,line)) 187 | { 188 | 189 | //std::cout<Add(Vector(vec[1], vec[2], 0.0)); 207 | i = 0; 208 | } 209 | } 210 | 211 | } 212 | file.close(); 213 | } 214 | else 215 | { 216 | std::cout<<"Error in csv file"<< '\n'; 217 | } 218 | 219 | 220 | 221 | MobilityHelper mobilityS; 222 | mobilityS.SetPositionAllocator(positionAllocS); 223 | mobilityS.SetMobilityModel("ns3::ConstantPositionMobilityModel"); //whatever it is 224 | mobilityS.Install(nodes); 225 | } 226 | 227 | void 228 | AodvExample::CreateDevices () 229 | { 230 | 231 | wifiMac.SetType ("ns3::AdhocWifiMac"); 232 | 233 | YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); 234 | wifiChannel.AddPropagationLoss("ns3::RangePropagationLossModel", 235 | "MaxRange", DoubleValue (txrange)); 236 | wifiPhy.SetChannel (wifiChannel.Create ()); 237 | WifiHelper wifi; 238 | wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0)); 239 | devices = wifi.Install (wifiPhy, wifiMac, nodes); 240 | 241 | if (pcap) 242 | { 243 | wifiPhy.EnablePcapAll (std::string ("aodv")); 244 | } 245 | } 246 | 247 | void 248 | AodvExample::InstallInternetStack () 249 | { 250 | 251 | AodvHelper aodv; 252 | // you can configure AODV attributes here using aodv.Set(name, value) 253 | InternetStackHelper stack; 254 | stack.SetRoutingHelper (aodv); // has effect on the next Install () 255 | stack.Install (nodes); 256 | Ipv4AddressHelper address; 257 | address.SetBase ("10.0.0.0", "255.0.0.0"); 258 | interfaces = address.Assign (devices); 259 | 260 | for(uint32_t i = 0; i < (size / 2); i++) 261 | { 262 | serverAddress[i] = Address (interfaces.GetAddress (i)); 263 | } 264 | 265 | /* 266 | if (printRoutes) 267 | { 268 | Ptr routingStream = Create ("aodv.routes", std::ios::out); 269 | aodv.PrintRoutingTableAllAt (Seconds (8), routingStream); 270 | } 271 | */ 272 | } 273 | 274 | void 275 | AodvExample::InstallApplications () 276 | { 277 | uint16_t i = 0; 278 | uint16_t j = 0; 279 | uint16_t k = 0; 280 | //uint16_t n = 10; 281 | uint16_t port = 4000; 282 | UdpServerHelper server (port); 283 | ApplicationContainer apps; 284 | 285 | for(i = 0; i < (size / 2); i++) 286 | { 287 | apps = server.Install (nodes.Get (i)); 288 | } 289 | 290 | apps.Start (Seconds (1.0)); 291 | apps.Stop (Seconds (simTime)); 292 | 293 | //Ptr sermon = server.GetServer(); 294 | 295 | // Create one UdpClient application to send UDP datagrams from node zero to 296 | // node one. 297 | 298 | uint32_t MaxPacketSize = 1024; 299 | Time interPacketInterval = Seconds (0.01); 300 | uint32_t maxPacketCount = 3; 301 | double interval_start = 2.0, interval_end = interval_start + interval; 302 | //std::cout << "Sending packets now.\n\n"; 303 | for(k = 1; k <= (size / 2); k++) 304 | { 305 | for(i = 0; i < k; i++) 306 | { 307 | UdpClientHelper client (serverAddress[i], port); 308 | client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); 309 | client.SetAttribute ("Interval", TimeValue (interPacketInterval)); 310 | client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize)); 311 | for(j = (size / 2); j < ((size / 2) + k); j++) 312 | { 313 | apps = client.Install (nodes.Get (j)); 314 | } 315 | } 316 | 317 | apps.Start (Seconds (interval_start)); 318 | apps.Stop (Seconds (interval_end)); 319 | interval_start = interval_end + 1.0; 320 | interval_end = interval_start + interval; 321 | } 322 | 323 | uint32_t rxPacketsum = 0; 324 | double Delaysum = 0; 325 | uint32_t txPacketsum = 0; 326 | uint32_t txBytessum = 0; 327 | uint32_t rxBytessum = 0; 328 | uint32_t txTimeFirst = 0; 329 | uint32_t rxTimeLast = 0; 330 | uint32_t lostPacketssum = 0; 331 | 332 | FlowMonitorHelper flowmon; 333 | Ptr monitor = flowmon.InstallAll(); 334 | 335 | Simulator::Stop (Seconds (10.0)); 336 | //std::cout<<"Packets sent. \n"; 337 | if (tracing == true) 338 | { 339 | wifiPhy.EnablePcapAll (outputFilename); 340 | //wifiPhy.EnablePcap (outputFilename, nodes.Get (0)); 341 | //csma.EnablePcap (outputFilename, csmaDevices.Get (0), true); 342 | } 343 | Simulator::Run (); 344 | 345 | k = 0; 346 | 347 | monitor->CheckForLostPackets (); 348 | 349 | Ptr classifier = DynamicCast (flowmon.GetClassifier ()); 350 | std::map stats = monitor->GetFlowStats (); 351 | 352 | for (std::map::const_iterator i = stats.begin (); i != stats.end (); ++i) 353 | { 354 | Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first); 355 | rxPacketsum += i->second.rxPackets; 356 | txPacketsum += i->second.txPackets; 357 | txBytessum += i->second.txBytes; 358 | rxBytessum += i->second.rxBytes; 359 | Delaysum += i->second.delaySum.GetSeconds(); 360 | lostPacketssum += i->second.lostPackets; 361 | txTimeFirst += i->second.timeFirstTxPacket.GetSeconds(); 362 | rxTimeLast += i->second.timeLastRxPacket.GetSeconds(); 363 | 364 | 365 | if ((t.sourceAddress < "10.0.0.101") && (t.destinationAddress < "10.0.0.101")) 366 | { 367 | if((i->second.txBytes >= 1000)||(i->second.rxBytes >= 1000))//&&(i->second.txBytes == i->second.rxBytes)) 368 | { 369 | k++; 370 | 371 | 372 | 373 | } 374 | 375 | //std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n"; 376 | //std::cout << " Tx Bytes: " << i->second.txBytes << "\n"; 377 | //std::cout << " Rx Bytes: " << i->second.rxBytes << "\n"; 378 | //std::cout << " Throughput: " << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() - i->second.timeFirstTxPacket.GetSeconds())/1024/1024 << " Mbps\n"; 379 | } 380 | } 381 | 382 | 383 | uint64_t timeDiff = (rxTimeLast - txTimeFirst); 384 | 385 | //uint64_t rcvd = sermon->GetReceived(); 386 | //uint16_t lst = sermon->GetLost(); 387 | //std::cout << k << " " << " " << rxTimeLast << " " < 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | using namespace std; 10 | 11 | int main () 12 | { 13 | std::string line; 14 | ifstream file("manet100.csv"); 15 | 16 | int i = 0, j = 0; 17 | double vec[3] = {0}; 18 | double vecfull[100][2] = {0}; 19 | double neigh[100]= {0}; 20 | int rmin = 25; 21 | double tmp = 0; 22 | double sum = 0; 23 | 24 | if(file.is_open()) 25 | { 26 | while(getline(file,line)) 27 | { 28 | char seps[] = ","; 29 | char *token; 30 | token = strtok(&line[0], seps); 31 | while(token != NULL) 32 | { 33 | vec[i] = atof(token); 34 | i++; 35 | token = strtok (NULL, ","); 36 | if(i == 3) 37 | { 38 | vecfull[j][0] = vec[1]; 39 | vecfull[j][1] = vec[2]; 40 | j++; 41 | i = 0; 42 | } 43 | } 44 | 45 | } 46 | file.close(); 47 | } 48 | else 49 | { 50 | std::cout<<"Error in csv file"<< '\n'; 51 | } 52 | 53 | for(i = 0; i < 100; i++) 54 | { 55 | for(j = 0; j < 100; j++) 56 | { 57 | if(i == j) 58 | {} 59 | else 60 | { 61 | tmp = ((vecfull[j][1]-vecfull[i][1])*(vecfull[j][1]-vecfull[i][1])) + ((vecfull[j][0]-vecfull[i][0])*(vecfull[j][0]-vecfull[i][0])); 62 | if(tmp <= (rmin*rmin)) 63 | { 64 | neigh[i]++; 65 | } 66 | } 67 | } 68 | std::cout << neigh[i] << "\n"; 69 | sum += neigh[i]; 70 | } 71 | sum = sum / 100; 72 | cout << "\nMean number of neighbors for rmin of " << rmin << "m are " << (sum / 100) << ".\n\n"; 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /project_3/compare.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "ns3/core-module.h" 5 | #include "ns3/network-module.h" 6 | #include "ns3/internet-module.h" 7 | #include "ns3/mobility-module.h" 8 | #include "ns3/wifi-module.h" 9 | #include "ns3/aodv-module.h" 10 | #include "ns3/olsr-module.h" 11 | #include "ns3/dsdv-module.h" 12 | #include "ns3/dsr-module.h" 13 | #include "ns3/applications-module.h" 14 | 15 | using namespace ns3; 16 | using namespace std; 17 | using namespace dsr; 18 | 19 | NS_LOG_COMPONENT_DEFINE ("manet-routing-compare"); 20 | 21 | class RoutingExperiment 22 | { 23 | public: 24 | RoutingExperiment (); 25 | void Run (int nSinks, double txp, std::string CSVfileName); 26 | std::string CommandSetup (int argc, char **argv); 27 | 28 | private: 29 | Ptr SetupPacketReceive (Ipv4Address addr, Ptr node); 30 | void ReceivePacket (Ptr socket); 31 | void CheckThroughput (); 32 | 33 | uint32_t port; 34 | uint32_t bytesTotal; 35 | uint32_t packetsReceived; 36 | 37 | std::string m_CSVfileName; 38 | int m_nSinks; 39 | std::string m_protocolName; 40 | double m_txp; 41 | bool m_traceMobility; 42 | uint32_t m_protocol; 43 | double txrange = 50; 44 | uint32_t interval = 10; 45 | string topology = "scratch/manet100.csv"; 46 | }; 47 | 48 | RoutingExperiment::RoutingExperiment () 49 | : port (9), 50 | bytesTotal (0), 51 | packetsReceived (0), 52 | m_CSVfileName ("manet-routing-output.csv"), 53 | m_traceMobility (false), 54 | m_protocol (2) // AODV 55 | { 56 | } 57 | 58 | static inline std::string 59 | PrintReceivedPacket (Ptr socket, Ptr packet, Address senderAddress) 60 | { 61 | std::ostringstream oss; 62 | 63 | oss << Simulator::Now ().GetSeconds () << " " << socket->GetNode ()->GetId (); 64 | 65 | if (InetSocketAddress::IsMatchingType (senderAddress)) 66 | { 67 | InetSocketAddress addr = InetSocketAddress::ConvertFrom (senderAddress); 68 | oss << " received one packet from " << addr.GetIpv4 (); 69 | } 70 | else 71 | { 72 | oss << " received one packet!"; 73 | } 74 | return oss.str (); 75 | } 76 | 77 | void 78 | RoutingExperiment::ReceivePacket (Ptr socket) 79 | { 80 | Ptr packet; 81 | Address senderAddress; 82 | while ((packet = socket->RecvFrom (senderAddress))) 83 | { 84 | bytesTotal += packet->GetSize (); 85 | packetsReceived += 1; 86 | NS_LOG_UNCOND (PrintReceivedPacket (socket, packet, senderAddress)); 87 | } 88 | } 89 | 90 | void 91 | RoutingExperiment::CheckThroughput () 92 | { 93 | double kbs = (bytesTotal * 8.0) / 1000; 94 | bytesTotal = 0; 95 | 96 | std::ofstream out (m_CSVfileName.c_str (), std::ios::app); 97 | 98 | out << (Simulator::Now ()).GetSeconds () << "," 99 | << kbs << "," 100 | << packetsReceived << "," 101 | << m_nSinks << "," 102 | << m_protocolName << "," 103 | << m_txp << "" 104 | << std::endl; 105 | 106 | out.close (); 107 | packetsReceived = 0; 108 | Simulator::Schedule (Seconds (1.0), &RoutingExperiment::CheckThroughput, this); 109 | } 110 | 111 | Ptr 112 | RoutingExperiment::SetupPacketReceive (Ipv4Address addr, Ptr node) 113 | { 114 | TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); 115 | Ptr sink = Socket::CreateSocket (node, tid); 116 | InetSocketAddress local = InetSocketAddress (addr, port); 117 | sink->Bind (local); 118 | sink->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket, this)); 119 | 120 | return sink; 121 | } 122 | 123 | std::string 124 | RoutingExperiment::CommandSetup (int argc, char **argv) 125 | { 126 | CommandLine cmd; 127 | cmd.AddValue ("CSVfileName", "The name of the CSV output file name", m_CSVfileName); 128 | cmd.AddValue ("traceMobility", "Enable mobility tracing", m_traceMobility); 129 | cmd.AddValue ("protocol", "1=OLSR;2=AODV;3=DSDV;4=DSR", m_protocol); 130 | cmd.Parse (argc, argv); 131 | return m_CSVfileName; 132 | } 133 | 134 | int 135 | main (int argc, char *argv[]) 136 | { 137 | RoutingExperiment experiment; 138 | std::string CSVfileName = experiment.CommandSetup (argc,argv); 139 | 140 | std::ofstream out (CSVfileName.c_str ()); 141 | out << "SimulationSecond," << 142 | "ReceiveRate," << 143 | "PacketsReceived," << 144 | "NumberOfSinks," << 145 | "RoutingProtocol," << 146 | "TransmissionPower" << 147 | std::endl; 148 | out.close (); 149 | 150 | int nSinks = 10; 151 | double txp = 7.5; 152 | 153 | experiment.Run (nSinks, txp, CSVfileName); 154 | } 155 | 156 | void 157 | RoutingExperiment::Run (int nSinks, double txp, std::string CSVfileName) 158 | { 159 | Packet::EnablePrinting (); 160 | m_nSinks = nSinks; 161 | m_txp = txp; 162 | m_CSVfileName = CSVfileName; 163 | 164 | int nWifis = 50; 165 | 166 | double TotalTime = 200.0; 167 | std::string rate ("2048bps"); 168 | std::string phyMode ("DsssRate11Mbps"); 169 | std::string tr_name ("manet-routing-compare"); 170 | int nodeSpeed = 20; 171 | int nodePause = 0; 172 | m_protocolName = "protocol"; 173 | 174 | Config::SetDefault ("ns3::OnOffApplication::PacketSize",StringValue ("64")); 175 | Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue (rate)); 176 | Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",StringValue (phyMode)); 177 | 178 | NodeContainer adhocNodes; 179 | adhocNodes.Create (nWifis); 180 | WifiHelper wifi; 181 | wifi.SetStandard (WIFI_PHY_STANDARD_80211b); 182 | 183 | YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); 184 | YansWifiChannelHelper wifiChannel; 185 | wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); 186 | wifiChannel.AddPropagationLoss("ns3::RangePropagationLossModel", 187 | "MaxRange", DoubleValue (txrange)); 188 | 189 | wifiPhy.SetChannel (wifiChannel.Create ()); 190 | WifiMacHelper wifiMac; 191 | wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", 192 | "DataMode",StringValue (phyMode), 193 | "ControlMode",StringValue (phyMode)); 194 | 195 | wifiPhy.Set ("TxPowerStart",DoubleValue (txp)); 196 | wifiPhy.Set ("TxPowerEnd", DoubleValue (txp)); 197 | 198 | wifiMac.SetType ("ns3::AdhocWifiMac"); 199 | NetDeviceContainer adhocDevices = wifi.Install (wifiPhy, wifiMac, adhocNodes); 200 | 201 | Ptr positionAllocS = CreateObject (); 202 | 203 | 204 | std::string line; 205 | ifstream file(topology); 206 | 207 | uint16_t i = 0 ; 208 | double vec[3]; 209 | 210 | if(file.is_open()) 211 | { 212 | while(getline(file,line)) 213 | { 214 | char seps[] = ","; 215 | char *token; 216 | token = strtok(&line[0], seps); 217 | while(token != NULL) 218 | { 219 | vec[i] = atof(token); 220 | i++; 221 | token = strtok (NULL, ","); 222 | if(i == 3) 223 | { 224 | positionAllocS->Add(Vector(vec[1], vec[2], 0.0)); 225 | i = 0; 226 | } 227 | } 228 | } 229 | file.close(); 230 | } 231 | else 232 | { 233 | std::cout<<"Error in csv file"<< '\n'; 234 | } 235 | 236 | 237 | 238 | MobilityHelper mobilityAdhoc; 239 | mobilityAdhoc.SetPositionAllocator(positionAllocS); 240 | mobilityAdhoc.SetMobilityModel("ns3::ConstantPositionMobilityModel"); 241 | mobilityAdhoc.Install(adhocNodes); 242 | 243 | AodvHelper aodv; 244 | OlsrHelper olsr; 245 | DsdvHelper dsdv; 246 | DsrHelper dsr; 247 | DsrMainHelper dsrMain; 248 | Ipv4ListRoutingHelper list; 249 | InternetStackHelper internet; 250 | 251 | switch (m_protocol) 252 | { 253 | case 1: 254 | list.Add (olsr, 100); 255 | m_protocolName = "OLSR"; 256 | break; 257 | case 2: 258 | list.Add (aodv, 100); 259 | m_protocolName = "AODV"; 260 | break; 261 | case 3: 262 | list.Add (dsdv, 100); 263 | m_protocolName = "DSDV"; 264 | break; 265 | case 4: 266 | m_protocolName = "DSR"; 267 | break; 268 | default: 269 | NS_FATAL_ERROR ("No such protocol:" << m_protocol); 270 | } 271 | 272 | if (m_protocol < 4) 273 | { 274 | internet.SetRoutingHelper (list); 275 | internet.Install (adhocNodes); 276 | } 277 | else if (m_protocol == 4) 278 | { 279 | internet.Install (adhocNodes); 280 | dsrMain.Install (dsr, adhocNodes); 281 | } 282 | 283 | NS_LOG_INFO ("assigning ip address"); 284 | 285 | Ipv4AddressHelper addressAdhoc; 286 | addressAdhoc.SetBase ("10.1.1.0", "255.255.255.0"); 287 | Ipv4InterfaceContainer adhocInterfaces; 288 | adhocInterfaces = addressAdhoc.Assign (adhocDevices); 289 | 290 | OnOffHelper onoff1 ("ns3::UdpSocketFactory",Address ()); 291 | onoff1.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]")); 292 | onoff1.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]")); 293 | 294 | for (int i = 0; i < nSinks; i++) 295 | { 296 | Ptr sink = SetupPacketReceive (adhocInterfaces.GetAddress (i), adhocNodes.Get (i)); 297 | 298 | AddressValue remoteAddress (InetSocketAddress (adhocInterfaces.GetAddress (i), port)); 299 | onoff1.SetAttribute ("Remote", remoteAddress); 300 | 301 | Ptr var = CreateObject (); 302 | ApplicationContainer temp = onoff1.Install (adhocNodes.Get (i + nSinks)); 303 | temp.Start (Seconds (var->GetValue (100.0,101.0))); 304 | temp.Stop (Seconds (TotalTime)); 305 | } 306 | 307 | std::stringstream ss; 308 | ss << nWifis; 309 | std::string nodes = ss.str (); 310 | 311 | std::stringstream ss2; 312 | ss2 << nodeSpeed; 313 | std::string sNodeSpeed = ss2.str (); 314 | 315 | std::stringstream ss3; 316 | ss3 << nodePause; 317 | std::string sNodePause = ss3.str (); 318 | 319 | std::stringstream ss4; 320 | ss4 << rate; 321 | std::string sRate = ss4.str (); 322 | 323 | AsciiTraceHelper ascii; 324 | MobilityHelper::EnableAsciiAll (ascii.CreateFileStream (tr_name + ".mob")); 325 | 326 | NS_LOG_INFO ("Run Simulation."); 327 | 328 | CheckThroughput (); 329 | 330 | Simulator::Stop (Seconds (TotalTime)); 331 | Simulator::Run (); 332 | 333 | Simulator::Destroy (); 334 | } 335 | 336 | -------------------------------------------------------------------------------- /project_3/dsr-08.cc: -------------------------------------------------------------------------------- 1 | #include "ns3/core-module.h" 2 | #include "ns3/network-module.h" 3 | #include "ns3/applications-module.h" 4 | #include "ns3/mobility-module.h" 5 | #include "ns3/config-store-module.h" 6 | #include "ns3/wifi-module.h" 7 | #include "ns3/internet-module.h" 8 | #include "ns3/dsr-module.h" 9 | #include "ns3/flow-monitor-module.h" 10 | #include "ns3/udp-client-server-helper.h" 11 | #include "ns3/udp-server.h" 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | using namespace ns3; 19 | using namespace std; 20 | 21 | NS_LOG_COMPONENT_DEFINE ("DsrTest"); 22 | 23 | int 24 | main (int argc, char *argv[]) 25 | { 26 | #if 0 27 | LogComponentEnable ("Ipv4L3Protocol", LOG_LEVEL_ALL); 28 | LogComponentEnable ("UdpL4Protocol", LOG_LEVEL_ALL); 29 | LogComponentEnable ("UdpSocketImpl", LOG_LEVEL_ALL); 30 | LogComponentEnable ("NetDevice", LOG_LEVEL_ALL); 31 | LogComponentEnable ("Ipv4EndPointDemux", LOG_LEVEL_ALL); 32 | #endif 33 | 34 | #if 0 35 | LogComponentEnable ("DsrOptions", LOG_LEVEL_ALL); 36 | LogComponentEnable ("DsrHelper", LOG_LEVEL_ALL); 37 | LogComponentEnable ("DsrRouting", LOG_LEVEL_ALL); 38 | LogComponentEnable ("DsrOptionHeader", LOG_LEVEL_ALL); 39 | LogComponentEnable ("DsrFsHeader", LOG_LEVEL_ALL); 40 | LogComponentEnable ("DsrGraReplyTable", LOG_LEVEL_ALL); 41 | LogComponentEnable ("DsrSendBuffer", LOG_LEVEL_ALL); 42 | LogComponentEnable ("DsrRouteCache", LOG_LEVEL_ALL); 43 | LogComponentEnable ("DsrMaintainBuffer", LOG_LEVEL_ALL); 44 | LogComponentEnable ("DsrRreqTable", LOG_LEVEL_ALL); 45 | LogComponentEnable ("DsrErrorBuffer", LOG_LEVEL_ALL); 46 | LogComponentEnable ("DsrNetworkQueue", LOG_LEVEL_ALL); 47 | #endif 48 | 49 | NS_LOG_INFO ("creating the nodes"); 50 | 51 | // General parameters 52 | uint32_t nWifis = 10; 53 | uint32_t nSinks = 10; 54 | double TotalTime = 100.0; 55 | //double dataTime = 500.0; 56 | //double ppers = 1; 57 | uint32_t packetSize = 64; 58 | //double dataStart = 100.0; // start sending data at 100s 59 | string topology = "scratch/manet100.csv"; 60 | 61 | uint32_t size = nWifis + nSinks; 62 | 63 | //mobility parameters 64 | double pauseTime = 0.0; 65 | double nodeSpeed = 20.0; 66 | double txpDistance = 500; 67 | 68 | Address serverAddress[50]; 69 | 70 | std::string rate = "0.512kbps"; 71 | std::string dataMode ("DsssRate11Mbps"); 72 | std::string phyMode ("DsssRate11Mbps"); 73 | 74 | //Allow users to override the default parameters and set it to new ones from CommandLine. 75 | CommandLine cmd; 76 | cmd.AddValue ("nWifis", "Number of wifi nodes", nWifis); 77 | cmd.AddValue ("nSinks", "Number of SINK traffic nodes", nSinks); 78 | cmd.AddValue ("rate", "CBR traffic rate(in kbps), Default:8", rate); 79 | cmd.AddValue ("nodeSpeed", "Node speed in RandomWayPoint model, Default:20", nodeSpeed); 80 | cmd.AddValue ("packetSize", "The packet size", packetSize); 81 | cmd.AddValue ("txpDistance", "Specify node's transmit range, Default:300", txpDistance); 82 | cmd.AddValue ("pauseTime", "pauseTime for mobility model, Default: 0", pauseTime); 83 | cmd.Parse (argc, argv); 84 | 85 | //SeedManager::SetSeed (10); 86 | //SeedManager::SetRun (1); 87 | 88 | NodeContainer adhocNodes; 89 | adhocNodes.Create (size); 90 | NetDeviceContainer allDevices; 91 | 92 | //std::cout << "Creating " << (unsigned)size << " nodes with transmission range " << txrange << "m.\n"; 93 | 94 | Ptr positionAllocS = CreateObject (); 95 | 96 | 97 | std::string line; 98 | ifstream file(topology); 99 | 100 | uint16_t i = 0 ; 101 | double vec[3]; 102 | 103 | if(file.is_open()) 104 | { 105 | while(getline(file,line)) 106 | { 107 | 108 | //std::cout<Add(Vector(vec[1], vec[2], 0.0)); 126 | i = 0; 127 | } 128 | } 129 | 130 | } 131 | file.close(); 132 | } 133 | else 134 | { 135 | std::cout<<"Error in csv file"<< '\n'; 136 | } 137 | 138 | MobilityHelper mobilityS; 139 | mobilityS.SetPositionAllocator(positionAllocS); 140 | mobilityS.SetMobilityModel("ns3::ConstantPositionMobilityModel"); //whatever it is 141 | mobilityS.Install (adhocNodes); 142 | 143 | /* NS_LOG_INFO ("setting the default phy and channel parameters"); 144 | Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue (phyMode)); 145 | Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200")); 146 | // disable fragmentation for frames below 2200 bytes 147 | Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200")); 148 | 149 | NS_LOG_INFO ("setting the default phy and channel parameters "); */ 150 | 151 | WifiHelper wifi; 152 | wifi.SetStandard (WIFI_PHY_STANDARD_80211b); 153 | YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); 154 | 155 | YansWifiChannelHelper wifiChannel; 156 | wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); 157 | wifiChannel.AddPropagationLoss ("ns3::RangePropagationLossModel", "MaxRange", DoubleValue (txpDistance)); 158 | wifiPhy.SetChannel (wifiChannel.Create ()); 159 | 160 | // Add a mac and disable rate control 161 | WifiMacHelper wifiMac; 162 | wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue (dataMode), "ControlMode", 163 | StringValue (phyMode)); 164 | 165 | wifiMac.SetType ("ns3::AdhocWifiMac"); 166 | allDevices = wifi.Install (wifiPhy, wifiMac, adhocNodes); 167 | 168 | NS_LOG_INFO ("Configure Tracing."); 169 | AsciiTraceHelper ascii; 170 | Ptr stream = ascii.CreateFileStream ("dsrtest.tr"); 171 | wifiPhy.EnableAsciiAll (stream); 172 | 173 | //AsciiTraceHelper ascii; 174 | //Ptr stream = ascii.CreateFileStream ("dsrtest.tr"); 175 | //wifiPhy.EnableAsciiAll (stream); 176 | /* 177 | MobilityHelper adhocMobility; 178 | ObjectFactory pos; 179 | pos.SetTypeId ("ns3::RandomRectanglePositionAllocator"); 180 | pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=300.0]")); 181 | pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1500.0]")); 182 | Ptr taPositionAlloc = pos.Create ()->GetObject (); 183 | 184 | std::ostringstream speedUniformRandomVariableStream; 185 | speedUniformRandomVariableStream << "ns3::UniformRandomVariable[Min=0.0|Max=" 186 | << nodeSpeed 187 | << "]"; 188 | 189 | std::ostringstream pauseConstantRandomVariableStream; 190 | pauseConstantRandomVariableStream << "ns3::ConstantRandomVariable[Constant=" 191 | << pauseTime 192 | << "]"; 193 | 194 | adhocMobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel", 195 | // "Speed", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=nodeSpeed]"), 196 | "Speed", StringValue (speedUniformRandomVariableStream.str ()), 197 | "Pause", StringValue (pauseConstantRandomVariableStream.str ()), 198 | "PositionAllocator", PointerValue (taPositionAlloc) 199 | ); 200 | adhocMobility.Install (adhocNodes); 201 | */ 202 | InternetStackHelper internet; 203 | DsrMainHelper dsrMain; 204 | DsrHelper dsr; 205 | internet.Install (adhocNodes); 206 | dsrMain.Install (dsr, adhocNodes); 207 | 208 | NS_LOG_INFO ("assigning ip address"); 209 | Ipv4AddressHelper address; 210 | address.SetBase ("10.1.1.0", "255.255.255.0"); 211 | Ipv4InterfaceContainer allInterfaces; 212 | allInterfaces = address.Assign (allDevices); 213 | 214 | uint16_t port = 9; 215 | //double randomStartTime = (1 / ppers) / nSinks; //distributed btw 1s evenly as we are sending 4pkt/s 216 | 217 | /* for (uint32_t i = 0; i < nSinks; ++i) 218 | { 219 | PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port)); 220 | ApplicationContainer apps_sink = sink.Install (adhocNodes.Get (i)); 221 | apps_sink.Start (Seconds (0.0)); 222 | apps_sink.Stop (Seconds (TotalTime)); 223 | 224 | OnOffHelper onoff1 ("ns3::UdpSocketFactory", Address (InetSocketAddress (allInterfaces.GetAddress (i), port))); 225 | onoff1.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]")); 226 | onoff1.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]")); 227 | onoff1.SetAttribute ("PacketSize", UintegerValue (packetSize)); 228 | onoff1.SetAttribute ("DataRate", DataRateValue (DataRate (rate))); 229 | 230 | ApplicationContainer apps1 = onoff1.Install (adhocNodes.Get (i + nWifis - nSinks)); 231 | apps1.Start (Seconds (dataStart + i * randomStartTime)); 232 | apps1.Stop (Seconds (dataTime + i * randomStartTime)); 233 | } 234 | */ 235 | 236 | 237 | uint16_t j = 0; 238 | uint16_t k = 0; 239 | //uint16_t n = 10; 240 | //uint16_t port = 4000; 241 | UdpServerHelper server (port); 242 | ApplicationContainer apps; 243 | 244 | for(i = 0; i < nSinks; i++) 245 | { 246 | apps = server.Install (adhocNodes.Get (i)); 247 | serverAddress[i] = Address (allInterfaces.GetAddress (i)); 248 | } 249 | 250 | apps.Start (Seconds (1.0)); 251 | apps.Stop (Seconds (TotalTime)); 252 | 253 | 254 | uint32_t interval = 5; 255 | uint32_t MaxPacketSize = 1024; 256 | Time interPacketInterval = Seconds (0.01); 257 | uint32_t maxPacketCount = 3; 258 | double interval_start = 2.0, interval_end = interval_start + interval; 259 | //std::cout << "Sending packets now.\n\n"; 260 | for(k = 1; k <= (size / 2); k++) 261 | { 262 | for(i = 0; i < k; i++) 263 | { 264 | UdpClientHelper client (serverAddress[i], port); 265 | client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); 266 | client.SetAttribute ("Interval", TimeValue (interPacketInterval)); 267 | client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize)); 268 | for(j = (size / 2); j < ((size / 2) + k); j++) 269 | { 270 | apps = client.Install (adhocNodes.Get (j)); 271 | } 272 | } 273 | 274 | apps.Start (Seconds (interval_start)); 275 | apps.Stop (Seconds (interval_end)); 276 | interval_start = interval_end + 1.0; 277 | interval_end = interval_start + interval; 278 | } 279 | 280 | NS_LOG_INFO ("Run Simulation."); 281 | Simulator::Stop (Seconds (TotalTime)); 282 | Simulator::Run (); 283 | Simulator::Destroy (); 284 | } 285 | 286 | -------------------------------------------------------------------------------- /project_3/manet-28.cc: -------------------------------------------------------------------------------- 1 | #include "ns3/aodv-module.h" 2 | #include "ns3/core-module.h" 3 | #include "ns3/network-module.h" 4 | #include "ns3/internet-module.h" 5 | #include "ns3/mobility-module.h" 6 | #include "ns3/point-to-point-module.h" 7 | #include "ns3/wifi-module.h" 8 | #include "ns3/v4ping-helper.h" 9 | #include "ns3/position-allocator.h" 10 | #include "ns3/propagation-loss-model.h" 11 | #include "ns3/applications-module.h" 12 | #include "ns3/v4ping.h" 13 | #include "ns3/flow-monitor-module.h" 14 | #include "ns3/udp-client-server-helper.h" 15 | #include "ns3/udp-server.h" 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | using namespace ns3; 22 | using namespace std; 23 | 24 | /** 25 | * \brief Test script. 26 | * 27 | * This script creates 1-dimensional grid topology and then ping last node from the first one: 28 | * 29 | * [10.0.0.1] <-- step --> [10.0.0.2] <-- step --> [10.0.0.3] <-- step --> [10.0.0.4] 30 | * 31 | * ping 10.0.0.4 32 | */ 33 | class AodvExample 34 | { 35 | public: 36 | AodvExample (); 37 | /// Configure script parameters, \return true on successful configuration 38 | bool Configure (int argc, char **argv); 39 | /// Run simulation 40 | void Run (); 41 | /// Report results 42 | void Report (std::ostream & os); 43 | 44 | private: 45 | 46 | // parameters 47 | /// Number of nodes 48 | uint32_t size; 49 | /// Distance between nodes, meters 50 | double step; 51 | /// Simulation time, seconds 52 | double simTime; 53 | /// Write per-device PCAP traces if true 54 | bool pcap; 55 | /// Print routes if true 56 | bool printRoutes; 57 | 58 | string topology = "scratch/manet100.csv"; 59 | 60 | double txrange = 50; 61 | 62 | uint32_t interval = 10; 63 | 64 | bool verbose = false; 65 | 66 | bool tracing = true; 67 | 68 | char* outputFilename = (char *)"manet"; 69 | 70 | // network 71 | NodeContainer nodes; 72 | NetDeviceContainer devices; 73 | Ipv4InterfaceContainer interfaces; 74 | Address serverAddress[50]; 75 | UdpServer sermon; 76 | YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); 77 | WifiMacHelper wifiMac; 78 | 79 | private: 80 | void CreateNodes (); 81 | void CreateDevices (); 82 | void InstallInternetStack (); 83 | void InstallApplications (); 84 | }; 85 | 86 | NS_LOG_COMPONENT_DEFINE ("ManetTest"); 87 | 88 | int main (int argc, char **argv) 89 | { 90 | AodvExample test; 91 | if (!test.Configure (argc, argv)) 92 | NS_FATAL_ERROR ("Configuration failed. Aborted."); 93 | 94 | test.Run (); 95 | test.Report (std::cout); 96 | return 0; 97 | } 98 | 99 | //----------------------------------------------------------------------------- 100 | AodvExample::AodvExample () : 101 | size (10), 102 | step (5), 103 | simTime (50), 104 | pcap (true), 105 | printRoutes (false) 106 | { 107 | } 108 | 109 | bool 110 | AodvExample::Configure (int argc, char **argv) 111 | { 112 | // Enable AODV logs by default. Comment this if too noisy 113 | // LogComponentEnable("AodvRoutingProtocol", LOG_LEVEL_ALL); 114 | 115 | SeedManager::SetSeed (12345); 116 | CommandLine cmd; 117 | 118 | cmd.AddValue ("pcap", "Write PCAP traces.", pcap); 119 | cmd.AddValue ("printRoutes", "Print routing table dumps.", printRoutes); 120 | cmd.AddValue ("size", "Number of nodes.", size); 121 | cmd.AddValue ("simTime", "Simulation time, in seconds.", simTime); 122 | cmd.AddValue ("outputFilename", "Output filename", outputFilename); 123 | cmd.AddValue ("topology", "Topology file.", topology); 124 | cmd.AddValue ("txrange", "Transmission range per node, in meters.", txrange); 125 | cmd.AddValue ("interval", "Interval between each iteration.", interval); 126 | cmd.AddValue ("verbose", "Verbose tracking.", verbose); 127 | cmd.AddValue ("tracing", "Enable pcap tracing", tracing); 128 | cmd.AddValue ("outputFilename", "Output filename", outputFilename); 129 | 130 | cmd.Parse (argc, argv); 131 | 132 | if (verbose) 133 | { 134 | LogComponentEnable ("UdpSocket", LOG_LEVEL_INFO); 135 | LogComponentEnable ("UdpServer", LOG_LEVEL_INFO); 136 | } 137 | 138 | return true; 139 | } 140 | 141 | void 142 | AodvExample::Run () 143 | { 144 | // Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time. 145 | CreateNodes (); 146 | CreateDevices (); 147 | InstallInternetStack (); 148 | InstallApplications (); 149 | 150 | //std::cout << "Starting simulation for " << simTime << " s ...\n"; 151 | 152 | Simulator::Stop (Seconds (simTime)); 153 | Simulator::Run (); 154 | Simulator::Destroy (); 155 | } 156 | 157 | void 158 | AodvExample::Report (std::ostream &) 159 | { 160 | } 161 | 162 | void 163 | AodvExample::CreateNodes () 164 | { 165 | std::cout << "Creating " << (unsigned)size << " nodes with transmission range " << txrange << "m.\n"; 166 | nodes.Create (size); 167 | // Name nodes 168 | for (uint32_t i = 0; i < size; ++i) 169 | { 170 | std::ostringstream os; 171 | os << "node-" << i; 172 | Names::Add (os.str (), nodes.Get (i)); 173 | } 174 | 175 | Ptr positionAllocS = CreateObject (); 176 | 177 | 178 | std::string line; 179 | ifstream file(topology); 180 | 181 | uint16_t i = 0 ; 182 | double vec[3]; 183 | 184 | if(file.is_open()) 185 | { 186 | while(getline(file,line)) 187 | { 188 | 189 | //std::cout<Add(Vector(vec[1], vec[2], 0.0)); 207 | i = 0; 208 | } 209 | } 210 | 211 | } 212 | file.close(); 213 | } 214 | else 215 | { 216 | std::cout<<"Error in csv file"<< '\n'; 217 | } 218 | 219 | 220 | 221 | MobilityHelper mobilityS; 222 | mobilityS.SetPositionAllocator(positionAllocS); 223 | mobilityS.SetMobilityModel("ns3::ConstantPositionMobilityModel"); //whatever it is 224 | mobilityS.Install(nodes); 225 | } 226 | 227 | void 228 | AodvExample::CreateDevices () 229 | { 230 | 231 | wifiMac.SetType ("ns3::AdhocWifiMac"); 232 | 233 | YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); 234 | wifiChannel.AddPropagationLoss("ns3::RangePropagationLossModel", 235 | "MaxRange", DoubleValue (txrange)); 236 | wifiPhy.SetChannel (wifiChannel.Create ()); 237 | WifiHelper wifi; 238 | wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0)); 239 | devices = wifi.Install (wifiPhy, wifiMac, nodes); 240 | 241 | if (pcap) 242 | { 243 | wifiPhy.EnablePcapAll (std::string ("aodv")); 244 | } 245 | } 246 | 247 | void 248 | AodvExample::InstallInternetStack () 249 | { 250 | 251 | AodvHelper aodv; 252 | // you can configure AODV attributes here using aodv.Set(name, value) 253 | InternetStackHelper stack; 254 | stack.SetRoutingHelper (aodv); // has effect on the next Install () 255 | stack.Install (nodes); 256 | Ipv4AddressHelper address; 257 | address.SetBase ("10.0.0.0", "255.0.0.0"); 258 | interfaces = address.Assign (devices); 259 | 260 | for(uint32_t i = 0; i < (size / 2); i++) 261 | { 262 | serverAddress[i] = Address (interfaces.GetAddress (i)); 263 | } 264 | 265 | /* 266 | if (printRoutes) 267 | { 268 | Ptr routingStream = Create ("aodv.routes", std::ios::out); 269 | aodv.PrintRoutingTableAllAt (Seconds (8), routingStream); 270 | } 271 | */ 272 | } 273 | 274 | void 275 | AodvExample::InstallApplications () 276 | { 277 | uint16_t i = 0; 278 | uint16_t j = 0; 279 | uint16_t k = 0; 280 | //uint16_t n = 10; 281 | uint16_t port = 4000; 282 | UdpServerHelper server (port); 283 | ApplicationContainer apps; 284 | 285 | for(i = 0; i < (size / 2); i++) 286 | { 287 | apps = server.Install (nodes.Get (i)); 288 | } 289 | 290 | apps.Start (Seconds (1.0)); 291 | apps.Stop (Seconds (simTime)); 292 | 293 | //Ptr sermon = server.GetServer(); 294 | 295 | // Create one UdpClient application to send UDP datagrams from node zero to 296 | // node one. 297 | 298 | uint32_t MaxPacketSize = 1024; 299 | Time interPacketInterval = Seconds (0.01); 300 | uint32_t maxPacketCount = 3; 301 | double interval_start = 2.0, interval_end = interval_start + interval; 302 | //std::cout << "Sending packets now.\n\n"; 303 | for(k = 1; k <= (size / 2); k++) 304 | { 305 | for(i = 0; i < k; i++) 306 | { 307 | UdpClientHelper client (serverAddress[i], port); 308 | client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); 309 | client.SetAttribute ("Interval", TimeValue (interPacketInterval)); 310 | client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize)); 311 | for(j = (size / 2); j < ((size / 2) + k); j++) 312 | { 313 | apps = client.Install (nodes.Get (j)); 314 | } 315 | } 316 | 317 | apps.Start (Seconds (interval_start)); 318 | apps.Stop (Seconds (interval_end)); 319 | interval_start = interval_end + 1.0; 320 | interval_end = interval_start + interval; 321 | } 322 | 323 | uint32_t rxPacketsum = 0; 324 | double Delaysum = 0; 325 | uint32_t txPacketsum = 0; 326 | uint32_t txBytessum = 0; 327 | uint32_t rxBytessum = 0; 328 | uint32_t txTimeFirst = 0; 329 | uint32_t rxTimeLast = 0; 330 | uint32_t lostPacketssum = 0; 331 | 332 | FlowMonitorHelper flowmon; 333 | Ptr monitor = flowmon.InstallAll(); 334 | 335 | Simulator::Stop (Seconds (10.0)); 336 | //std::cout<<"Packets sent. \n"; 337 | if (tracing == true) 338 | { 339 | wifiPhy.EnablePcapAll (outputFilename); 340 | //wifiPhy.EnablePcap (outputFilename, nodes.Get (0)); 341 | //csma.EnablePcap (outputFilename, csmaDevices.Get (0), true); 342 | } 343 | Simulator::Run (); 344 | 345 | k = 0; 346 | 347 | monitor->CheckForLostPackets (); 348 | 349 | Ptr classifier = DynamicCast (flowmon.GetClassifier ()); 350 | std::map stats = monitor->GetFlowStats (); 351 | 352 | for (std::map::const_iterator i = stats.begin (); i != stats.end (); ++i) 353 | { 354 | Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first); 355 | rxPacketsum += i->second.rxPackets; 356 | txPacketsum += i->second.txPackets; 357 | txBytessum += i->second.txBytes; 358 | rxBytessum += i->second.rxBytes; 359 | Delaysum += i->second.delaySum.GetSeconds(); 360 | lostPacketssum += i->second.lostPackets; 361 | txTimeFirst += i->second.timeFirstTxPacket.GetSeconds(); 362 | rxTimeLast += i->second.timeLastRxPacket.GetSeconds(); 363 | 364 | 365 | if ((t.sourceAddress < "10.0.0.101") && (t.destinationAddress < "10.0.0.101")) 366 | { 367 | if((i->second.txBytes >= 1000)||(i->second.rxBytes >= 1000))//&&(i->second.txBytes == i->second.rxBytes)) 368 | { 369 | k++; 370 | 371 | 372 | 373 | } 374 | 375 | //std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n"; 376 | //std::cout << " Tx Bytes: " << i->second.txBytes << "\n"; 377 | //std::cout << " Rx Bytes: " << i->second.rxBytes << "\n"; 378 | //std::cout << " Throughput: " << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() - i->second.timeFirstTxPacket.GetSeconds())/1024/1024 << " Mbps\n"; 379 | } 380 | } 381 | 382 | 383 | uint64_t timeDiff = (rxTimeLast - txTimeFirst); 384 | 385 | //uint64_t rcvd = sermon->GetReceived(); 386 | //uint16_t lst = sermon->GetLost(); 387 | //std::cout << k << " " << " " << rxTimeLast << " " < 18 | #include 19 | #include 20 | #include 21 | 22 | using namespace ns3; 23 | using namespace std; 24 | 25 | NS_LOG_COMPONENT_DEFINE ("SimplePointToPointOlsrExample"); 26 | 27 | int 28 | main (int argc, char *argv[]) 29 | { uint32_t size=30; 30 | double simTime=10; 31 | string topology = "scratch/manet100.csv"; 32 | double txrange = 50; 33 | uint32_t interval = 10; 34 | 35 | 36 | bool tracing = true; 37 | 38 | char* outputFilename = (char *)"manet"; 39 | 40 | // Users may find it convenient to turn on explicit debugging 41 | // for selected modules; the below lines suggest how to do this 42 | #if 0 43 | LogComponentEnable ("SimpleGlobalRoutingExample", LOG_LEVEL_INFO); 44 | #endif 45 | 46 | // Set up some default values for the simulation. Use the 47 | 48 | Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (210)); 49 | Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("448kb/s")); 50 | 51 | //DefaultValue::Bind ("DropTailQueue::m_maxPackets", 30); 52 | 53 | // Allow the user to override any of the defaults and the above 54 | // DefaultValue::Bind ()s at run-time, via command-line arguments 55 | CommandLine cmd; 56 | cmd.Parse (argc, argv); 57 | 58 | // Here, we will explicitly create four nodes. In more sophisticated 59 | // topologies, we could configure a node factory. 60 | NS_LOG_INFO ("Create nodes."); 61 | NodeContainer nodes; 62 | NetDeviceContainer devices; 63 | Ipv4InterfaceContainer interfaces; 64 | Address serverAddress[50]; 65 | UdpServer sermon; 66 | YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); 67 | WifiMacHelper wifiMac; 68 | 69 | nodes.Create (size); 70 | Ptr positionAllocS = CreateObject (); 71 | 72 | 73 | std::string line; 74 | ifstream file(topology); 75 | 76 | uint16_t i = 0; 77 | double vec[3]; 78 | 79 | if(file.is_open()) 80 | { 81 | while(getline(file,line)) 82 | { 83 | 84 | //std::cout<Add(Vector(vec[1], vec[2], 0.0)); 102 | i = 0; 103 | } 104 | } 105 | 106 | } 107 | file.close(); 108 | } 109 | else 110 | { 111 | std::cout<<"Error in csv file"<< '\n'; 112 | } 113 | 114 | 115 | 116 | MobilityHelper mobilityS; 117 | mobilityS.SetPositionAllocator(positionAllocS); 118 | mobilityS.SetMobilityModel("ns3::ConstantPositionMobilityModel"); //whatever it is 119 | mobilityS.Install(nodes); 120 | 121 | 122 | 123 | // Enable OLSR 124 | NS_LOG_INFO ("Enabling OLSR Routing."); 125 | OlsrHelper olsr; 126 | // We create the channels first without any IP addressing information 127 | NS_LOG_INFO ("Create channels."); 128 | YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); 129 | wifiChannel.AddPropagationLoss("ns3::RangePropagationLossModel", 130 | "MaxRange", DoubleValue (txrange)); 131 | wifiPhy.SetChannel (wifiChannel.Create ()); 132 | WifiHelper wifi; 133 | wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0)); 134 | devices = wifi.Install (wifiPhy, wifiMac, nodes); 135 | AsciiTraceHelper ascii; 136 | Ptr stream = ascii.CreateFileStream ("olsr.tr"); 137 | wifiPhy.EnableAsciiAll (stream); 138 | // Later, we add IP addresses. 139 | NS_LOG_INFO ("Assign IP Addresses."); 140 | InternetStackHelper stack; 141 | stack.SetRoutingHelper (olsr); // has effect on the next Install () 142 | stack.Install (nodes); 143 | Ipv4AddressHelper address; 144 | address.SetBase ("10.0.0.0", "255.0.0.0"); 145 | interfaces = address.Assign (devices); 146 | for(uint32_t i = 0; i < (size / 2); i++) 147 | { 148 | serverAddress[i] = Address (interfaces.GetAddress (i)); 149 | } 150 | 151 | uint16_t j = 0; 152 | uint16_t k = 0; 153 | //uint16_t n = 10; 154 | uint16_t port = 4000; 155 | UdpServerHelper server (port); 156 | ApplicationContainer apps; 157 | 158 | for(i = 0; i < (size / 2); i++) 159 | { 160 | apps = server.Install (nodes.Get (i)); 161 | } 162 | 163 | apps.Start (Seconds (1.0)); 164 | apps.Stop (Seconds (simTime)); 165 | 166 | // Create a packet sink to receive these packets 167 | // PacketSinkHelper sink ("ns3::UdpSocketFactory", 168 | // InetSocketAddress (Ipv4Address::GetAny (), port)); 169 | 170 | uint32_t MaxPacketSize = 1024; 171 | Time interPacketInterval = Seconds (0.01); 172 | uint32_t maxPacketCount = 3; 173 | double interval_start = 2.0, interval_end = interval_start + interval; 174 | //std::cout << "Sending packets now.\n\n"; 175 | for(k = 1; k <= (size / 2); k++) 176 | { 177 | for(i = 0; i < k; i++) 178 | { 179 | UdpClientHelper client (serverAddress[i], port); 180 | client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); 181 | client.SetAttribute ("Interval", TimeValue (interPacketInterval)); 182 | client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize)); 183 | for(j = (size / 2); j < ((size / 2) + k); j++) 184 | { 185 | apps = client.Install (nodes.Get (j)); 186 | } 187 | } 188 | 189 | apps.Start (Seconds (interval_start)); 190 | apps.Stop (Seconds (interval_end)); 191 | interval_start = interval_end + 1.0; 192 | interval_end = interval_start + interval; 193 | } 194 | 195 | uint32_t rxPacketsum = 0; 196 | double Delaysum = 0; 197 | uint32_t txPacketsum = 0; 198 | uint32_t txBytessum = 0; 199 | uint32_t rxBytessum = 0; 200 | uint32_t txTimeFirst = 0; 201 | uint32_t rxTimeLast = 0; 202 | uint32_t lostPacketssum = 0; 203 | 204 | FlowMonitorHelper flowmon; 205 | Ptr monitor = flowmon.InstallAll(); 206 | 207 | Simulator::Stop (Seconds (10.0)); 208 | //std::cout<<"Packets sent. \n"; 209 | if (tracing == true) 210 | { 211 | wifiPhy.EnablePcapAll (outputFilename); 212 | //wifiPhy.EnablePcap (outputFilename, nodes.Get (0)); 213 | //csma.EnablePcap (outputFilename, csmaDevices.Get (0), true); 214 | } 215 | Simulator::Run (); 216 | 217 | k = 0; 218 | 219 | monitor->CheckForLostPackets (); 220 | 221 | Ptr classifier = DynamicCast (flowmon.GetClassifier ()); 222 | std::map stats = monitor->GetFlowStats (); 223 | 224 | for (std::map::const_iterator i = stats.begin (); i != stats.end (); ++i) 225 | { 226 | Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first); 227 | rxPacketsum += i->second.rxPackets; 228 | txPacketsum += i->second.txPackets; 229 | txBytessum += i->second.txBytes; 230 | rxBytessum += i->second.rxBytes; 231 | Delaysum += i->second.delaySum.GetSeconds(); 232 | lostPacketssum += i->second.lostPackets; 233 | txTimeFirst += i->second.timeFirstTxPacket.GetSeconds(); 234 | rxTimeLast += i->second.timeLastRxPacket.GetSeconds(); 235 | 236 | 237 | if ((t.sourceAddress < "10.0.0.101") && (t.destinationAddress < "10.0.0.101")) 238 | { 239 | if((i->second.txBytes >= 1000)||(i->second.rxBytes >= 1000))//&&(i->second.txBytes == i->second.rxBytes)) 240 | { 241 | k++; 242 | 243 | 244 | 245 | } 246 | 247 | std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n"; 248 | std::cout << " Tx Bytes: " << i->second.txBytes << "\n"; 249 | std::cout << " Rx Bytes: " << i->second.rxBytes << "\n"; 250 | std::cout << " Throughput: " << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() - i->second.timeFirstTxPacket.GetSeconds())/1024/1024 << " Mbps\n"; 251 | } 252 | } 253 | 254 | 255 | uint64_t timeDiff = (rxTimeLast - txTimeFirst); 256 | 257 | //uint64_t rcvd = sermon->GetReceived(); 258 | //uint16_t lst = sermon->GetLost(); 259 | //std::cout << k << " " << " " << rxTimeLast << " " < 14 | 15 | using namespace ns3; 16 | 17 | NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample"); 18 | 19 | int 20 | main (int argc, char *argv[]) 21 | { 22 | bool verbose = true; 23 | uint32_t nCsma = 1; 24 | uint32_t nWifi = 25; 25 | bool tracing = true; 26 | 27 | double total_time = 150.0, interval_start, interval_end, interval; 28 | 29 | //double pauseTime = 0.2; 30 | //double nodeSpeed = 20.0; 31 | double txrange = 150.0; 32 | 33 | CommandLine cmd; 34 | cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma); 35 | cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi); 36 | cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose); 37 | cmd.AddValue ("tracing", "Enable pcap tracing", tracing); 38 | 39 | cmd.Parse (argc,argv); 40 | 41 | // Check for valid number of csma or wifi nodes 42 | // 250 should be enough, otherwise IP addresses 43 | // soon become an issue 44 | if (nWifi > 250 || nCsma > 250) 45 | { 46 | std::cout << "Too many wifi or csma nodes, no more than 250 each." << std::endl; 47 | return 1; 48 | } 49 | 50 | if (verbose) 51 | { 52 | //LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); 53 | //LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); 54 | } 55 | 56 | NodeContainer p2pNodes; 57 | p2pNodes.Create (2); 58 | 59 | PointToPointHelper pointToPoint; 60 | pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("50Mbps")); 61 | pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); 62 | 63 | NetDeviceContainer p2pDevices; 64 | p2pDevices = pointToPoint.Install (p2pNodes); 65 | 66 | NodeContainer csmaNodes; 67 | csmaNodes.Add (p2pNodes.Get (0)); 68 | csmaNodes.Create (nCsma); 69 | 70 | CsmaHelper csma; 71 | csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); 72 | csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); 73 | 74 | NetDeviceContainer csmaDevices; 75 | csmaDevices = csma.Install (csmaNodes); 76 | 77 | NodeContainer wifiStaNodes; 78 | wifiStaNodes.Create (nWifi); 79 | NodeContainer wifiApNode = p2pNodes.Get (1); 80 | 81 | YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); 82 | channel.AddPropagationLoss("ns3::RangePropagationLossModel", "MaxRange", DoubleValue (txrange)); 83 | YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); 84 | phy.SetChannel (channel.Create ()); 85 | 86 | 87 | WifiHelper wifi; 88 | wifi.SetRemoteStationManager ("ns3::AarfWifiManager"); 89 | 90 | WifiMacHelper mac; 91 | Ssid ssid = Ssid ("ns-3-ssid"); 92 | mac.SetType ("ns3::AdhocWifiMac", 93 | "Ssid", SsidValue (ssid)); 94 | 95 | NetDeviceContainer staDevices; 96 | staDevices = wifi.Install (phy, mac, wifiStaNodes); 97 | 98 | mac.SetType ("ns3::ApWifiMac", 99 | "Ssid", SsidValue (ssid)); 100 | 101 | NetDeviceContainer apDevices; 102 | apDevices = wifi.Install (phy, mac, wifiApNode); 103 | 104 | 105 | //MobilityHelper mobility; 106 | 107 | /* 108 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 109 | "MinX", DoubleValue (0.0), 110 | "MinY", DoubleValue (0.0), 111 | "DeltaX", DoubleValue (5.0), 112 | "DeltaY", DoubleValue (10.0), 113 | "GridWidth", UintegerValue (3), 114 | "LayoutType", StringValue ("RowFirst")); 115 | 116 | 117 | mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", 118 | "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50))); 119 | */ 120 | 121 | MobilityHelper mobility; 122 | 123 | Ptr positionAllocS = CreateObject (); 124 | positionAllocS->Add(Vector(0.0, 0.0, 0.0)); 125 | mobility.SetPositionAllocator(positionAllocS); 126 | mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); 127 | mobility.Install (wifiApNode); 128 | positionAllocS->Add(Vector(-1.0, 0.0, 0.0)); 129 | mobility.SetPositionAllocator(positionAllocS); 130 | mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); 131 | mobility.Install (csmaNodes); 132 | 133 | ObjectFactory pos; 134 | pos.SetTypeId ("ns3::RandomRectanglePositionAllocator"); 135 | pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); 136 | pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); 137 | Ptr taPositionAlloc = pos.Create ()->GetObject (); 138 | 139 | mobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel", 140 | "Speed", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=20.0]"), 141 | //"Speed", StringValue (speedUniformRandomVariableStream.str ()), 142 | "Pause", StringValue ("ns3::ConstantRandomVariable[Constant=0.5]"), 143 | //"Pause", StringValue (pauseConstantRandomVariableStream.str ()), 144 | "PositionAllocator", PointerValue (taPositionAlloc) 145 | ); 146 | 147 | mobility.Install (wifiStaNodes); 148 | 149 | //mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 150 | 151 | 152 | //mobility.Install(node); 153 | 154 | 155 | 156 | /* 157 | MobilityHelper adhocMobility; 158 | ObjectFactory pos; 159 | pos.SetTypeId ("ns3::RandomRectanglePositionAllocator"); 160 | pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); 161 | pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); 162 | Ptr taPositionAlloc = pos.Create ()->GetObject (); 163 | 164 | std::ostringstream speedUniformRandomVariableStream; 165 | speedUniformRandomVariableStream << "ns3::UniformRandomVariable[Min=0.0|Max=" 166 | << nodeSpeed 167 | << "]"; 168 | 169 | std::ostringstream pauseConstantRandomVariableStream; 170 | pauseConstantRandomVariableStream << "ns3::ConstantRandomVariable[Constant=" 171 | << pauseTime 172 | << "]"; 173 | 174 | adhocMobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel", 175 | // "Speed", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=nodeSpeed]"), 176 | "Speed", StringValue (speedUniformRandomVariableStream.str ()), 177 | "Pause", StringValue (pauseConstantRandomVariableStream.str ()), 178 | "PositionAllocator", PointerValue (taPositionAlloc) 179 | ); 180 | adhocMobility.Install (wifiStaNodes); 181 | */ 182 | 183 | InternetStackHelper stack; 184 | AodvHelper aodv; 185 | stack.SetRoutingHelper (aodv); // has effect on the next Install () 186 | stack.Install (csmaNodes); 187 | stack.Install (wifiApNode); 188 | stack.Install (wifiStaNodes); 189 | 190 | Ipv4AddressHelper address; 191 | 192 | address.SetBase ("10.1.1.0", "255.255.255.0"); 193 | Ipv4InterfaceContainer p2pInterfaces; 194 | p2pInterfaces = address.Assign (p2pDevices); 195 | 196 | address.SetBase ("10.1.2.0", "255.255.255.0"); 197 | Ipv4InterfaceContainer csmaInterfaces; 198 | csmaInterfaces = address.Assign (csmaDevices); 199 | 200 | Ipv4InterfaceContainer wifiInterfaces; 201 | address.SetBase ("10.1.3.0", "255.255.255.0"); 202 | wifiInterfaces = address.Assign (staDevices); 203 | address.Assign (apDevices); 204 | 205 | UdpEchoServerHelper echoServer (9); 206 | 207 | uint32_t i = 0, j = 0; 208 | 209 | for (i = 0; i < nWifi; i++) 210 | { 211 | ApplicationContainer serverApps = echoServer.Install (wifiStaNodes.Get (i)); 212 | serverApps.Start (Seconds (0.5)); 213 | serverApps.Stop (Seconds (total_time)); 214 | } 215 | 216 | interval = 1.0; 217 | interval_start = 1.0; 218 | interval_end = 0.0; //interval_start + interval; 219 | 220 | uint16_t iter = total_time / interval; 221 | 222 | for (j = 0; j < iter; j++) 223 | { 224 | interval_start = interval_end + 1.0; 225 | interval_end = interval_start + interval; 226 | //std::cout << "\n" << j; 227 | 228 | for (i = 0; i < nWifi; i++) 229 | { 230 | UdpEchoClientHelper echoClient (wifiInterfaces.GetAddress (i), 9); 231 | echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); 232 | echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); 233 | echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); 234 | 235 | ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0)); 236 | clientApps.Start (Seconds (interval_start)); 237 | clientApps.Stop (Seconds (interval_end)); 238 | } 239 | 240 | } 241 | 242 | Ipv4GlobalRoutingHelper::PopulateRoutingTables (); 243 | 244 | 245 | uint32_t rxPacketsum = 0; 246 | double Delaysum = 0; 247 | double rxTimeSum = 0, txTimeSum = 0; 248 | uint32_t txPacketsum = 0; 249 | uint32_t txBytessum = 0; 250 | uint32_t rxBytessum = 0; 251 | uint32_t txTimeFirst = 0; 252 | uint32_t rxTimeLast = 0; 253 | uint32_t lostPacketssum = 0; 254 | 255 | FlowMonitorHelper flowmon; 256 | Ptr monitor = flowmon.InstallAll(); 257 | 258 | Simulator::Stop (Seconds (total_time)); 259 | 260 | if (tracing == true) 261 | { 262 | pointToPoint.EnablePcapAll ("aodv"); 263 | //phy.EnablePcap ("third", apDevices.Get (0)); 264 | csma.EnablePcap ("third", csmaDevices.Get (0), true); 265 | } 266 | 267 | 268 | 269 | AnimationInterface anim ("proj4-adov.xml"); 270 | //AnimationInterface::SetBoundary (0, 0, 1000, 1000); 271 | 272 | Simulator::Run (); 273 | 274 | monitor->CheckForLostPackets (); 275 | 276 | Ptr classifier = DynamicCast (flowmon.GetClassifier ()); 277 | std::map stats = monitor->GetFlowStats (); 278 | 279 | for (std::map::const_iterator i = stats.begin (); i != stats.end (); ++i) 280 | { 281 | Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first); 282 | 283 | rxPacketsum += i->second.rxPackets; 284 | txPacketsum += i->second.txPackets; 285 | txBytessum += i->second.txBytes; 286 | rxBytessum += i->second.rxBytes; 287 | Delaysum += i->second.delaySum.GetSeconds(); 288 | lostPacketssum += i->second.lostPackets; 289 | 290 | if(txTimeFirst == 0) 291 | { 292 | txTimeFirst = i->second.timeFirstTxPacket.GetSeconds(); 293 | } 294 | 295 | rxTimeLast = i->second.timeLastRxPacket.GetSeconds(); 296 | lostPacketssum += i->second.lostPackets; 297 | Delaysum += i->second.delaySum.GetSeconds(); 298 | 299 | if ((t.destinationAddress == "10.1.1.1")) 300 | { 301 | if((i->second.txBytes >= 1000)&&(i->second.rxBytes >= 1000))//&&(i->second.txBytes == i->second.rxBytes)) 302 | { 303 | txTimeSum += i->second.timeFirstTxPacket.GetSeconds(); 304 | rxTimeSum += i->second.timeLastRxPacket.GetSeconds(); 305 | //rxPacketsum += i->second.rxPackets; 306 | //std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n"; 307 | //std::cout << " Tx Bytes: " << i->second.txBytes << "\n"; 308 | //std::cout << " Rx Bytes: " << i->second.rxBytes << "\n"; 309 | //std::cout << " Throughput: " << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() - i->second.timeFirstTxPacket.GetSeconds())/1024/1024 << " Mbps\n"; 310 | } 311 | } 312 | } 313 | 314 | //std::cout << "\n\n" << rxPacketsum << "\n"; 315 | uint64_t timeDiff = (rxTimeLast - txTimeFirst); 316 | double timeDiff2 = (rxTimeSum - txTimeSum) / rxPacketsum; 317 | 318 | //uint64_t rcvd = sermon->GetReceived(); 319 | //uint16_t lst = sermon->GetLost(); 320 | //std::cout << k << " " << " " << rxTimeLast << " " < 14 | 15 | using namespace ns3; 16 | 17 | NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample"); 18 | 19 | int 20 | main (int argc, char *argv[]) 21 | { 22 | bool verbose = true; 23 | uint32_t nCsma = 0; 24 | uint32_t nWifi = 100; 25 | bool tracing = true; 26 | 27 | double total_time = 150.0, interval_start, interval_end, interval; 28 | 29 | //double pauseTime = 0.2; 30 | //double nodeSpeed = 20.0; 31 | //double txrange = 150.0; 32 | 33 | //uint32_t bytesTotal; 34 | //uint32_t packetsReceived; 35 | 36 | CommandLine cmd; 37 | cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma); 38 | cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi); 39 | cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose); 40 | cmd.AddValue ("tracing", "Enable pcap tracing", tracing); 41 | 42 | cmd.Parse (argc,argv); 43 | 44 | // Check for valid number of csma or wifi nodes 45 | // 250 should be enough, otherwise IP addresses 46 | // soon become an issue 47 | if (nWifi > 250 || nCsma > 250) 48 | { 49 | std::cout << "Too many wifi or csma nodes, no more than 250 each." << std::endl; 50 | return 1; 51 | } 52 | 53 | if (verbose) 54 | { 55 | LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); 56 | LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); 57 | } 58 | 59 | NodeContainer p2pNodes; 60 | p2pNodes.Create (2); 61 | 62 | PointToPointHelper pointToPoint; 63 | pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("50Mbps")); 64 | pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); 65 | 66 | NetDeviceContainer p2pDevices; 67 | p2pDevices = pointToPoint.Install (p2pNodes); 68 | 69 | NodeContainer csmaNodes; 70 | csmaNodes.Add (p2pNodes.Get (0)); 71 | csmaNodes.Create (nCsma); 72 | 73 | CsmaHelper csma; 74 | csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); 75 | csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); 76 | 77 | NetDeviceContainer csmaDevices; 78 | csmaDevices = csma.Install (csmaNodes); 79 | 80 | NodeContainer wifiStaNodes; 81 | wifiStaNodes.Create (nWifi); 82 | NodeContainer wifiApNode = p2pNodes.Get (1); 83 | 84 | YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); 85 | //channel.AddPropagationLoss("ns3::RangePropagationLossModel", "MaxRange", DoubleValue (txrange)); 86 | YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); 87 | phy.SetChannel (channel.Create ()); 88 | 89 | 90 | WifiHelper wifi; 91 | wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager"); 92 | 93 | WifiMacHelper mac; 94 | //Ssid ssid = Ssid ("ns-3-ssid"); 95 | mac.SetType ("ns3::AdhocWifiMac"); 96 | 97 | NetDeviceContainer staDevices; 98 | staDevices = wifi.Install (phy, mac, wifiStaNodes); 99 | 100 | mac.SetType ("ns3::AdhocWifiMac"); 101 | 102 | NetDeviceContainer apDevices; 103 | apDevices = wifi.Install (phy, mac, wifiApNode); 104 | 105 | 106 | //MobilityHelper mobility; 107 | 108 | /* 109 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 110 | "MinX", DoubleValue (0.0), 111 | "MinY", DoubleValue (0.0), 112 | "DeltaX", DoubleValue (5.0), 113 | "DeltaY", DoubleValue (10.0), 114 | "GridWidth", UintegerValue (3), 115 | "LayoutType", StringValue ("RowFirst")); 116 | 117 | 118 | mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", 119 | "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50))); 120 | */ 121 | 122 | MobilityHelper mobility; 123 | ObjectFactory pos; 124 | pos.SetTypeId ("ns3::RandomRectanglePositionAllocator"); 125 | pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=10.0]")); 126 | pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=10.0]")); 127 | Ptr taPositionAlloc = pos.Create ()->GetObject (); 128 | 129 | mobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel", 130 | "Speed", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=20.0]"), 131 | //"Speed", StringValue (speedUniformRandomVariableStream.str ()), 132 | "Pause", StringValue ("ns3::ConstantRandomVariable[Constant=0.5]"), 133 | //"Pause", StringValue (pauseConstantRandomVariableStream.str ()), 134 | "PositionAllocator", PointerValue (taPositionAlloc) 135 | ); 136 | 137 | mobility.Install (wifiStaNodes); 138 | 139 | //mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 140 | 141 | Ptr positionAllocS = CreateObject (); 142 | positionAllocS->Add(Vector(0.0, 0.0, 0.0)); 143 | mobility.SetPositionAllocator(positionAllocS); 144 | mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); 145 | //mobility.Install(node); 146 | 147 | mobility.Install (wifiApNode); 148 | 149 | /* 150 | MobilityHelper adhocMobility; 151 | ObjectFactory pos; 152 | pos.SetTypeId ("ns3::RandomRectanglePositionAllocator"); 153 | pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); 154 | pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); 155 | Ptr taPositionAlloc = pos.Create ()->GetObject (); 156 | 157 | std::ostringstream speedUniformRandomVariableStream; 158 | speedUniformRandomVariableStream << "ns3::UniformRandomVariable[Min=0.0|Max=" 159 | << nodeSpeed 160 | << "]"; 161 | 162 | std::ostringstream pauseConstantRandomVariableStream; 163 | pauseConstantRandomVariableStream << "ns3::ConstantRandomVariable[Constant=" 164 | << pauseTime 165 | << "]"; 166 | 167 | adhocMobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel", 168 | // "Speed", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=nodeSpeed]"), 169 | "Speed", StringValue (speedUniformRandomVariableStream.str ()), 170 | "Pause", StringValue (pauseConstantRandomVariableStream.str ()), 171 | "PositionAllocator", PointerValue (taPositionAlloc) 172 | ); 173 | adhocMobility.Install (wifiStaNodes); 174 | */ 175 | 176 | 177 | DsdvHelper dsdv; 178 | InternetStackHelper stack; 179 | 180 | dsdv.Set ("PeriodicUpdateInterval", TimeValue (Seconds (100))); 181 | dsdv.Set ("SettlingTime", TimeValue (Seconds (50))); 182 | stack.SetRoutingHelper (dsdv); // has effect on the next Install () 183 | /* 184 | Ipv4ListRoutingHelper list; 185 | std::string m_protocolName; 186 | list.Add (dsdv, 100); 187 | m_protocolName = "DSDV"; 188 | stack.SetRoutingHelper (list); 189 | */ 190 | stack.Install (csmaNodes); 191 | stack.Install (wifiApNode); 192 | stack.Install (wifiStaNodes); 193 | 194 | Ipv4AddressHelper address; 195 | 196 | address.SetBase ("10.1.1.0", "255.255.255.0"); 197 | Ipv4InterfaceContainer p2pInterfaces; 198 | p2pInterfaces = address.Assign (p2pDevices); 199 | 200 | address.SetBase ("10.1.2.0", "255.255.255.0"); 201 | Ipv4InterfaceContainer csmaInterfaces; 202 | csmaInterfaces = address.Assign (csmaDevices); 203 | 204 | Ipv4InterfaceContainer wifiInterfaces; 205 | address.SetBase ("10.1.3.0", "255.255.255.0"); 206 | wifiInterfaces = address.Assign (staDevices); 207 | address.Assign (apDevices); 208 | 209 | UdpEchoServerHelper echoServer (9); 210 | 211 | uint32_t i = 0, j = 0; 212 | 213 | TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); 214 | 215 | for (i = 0; i < nWifi; i++) 216 | { 217 | ApplicationContainer serverApps = echoServer.Install (wifiStaNodes.Get (i)); 218 | serverApps.Start (Seconds (0.1)); 219 | serverApps.Stop (Seconds (total_time)); 220 | /* 221 | Ptr node = NodeList::GetNode (i); 222 | Ipv4Address nodeAddress = node->GetObject ()->GetAddress (1, 0).GetLocal (); 223 | InetSocketAddress local = InetSocketAddress (nodeAddress, 9); 224 | Ptr sink = Socket::CreateSocket (node, tid); 225 | sink->Bind (local); 226 | //sink->SetRecvCallback (MakeCallback ( &DsdvManetExample::ReceivePacket, this)); 227 | NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << " Received one packet!"); 228 | Ptr packet; 229 | while ((packet = sink->Recv ())) 230 | { 231 | bytesTotal += packet->GetSize (); 232 | packetsReceived += 1; 233 | } 234 | */ 235 | 236 | } 237 | 238 | interval = 9.0; 239 | interval_start = 1.0; 240 | interval_end = 0.0; //interval_start + interval; 241 | 242 | for (j = 0; j < 10; j++) 243 | { 244 | interval_start = interval_end + 1.0; 245 | interval_end = interval_start + interval; 246 | //std::cout << "\n" << j; 247 | 248 | for (i = 0; i < nWifi; i++) 249 | { 250 | UdpEchoClientHelper echoClient (wifiInterfaces.GetAddress (i), 9); 251 | echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); 252 | echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); 253 | echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); 254 | 255 | ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0)); 256 | clientApps.Start (Seconds (interval_start)); 257 | clientApps.Stop (Seconds (interval_end)); 258 | } 259 | 260 | } 261 | 262 | Ipv4GlobalRoutingHelper::PopulateRoutingTables (); 263 | 264 | 265 | uint32_t rxPacketsum = 0; 266 | uint32_t rxPacketsum2 = 0; 267 | FlowMonitorHelper flowmon; 268 | Ptr monitor = flowmon.InstallAll(); 269 | 270 | Simulator::Stop (Seconds (total_time)); 271 | 272 | if (tracing == true) 273 | { 274 | pointToPoint.EnablePcapAll ("third"); 275 | phy.EnablePcap ("third", apDevices.Get (0)); 276 | csma.EnablePcap ("third", csmaDevices.Get (0), true); 277 | } 278 | 279 | //AnimationInterface::SetBoundary (0, 0, 1000, 1000); 280 | 281 | //AnimationInterface anim ("proj4-dsdv.xml"); 282 | 283 | 284 | Simulator::Run (); 285 | 286 | monitor->CheckForLostPackets (); 287 | 288 | Ptr classifier = DynamicCast (flowmon.GetClassifier ()); 289 | std::map stats = monitor->GetFlowStats (); 290 | 291 | for (std::map::const_iterator i = stats.begin (); i != stats.end (); ++i) 292 | { 293 | Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first); 294 | 295 | if ((t.destinationAddress == "10.1.1.1")) 296 | { 297 | if((i->second.txBytes >= 1000)&&(i->second.rxBytes >= 1000))//&&(i->second.txBytes == i->second.rxBytes)) 298 | { 299 | rxPacketsum += i->second.rxPackets; 300 | std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n"; 301 | std::cout << " Tx Bytes: " << i->second.txBytes << "\n"; 302 | std::cout << " Rx Bytes: " << i->second.rxBytes << "\n"; 303 | //std::cout << " Throughput: " << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() - i->second.timeFirstTxPacket.GetSeconds())/1024/1024 << " Mbps\n"; 304 | } 305 | } 306 | if ((t.destinationAddress < "10.1.3.100")) 307 | { 308 | //if((i->second.txBytes >= 1000)&&(i->second.rxBytes >= 1000))//&&(i->second.txBytes == i->second.rxBytes)) 309 | //{ 310 | rxPacketsum2 += i->second.rxPackets; 311 | std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n"; 312 | std::cout << " Tx Bytes: " << i->second.txBytes << "\n"; 313 | std::cout << " Rx Bytes: " << i->second.rxBytes << "\n"; 314 | //std::cout << " Throughput: " << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() - i->second.timeFirstTxPacket.GetSeconds())/1024/1024 << " Mbps\n"; 315 | //} 316 | } 317 | } 318 | 319 | std::cout << "\n\n" << rxPacketsum << "\n"; 320 | std::cout << "\n\n" << rxPacketsum2 << "\n"; 321 | Simulator::Destroy (); 322 | return 0; 323 | } 324 | -------------------------------------------------------------------------------- /project_4/olsr-proj-4-broadcast.cc: -------------------------------------------------------------------------------- 1 | #include "ns3/core-module.h" 2 | #include "ns3/point-to-point-module.h" 3 | #include "ns3/network-module.h" 4 | #include "ns3/applications-module.h" 5 | #include "ns3/wifi-module.h" 6 | #include "ns3/mobility-module.h" 7 | #include "ns3/csma-module.h" 8 | #include "ns3/internet-module.h" 9 | #include "ns3/netanim-module.h" 10 | #include "ns3/olsr-module.h" 11 | #include "ns3/flow-monitor-module.h" 12 | #include "ns3/animation-interface.h" 13 | #include 14 | 15 | using namespace ns3; 16 | 17 | NS_LOG_COMPONENT_DEFINE ("OLSRExample"); 18 | 19 | int 20 | main (int argc, char *argv[]) 21 | { 22 | bool verbose = true; 23 | uint32_t nCsma = 1; 24 | uint32_t nWifi = 10; 25 | bool tracing = true; 26 | 27 | double total_time = 15.0, interval_start, interval_end, interval; 28 | 29 | //double pauseTime = 0.2; 30 | //double nodeSpeed = 20.0; 31 | double txrange = 150.0; 32 | 33 | CommandLine cmd; 34 | cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma); 35 | cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi); 36 | cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose); 37 | cmd.AddValue ("tracing", "Enable pcap tracing", tracing); 38 | 39 | cmd.Parse (argc,argv); 40 | 41 | // Check for valid number of csma or wifi nodes 42 | // 250 should be enough, otherwise IP addresses 43 | // soon become an issue 44 | if (nWifi > 250 || nCsma > 250) 45 | { 46 | std::cout << "Too many wifi or csma nodes, no more than 250 each." << std::endl; 47 | return 1; 48 | } 49 | 50 | if (verbose) 51 | { 52 | //LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); 53 | //LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); 54 | } 55 | 56 | NodeContainer p2pNodes; 57 | p2pNodes.Create (2); 58 | 59 | PointToPointHelper pointToPoint; 60 | pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("50Mbps")); 61 | pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); 62 | 63 | NetDeviceContainer p2pDevices; 64 | p2pDevices = pointToPoint.Install (p2pNodes); 65 | 66 | NodeContainer csmaNodes; 67 | csmaNodes.Add (p2pNodes.Get (0)); 68 | csmaNodes.Create (nCsma); 69 | 70 | CsmaHelper csma; 71 | csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); 72 | csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); 73 | 74 | NetDeviceContainer csmaDevices; 75 | csmaDevices = csma.Install (csmaNodes); 76 | 77 | NodeContainer wifiStaNodes; 78 | wifiStaNodes.Create (nWifi); 79 | NodeContainer wifiApNode = p2pNodes.Get (1); 80 | 81 | YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); 82 | channel.AddPropagationLoss("ns3::RangePropagationLossModel", "MaxRange", DoubleValue (txrange)); 83 | YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); 84 | phy.SetChannel (channel.Create ()); 85 | 86 | 87 | WifiHelper wifi; 88 | wifi.SetRemoteStationManager ("ns3::AarfWifiManager"); 89 | 90 | WifiMacHelper mac; 91 | Ssid ssid = Ssid ("ns-3-ssid"); 92 | mac.SetType ("ns3::StaWifiMac", 93 | "Ssid", SsidValue (ssid), 94 | "ActiveProbing", BooleanValue (false)); 95 | 96 | NetDeviceContainer staDevices; 97 | staDevices = wifi.Install (phy, mac, wifiStaNodes); 98 | 99 | mac.SetType ("ns3::ApWifiMac", 100 | "Ssid", SsidValue (ssid)); 101 | 102 | NetDeviceContainer apDevices; 103 | apDevices = wifi.Install (phy, mac, wifiApNode); 104 | 105 | 106 | //MobilityHelper mobility; 107 | 108 | /* 109 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 110 | "MinX", DoubleValue (0.0), 111 | "MinY", DoubleValue (0.0), 112 | "DeltaX", DoubleValue (5.0), 113 | "DeltaY", DoubleValue (10.0), 114 | "GridWidth", UintegerValue (3), 115 | "LayoutType", StringValue ("RowFirst")); 116 | 117 | 118 | mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", 119 | "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50))); 120 | */ 121 | 122 | MobilityHelper mobility; 123 | 124 | Ptr positionAllocS = CreateObject (); 125 | positionAllocS->Add(Vector(0.0, 0.0, 0.0)); 126 | mobility.SetPositionAllocator(positionAllocS); 127 | mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); 128 | mobility.Install (wifiApNode); 129 | positionAllocS->Add(Vector(-1.0, 0.0, 0.0)); 130 | mobility.SetPositionAllocator(positionAllocS); 131 | mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); 132 | mobility.Install (csmaNodes); 133 | 134 | ObjectFactory pos; 135 | pos.SetTypeId ("ns3::RandomRectanglePositionAllocator"); 136 | pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); 137 | pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); 138 | Ptr taPositionAlloc = pos.Create ()->GetObject (); 139 | 140 | mobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel", 141 | "Speed", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=20.0]"), 142 | //"Speed", StringValue (speedUniformRandomVariableStream.str ()), 143 | "Pause", StringValue ("ns3::ConstantRandomVariable[Constant=0.5]"), 144 | //"Pause", StringValue (pauseConstantRandomVariableStream.str ()), 145 | "PositionAllocator", PointerValue (taPositionAlloc) 146 | ); 147 | 148 | mobility.Install (wifiStaNodes); 149 | 150 | //mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 151 | 152 | 153 | //mobility.Install(node); 154 | 155 | 156 | 157 | /* 158 | MobilityHelper adhocMobility; 159 | ObjectFactory pos; 160 | pos.SetTypeId ("ns3::RandomRectanglePositionAllocator"); 161 | pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); 162 | pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); 163 | Ptr taPositionAlloc = pos.Create ()->GetObject (); 164 | 165 | std::ostringstream speedUniformRandomVariableStream; 166 | speedUniformRandomVariableStream << "ns3::UniformRandomVariable[Min=0.0|Max=" 167 | << nodeSpeed 168 | << "]"; 169 | 170 | std::ostringstream pauseConstantRandomVariableStream; 171 | pauseConstantRandomVariableStream << "ns3::ConstantRandomVariable[Constant=" 172 | << pauseTime 173 | << "]"; 174 | 175 | adhocMobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel", 176 | // "Speed", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=nodeSpeed]"), 177 | "Speed", StringValue (speedUniformRandomVariableStream.str ()), 178 | "Pause", StringValue (pauseConstantRandomVariableStream.str ()), 179 | "PositionAllocator", PointerValue (taPositionAlloc) 180 | ); 181 | adhocMobility.Install (wifiStaNodes); 182 | */ 183 | 184 | OlsrHelper olsr; 185 | InternetStackHelper stack; 186 | stack.SetRoutingHelper (olsr); // has effect on the next Install () 187 | stack.Install (csmaNodes); 188 | stack.Install (wifiApNode); 189 | stack.Install (wifiStaNodes); 190 | 191 | Ipv4AddressHelper address; 192 | 193 | address.SetBase ("10.1.1.0", "255.255.255.0"); 194 | Ipv4InterfaceContainer p2pInterfaces; 195 | p2pInterfaces = address.Assign (p2pDevices); 196 | 197 | address.SetBase ("10.1.2.0", "255.255.255.0"); 198 | Ipv4InterfaceContainer csmaInterfaces; 199 | csmaInterfaces = address.Assign (csmaDevices); 200 | 201 | Ipv4InterfaceContainer wifiInterfaces; 202 | address.SetBase ("10.1.3.0", "255.255.255.0"); 203 | wifiInterfaces = address.Assign (staDevices); 204 | address.Assign (apDevices); 205 | 206 | UdpEchoServerHelper echoServer (9); 207 | 208 | uint32_t i = 0, j = 0; 209 | 210 | OnOffHelper onoff ("ns3::UdpSocketFactory", Address (InetSocketAddress (Ipv4Address ("10.1.3.255"), 9))); 211 | onoff.SetConstantRate (DataRate ("500kb/s")); 212 | 213 | ApplicationContainer app = onoff.Install (p2pNodes.Get (0)); 214 | app.Start (Seconds (0.5)); 215 | app.Stop (Seconds (total_time)); 216 | 217 | interval = 1.0; 218 | interval_start = 1.0; 219 | interval_end = 0.0; //interval_start + interval; 220 | 221 | uint16_t iter = total_time / interval; 222 | 223 | 224 | for (j = 0; j < iter; j++) 225 | { 226 | interval_start = interval_end + 1.0; 227 | interval_end = interval_start + interval; 228 | //std::cout << "\n" << j; 229 | 230 | for (i = 0; i < nWifi; i++) 231 | { 232 | PacketSinkHelper sink ("ns3::UdpSocketFactory", Address (InetSocketAddress (Ipv4Address::GetAny (), 9))); 233 | app = sink.Install (wifiStaNodes.Get (i)); // or it should be 1, i don't know 234 | //app.Add (sink.Install (csmaNodes.Get (i))); 235 | 236 | //clientApps.Start (Seconds (interval_start)); 237 | //clientApps.Stop (Seconds (interval_end)); 238 | } 239 | 240 | } 241 | 242 | Ipv4GlobalRoutingHelper::PopulateRoutingTables (); 243 | 244 | 245 | uint32_t rxPacketsum = 0; 246 | double Delaysum = 0; 247 | double rxTimeSum = 0, txTimeSum = 0; 248 | uint32_t txPacketsum = 0; 249 | uint32_t txBytessum = 0; 250 | uint32_t rxBytessum = 0; 251 | uint32_t txTimeFirst = 0; 252 | uint32_t rxTimeLast = 0; 253 | uint32_t lostPacketssum = 0; 254 | 255 | FlowMonitorHelper flowmon; 256 | Ptr monitor = flowmon.InstallAll(); 257 | 258 | Simulator::Stop (Seconds (total_time)); 259 | 260 | if (tracing == true) 261 | { 262 | pointToPoint.EnablePcapAll ("olsr"); 263 | phy.EnablePcap ("olsr-test", staDevices.Get (3)); 264 | //csma.EnablePcap ("third", csmaDevices.Get (0), true); 265 | } 266 | 267 | 268 | 269 | AnimationInterface anim ("proj4-olsr.xml"); 270 | //AnimationInterface::SetBoundary (0, 0, 1000, 1000); 271 | 272 | Simulator::Run (); 273 | 274 | monitor->CheckForLostPackets (); 275 | 276 | Ptr classifier = DynamicCast (flowmon.GetClassifier ()); 277 | std::map stats = monitor->GetFlowStats (); 278 | 279 | for (std::map::const_iterator i = stats.begin (); i != stats.end (); ++i) 280 | { 281 | Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first); 282 | 283 | rxPacketsum += i->second.rxPackets; 284 | txPacketsum += i->second.txPackets; 285 | txBytessum += i->second.txBytes; 286 | rxBytessum += i->second.rxBytes; 287 | Delaysum += i->second.delaySum.GetSeconds(); 288 | lostPacketssum += i->second.lostPackets; 289 | 290 | if(txTimeFirst == 0) 291 | { 292 | txTimeFirst = i->second.timeFirstTxPacket.GetSeconds(); 293 | } 294 | 295 | rxTimeLast = i->second.timeLastRxPacket.GetSeconds(); 296 | lostPacketssum += i->second.lostPackets; 297 | Delaysum += i->second.delaySum.GetSeconds(); 298 | txTimeSum += i->second.timeFirstTxPacket.GetSeconds(); 299 | rxTimeSum += i->second.timeLastRxPacket.GetSeconds(); 300 | 301 | if ((t.destinationAddress == "10.1.1.1")) 302 | { 303 | if((i->second.txBytes >= 1000)&&(i->second.rxBytes >= 1000))//&&(i->second.txBytes == i->second.rxBytes)) 304 | { 305 | 306 | //rxPacketsum += i->second.rxPackets; 307 | //std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n"; 308 | //std::cout << " Tx Bytes: " << i->second.txBytes << "\n"; 309 | //std::cout << " Rx Bytes: " << i->second.rxBytes << "\n"; 310 | //std::cout << " Throughput: " << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() - i->second.timeFirstTxPacket.GetSeconds())/1024/1024 << " Mbps\n"; 311 | } 312 | } 313 | } 314 | 315 | //std::cout << "\n\n" << rxPacketsum << "\n"; 316 | uint64_t timeDiff = (rxTimeLast - txTimeFirst); 317 | double timeDiff2 = (rxTimeSum - txTimeSum) / rxPacketsum; 318 | 319 | //uint64_t rcvd = sermon->GetReceived(); 320 | //uint16_t lst = sermon->GetLost(); 321 | //std::cout << k << " " << " " << rxTimeLast << " " <GetTotalRx(); 325 | 326 | std::cout << " Total Tx Packets: " << txPacketsum << "\n"; 327 | std::cout << " Total Rx Packets: " << rxPacketsum << "\n"; 328 | std::cout << " Total Packets Lost: " << (txPacketsum - rxPacketsum) << "\n"; 329 | std::cout << " Average Round trip time of Packet: " << timeDiff2 << "\n"; 330 | std::cout << " Throughput: " << ((rxBytessum * 8.0) / timeDiff)/1024<<" Kbps"<<"\n"; 331 | std::cout << " Packets Loss Ratio: " << (((txPacketsum - rxPacketsum) * 100) /txPacketsum) << "%" << "\n"; 332 | std::cout << " Packets Delivery Ratio: " << ((rxPacketsum * 100) /txPacketsum) << "%" << "\n"; 333 | Simulator::Destroy (); 334 | return 0; 335 | } 336 | -------------------------------------------------------------------------------- /project_4/olsr-proj-4-p2p.cc: -------------------------------------------------------------------------------- 1 | #include "ns3/core-module.h" 2 | #include "ns3/point-to-point-module.h" 3 | #include "ns3/network-module.h" 4 | #include "ns3/applications-module.h" 5 | #include "ns3/wifi-module.h" 6 | #include "ns3/mobility-module.h" 7 | #include "ns3/csma-module.h" 8 | #include "ns3/internet-module.h" 9 | #include "ns3/netanim-module.h" 10 | #include "ns3/olsr-helper.h" 11 | #include "ns3/flow-monitor-module.h" 12 | #include 13 | 14 | using namespace ns3; 15 | 16 | NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample"); 17 | 18 | int 19 | main (int argc, char *argv[]) 20 | { 21 | bool verbose = true; 22 | uint32_t nCsma = 1; 23 | uint32_t nWifi = 25; 24 | bool tracing = false; 25 | 26 | double total_time = 150.0, interval_start, interval_end, interval; 27 | 28 | //double pauseTime = 0.2; 29 | //double nodeSpeed = 20.0; 30 | double txrange = 150.0; 31 | 32 | CommandLine cmd; 33 | cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma); 34 | cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi); 35 | cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose); 36 | cmd.AddValue ("tracing", "Enable pcap tracing", tracing); 37 | 38 | cmd.Parse (argc,argv); 39 | 40 | // Check for valid number of csma or wifi nodes 41 | // 250 should be enough, otherwise IP addresses 42 | // soon become an issue 43 | if (nWifi > 250 || nCsma > 250) 44 | { 45 | std::cout << "Too many wifi or csma nodes, no more than 250 each." << std::endl; 46 | return 1; 47 | } 48 | 49 | if (verbose) 50 | { 51 | //LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); 52 | //LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); 53 | } 54 | 55 | NodeContainer p2pNodes; 56 | p2pNodes.Create (2); 57 | 58 | PointToPointHelper pointToPoint; 59 | pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("50Mbps")); 60 | pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); 61 | 62 | NetDeviceContainer p2pDevices; 63 | p2pDevices = pointToPoint.Install (p2pNodes); 64 | 65 | NodeContainer csmaNodes; 66 | csmaNodes.Add (p2pNodes.Get (0)); 67 | csmaNodes.Create (nCsma); 68 | 69 | CsmaHelper csma; 70 | csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); 71 | csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); 72 | 73 | NetDeviceContainer csmaDevices; 74 | csmaDevices = csma.Install (csmaNodes); 75 | 76 | NodeContainer wifiStaNodes; 77 | wifiStaNodes.Create (nWifi); 78 | NodeContainer wifiApNode = p2pNodes.Get (1); 79 | 80 | YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); 81 | channel.AddPropagationLoss("ns3::RangePropagationLossModel", "MaxRange", DoubleValue (txrange)); 82 | YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); 83 | phy.SetChannel (channel.Create ()); 84 | 85 | 86 | WifiHelper wifi; 87 | wifi.SetRemoteStationManager ("ns3::AarfWifiManager"); 88 | 89 | WifiMacHelper mac; 90 | Ssid ssid = Ssid ("ns-3-ssid"); 91 | mac.SetType ("ns3::StaWifiMac", 92 | "Ssid", SsidValue (ssid), 93 | "ActiveProbing", BooleanValue (false)); 94 | 95 | NetDeviceContainer staDevices; 96 | staDevices = wifi.Install (phy, mac, wifiStaNodes); 97 | 98 | mac.SetType ("ns3::ApWifiMac", 99 | "Ssid", SsidValue (ssid)); 100 | 101 | NetDeviceContainer apDevices; 102 | apDevices = wifi.Install (phy, mac, wifiApNode); 103 | 104 | 105 | //MobilityHelper mobility; 106 | 107 | /* 108 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 109 | "MinX", DoubleValue (0.0), 110 | "MinY", DoubleValue (0.0), 111 | "DeltaX", DoubleValue (5.0), 112 | "DeltaY", DoubleValue (10.0), 113 | "GridWidth", UintegerValue (3), 114 | "LayoutType", StringValue ("RowFirst")); 115 | 116 | 117 | mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", 118 | "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50))); 119 | */ 120 | 121 | MobilityHelper mobility; 122 | 123 | Ptr positionAllocS = CreateObject (); 124 | positionAllocS->Add(Vector(0.0, 0.0, 0.0)); 125 | mobility.SetPositionAllocator(positionAllocS); 126 | mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); 127 | mobility.Install (wifiApNode); 128 | positionAllocS->Add(Vector(-1.0, 0.0, 0.0)); 129 | mobility.SetPositionAllocator(positionAllocS); 130 | mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); 131 | mobility.Install (csmaNodes); 132 | 133 | ObjectFactory pos; 134 | pos.SetTypeId ("ns3::RandomRectanglePositionAllocator"); 135 | pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); 136 | pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); 137 | Ptr taPositionAlloc = pos.Create ()->GetObject (); 138 | 139 | mobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel", 140 | "Speed", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=20.0]"), 141 | //"Speed", StringValue (speedUniformRandomVariableStream.str ()), 142 | "Pause", StringValue ("ns3::ConstantRandomVariable[Constant=0.5]"), 143 | //"Pause", StringValue (pauseConstantRandomVariableStream.str ()), 144 | "PositionAllocator", PointerValue (taPositionAlloc) 145 | ); 146 | 147 | mobility.Install (wifiStaNodes); 148 | 149 | //mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 150 | 151 | /* 152 | MobilityHelper adhocMobility; 153 | ObjectFactory pos; 154 | pos.SetTypeId ("ns3::RandomRectanglePositionAllocator"); 155 | pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); 156 | pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); 157 | Ptr taPositionAlloc = pos.Create ()->GetObject (); 158 | 159 | std::ostringstream speedUniformRandomVariableStream; 160 | speedUniformRandomVariableStream << "ns3::UniformRandomVariable[Min=0.0|Max=" 161 | << nodeSpeed 162 | << "]"; 163 | 164 | std::ostringstream pauseConstantRandomVariableStream; 165 | pauseConstantRandomVariableStream << "ns3::ConstantRandomVariable[Constant=" 166 | << pauseTime 167 | << "]"; 168 | 169 | adhocMobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel", 170 | // "Speed", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=nodeSpeed]"), 171 | "Speed", StringValue (speedUniformRandomVariableStream.str ()), 172 | "Pause", StringValue (pauseConstantRandomVariableStream.str ()), 173 | "PositionAllocator", PointerValue (taPositionAlloc) 174 | ); 175 | adhocMobility.Install (wifiStaNodes); 176 | */ 177 | 178 | 179 | OlsrHelper olsr; 180 | InternetStackHelper stack; 181 | stack.SetRoutingHelper (olsr); // has effect on the next Install () 182 | stack.Install (csmaNodes); 183 | stack.Install (wifiApNode); 184 | stack.Install (wifiStaNodes); 185 | 186 | Ipv4AddressHelper address; 187 | 188 | address.SetBase ("10.1.1.0", "255.255.255.0"); 189 | Ipv4InterfaceContainer p2pInterfaces; 190 | p2pInterfaces = address.Assign (p2pDevices); 191 | 192 | address.SetBase ("10.1.2.0", "255.255.255.0"); 193 | Ipv4InterfaceContainer csmaInterfaces; 194 | csmaInterfaces = address.Assign (csmaDevices); 195 | 196 | Ipv4InterfaceContainer wifiInterfaces; 197 | address.SetBase ("10.1.3.0", "255.255.255.0"); 198 | wifiInterfaces = address.Assign (staDevices); 199 | address.Assign (apDevices); 200 | 201 | UdpEchoServerHelper echoServer (9); 202 | 203 | uint32_t i = 0, j = 0; 204 | 205 | for (i = 0; i < nWifi; i++) 206 | { 207 | ApplicationContainer serverApps = echoServer.Install (wifiStaNodes.Get (i)); 208 | serverApps.Start (Seconds (0.5)); 209 | serverApps.Stop (Seconds (total_time)); 210 | } 211 | 212 | interval = 1.0; 213 | interval_start = 1.0; 214 | interval_end = 0.0; //interval_start + interval; 215 | 216 | uint16_t iter = total_time / interval; 217 | 218 | for (j = 0; j < iter; j++) 219 | { 220 | interval_start = interval_end + 1.0; 221 | interval_end = interval_start + interval; 222 | //std::cout << "\n" << j; 223 | 224 | for (i = 0; i < nWifi; i++) 225 | { 226 | UdpEchoClientHelper echoClient (wifiInterfaces.GetAddress (i), 9); 227 | echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); 228 | echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); 229 | echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); 230 | 231 | ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0)); 232 | clientApps.Start (Seconds (interval_start)); 233 | clientApps.Stop (Seconds (interval_end)); 234 | } 235 | 236 | } 237 | 238 | Ipv4GlobalRoutingHelper::PopulateRoutingTables (); 239 | 240 | uint32_t rxPacketsum = 0; 241 | double Delaysum = 0; 242 | double rxTimeSum = 0, txTimeSum = 0; 243 | uint32_t txPacketsum = 0; 244 | uint32_t txBytessum = 0; 245 | uint32_t rxBytessum = 0; 246 | uint32_t txTimeFirst = 0; 247 | uint32_t rxTimeLast = 0; 248 | uint32_t lostPacketssum = 0; 249 | 250 | FlowMonitorHelper flowmon; 251 | Ptr monitor = flowmon.InstallAll(); 252 | 253 | Simulator::Stop (Seconds (total_time)); 254 | 255 | if (tracing == true) 256 | { 257 | pointToPoint.EnablePcapAll ("aodv"); 258 | //phy.EnablePcap ("third", apDevices.Get (0)); 259 | csma.EnablePcap ("third", csmaDevices.Get (0), true); 260 | } 261 | 262 | 263 | 264 | AnimationInterface anim ("proj4-olsr.xml"); 265 | //AnimationInterface::SetBoundary (0, 0, 1000, 1000); 266 | 267 | Simulator::Run (); 268 | 269 | monitor->CheckForLostPackets (); 270 | 271 | Ptr classifier = DynamicCast (flowmon.GetClassifier ()); 272 | std::map stats = monitor->GetFlowStats (); 273 | 274 | for (std::map::const_iterator i = stats.begin (); i != stats.end (); ++i) 275 | { 276 | Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first); 277 | 278 | rxPacketsum += i->second.rxPackets; 279 | txPacketsum += i->second.txPackets; 280 | txBytessum += i->second.txBytes; 281 | rxBytessum += i->second.rxBytes; 282 | Delaysum += i->second.delaySum.GetSeconds(); 283 | lostPacketssum += i->second.lostPackets; 284 | 285 | if(txTimeFirst == 0) 286 | { 287 | txTimeFirst = i->second.timeFirstTxPacket.GetSeconds(); 288 | } 289 | 290 | rxTimeLast = i->second.timeLastRxPacket.GetSeconds(); 291 | lostPacketssum += i->second.lostPackets; 292 | Delaysum += i->second.delaySum.GetSeconds(); 293 | 294 | if ((t.destinationAddress == "10.1.1.1")) 295 | { 296 | if((i->second.txBytes >= 1000)&&(i->second.rxBytes >= 1000))//&&(i->second.txBytes == i->second.rxBytes)) 297 | { 298 | txTimeSum += i->second.timeFirstTxPacket.GetSeconds(); 299 | rxTimeSum += i->second.timeLastRxPacket.GetSeconds(); 300 | //rxPacketsum += i->second.rxPackets; 301 | //std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n"; 302 | //std::cout << " Tx Bytes: " << i->second.txBytes << "\n"; 303 | //std::cout << " Rx Bytes: " << i->second.rxBytes << "\n"; 304 | //std::cout << " Throughput: " << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() - i->second.timeFirstTxPacket.GetSeconds())/1024/1024 << " Mbps\n"; 305 | } 306 | } 307 | } 308 | 309 | //std::cout << "\n\n" << rxPacketsum << "\n"; 310 | uint64_t timeDiff = (rxTimeLast - txTimeFirst); 311 | double timeDiff2 = (rxTimeSum - txTimeSum) / rxPacketsum; 312 | 313 | //uint64_t rcvd = sermon->GetReceived(); 314 | //uint16_t lst = sermon->GetLost(); 315 | //std::cout << k << " " << " " << rxTimeLast << " " < 15 | #include 16 | 17 | using namespace ns3; 18 | 19 | class GpsrExample 20 | { 21 | public: 22 | GpsrExample (); 23 | /// Configure script parameters, \return true on successful configuration 24 | bool Configure (int argc, char **argv); 25 | /// Run simulation 26 | void Run (); 27 | /// Report results 28 | void Report (std::ostream & os); 29 | 30 | private: 31 | ///\name parameters 32 | //\{ 33 | /// Number of nodes 34 | uint32_t size; 35 | /// Width of the Node Grid 36 | uint32_t gridWidth; 37 | /// Distance between nodes, meters 38 | double step; 39 | /// Simulation time, seconds 40 | double totalTime; 41 | /// Write per-device PCAP traces if true 42 | bool pcap; 43 | //\} 44 | 45 | ///\name network 46 | //\{ 47 | NodeContainer nodes; 48 | NetDeviceContainer devices; 49 | Ipv4InterfaceContainer interfaces; 50 | //\} 51 | 52 | private: 53 | void CreateNodes (); 54 | void CreateDevices (); 55 | void InstallInternetStack (); 56 | void InstallApplications (); 57 | }; 58 | 59 | int main (int argc, char **argv) 60 | { 61 | GpsrExample test; 62 | if (! test.Configure(argc, argv)) 63 | NS_FATAL_ERROR ("Configuration failed. Aborted."); 64 | 65 | 66 | test.Run (); 67 | test.Report (std::cout); 68 | return 0; 69 | } 70 | 71 | //----------------------------------------------------------------------------- 72 | GpsrExample::GpsrExample () : 73 | // Number of Nodes 74 | size (2), 75 | // Grid Width 76 | gridWidth(2), 77 | // Distance between nodes 78 | step (100), //TODO Distance changed to the limit between nodes: test to see if there are transmitions 79 | // Simulation time 80 | totalTime (30), 81 | // Generate capture files for each node 82 | pcap (true) 83 | 84 | { 85 | } 86 | 87 | bool 88 | GpsrExample::Configure (int argc, char **argv) 89 | { 90 | // Enable GPSR logs by default. Comment this if too noisy 91 | // LogComponentEnable("GpsrRoutingProtocol", LOG_LEVEL_ALL); 92 | 93 | SeedManager::SetSeed(12345); 94 | CommandLine cmd; 95 | 96 | cmd.AddValue ("pcap", "Write PCAP traces.", pcap); 97 | cmd.AddValue ("size", "Number of nodes.", size); 98 | cmd.AddValue ("time", "Simulation time, s.", totalTime); 99 | cmd.AddValue ("step", "Grid step, m", step); 100 | 101 | cmd.Parse (argc, argv); 102 | return true; 103 | } 104 | 105 | void 106 | GpsrExample::Run () 107 | { 108 | // Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time. 109 | CreateNodes (); 110 | CreateDevices (); 111 | InstallInternetStack (); 112 | InstallApplications (); 113 | 114 | GpsrHelper gpsr; 115 | gpsr.Install (); 116 | 117 | std::cout << "Starting simulation for " << totalTime << " s ...\n"; 118 | 119 | Simulator::Stop (Seconds (totalTime)); 120 | Simulator::Run (); 121 | Simulator::Destroy (); 122 | } 123 | 124 | void 125 | GpsrExample::Report (std::ostream &) 126 | { 127 | } 128 | 129 | void 130 | GpsrExample::CreateNodes () 131 | { 132 | std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n"; 133 | nodes.Create (size); 134 | // Name nodes 135 | for (uint32_t i = 0; i < size; ++i) 136 | { 137 | std::ostringstream os; 138 | os << "node-" << i; 139 | Names::Add (os.str (), nodes.Get (i)); 140 | } 141 | // Create static grid 142 | MobilityHelper mobility; 143 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 144 | "MinX", DoubleValue (0.0), 145 | "MinY", DoubleValue (0.0), 146 | "DeltaX", DoubleValue (step), 147 | "DeltaY", DoubleValue (step), 148 | "GridWidth", UintegerValue (gridWidth), 149 | "LayoutType", StringValue ("RowFirst")); 150 | mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 151 | mobility.Install (nodes); 152 | } 153 | 154 | void 155 | GpsrExample::CreateDevices () 156 | { 157 | NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); 158 | wifiMac.SetType ("ns3::AdhocWifiMac"); 159 | YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); 160 | YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); 161 | wifiPhy.SetChannel (wifiChannel.Create ()); 162 | WifiHelper wifi = WifiHelper::Default (); 163 | wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0)); 164 | devices = wifi.Install (wifiPhy, wifiMac, nodes); 165 | 166 | if (pcap) 167 | { 168 | wifiPhy.EnablePcapAll (std::string ("gpsr")); 169 | } 170 | } 171 | 172 | void 173 | GpsrExample::InstallInternetStack () 174 | { 175 | GpsrHelper gpsr; 176 | // you can configure GPSR attributes here using gpsr.Set(name, value) 177 | InternetStackHelper stack; 178 | stack.SetRoutingHelper (gpsr); 179 | stack.Install (nodes); 180 | Ipv4AddressHelper address; 181 | address.SetBase ("10.0.0.0", "255.255.0.0"); 182 | interfaces = address.Assign (devices); 183 | } 184 | 185 | void 186 | GpsrExample::InstallApplications () 187 | { 188 | 189 | uint16_t port = 9; // well-known echo port number 190 | uint32_t packetSize = 1024; // size of the packets being transmitted 191 | uint32_t maxPacketCount = 100; // number of packets to transmit 192 | Time interPacketInterval = Seconds (1.); // interval between packet transmissions 193 | 194 | // Set-up a server Application on the bottom-right node of the grid 195 | UdpEchoServerHelper server1 (port); 196 | uint16_t server1Position = size-1; //bottom right 197 | ApplicationContainer apps = server1.Install (nodes.Get(server1Position)); 198 | apps.Start (Seconds (1.0)); 199 | apps.Stop (Seconds (totalTime-0.1)); 200 | 201 | // Set-up a client Application, connected to 'server', to be run on the top-left node of the grid 202 | UdpEchoClientHelper client (interfaces.GetAddress (server1Position), port); 203 | client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); 204 | client.SetAttribute ("Interval", TimeValue (interPacketInterval)); 205 | client.SetAttribute ("PacketSize", UintegerValue (packetSize)); 206 | uint16_t clientPosition = 0; //top left 207 | apps = client.Install (nodes.Get (clientPosition)); 208 | apps.Start (Seconds (2.0)); 209 | apps.Stop (Seconds (totalTime-0.1)); 210 | 211 | } 212 | 213 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/examples/gpsr-test2.cc: -------------------------------------------------------------------------------- 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 | 3 | #include "ns3/gpsr-module.h" 4 | #include "ns3/core-module.h" 5 | #include "ns3/network-module.h" 6 | #include "ns3/internet-module.h" 7 | #include "ns3/mobility-module.h" 8 | #include "ns3/point-to-point-module.h" 9 | #include "ns3/wifi-module.h" 10 | #include "ns3/v4ping-helper.h" 11 | #include "ns3/udp-echo-server.h" 12 | #include "ns3/udp-echo-client.h" 13 | #include "ns3/udp-echo-helper.h" 14 | #include 15 | #include 16 | 17 | using namespace ns3; 18 | 19 | class GpsrExample 20 | { 21 | public: 22 | GpsrExample (); 23 | /// Configure script parameters, \return true on successful configuration 24 | bool Configure (int argc, char **argv); 25 | /// Run simulation 26 | void Run (); 27 | /// Report results 28 | void Report (std::ostream & os); 29 | 30 | private: 31 | ///\name parameters 32 | //\{ 33 | /// Number of nodes 34 | uint32_t size; 35 | /// Width of the Node Grid 36 | uint32_t gridWidth; 37 | /// Distance between nodes, meters 38 | double step; 39 | /// Simulation time, seconds 40 | double totalTime; 41 | /// Write per-device PCAP traces if true 42 | bool pcap; 43 | //\} 44 | 45 | ///\name network 46 | //\{ 47 | NodeContainer nodes; 48 | NetDeviceContainer devices; 49 | Ipv4InterfaceContainer interfaces; 50 | //\} 51 | 52 | private: 53 | void CreateNodes (); 54 | void CreateDevices (); 55 | void InstallInternetStack (); 56 | void InstallApplications (); 57 | }; 58 | 59 | int main (int argc, char **argv) 60 | { 61 | GpsrExample test; 62 | if (! test.Configure(argc, argv)) 63 | NS_FATAL_ERROR ("Configuration failed. Aborted."); 64 | 65 | test.Run (); 66 | test.Report (std::cout); 67 | return 0; 68 | } 69 | 70 | //----------------------------------------------------------------------------- 71 | GpsrExample::GpsrExample () : 72 | // Number of Nodes 73 | size (2), 74 | // Grid Width 75 | gridWidth(2), 76 | // Distance between nodes 77 | step (200), 78 | // Simulation time 79 | totalTime (30), 80 | // Generate capture files for each node 81 | pcap (true) 82 | { 83 | } 84 | 85 | bool 86 | GpsrExample::Configure (int argc, char **argv) 87 | { 88 | // Enable GPSR logs by default. Comment this if too noisy 89 | //LogComponentEnable("GpsrRoutingProtocol", LOG_LEVEL_ALL); 90 | 91 | SeedManager::SetSeed(12345); 92 | CommandLine cmd; 93 | 94 | cmd.AddValue ("pcap", "Write PCAP traces.", pcap); 95 | cmd.AddValue ("size", "Number of nodes.", size); 96 | cmd.AddValue ("time", "Simulation time, s.", totalTime); 97 | cmd.AddValue ("step", "Grid step, m", step); 98 | 99 | cmd.Parse (argc, argv); 100 | return true; 101 | } 102 | 103 | void 104 | GpsrExample::Run () 105 | { 106 | // Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time. 107 | CreateNodes (); 108 | CreateDevices (); 109 | InstallInternetStack (); 110 | InstallApplications (); 111 | 112 | GpsrHelper gpsr; 113 | gpsr.Install (); 114 | 115 | std::cout << "Starting simulation for " << totalTime << " s ...\n"; 116 | 117 | Simulator::Stop (Seconds (totalTime)); 118 | Simulator::Run (); 119 | Simulator::Destroy (); 120 | } 121 | 122 | void 123 | GpsrExample::Report (std::ostream &) 124 | { 125 | } 126 | 127 | void 128 | GpsrExample::CreateNodes () 129 | { 130 | std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n"; 131 | nodes.Create (size); 132 | // Name nodes 133 | for (uint32_t i = 0; i < size; ++i) 134 | { 135 | std::ostringstream os; 136 | os << "node-" << i; 137 | Names::Add (os.str (), nodes.Get (i)); 138 | } 139 | // Create static grid 140 | MobilityHelper mobility; 141 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 142 | "MinX", DoubleValue (0.0), 143 | "MinY", DoubleValue (0.0), 144 | "DeltaX", DoubleValue (step), 145 | "DeltaY", DoubleValue (step), 146 | "GridWidth", UintegerValue (gridWidth), 147 | "LayoutType", StringValue ("RowFirst")); 148 | mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 149 | mobility.Install (nodes); 150 | } 151 | 152 | void 153 | GpsrExample::CreateDevices () 154 | { 155 | NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); 156 | wifiMac.SetType ("ns3::AdhocWifiMac"); 157 | YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); 158 | YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); 159 | wifiPhy.SetChannel (wifiChannel.Create ()); 160 | WifiHelper wifi = WifiHelper::Default (); 161 | wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0)); 162 | devices = wifi.Install (wifiPhy, wifiMac, nodes); 163 | 164 | if (pcap) 165 | { 166 | wifiPhy.EnablePcapAll (std::string ("gpsr")); 167 | } 168 | } 169 | 170 | void 171 | GpsrExample::InstallInternetStack () 172 | { 173 | GpsrHelper gpsr; 174 | // you can configure GPSR attributes here using gpsr.Set(name, value) 175 | InternetStackHelper stack; 176 | stack.SetRoutingHelper (gpsr); 177 | stack.Install (nodes); 178 | Ipv4AddressHelper address; 179 | address.SetBase ("10.0.0.0", "255.255.0.0"); 180 | interfaces = address.Assign (devices); 181 | } 182 | 183 | void 184 | GpsrExample::InstallApplications () 185 | { 186 | 187 | uint16_t port = 9; // well-known echo port number 188 | uint32_t packetSize = 1024; // size of the packets being transmitted 189 | uint32_t maxPacketCount = 100; // number of packets to transmit 190 | Time interPacketInterval = Seconds (1.); // interval between packet transmissions 191 | 192 | // Set-up a server Application on the bottom-right node of the grid 193 | UdpEchoServerHelper server1 (port); 194 | uint16_t server1Position = size-1; //bottom right 195 | ApplicationContainer apps = server1.Install (nodes.Get(server1Position)); 196 | apps.Start (Seconds (1.0)); 197 | apps.Stop (Seconds (totalTime-0.1)); 198 | 199 | // Set-up a client Application, connected to 'server', to be run on the top-left node of the grid 200 | UdpEchoClientHelper client (interfaces.GetAddress (server1Position), port); 201 | client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); 202 | client.SetAttribute ("Interval", TimeValue (interPacketInterval)); 203 | client.SetAttribute ("PacketSize", UintegerValue (packetSize)); 204 | uint16_t clientPosition = 0; //top left 205 | apps = client.Install (nodes.Get (clientPosition)); 206 | apps.Start (Seconds (2.0)); 207 | apps.Stop (Seconds (totalTime-0.1)); 208 | 209 | } 210 | 211 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/examples/gpsr-test3.cc: -------------------------------------------------------------------------------- 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 | 3 | #include "ns3/gpsr-module.h" 4 | #include "ns3/core-module.h" 5 | #include "ns3/network-module.h" 6 | #include "ns3/internet-module.h" 7 | #include "ns3/mobility-module.h" 8 | #include "ns3/point-to-point-module.h" 9 | #include "ns3/wifi-module.h" 10 | #include "ns3/v4ping-helper.h" 11 | #include "ns3/udp-echo-server.h" 12 | #include "ns3/udp-echo-client.h" 13 | #include "ns3/udp-echo-helper.h" 14 | #include 15 | #include 16 | 17 | using namespace ns3; 18 | 19 | class GpsrExample 20 | { 21 | public: 22 | GpsrExample (); 23 | /// Configure script parameters, \return true on successful configuration 24 | bool Configure (int argc, char **argv); 25 | /// Run simulation 26 | void Run (); 27 | /// Report results 28 | void Report (std::ostream & os); 29 | 30 | private: 31 | ///\name parameters 32 | //\{ 33 | /// Number of nodes 34 | uint32_t size; 35 | /// Width of the Node Grid 36 | uint32_t gridWidth; 37 | /// Distance between nodes, meters 38 | double step; 39 | /// Simulation time, seconds 40 | double totalTime; 41 | /// Write per-device PCAP traces if true 42 | bool pcap; 43 | //\} 44 | 45 | ///\name network 46 | //\{ 47 | NodeContainer nodes; 48 | NetDeviceContainer devices; 49 | Ipv4InterfaceContainer interfaces; 50 | //\} 51 | 52 | private: 53 | void CreateNodes (); 54 | void CreateDevices (); 55 | void InstallInternetStack (); 56 | void InstallApplications (); 57 | }; 58 | 59 | int main (int argc, char **argv) 60 | { 61 | GpsrExample test; 62 | if (! test.Configure(argc, argv)) 63 | NS_FATAL_ERROR ("Configuration failed. Aborted."); 64 | 65 | test.Run (); 66 | test.Report (std::cout); 67 | return 0; 68 | } 69 | 70 | //----------------------------------------------------------------------------- 71 | GpsrExample::GpsrExample () : 72 | // Number of Nodes 73 | size (4), 74 | // Grid Width 75 | gridWidth(2), 76 | // Distance between nodes 77 | step (100), //TODO Distance changed to the limit between nodes: test to see if there are transmitions 78 | // Simulation time 79 | totalTime (30), 80 | // Generate capture files for each node 81 | pcap (true) 82 | { 83 | } 84 | 85 | bool 86 | GpsrExample::Configure (int argc, char **argv) 87 | { 88 | // Enable GPSR logs by default. Comment this if too noisy 89 | //LogComponentEnable("GpsrRoutingProtocol", LOG_LEVEL_ALL); 90 | 91 | SeedManager::SetSeed(12345); 92 | CommandLine cmd; 93 | 94 | cmd.AddValue ("pcap", "Write PCAP traces.", pcap); 95 | cmd.AddValue ("size", "Number of nodes.", size); 96 | cmd.AddValue ("time", "Simulation time, s.", totalTime); 97 | cmd.AddValue ("step", "Grid step, m", step); 98 | 99 | cmd.Parse (argc, argv); 100 | return true; 101 | } 102 | 103 | void 104 | GpsrExample::Run () 105 | { 106 | // Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time. 107 | CreateNodes (); 108 | CreateDevices (); 109 | InstallInternetStack (); 110 | InstallApplications (); 111 | 112 | GpsrHelper gpsr; 113 | gpsr.Set ("LocationServiceName", StringValue ("GOD")); 114 | gpsr.Install (); 115 | 116 | std::cout << "Starting simulation for " << totalTime << " s ...\n"; 117 | 118 | Simulator::Stop (Seconds (totalTime)); 119 | Simulator::Run (); 120 | Simulator::Destroy (); 121 | } 122 | 123 | void 124 | GpsrExample::Report (std::ostream &) 125 | { 126 | } 127 | 128 | void 129 | GpsrExample::CreateNodes () 130 | { 131 | std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n"; 132 | nodes.Create (size); 133 | // Name nodes 134 | for (uint32_t i = 0; i < size; ++i) 135 | { 136 | std::ostringstream os; 137 | os << "node-" << i; 138 | Names::Add (os.str (), nodes.Get (i)); 139 | } 140 | // Create static grid 141 | MobilityHelper mobility; 142 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 143 | "MinX", DoubleValue (0.0), 144 | "MinY", DoubleValue (0.0), 145 | "DeltaX", DoubleValue (step), 146 | "DeltaY", DoubleValue (step), 147 | "GridWidth", UintegerValue (gridWidth), 148 | "LayoutType", StringValue ("RowFirst")); 149 | mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 150 | mobility.Install (nodes); 151 | } 152 | 153 | void 154 | GpsrExample::CreateDevices () 155 | { 156 | NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); 157 | wifiMac.SetType ("ns3::AdhocWifiMac"); 158 | YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); 159 | YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); 160 | wifiPhy.SetChannel (wifiChannel.Create ()); 161 | WifiHelper wifi = WifiHelper::Default (); 162 | wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (1492)); 163 | devices = wifi.Install (wifiPhy, wifiMac, nodes); 164 | 165 | if (pcap) 166 | { 167 | wifiPhy.EnablePcapAll (std::string ("gpsr")); 168 | } 169 | } 170 | 171 | void 172 | GpsrExample::InstallInternetStack () 173 | { 174 | GpsrHelper gpsr; 175 | // you can configure GPSR attributes here using gpsr.Set(name, value) 176 | InternetStackHelper stack; 177 | stack.SetRoutingHelper (gpsr); 178 | stack.Install (nodes); 179 | Ipv4AddressHelper address; 180 | address.SetBase ("10.0.0.0", "255.255.0.0"); 181 | interfaces = address.Assign (devices); 182 | } 183 | 184 | void 185 | GpsrExample::InstallApplications () 186 | { 187 | 188 | uint16_t port = 9; // well-known echo port number 189 | uint32_t packetSize = 1024; // size of the packets being transmitted 190 | uint32_t maxPacketCount = 50; // number of packets to transmit 191 | Time interPacketInterval = Seconds (0.5); // interval between packet transmissions 192 | 193 | // Set-up a server Application on the bottom-right node of the grid 194 | UdpEchoServerHelper server (port); 195 | uint16_t serverPosition = size-1; //bottom right 196 | ApplicationContainer apps = server.Install (nodes.Get(serverPosition)); 197 | apps.Start (Seconds (1.0)); 198 | apps.Stop (Seconds (totalTime-0.1)); 199 | 200 | // Set-up a client Application, connected to 'server', to be run on the top-left node of the grid 201 | UdpEchoClientHelper client (interfaces.GetAddress (serverPosition), port); 202 | client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); 203 | client.SetAttribute ("Interval", TimeValue (interPacketInterval)); 204 | client.SetAttribute ("PacketSize", UintegerValue (packetSize)); 205 | uint16_t clientPosition = 0; //top left 206 | apps = client.Install (nodes.Get (clientPosition)); 207 | apps.Start (Seconds (2.0)); 208 | apps.Stop (Seconds (totalTime-0.1)); 209 | } 210 | 211 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/examples/gpsr-test4.cc: -------------------------------------------------------------------------------- 1 | 2 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 3 | 4 | #include "ns3/gpsr-module.h" 5 | #include "ns3/core-module.h" 6 | #include "ns3/network-module.h" 7 | #include "ns3/internet-module.h" 8 | #include "ns3/mobility-module.h" 9 | #include "ns3/point-to-point-module.h" 10 | //#include "ns3/common-module.h" 11 | //#include "ns3/node-module.h" 12 | //#include "ns3/helper-module.h" 13 | //#include "ns3/mobility-module.h" 14 | //#include "ns3/contrib-module.h" 15 | #include "ns3/wifi-module.h" 16 | #include "ns3/v4ping-helper.h" 17 | #include "ns3/udp-echo-server.h" 18 | #include "ns3/udp-echo-client.h" 19 | #include "ns3/udp-echo-helper.h" 20 | #include 21 | #include 22 | 23 | using namespace ns3; 24 | 25 | class GpsrExample 26 | { 27 | public: 28 | GpsrExample (); 29 | /// Configure script parameters, \return true on successful configuration 30 | bool Configure (int argc, char **argv); 31 | /// Run simulation 32 | void Run (); 33 | /// Report results 34 | void Report (std::ostream & os); 35 | 36 | private: 37 | ///\name parameters 38 | //\{ 39 | /// Number of nodes 40 | uint32_t size; 41 | /// Width of the Node Grid 42 | uint32_t gridWidth; 43 | /// Distance between nodes, meters 44 | double step; 45 | /// Simulation time, seconds 46 | double totalTime; 47 | /// Write per-device PCAP traces if true 48 | bool pcap; 49 | //\} 50 | 51 | ///\name network 52 | //\{ 53 | NodeContainer nodes; 54 | NetDeviceContainer devices; 55 | Ipv4InterfaceContainer interfaces; 56 | //\} 57 | 58 | private: 59 | void CreateNodes (); 60 | void CreateDevices (); 61 | void InstallInternetStack (); 62 | void InstallApplications (); 63 | }; 64 | 65 | int main (int argc, char **argv) 66 | { 67 | GpsrExample test; 68 | if (! test.Configure(argc, argv)) 69 | NS_FATAL_ERROR ("Configuration failed. Aborted."); 70 | 71 | test.Run (); 72 | test.Report (std::cout); 73 | return 0; 74 | } 75 | 76 | //----------------------------------------------------------------------------- 77 | GpsrExample::GpsrExample () : 78 | // Number of Nodes 79 | size (100), 80 | // Grid Width 81 | gridWidth(10), 82 | // Distance between nodes 83 | step (100), //TODO Distance changed to the limit between nodes: test to see if there are transmitions 84 | // Simulation time 85 | totalTime (30), 86 | // Generate capture files for each node 87 | pcap (true) 88 | { 89 | } 90 | 91 | bool 92 | GpsrExample::Configure (int argc, char **argv) 93 | { 94 | // Enable GPSR logs by default. Comment this if too noisy 95 | //LogComponentEnable("GpsrRoutingProtocol", LOG_LEVEL_ALL); 96 | 97 | SeedManager::SetSeed(12345); 98 | CommandLine cmd; 99 | 100 | cmd.AddValue ("pcap", "Write PCAP traces.", pcap); 101 | cmd.AddValue ("size", "Number of nodes.", size); 102 | cmd.AddValue ("time", "Simulation time, s.", totalTime); 103 | cmd.AddValue ("step", "Grid step, m", step); 104 | 105 | cmd.Parse (argc, argv); 106 | return true; 107 | } 108 | 109 | void 110 | GpsrExample::Run () 111 | { 112 | // Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time. 113 | CreateNodes (); 114 | CreateDevices (); 115 | InstallInternetStack (); 116 | InstallApplications (); 117 | 118 | GpsrHelper gpsr; 119 | gpsr.Set ("LocationServiceName", StringValue ("GOD")); 120 | gpsr.Install (); 121 | 122 | std::cout << "Starting simulation for " << totalTime << " s ...\n"; 123 | 124 | Simulator::Stop (Seconds (totalTime)); 125 | Simulator::Run (); 126 | Simulator::Destroy (); 127 | } 128 | 129 | void 130 | GpsrExample::Report (std::ostream &) 131 | { 132 | } 133 | 134 | void 135 | GpsrExample::CreateNodes () 136 | { 137 | std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n"; 138 | nodes.Create (size); 139 | // Name nodes 140 | for (uint32_t i = 0; i < size; ++i) 141 | { 142 | std::ostringstream os; 143 | os << "node-" << i; 144 | Names::Add (os.str (), nodes.Get (i)); 145 | } 146 | // Create static grid 147 | MobilityHelper mobility; 148 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 149 | "MinX", DoubleValue (0.0), 150 | "MinY", DoubleValue (0.0), 151 | "DeltaX", DoubleValue (step), 152 | "DeltaY", DoubleValue (step), 153 | "GridWidth", UintegerValue (gridWidth), 154 | "LayoutType", StringValue ("RowFirst")); 155 | mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 156 | mobility.Install (nodes); 157 | } 158 | 159 | void 160 | GpsrExample::CreateDevices () 161 | { 162 | NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); 163 | wifiMac.SetType ("ns3::AdhocWifiMac"); 164 | YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); 165 | YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); 166 | wifiPhy.SetChannel (wifiChannel.Create ()); 167 | WifiHelper wifi = WifiHelper::Default (); 168 | wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0)); 169 | devices = wifi.Install (wifiPhy, wifiMac, nodes); 170 | 171 | if (pcap) 172 | { 173 | wifiPhy.EnablePcapAll (std::string ("gpsr")); 174 | } 175 | } 176 | 177 | void 178 | GpsrExample::InstallInternetStack () 179 | { 180 | GpsrHelper gpsr; 181 | // you can configure GPSR attributes here using gpsr.Set(name, value) 182 | InternetStackHelper stack; 183 | stack.SetRoutingHelper (gpsr); 184 | stack.Install (nodes); 185 | Ipv4AddressHelper address; 186 | address.SetBase ("10.0.0.0", "255.255.0.0"); 187 | interfaces = address.Assign (devices); 188 | } 189 | 190 | void 191 | GpsrExample::InstallApplications () 192 | { 193 | 194 | uint16_t port = 9; // well-known echo port number 195 | uint32_t packetSize = 1024; // size of the packets being transmitted 196 | uint32_t maxPacketCount = 100; // number of packets to transmit 197 | Time interPacketInterval = Seconds (1.); // interval between packet transmissions 198 | 199 | // Set-up a server Application on the bottom-right node of the grid 200 | UdpEchoServerHelper server1 (port); 201 | uint16_t server1Position = size-1; //bottom right 202 | ApplicationContainer apps = server1.Install (nodes.Get(server1Position)); 203 | apps.Start (Seconds (1.0)); 204 | apps.Stop (Seconds (totalTime-0.1)); 205 | 206 | // Set-up a client Application, connected to 'server1', to be run on the top-left node of the grid 207 | UdpEchoClientHelper client1 (interfaces.GetAddress (server1Position), port); 208 | client1.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); 209 | client1.SetAttribute ("Interval", TimeValue (interPacketInterval)); 210 | client1.SetAttribute ("PacketSize", UintegerValue (packetSize)); 211 | uint16_t client1Position = 0; //top left 212 | apps = client1.Install (nodes.Get (client1Position)); 213 | apps.Start (Seconds (2.0)); 214 | apps.Stop (Seconds (totalTime-0.1)); 215 | 216 | // Set-up a server Application on the top-right node of the grid 217 | UdpEchoServerHelper server2 (port); 218 | uint16_t server2Position = 9; 219 | apps = server2.Install (nodes.Get(server2Position)); //top right 220 | apps.Start (Seconds (1.0)); 221 | apps.Stop (Seconds (totalTime-0.1)); 222 | 223 | // Set-up a client Application, connected to 'server2', to be run on the bottom-left node of the grid 224 | UdpEchoClientHelper client2 (interfaces.GetAddress (server2Position), port); 225 | client2.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); 226 | client2.SetAttribute ("Interval", TimeValue (interPacketInterval)); 227 | client2.SetAttribute ("PacketSize", UintegerValue (packetSize)); 228 | uint16_t client2Position = size-9; //bottom left 229 | apps = client2.Install (nodes.Get (client2Position)); 230 | apps.Start (Seconds (2.0)); 231 | apps.Stop (Seconds (totalTime-0.1)); 232 | 233 | } 234 | 235 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/examples/gpsr-test5.cc: -------------------------------------------------------------------------------- 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 | 3 | #include "ns3/gpsr-module.h" 4 | #include "ns3/core-module.h" 5 | #include "ns3/network-module.h" 6 | #include "ns3/internet-module.h" 7 | #include "ns3/mobility-module.h" 8 | #include "ns3/point-to-point-module.h" 9 | #include "ns3/wifi-module.h" 10 | #include "ns3/v4ping-helper.h" 11 | #include "ns3/udp-echo-server.h" 12 | #include "ns3/udp-echo-client.h" 13 | #include "ns3/udp-echo-helper.h" 14 | #include 15 | #include 16 | 17 | 18 | using namespace ns3; 19 | 20 | void ReceivePacket (Ptr socket) 21 | { 22 | NS_LOG_UNCOND ("Received one packet!"); 23 | } 24 | 25 | static void GenerateTraffic (Ptr socket, uint32_t pktSize, 26 | uint32_t pktCount, Time pktInterval ) 27 | { 28 | if (pktCount > 0) 29 | { 30 | socket->Send (Create (pktSize)); 31 | Simulator::Schedule (pktInterval, &GenerateTraffic, 32 | socket, pktSize,pktCount-1, pktInterval); 33 | } 34 | else 35 | { 36 | socket->Close (); 37 | } 38 | } 39 | 40 | 41 | class GpsrExample 42 | { 43 | public: 44 | GpsrExample (); 45 | /// Configure script parameters, \return true on successful configuration 46 | bool Configure (int argc, char **argv); 47 | /// Run simulation 48 | void Run (); 49 | /// Report results 50 | void Report (std::ostream & os); 51 | 52 | private: 53 | ///\name parameters 54 | //\{ 55 | /// Number of nodes 56 | uint32_t size; 57 | /// Width of the Node Grid 58 | uint32_t gridWidth; 59 | /// Distance between nodes, meters 60 | double step; 61 | /// Simulation time, seconds 62 | double totalTime; 63 | /// Write per-device PCAP traces if true 64 | bool pcap; 65 | //\} 66 | 67 | ///\name network 68 | //\{ 69 | NodeContainer nodes; 70 | NetDeviceContainer devices; 71 | Ipv4InterfaceContainer interfaces; 72 | //\} 73 | 74 | private: 75 | void CreateNodes (); 76 | void CreateDevices (); 77 | void InstallInternetStack (); 78 | void InstallApplications (); 79 | }; 80 | 81 | int main (int argc, char **argv) 82 | { 83 | GpsrExample test; 84 | if (! test.Configure(argc, argv)) 85 | NS_FATAL_ERROR ("Configuration failed. Aborted."); 86 | 87 | test.Run (); 88 | test.Report (std::cout); 89 | return 0; 90 | } 91 | 92 | //----------------------------------------------------------------------------- 93 | GpsrExample::GpsrExample () : 94 | // Number of Nodes 95 | size (8), 96 | // Grid Width 97 | gridWidth (4), 98 | // Distance between nodes 99 | step (100), //TODO Distance changed to the limit between nodes: test to see if there are transmitions 100 | // Simulation time 101 | totalTime (30), 102 | // Generate capture files for each node 103 | pcap (true) 104 | { 105 | } 106 | 107 | bool 108 | GpsrExample::Configure (int argc, char **argv) 109 | { 110 | // Enable GPSR logs by default. Comment this if too noisy 111 | // LogComponentEnable("GpsrRoutingProtocol", LOG_LEVEL_ALL); 112 | 113 | SeedManager::SetSeed(12345); 114 | CommandLine cmd; 115 | 116 | cmd.AddValue ("pcap", "Write PCAP traces.", pcap); 117 | cmd.AddValue ("size", "Number of nodes.", size); 118 | cmd.AddValue ("time", "Simulation time, s.", totalTime); 119 | cmd.AddValue ("step", "Grid step, m", step); 120 | 121 | cmd.Parse (argc, argv); 122 | return true; 123 | } 124 | 125 | void 126 | GpsrExample::Run () 127 | { 128 | //Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time. 129 | CreateNodes (); 130 | CreateDevices (); 131 | InstallInternetStack (); 132 | InstallApplications (); 133 | 134 | GpsrHelper gpsr; 135 | gpsr.Install (); 136 | 137 | std::cout << "Starting simulation for " << totalTime << " s ...\n"; 138 | 139 | Simulator::Stop (Seconds (totalTime)); 140 | Simulator::Run (); 141 | Simulator::Destroy (); 142 | } 143 | 144 | void 145 | GpsrExample::Report (std::ostream &) 146 | { 147 | } 148 | 149 | void 150 | GpsrExample::CreateNodes () 151 | { 152 | std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n"; 153 | nodes.Create (size); 154 | // Name nodes 155 | for (uint32_t i = 0; i < size; ++i) 156 | { 157 | std::ostringstream os; 158 | // Set the Node name to the corresponding IP host address 159 | os << "node-" << i+1; 160 | Names::Add (os.str (), nodes.Get (i)); 161 | } 162 | 163 | 164 | // Create static grid 165 | MobilityHelper mobility; 166 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 167 | "MinX", DoubleValue (0.0), 168 | "MinY", DoubleValue (0.0), 169 | "DeltaX", DoubleValue (step), 170 | "DeltaY", DoubleValue (step), 171 | "GridWidth", UintegerValue (gridWidth), 172 | "LayoutType", StringValue ("RowFirst")); 173 | mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 174 | mobility.Install (nodes); 175 | } 176 | 177 | void 178 | GpsrExample::CreateDevices () 179 | { 180 | NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); 181 | wifiMac.SetType ("ns3::AdhocWifiMac"); 182 | YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); 183 | YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); 184 | wifiPhy.SetChannel (wifiChannel.Create ()); 185 | WifiHelper wifi = WifiHelper::Default (); 186 | wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (1560)); 187 | devices = wifi.Install (wifiPhy, wifiMac, nodes); 188 | 189 | // Enable Captures, if necessary 190 | if (pcap) 191 | { 192 | wifiPhy.EnablePcapAll (std::string ("gpsr")); 193 | } 194 | } 195 | 196 | void 197 | GpsrExample::InstallInternetStack () 198 | { 199 | GpsrHelper gpsr; 200 | // you can configure GPSR attributes here using gpsr.Set(name, value) 201 | InternetStackHelper stack; 202 | stack.SetRoutingHelper (gpsr); 203 | stack.Install (nodes); 204 | Ipv4AddressHelper address; 205 | address.SetBase ("10.0.0.0", "255.255.0.0"); 206 | interfaces = address.Assign (devices); 207 | } 208 | 209 | void 210 | GpsrExample::InstallApplications () 211 | { 212 | 213 | uint16_t port = 9; // well-known echo port number 214 | uint32_t packetSize = 1024; // size of the exchanged packets 215 | uint32_t maxPacketCount = 100; // number of packets to transmit 216 | Time interPacketInterval = Seconds (0.5); // interval between packet transmitions 217 | 218 | // Set-up a server Application, to be run on the bottom-right node of the grid 219 | UdpEchoServerHelper server (port); 220 | uint16_t serverPosition = size-1; // bottom-right 221 | ApplicationContainer apps = server.Install (nodes.Get(serverPosition)); 222 | apps.Start (Seconds (5.0)); // Server Start Time 223 | apps.Stop (Seconds (totalTime-0.1)); // Server Stop Time 224 | 225 | // Set-up a client Application, connected to 'server', to be run on the bottom-left node of the grid 226 | UdpEchoClientHelper client (interfaces.GetAddress (serverPosition), port); 227 | client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); 228 | client.SetAttribute ("Interval", TimeValue (interPacketInterval)); 229 | client.SetAttribute ("PacketSize", UintegerValue (packetSize)); 230 | uint16_t clientPostion = 4; // bottom-left 231 | apps = client.Install (nodes.Get (clientPostion)); 232 | apps.Start (Seconds (6.0)); // Client Start Time 233 | apps.Stop (Seconds (totalTime-0.1)); // Client Stop Time 234 | 235 | // Move away nodes 0 and 6, in order to create test scenario 236 | uint16_t nodesToRemove[] = {0, 6}; 237 | Ptr node; 238 | Ptr mob; 239 | for(int i=0; i<2 ;i++){ 240 | node = nodes.Get (nodesToRemove[i]); 241 | mob = node->GetObject(); 242 | Simulator::Schedule (Seconds (1), &MobilityModel::SetPosition, mob, Vector (1e5, 1e5, 1e5)); 243 | } 244 | } 245 | 246 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/examples/gpsr-test6.cc: -------------------------------------------------------------------------------- 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 | 3 | #include "ns3/gpsr-module.h" 4 | #include "ns3/core-module.h" 5 | #include "ns3/network-module.h" 6 | #include "ns3/internet-module.h" 7 | #include "ns3/mobility-module.h" 8 | #include "ns3/point-to-point-module.h" 9 | #include "ns3/wifi-module.h" 10 | #include "ns3/v4ping-helper.h" 11 | #include "ns3/udp-echo-server.h" 12 | #include "ns3/udp-echo-client.h" 13 | #include "ns3/udp-echo-helper.h" 14 | #include 15 | #include 16 | 17 | using namespace ns3; 18 | 19 | void ReceivePacket (Ptr socket) 20 | { 21 | NS_LOG_UNCOND ("Received one packet!"); 22 | } 23 | 24 | static void GenerateTraffic (Ptr socket, uint32_t pktSize, 25 | uint32_t pktCount, Time pktInterval ) 26 | { 27 | if (pktCount > 0) 28 | { 29 | socket->Send (Create (pktSize)); 30 | Simulator::Schedule (pktInterval, &GenerateTraffic, 31 | socket, pktSize,pktCount-1, pktInterval); 32 | } 33 | else 34 | { 35 | socket->Close (); 36 | } 37 | } 38 | 39 | 40 | class GpsrExample 41 | { 42 | public: 43 | GpsrExample (); 44 | /// Configure script parameters, \return true on successful configuration 45 | bool Configure (int argc, char **argv); 46 | /// Run simulation 47 | void Run (); 48 | /// Report results 49 | void Report (std::ostream & os); 50 | 51 | private: 52 | ///\name parameters 53 | //\{ 54 | /// Number of nodes 55 | uint32_t size; 56 | /// Width of the Node Grid 57 | uint32_t gridWidth; 58 | /// Distance between nodes, meters 59 | double step; 60 | /// Simulation time, seconds 61 | double totalTime; 62 | /// Write per-device PCAP traces if true 63 | bool pcap; 64 | //\} 65 | 66 | ///\name network 67 | //\{ 68 | NodeContainer nodes; 69 | NetDeviceContainer devices; 70 | Ipv4InterfaceContainer interfaces; 71 | //\} 72 | 73 | private: 74 | void CreateNodes (); 75 | void CreateDevices (); 76 | void InstallInternetStack (); 77 | void InstallApplications (); 78 | }; 79 | 80 | int main (int argc, char **argv) 81 | { 82 | GpsrExample test; 83 | if (! test.Configure(argc, argv)) 84 | NS_FATAL_ERROR ("Configuration failed. Aborted."); 85 | 86 | test.Run (); 87 | test.Report (std::cout); 88 | return 0; 89 | } 90 | 91 | //----------------------------------------------------------------------------- 92 | GpsrExample::GpsrExample () : 93 | // Number of Nodes 94 | size (20), 95 | // Grid Width 96 | gridWidth (4), 97 | // Distance between nodes 98 | step (100), //TODO Distance changed to the limit between nodes: test to see if there are transmitions 99 | // Simulation time 100 | totalTime (30), 101 | // Generate capture files for each node 102 | pcap (true) 103 | { 104 | } 105 | 106 | bool 107 | GpsrExample::Configure (int argc, char **argv) 108 | { 109 | // Enable GPSR logs by default. Comment this if too noisy 110 | // LogComponentEnable("GpsrRoutingProtocol", LOG_LEVEL_ALL); 111 | 112 | SeedManager::SetSeed(12345); 113 | CommandLine cmd; 114 | 115 | cmd.AddValue ("pcap", "Write PCAP traces.", pcap); 116 | cmd.AddValue ("size", "Number of nodes.", size); 117 | cmd.AddValue ("time", "Simulation time, s.", totalTime); 118 | cmd.AddValue ("step", "Grid step, m", step); 119 | 120 | cmd.Parse (argc, argv); 121 | return true; 122 | } 123 | 124 | void 125 | GpsrExample::Run () 126 | { 127 | //Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time. 128 | CreateNodes (); 129 | CreateDevices (); 130 | InstallInternetStack (); 131 | InstallApplications (); 132 | 133 | GpsrHelper gpsr; 134 | gpsr.Install (); 135 | 136 | std::cout << "Starting simulation for " << totalTime << " s ...\n"; 137 | 138 | Simulator::Stop (Seconds (totalTime)); 139 | Simulator::Run (); 140 | Simulator::Destroy (); 141 | } 142 | 143 | void 144 | GpsrExample::Report (std::ostream &) 145 | { 146 | } 147 | 148 | void 149 | GpsrExample::CreateNodes () 150 | { 151 | std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n"; 152 | nodes.Create (size); 153 | // Name nodes 154 | for (uint32_t i = 0; i < size; ++i) 155 | { 156 | std::ostringstream os; 157 | // Set the Node name to the corresponding IP host address 158 | os << "node-" << i+1; 159 | Names::Add (os.str (), nodes.Get (i)); 160 | } 161 | 162 | 163 | // Create static grid 164 | MobilityHelper mobility; 165 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 166 | "MinX", DoubleValue (0.0), 167 | "MinY", DoubleValue (0.0), 168 | "DeltaX", DoubleValue (step), 169 | "DeltaY", DoubleValue (step), 170 | "GridWidth", UintegerValue (gridWidth), 171 | "LayoutType", StringValue ("RowFirst")); 172 | mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 173 | mobility.Install (nodes); 174 | } 175 | 176 | void 177 | GpsrExample::CreateDevices () 178 | { 179 | NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); 180 | wifiMac.SetType ("ns3::AdhocWifiMac"); 181 | YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); 182 | YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); 183 | wifiPhy.SetChannel (wifiChannel.Create ()); 184 | WifiHelper wifi = WifiHelper::Default (); 185 | wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (1560)); 186 | devices = wifi.Install (wifiPhy, wifiMac, nodes); 187 | 188 | // Enable Captures, if necessary 189 | if (pcap) 190 | { 191 | wifiPhy.EnablePcapAll (std::string ("gpsr")); 192 | } 193 | } 194 | 195 | void 196 | GpsrExample::InstallInternetStack () 197 | { 198 | GpsrHelper gpsr; 199 | // you can configure GPSR attributes here using gpsr.Set(name, value) 200 | InternetStackHelper stack; 201 | stack.SetRoutingHelper (gpsr); 202 | stack.Install (nodes); 203 | Ipv4AddressHelper address; 204 | address.SetBase ("10.0.0.0", "255.255.0.0"); 205 | interfaces = address.Assign (devices); 206 | } 207 | 208 | void 209 | GpsrExample::InstallApplications () 210 | { 211 | 212 | uint16_t port = 9; // well-known echo port number 213 | uint32_t packetSize = 1024; // size of the exchanged packets 214 | uint32_t maxPacketCount = 100; // number of packets to transmit 215 | Time interPacketInterval = Seconds (0.5); // interval between packet transmitions 216 | 217 | // Set-up a server Application, to be run on the bottom-right node of the grid 218 | UdpEchoServerHelper server (port); 219 | uint16_t serverPosition = 15; 220 | ApplicationContainer apps = server.Install (nodes.Get(serverPosition)); 221 | apps.Start (Seconds (1.0)); // Server Start Time 222 | apps.Stop (Seconds (totalTime-0.1)); // Server Stop Time 223 | 224 | // Set-up a client Application, connected to 'server', to be run on the top-left node of the grid 225 | UdpEchoClientHelper client (interfaces.GetAddress (serverPosition), port); 226 | client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); 227 | client.SetAttribute ("Interval", TimeValue (interPacketInterval)); 228 | client.SetAttribute ("PacketSize", UintegerValue (packetSize)); 229 | uint16_t clientPostion = 8; 230 | apps = client.Install (nodes.Get (clientPostion)); 231 | apps.Start (Seconds (2.0)); // Client Start Time 232 | apps.Stop (Seconds (totalTime-0.1)); // Client Stop Time 233 | 234 | // Move away nodes described in the array, in order to create test scenario 235 | uint16_t nodesToRemove[] = {5, 6, 10, 12, 13, 14}; 236 | Ptr node; 237 | Ptr mob; 238 | for(int i=0; i<6 ;i++){ 239 | node = nodes.Get (nodesToRemove[i]); 240 | mob = node->GetObject(); 241 | Simulator::Schedule (Seconds (1), &MobilityModel::SetPosition, mob, Vector (1e5, 1e5, 1e5)); 242 | } 243 | 244 | 245 | 246 | /*NODE PLACEMENT: 247 | * * * * 248 | * * 249 | * * * 250 | * 251 | * * * * 252 | 100 m appart*/ 253 | 254 | } 255 | 256 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/examples/gpsr-test7.cc: -------------------------------------------------------------------------------- 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 | 3 | #include "ns3/gpsr-module.h" 4 | #include "ns3/core-module.h" 5 | #include "ns3/network-module.h" 6 | #include "ns3/internet-module.h" 7 | #include "ns3/mobility-module.h" 8 | #include "ns3/point-to-point-module.h" 9 | #include "ns3/wifi-module.h" 10 | #include "ns3/v4ping-helper.h" 11 | #include "ns3/udp-echo-server.h" 12 | #include "ns3/udp-echo-client.h" 13 | #include "ns3/udp-echo-helper.h" 14 | #include 15 | #include 16 | 17 | using namespace ns3; 18 | 19 | void ReceivePacket (Ptr socket) 20 | { 21 | NS_LOG_UNCOND ("Received one packet!"); 22 | } 23 | 24 | static void GenerateTraffic (Ptr socket, uint32_t pktSize, 25 | uint32_t pktCount, Time pktInterval ) 26 | { 27 | if (pktCount > 0) 28 | { 29 | socket->Send (Create (pktSize)); 30 | Simulator::Schedule (pktInterval, &GenerateTraffic, 31 | socket, pktSize,pktCount-1, pktInterval); 32 | } 33 | else 34 | { 35 | socket->Close (); 36 | } 37 | } 38 | 39 | 40 | class GpsrExample 41 | { 42 | public: 43 | GpsrExample (); 44 | /// Configure script parameters, \return true on successful configuration 45 | bool Configure (int argc, char **argv); 46 | /// Run simulation 47 | void Run (); 48 | /// Report results 49 | void Report (std::ostream & os); 50 | 51 | private: 52 | ///\name parameters 53 | //\{ 54 | /// Number of nodes 55 | uint32_t size; 56 | /// Width of the Node Grid 57 | uint32_t gridWidth; 58 | /// Distance between nodes, meters 59 | double step; 60 | /// Simulation time, seconds 61 | double totalTime; 62 | /// Write per-device PCAP traces if true 63 | bool pcap; 64 | //\} 65 | 66 | ///\name network 67 | //\{ 68 | NodeContainer nodes; 69 | NetDeviceContainer devices; 70 | Ipv4InterfaceContainer interfaces; 71 | //\} 72 | 73 | private: 74 | void CreateNodes (); 75 | void CreateDevices (); 76 | void InstallInternetStack (); 77 | void InstallApplications (); 78 | }; 79 | 80 | int main (int argc, char **argv) 81 | { 82 | GpsrExample test; 83 | if (! test.Configure(argc, argv)) 84 | NS_FATAL_ERROR ("Configuration failed. Aborted."); 85 | 86 | test.Run (); 87 | test.Report (std::cout); 88 | return 0; 89 | } 90 | 91 | //----------------------------------------------------------------------------- 92 | GpsrExample::GpsrExample () : 93 | // Number of Nodes 94 | size (100), 95 | // Grid Width 96 | gridWidth (10), 97 | // Distance between nodes 98 | step (100), //TODO Distance changed to the limit between nodes: test to see if there are transmitions 99 | // Simulation time 100 | totalTime (30), 101 | // Generate capture files for each node 102 | pcap (true) 103 | { 104 | } 105 | 106 | bool 107 | GpsrExample::Configure (int argc, char **argv) 108 | { 109 | // Enable GPSR logs by default. Comment this if too noisy 110 | // LogComponentEnable("GpsrRoutingProtocol", LOG_LEVEL_ALL); 111 | 112 | SeedManager::SetSeed(12345); 113 | CommandLine cmd; 114 | 115 | cmd.AddValue ("pcap", "Write PCAP traces.", pcap); 116 | cmd.AddValue ("size", "Number of nodes.", size); 117 | cmd.AddValue ("time", "Simulation time, s.", totalTime); 118 | cmd.AddValue ("step", "Grid step, m", step); 119 | 120 | cmd.Parse (argc, argv); 121 | return true; 122 | } 123 | 124 | void 125 | GpsrExample::Run () 126 | { 127 | //Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time. 128 | CreateNodes (); 129 | CreateDevices (); 130 | InstallInternetStack (); 131 | InstallApplications (); 132 | 133 | GpsrHelper gpsr; 134 | gpsr.Install (); 135 | 136 | std::cout << "Starting simulation for " << totalTime << " s ...\n"; 137 | 138 | Simulator::Stop (Seconds (totalTime)); 139 | Simulator::Run (); 140 | Simulator::Destroy (); 141 | } 142 | 143 | void 144 | GpsrExample::Report (std::ostream &) 145 | { 146 | } 147 | 148 | void 149 | GpsrExample::CreateNodes () 150 | { 151 | std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n"; 152 | nodes.Create (size); 153 | // Name nodes 154 | for (uint32_t i = 0; i < size; ++i) 155 | { 156 | std::ostringstream os; 157 | // Set the Node name to the corresponding IP host address 158 | os << "node-" << i+1; 159 | Names::Add (os.str (), nodes.Get (i)); 160 | } 161 | 162 | 163 | // Create static grid 164 | MobilityHelper mobility; 165 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 166 | "MinX", DoubleValue (0.0), 167 | "MinY", DoubleValue (0.0), 168 | "DeltaX", DoubleValue (step), 169 | "DeltaY", DoubleValue (step), 170 | "GridWidth", UintegerValue (gridWidth), 171 | "LayoutType", StringValue ("RowFirst")); 172 | mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 173 | mobility.Install (nodes); 174 | } 175 | 176 | void 177 | GpsrExample::CreateDevices () 178 | { 179 | NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); 180 | wifiMac.SetType ("ns3::AdhocWifiMac"); 181 | YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); 182 | YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); 183 | wifiPhy.SetChannel (wifiChannel.Create ()); 184 | WifiHelper wifi = WifiHelper::Default (); 185 | wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (1560)); 186 | devices = wifi.Install (wifiPhy, wifiMac, nodes); 187 | 188 | // Enable Captures, if necessary 189 | if (pcap) 190 | { 191 | wifiPhy.EnablePcapAll (std::string ("gpsr")); 192 | } 193 | } 194 | 195 | void 196 | GpsrExample::InstallInternetStack () 197 | { 198 | GpsrHelper gpsr; 199 | // you can configure GPSR attributes here using gpsr.Set(name, value) 200 | InternetStackHelper stack; 201 | stack.SetRoutingHelper (gpsr); 202 | stack.Install (nodes); 203 | Ipv4AddressHelper address; 204 | address.SetBase ("10.0.0.0", "255.255.0.0"); 205 | interfaces = address.Assign (devices); 206 | } 207 | 208 | void 209 | GpsrExample::InstallApplications () 210 | { 211 | 212 | uint16_t port = 9; // well-known echo port number 213 | uint32_t packetSize = 1024; // size of the exchanged packets 214 | uint32_t maxPacketCount = 100; // number of packets to transmit 215 | Time interPacketInterval = Seconds (0.5); // interval between packet transmitions 216 | 217 | // Set-up a server Application, to be run on the bottom-right node of the grid 218 | UdpEchoServerHelper server (port); 219 | uint16_t serverPosition = size-1; // bottom-right 220 | ApplicationContainer apps = server.Install (nodes.Get(serverPosition)); 221 | apps.Start (Seconds (1.0)); // Server Start Time 222 | apps.Stop (Seconds (totalTime-0.1)); // Server Stop Time 223 | 224 | // Set-up a client Application, connected to 'server', to be run on the top-left node of the grid 225 | UdpEchoClientHelper client (interfaces.GetAddress (serverPosition), port); 226 | client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); 227 | client.SetAttribute ("Interval", TimeValue (interPacketInterval)); 228 | client.SetAttribute ("PacketSize", UintegerValue (packetSize)); 229 | uint16_t clientPostion = 0; 230 | apps = client.Install (nodes.Get (clientPostion)); 231 | apps.Start (Seconds (2.0)); // Client Start Time 232 | apps.Stop (Seconds (totalTime-0.1)); // Client Stop Time 233 | 234 | 235 | // Move nodes 3-6, on lines 3-6, away (this should change the path of the transmition) 236 | Ptr node; 237 | Ptr mob; 238 | for(int l=3;l<=6;l++){ 239 | for(int n=3;n<=6;n++){ 240 | node = nodes.Get (l*10+n); 241 | mob = node->GetObject (); 242 | Simulator::Schedule (Seconds (totalTime/2), &MobilityModel::SetPosition, mob, Vector (1e5, 1e5, 1e5)); 243 | } 244 | } 245 | 246 | } 247 | 248 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/examples/wscript: -------------------------------------------------------------------------------- 1 | ## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- 2 | 3 | def build(bld): 4 | obj = bld.create_ns3_program('gpsr', 5 | ['wifi', 'internet', 'gpsr']) 6 | obj.source = 'gpsr-test1.cc' 7 | 8 | obj = bld.create_ns3_program('gpsr-test2', 9 | ['wifi', 'internet', 'gpsr']) 10 | obj.source = 'gpsr-test2.cc' 11 | 12 | obj = bld.create_ns3_program('gpsr-test3', 13 | ['wifi', 'internet', 'gpsr']) 14 | obj.source = 'gpsr-test3.cc' 15 | 16 | obj = bld.create_ns3_program('gpsr-test4', 17 | ['wifi', 'internet', 'gpsr']) 18 | obj.source = 'gpsr-test4.cc' 19 | 20 | obj = bld.create_ns3_program('gpsr-test5', 21 | ['wifi', 'internet', 'gpsr']) 22 | obj.source = 'gpsr-test5.cc' 23 | 24 | obj = bld.create_ns3_program('gpsr-test6', 25 | ['wifi', 'internet', 'gpsr']) 26 | obj.source = 'gpsr-test6.cc' 27 | 28 | obj = bld.create_ns3_program('gpsr-test7', 29 | ['wifi', 'internet', 'gpsr']) 30 | obj.source = 'gpsr-test7.cc' 31 | 32 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/helper/gpsr-helper.cc: -------------------------------------------------------------------------------- 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 | /* 3 | * Copyright (c) 2009 IITP RAS 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation; 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | * 18 | * Authors: António Fonseca , written after OlsrHelper by Mathieu Lacage 19 | */ 20 | #include "gpsr-helper.h" 21 | #include "ns3/gpsr.h" 22 | #include "ns3/node-list.h" 23 | #include "ns3/names.h" 24 | #include "ns3/ipv4-list-routing.h" 25 | #include "ns3/node-container.h" 26 | #include "ns3/callback.h" 27 | #include "ns3/udp-l4-protocol.h" 28 | 29 | 30 | namespace ns3 { 31 | 32 | GpsrHelper::GpsrHelper () 33 | : Ipv4RoutingHelper () 34 | { 35 | m_agentFactory.SetTypeId ("ns3::gpsr::RoutingProtocol"); 36 | } 37 | 38 | GpsrHelper* 39 | GpsrHelper::Copy (void) const 40 | { 41 | return new GpsrHelper (*this); 42 | } 43 | 44 | Ptr 45 | GpsrHelper::Create (Ptr node) const 46 | { 47 | //Ptr ipv4l4 = node->GetObject (); 48 | Ptr gpsr = m_agentFactory.Create (); 49 | //gpsr->SetDownTarget (ipv4l4->GetDownTarget ()); 50 | //ipv4l4->SetDownTarget (MakeCallback (&gpsr::RoutingProtocol::AddHeaders, gpsr)); 51 | node->AggregateObject (gpsr); 52 | return gpsr; 53 | } 54 | 55 | void 56 | GpsrHelper::Set (std::string name, const AttributeValue &value) 57 | { 58 | m_agentFactory.Set (name, value); 59 | } 60 | 61 | 62 | void 63 | GpsrHelper::Install (void) const 64 | { 65 | NodeContainer c = NodeContainer::GetGlobal (); 66 | for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i) 67 | { 68 | Ptr node = (*i); 69 | Ptr udp = node->GetObject (); 70 | Ptr gpsr = node->GetObject (); 71 | gpsr->SetDownTarget (udp->GetDownTarget ()); 72 | udp->SetDownTarget (MakeCallback(&gpsr::RoutingProtocol::AddHeaders, gpsr)); 73 | } 74 | 75 | 76 | } 77 | 78 | 79 | } 80 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/helper/gpsr-helper.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 | /* 3 | * Copyright (c) 2009 IITP RAS 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation; 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | * 18 | * Authors: Pavel Boyko , written after OlsrHelper by Mathieu Lacage 19 | */ 20 | #ifndef GPSRHELPER_H_ 21 | #define GPSRHELPER_H_ 22 | 23 | #include "ns3/object-factory.h" 24 | #include "ns3/node.h" 25 | #include "ns3/node-container.h" 26 | #include "ns3/ipv4-routing-helper.h" 27 | 28 | 29 | 30 | namespace ns3 { 31 | /** 32 | * \ingroup gpsr 33 | * \brief Helper class that adds GPSR routing to nodes. 34 | */ 35 | class GpsrHelper : public Ipv4RoutingHelper 36 | { 37 | public: 38 | GpsrHelper (); 39 | 40 | /** 41 | * \internal 42 | * \returns pointer to clone of this OlsrHelper 43 | * 44 | * This method is mainly for internal use by the other helpers; 45 | * clients are expected to free the dynamic memory allocated by this method 46 | */ 47 | GpsrHelper* Copy (void) const; 48 | 49 | /** 50 | * \param node the node on which the routing protocol will run 51 | * \returns a newly-created routing protocol 52 | * 53 | * This method will be called by ns3::InternetStackHelper::Install 54 | * 55 | * TODO: support installing GPSR on the subset of all available IP interfaces 56 | */ 57 | virtual Ptr Create (Ptr node) const; 58 | /** 59 | * \param name the name of the attribute to set 60 | * \param value the value of the attribute to set. 61 | * 62 | * This method controls the attributes of ns3::gpsr::RoutingProtocol 63 | */ 64 | void Set (std::string name, const AttributeValue &value); 65 | 66 | void Install (void) const; 67 | 68 | private: 69 | ObjectFactory m_agentFactory; 70 | }; 71 | 72 | } 73 | #endif /* GPSRHELPER_H_ */ 74 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/model/TODO: -------------------------------------------------------------------------------- 1 | preparar pRa funcionar com o LS 2 | Mandar loopback no routeoutput DONE 3 | no route input ver tag e mandar pa deferedrouteoutput DONE 4 | por millagre chama sendpacketfromqeue (com timers ou assim. o aodv faz no RecvReply....) DONE 5 | no sendpacket se ja tiver pronto tira tag e envia DONE 6 | 7 | mudar o invalid vector (-1, -1, 0) DONE 8 | 9 | tirar batota do l3... http://code.nsnam.org/tomh/ns-3-dev/rev/dc40e27bf514 10 | code style 11 | do the recovery-mode search for "//FIXME here should call the recovery-mode" 12 | por instancia do LS DONE 13 | Chamar LS generico DONE 14 | 15 | eventually: 16 | work with multiple interfaces 17 | tirar prints a mais DONE 18 | 19 | 20 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/model/gpsr-packet.cc: -------------------------------------------------------------------------------- 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 | 3 | #include "gpsr-packet.h" 4 | #include "ns3/address-utils.h" 5 | #include "ns3/packet.h" 6 | #include "ns3/log.h" 7 | 8 | NS_LOG_COMPONENT_DEFINE ("GpsrPacket"); 9 | 10 | namespace ns3 { 11 | namespace gpsr { 12 | 13 | NS_OBJECT_ENSURE_REGISTERED (TypeHeader); 14 | 15 | TypeHeader::TypeHeader (MessageType t = GPSRTYPE_HELLO) 16 | : m_type (t), 17 | m_valid (true) 18 | { 19 | } 20 | 21 | TypeId 22 | TypeHeader::GetTypeId () 23 | { 24 | static TypeId tid = TypeId ("ns3::gpsr::TypeHeader") 25 | .SetParent
() 26 | .AddConstructor () 27 | ; 28 | return tid; 29 | } 30 | 31 | TypeId 32 | TypeHeader::GetInstanceTypeId () const 33 | { 34 | return GetTypeId (); 35 | } 36 | 37 | uint32_t 38 | TypeHeader::GetSerializedSize () const 39 | { 40 | return 1; 41 | } 42 | 43 | void 44 | TypeHeader::Serialize (Buffer::Iterator i) const 45 | { 46 | i.WriteU8 ((uint8_t) m_type); 47 | } 48 | 49 | uint32_t 50 | TypeHeader::Deserialize (Buffer::Iterator start) 51 | { 52 | Buffer::Iterator i = start; 53 | uint8_t type = i.ReadU8 (); 54 | m_valid = true; 55 | switch (type) 56 | { 57 | case GPSRTYPE_HELLO: 58 | case GPSRTYPE_POS: 59 | { 60 | m_type = (MessageType) type; 61 | break; 62 | } 63 | default: 64 | m_valid = false; 65 | } 66 | uint32_t dist = i.GetDistanceFrom (start); 67 | NS_ASSERT (dist == GetSerializedSize ()); 68 | return dist; 69 | } 70 | 71 | void 72 | TypeHeader::Print (std::ostream &os) const 73 | { 74 | switch (m_type) 75 | { 76 | case GPSRTYPE_HELLO: 77 | { 78 | os << "HELLO"; 79 | break; 80 | } 81 | case GPSRTYPE_POS: 82 | { 83 | os << "POSITION"; 84 | break; 85 | } 86 | default: 87 | os << "UNKNOWN_TYPE"; 88 | } 89 | } 90 | 91 | bool 92 | TypeHeader::operator== (TypeHeader const & o) const 93 | { 94 | return (m_type == o.m_type && m_valid == o.m_valid); 95 | } 96 | 97 | std::ostream & 98 | operator<< (std::ostream & os, TypeHeader const & h) 99 | { 100 | h.Print (os); 101 | return os; 102 | } 103 | 104 | //----------------------------------------------------------------------------- 105 | // HELLO 106 | //----------------------------------------------------------------------------- 107 | HelloHeader::HelloHeader (uint64_t originPosx, uint64_t originPosy) 108 | : m_originPosx (originPosx), 109 | m_originPosy (originPosy) 110 | { 111 | } 112 | 113 | NS_OBJECT_ENSURE_REGISTERED (HelloHeader); 114 | 115 | TypeId 116 | HelloHeader::GetTypeId () 117 | { 118 | static TypeId tid = TypeId ("ns3::gpsr::HelloHeader") 119 | .SetParent
() 120 | .AddConstructor () 121 | ; 122 | return tid; 123 | } 124 | 125 | TypeId 126 | HelloHeader::GetInstanceTypeId () const 127 | { 128 | return GetTypeId (); 129 | } 130 | 131 | uint32_t 132 | HelloHeader::GetSerializedSize () const 133 | { 134 | return 16; 135 | } 136 | 137 | void 138 | HelloHeader::Serialize (Buffer::Iterator i) const 139 | { 140 | NS_LOG_DEBUG ("Serialize X " << m_originPosx << " Y " << m_originPosy); 141 | 142 | 143 | i.WriteHtonU64 (m_originPosx); 144 | i.WriteHtonU64 (m_originPosy); 145 | 146 | } 147 | 148 | uint32_t 149 | HelloHeader::Deserialize (Buffer::Iterator start) 150 | { 151 | 152 | Buffer::Iterator i = start; 153 | 154 | m_originPosx = i.ReadNtohU64 (); 155 | m_originPosy = i.ReadNtohU64 (); 156 | 157 | NS_LOG_DEBUG ("Deserialize X " << m_originPosx << " Y " << m_originPosy); 158 | 159 | uint32_t dist = i.GetDistanceFrom (start); 160 | NS_ASSERT (dist == GetSerializedSize ()); 161 | return dist; 162 | } 163 | 164 | void 165 | HelloHeader::Print (std::ostream &os) const 166 | { 167 | os << " PositionX: " << m_originPosx 168 | << " PositionY: " << m_originPosy; 169 | } 170 | 171 | std::ostream & 172 | operator<< (std::ostream & os, HelloHeader const & h) 173 | { 174 | h.Print (os); 175 | return os; 176 | } 177 | 178 | 179 | 180 | bool 181 | HelloHeader::operator== (HelloHeader const & o) const 182 | { 183 | return (m_originPosx == o.m_originPosx && m_originPosy == o.m_originPosy); 184 | } 185 | 186 | 187 | 188 | 189 | 190 | //----------------------------------------------------------------------------- 191 | // Position 192 | //----------------------------------------------------------------------------- 193 | PositionHeader::PositionHeader (uint64_t dstPosx, uint64_t dstPosy, uint32_t updated, uint64_t recPosx, uint64_t recPosy, uint8_t inRec, uint64_t lastPosx, uint64_t lastPosy) 194 | : m_dstPosx (dstPosx), 195 | m_dstPosy (dstPosy), 196 | m_updated (updated), 197 | m_recPosx (recPosx), 198 | m_recPosy (recPosy), 199 | m_inRec (inRec), 200 | m_lastPosx (lastPosx), 201 | m_lastPosy (lastPosy) 202 | { 203 | } 204 | 205 | NS_OBJECT_ENSURE_REGISTERED (PositionHeader); 206 | 207 | TypeId 208 | PositionHeader::GetTypeId () 209 | { 210 | static TypeId tid = TypeId ("ns3::gpsr::PositionHeader") 211 | .SetParent
() 212 | .AddConstructor () 213 | ; 214 | return tid; 215 | } 216 | 217 | TypeId 218 | PositionHeader::GetInstanceTypeId () const 219 | { 220 | return GetTypeId (); 221 | } 222 | 223 | uint32_t 224 | PositionHeader::GetSerializedSize () const 225 | { 226 | return 53; 227 | } 228 | 229 | void 230 | PositionHeader::Serialize (Buffer::Iterator i) const 231 | { 232 | i.WriteU64 (m_dstPosx); 233 | i.WriteU64 (m_dstPosy); 234 | i.WriteU32 (m_updated); 235 | i.WriteU64 (m_recPosx); 236 | i.WriteU64 (m_recPosy); 237 | i.WriteU8 (m_inRec); 238 | i.WriteU64 (m_lastPosx); 239 | i.WriteU64 (m_lastPosy); 240 | } 241 | 242 | uint32_t 243 | PositionHeader::Deserialize (Buffer::Iterator start) 244 | { 245 | Buffer::Iterator i = start; 246 | m_dstPosx = i.ReadU64 (); 247 | m_dstPosy = i.ReadU64 (); 248 | m_updated = i.ReadU32 (); 249 | m_recPosx = i.ReadU64 (); 250 | m_recPosy = i.ReadU64 (); 251 | m_inRec = i.ReadU8 (); 252 | m_lastPosx = i.ReadU64 (); 253 | m_lastPosy = i.ReadU64 (); 254 | 255 | uint32_t dist = i.GetDistanceFrom (start); 256 | NS_ASSERT (dist == GetSerializedSize ()); 257 | return dist; 258 | } 259 | 260 | void 261 | PositionHeader::Print (std::ostream &os) const 262 | { 263 | os << " PositionX: " << m_dstPosx 264 | << " PositionY: " << m_dstPosy 265 | << " Updated: " << m_updated 266 | << " RecPositionX: " << m_recPosx 267 | << " RecPositionY: " << m_recPosy 268 | << " inRec: " << m_inRec 269 | << " LastPositionX: " << m_lastPosx 270 | << " LastPositionY: " << m_lastPosy; 271 | } 272 | 273 | std::ostream & 274 | operator<< (std::ostream & os, PositionHeader const & h) 275 | { 276 | h.Print (os); 277 | return os; 278 | } 279 | 280 | 281 | 282 | bool 283 | PositionHeader::operator== (PositionHeader const & o) const 284 | { 285 | return (m_dstPosx == o.m_dstPosx && m_dstPosy == o.m_dstPosy && m_updated == o.m_updated && m_recPosx == o.m_recPosx && m_recPosy == o.m_recPosy && m_inRec == o.m_inRec && m_lastPosx == o.m_lastPosx && m_lastPosy == o.m_lastPosy); 286 | } 287 | 288 | 289 | } 290 | } 291 | 292 | 293 | 294 | 295 | 296 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/model/gpsr-packet.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 | 3 | #ifndef GPSRPACKET_H 4 | #define GPSRPACKET_H 5 | 6 | #include 7 | #include "ns3/header.h" 8 | #include "ns3/enum.h" 9 | #include "ns3/ipv4-address.h" 10 | #include 11 | #include "ns3/nstime.h" 12 | #include "ns3/vector.h" 13 | 14 | namespace ns3 { 15 | namespace gpsr { 16 | 17 | 18 | 19 | enum MessageType 20 | { 21 | GPSRTYPE_HELLO = 1, //!< GPSRTYPE_HELLO 22 | GPSRTYPE_POS = 2, //!< GPSRTYPE_POS 23 | }; 24 | 25 | /** 26 | * \ingroup gpsr 27 | * \brief GPSR types 28 | */ 29 | class TypeHeader : public Header 30 | { 31 | public: 32 | /// c-tor 33 | TypeHeader (MessageType t); 34 | 35 | ///\name Header serialization/deserialization 36 | //\{ 37 | static TypeId GetTypeId (); 38 | TypeId GetInstanceTypeId () const; 39 | uint32_t GetSerializedSize () const; 40 | void Serialize (Buffer::Iterator start) const; 41 | uint32_t Deserialize (Buffer::Iterator start); 42 | void Print (std::ostream &os) const; 43 | //\} 44 | 45 | /// Return type 46 | MessageType Get () const 47 | { 48 | return m_type; 49 | } 50 | /// Check that type if valid 51 | bool IsValid () const 52 | { 53 | return m_valid; //FIXME that way it wont work 54 | } 55 | bool operator== (TypeHeader const & o) const; 56 | private: 57 | MessageType m_type; 58 | bool m_valid; 59 | }; 60 | 61 | std::ostream & operator<< (std::ostream & os, TypeHeader const & h); 62 | 63 | class HelloHeader : public Header 64 | { 65 | public: 66 | /// c-tor 67 | HelloHeader (uint64_t originPosx = 0, uint64_t originPosy = 0); 68 | 69 | ///\name Header serialization/deserialization 70 | //\{ 71 | static TypeId GetTypeId (); 72 | TypeId GetInstanceTypeId () const; 73 | uint32_t GetSerializedSize () const; 74 | void Serialize (Buffer::Iterator start) const; 75 | uint32_t Deserialize (Buffer::Iterator start); 76 | void Print (std::ostream &os) const; 77 | //\} 78 | 79 | ///\name Fields 80 | //\{ 81 | void SetOriginPosx (uint64_t posx) 82 | { 83 | m_originPosx = posx; 84 | } 85 | uint64_t GetOriginPosx () const 86 | { 87 | return m_originPosx; 88 | } 89 | void SetOriginPosy (uint64_t posy) 90 | { 91 | m_originPosy = posy; 92 | } 93 | uint64_t GetOriginPosy () const 94 | { 95 | return m_originPosy; 96 | } 97 | //\} 98 | 99 | 100 | bool operator== (HelloHeader const & o) const; 101 | private: 102 | uint64_t m_originPosx; ///< Originator Position x 103 | uint64_t m_originPosy; ///< Originator Position x 104 | }; 105 | 106 | std::ostream & operator<< (std::ostream & os, HelloHeader const &); 107 | 108 | class PositionHeader : public Header 109 | { 110 | public: 111 | /// c-tor 112 | PositionHeader (uint64_t dstPosx = 0, uint64_t dstPosy = 0, uint32_t updated = 0, uint64_t recPosx = 0, uint64_t recPosy = 0, uint8_t inRec = 0, uint64_t lastPosx = 0, uint64_t lastPosy = 0); 113 | 114 | ///\name Header serialization/deserialization 115 | //\{ 116 | static TypeId GetTypeId (); 117 | TypeId GetInstanceTypeId () const; 118 | uint32_t GetSerializedSize () const; 119 | void Serialize (Buffer::Iterator start) const; 120 | uint32_t Deserialize (Buffer::Iterator start); 121 | void Print (std::ostream &os) const; 122 | //\} 123 | 124 | ///\name Fields 125 | //\{ 126 | void SetDstPosx (uint64_t posx) 127 | { 128 | m_dstPosx = posx; 129 | } 130 | uint64_t GetDstPosx () const 131 | { 132 | return m_dstPosx; 133 | } 134 | void SetDstPosy (uint64_t posy) 135 | { 136 | m_dstPosy = posy; 137 | } 138 | uint64_t GetDstPosy () const 139 | { 140 | return m_dstPosy; 141 | } 142 | void SetUpdated (uint32_t updated) 143 | { 144 | m_updated = updated; 145 | } 146 | uint32_t GetUpdated () const 147 | { 148 | return m_updated; 149 | } 150 | void SetRecPosx (uint64_t posx) 151 | { 152 | m_recPosx = posx; 153 | } 154 | uint64_t GetRecPosx () const 155 | { 156 | return m_recPosx; 157 | } 158 | void SetRecPosy (uint64_t posy) 159 | { 160 | m_recPosy = posy; 161 | } 162 | uint64_t GetRecPosy () const 163 | { 164 | return m_recPosy; 165 | } 166 | void SetInRec (uint8_t rec) 167 | { 168 | m_inRec = rec; 169 | } 170 | uint8_t GetInRec () const 171 | { 172 | return m_inRec; 173 | } 174 | void SetLastPosx (uint64_t posx) 175 | { 176 | m_lastPosx = posx; 177 | } 178 | uint64_t GetLastPosx () const 179 | { 180 | return m_lastPosx; 181 | } 182 | void SetLastPosy (uint64_t posy) 183 | { 184 | m_lastPosy = posy; 185 | } 186 | uint64_t GetLastPosy () const 187 | { 188 | return m_lastPosy; 189 | } 190 | 191 | 192 | bool operator== (PositionHeader const & o) const; 193 | private: 194 | uint64_t m_dstPosx; ///< Destination Position x 195 | uint64_t m_dstPosy; ///< Destination Position x 196 | uint32_t m_updated; ///< Time of last update 197 | uint64_t m_recPosx; ///< x of position that entered Recovery-mode 198 | uint64_t m_recPosy; ///< y of position that entered Recovery-mode 199 | uint8_t m_inRec; ///< 1 if in Recovery-mode, 0 otherwise 200 | uint64_t m_lastPosx; ///< x of position of previous hop 201 | uint64_t m_lastPosy; ///< y of position of previous hop 202 | 203 | }; 204 | 205 | std::ostream & operator<< (std::ostream & os, PositionHeader const &); 206 | 207 | } 208 | } 209 | #endif /* GPSRPACKET_H */ 210 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/model/gpsr-ptable.cc: -------------------------------------------------------------------------------- 1 | #include "gpsr-ptable.h" 2 | #include "ns3/simulator.h" 3 | #include "ns3/log.h" 4 | #include 5 | 6 | NS_LOG_COMPONENT_DEFINE ("GpsrTable"); 7 | 8 | 9 | namespace ns3 { 10 | namespace gpsr { 11 | 12 | /* 13 | GPSR position table 14 | */ 15 | 16 | PositionTable::PositionTable () 17 | { 18 | m_txErrorCallback = MakeCallback (&PositionTable::ProcessTxError, this); 19 | m_entryLifeTime = Seconds (2); //FIXME fazer isto parametrizavel de acordo com tempo de hello 20 | 21 | } 22 | 23 | Time 24 | PositionTable::GetEntryUpdateTime (Ipv4Address id) 25 | { 26 | if (id == Ipv4Address::GetZero ()) 27 | { 28 | return Time (Seconds (0)); 29 | } 30 | std::map >::iterator i = m_table.find (id); 31 | return i->second.second; 32 | } 33 | 34 | /** 35 | * \brief Adds entry in position table 36 | */ 37 | void 38 | PositionTable::AddEntry (Ipv4Address id, Vector position) 39 | { 40 | std::map >::iterator i = m_table.find (id); 41 | if (i != m_table.end () || id.IsEqual (i->first)) 42 | { 43 | m_table.erase (id); 44 | m_table.insert (std::make_pair (id, std::make_pair (position, Simulator::Now ()))); 45 | return; 46 | } 47 | 48 | 49 | m_table.insert (std::make_pair (id, std::make_pair (position, Simulator::Now ()))); 50 | } 51 | 52 | /** 53 | * \brief Deletes entry in position table 54 | */ 55 | void PositionTable::DeleteEntry (Ipv4Address id) 56 | { 57 | m_table.erase (id); 58 | } 59 | 60 | /** 61 | * \brief Gets position from position table 62 | * \param id Ipv4Address to get position from 63 | * \return Position of that id or NULL if not known 64 | */ 65 | Vector 66 | PositionTable::GetPosition (Ipv4Address id) 67 | { 68 | 69 | NodeList::Iterator listEnd = NodeList::End (); 70 | for (NodeList::Iterator i = NodeList::Begin (); i != listEnd; i++) 71 | { 72 | Ptr node = *i; 73 | if (node->GetObject ()->GetAddress (1, 0).GetLocal () == id) 74 | { 75 | return node->GetObject ()->GetPosition (); 76 | } 77 | } 78 | return PositionTable::GetInvalidPosition (); 79 | 80 | } 81 | 82 | /** 83 | * \brief Checks if a node is a neighbour 84 | * \param id Ipv4Address of the node to check 85 | * \return True if the node is neighbour, false otherwise 86 | */ 87 | bool 88 | PositionTable::isNeighbour (Ipv4Address id) 89 | { 90 | 91 | std::map >::iterator i = m_table.find (id); 92 | if (i != m_table.end () || id.IsEqual (i->first)) 93 | { 94 | return true; 95 | } 96 | 97 | return false; 98 | } 99 | 100 | 101 | /** 102 | * \brief remove entries with expired lifetime 103 | */ 104 | void 105 | PositionTable::Purge () 106 | { 107 | 108 | if(m_table.empty ()) 109 | { 110 | return; 111 | } 112 | 113 | std::list toErase; 114 | 115 | std::map >::iterator i = m_table.begin (); 116 | std::map >::iterator listEnd = m_table.end (); 117 | 118 | for (; !(i == listEnd); i++) 119 | { 120 | 121 | if (m_entryLifeTime + GetEntryUpdateTime (i->first) <= Simulator::Now ()) 122 | { 123 | toErase.insert (toErase.begin (), i->first); 124 | 125 | } 126 | } 127 | toErase.unique (); 128 | 129 | std::list::iterator end = toErase.end (); 130 | 131 | for (std::list::iterator it = toErase.begin (); it != end; ++it) 132 | { 133 | 134 | m_table.erase (*it); 135 | 136 | } 137 | } 138 | 139 | /** 140 | * \brief clears all entries 141 | */ 142 | void 143 | PositionTable::Clear () 144 | { 145 | m_table.clear (); 146 | } 147 | 148 | /** 149 | * \brief Gets next hop according to GPSR protocol 150 | * \param position the position of the destination node 151 | * \param nodePos the position of the node that has the packet 152 | * \return Ipv4Address of the next hop, Ipv4Address::GetZero () if no nighbour was found in greedy mode 153 | */ 154 | Ipv4Address 155 | PositionTable::BestNeighbor (Vector position, Vector nodePos) 156 | { 157 | Purge (); 158 | 159 | double initialDistance = CalculateDistance (nodePos, position); 160 | 161 | if (m_table.empty ()) 162 | { 163 | NS_LOG_DEBUG ("BestNeighbor table is empty; Position: " << position); 164 | return Ipv4Address::GetZero (); 165 | } //if table is empty (no neighbours) 166 | 167 | Ipv4Address bestFoundID = m_table.begin ()->first; 168 | double bestFoundDistance = CalculateDistance (m_table.begin ()->second.first, position); 169 | std::map >::iterator i; 170 | for (i = m_table.begin (); !(i == m_table.end ()); i++) 171 | { 172 | if (bestFoundDistance > CalculateDistance (i->second.first, position)) 173 | { 174 | bestFoundID = i->first; 175 | bestFoundDistance = CalculateDistance (i->second.first, position); 176 | } 177 | } 178 | 179 | if(initialDistance > bestFoundDistance) 180 | return bestFoundID; 181 | else 182 | return Ipv4Address::GetZero (); //so it enters Recovery-mode 183 | 184 | } 185 | 186 | 187 | /** 188 | * \brief Gets next hop according to GPSR recovery-mode protocol (right hand rule) 189 | * \param previousHop the position of the node that sent the packet to this node 190 | * \param nodePos the position of the destination node 191 | * \return Ipv4Address of the next hop, Ipv4Address::GetZero () if no nighbour was found in greedy mode 192 | */ 193 | Ipv4Address 194 | PositionTable::BestAngle (Vector previousHop, Vector nodePos) 195 | { 196 | Purge (); 197 | 198 | if (m_table.empty ()) 199 | { 200 | NS_LOG_DEBUG ("BestNeighbor table is empty; Position: " << nodePos); 201 | return Ipv4Address::GetZero (); 202 | } //if table is empty (no neighbours) 203 | 204 | double tmpAngle; 205 | Ipv4Address bestFoundID = Ipv4Address::GetZero (); 206 | 207 | double bestFoundAngle = 360; 208 | std::map >::iterator i; 209 | 210 | for (i = m_table.begin (); !(i == m_table.end ()); i++) 211 | { 212 | tmpAngle = GetAngle(nodePos, previousHop, i->second.first); 213 | if (bestFoundAngle > tmpAngle && tmpAngle != 0) 214 | { 215 | bestFoundID = i->first; 216 | bestFoundAngle = tmpAngle; 217 | } 218 | } 219 | if(bestFoundID == Ipv4Address::GetZero ()) //only if the only neighbour is who sent the packet 220 | { 221 | bestFoundID = m_table.begin ()->first; 222 | } 223 | 224 | return bestFoundID; 225 | } 226 | 227 | 228 | //Gives angle between the vector CentrePos-Refpos to the vector CentrePos-node counterclockwise 229 | double 230 | PositionTable::GetAngle (Vector centrePos, Vector refPos, Vector node){ 231 | double const PI = 4*atan(1); 232 | 233 | std::complex A = std::complex(centrePos.x,centrePos.y); 234 | std::complex B = std::complex(node.x,node.y); 235 | std::complex C = std::complex(refPos.x,refPos.y); //Change B with C if you want angles clockwise 236 | 237 | std::complex AB; //reference edge 238 | std::complex AC; 239 | std::complex tmp; 240 | std::complex tmpCplx; 241 | 242 | std::complex Angle; 243 | 244 | AB = B - A; 245 | AB = (real(AB)/norm(AB)) + (std::complex(0.0,1.0)*(imag(AB)/norm(AB))); 246 | 247 | AC = C - A; 248 | AC = (real(AC)/norm(AC)) + (std::complex(0.0,1.0)*(imag(AC)/norm(AC))); 249 | 250 | tmp = log(AC/AB); 251 | tmpCplx = std::complex(0.0,-1.0); 252 | Angle = tmp*tmpCplx; 253 | Angle *= (180/PI); 254 | if (real(Angle)<0) 255 | Angle = 360+real(Angle); 256 | 257 | return real(Angle); 258 | 259 | } 260 | 261 | 262 | 263 | 264 | 265 | /** 266 | * \ProcessTxError 267 | */ 268 | void PositionTable::ProcessTxError (WifiMacHeader const & hdr) 269 | { 270 | } 271 | 272 | 273 | 274 | //FIXME ainda preciso disto agr que o LS ja n está aqui??????? 275 | 276 | /** 277 | * \brief Returns true if is in search for destionation 278 | */ 279 | bool PositionTable::IsInSearch (Ipv4Address id) 280 | { 281 | return false; 282 | } 283 | 284 | bool PositionTable::HasPosition (Ipv4Address id) 285 | { 286 | return true; 287 | } 288 | 289 | 290 | } // gpsr 291 | } // ns3 292 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/model/gpsr-ptable.h: -------------------------------------------------------------------------------- 1 | #ifndef GPSR_PTABLE_H 2 | #define GPSR_PTABLE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "ns3/ipv4.h" 8 | #include "ns3/timer.h" 9 | #include 10 | #include "ns3/node.h" 11 | #include "ns3/node-list.h" 12 | #include "ns3/mobility-model.h" 13 | #include "ns3/vector.h" 14 | #include "ns3/wifi-mac-header.h" 15 | #include "ns3/random-variable-stream.h" 16 | #include 17 | 18 | namespace ns3 { 19 | namespace gpsr { 20 | 21 | /* 22 | * \ingroup gpsr 23 | * \brief Position table used by GPSR 24 | */ 25 | class PositionTable 26 | { 27 | public: 28 | /// c-tor 29 | PositionTable (); 30 | 31 | /** 32 | * \brief Gets the last time the entry was updated 33 | * \param id Ipv4Address to get time of update from 34 | * \return Time of last update to the position 35 | */ 36 | Time GetEntryUpdateTime (Ipv4Address id); 37 | 38 | /** 39 | * \brief Adds entry in position table 40 | */ 41 | void AddEntry (Ipv4Address id, Vector position); 42 | 43 | /** 44 | * \brief Deletes entry in position table 45 | */ 46 | void DeleteEntry (Ipv4Address id); 47 | 48 | /** 49 | * \brief Gets position from position table 50 | * \param id Ipv4Address to get position from 51 | * \return Position of that id or NULL if not known 52 | */ 53 | Vector GetPosition (Ipv4Address id); 54 | 55 | /** 56 | * \brief Checks if a node is a neighbour 57 | * \param id Ipv4Address of the node to check 58 | * \return True if the node is neighbour, false otherwise 59 | */ 60 | bool isNeighbour (Ipv4Address id); 61 | 62 | /** 63 | * \brief remove entries with expired lifetime 64 | */ 65 | void Purge (); 66 | 67 | /** 68 | * \brief clears all entries 69 | */ 70 | void Clear (); 71 | 72 | /** 73 | * \Get Callback to ProcessTxError 74 | */ 75 | Callback GetTxErrorCallback () const 76 | { 77 | return m_txErrorCallback; 78 | } 79 | 80 | /** 81 | * \brief Gets next hop according to GPSR protocol 82 | * \param position the position of the destination node 83 | * \param nodePos the position of the node that has the packet 84 | * \return Ipv4Address of the next hop, Ipv4Address::GetZero () if no nighbour was found in greedy mode 85 | */ 86 | Ipv4Address BestNeighbor (Vector position, Vector nodePos); 87 | 88 | bool IsInSearch (Ipv4Address id); 89 | 90 | bool HasPosition (Ipv4Address id); 91 | 92 | static Vector GetInvalidPosition () 93 | { 94 | return Vector (-1, -1, 0); 95 | } 96 | 97 | /** 98 | * \brief Gets next hop according to GPSR recovery-mode protocol (right hand rule) 99 | * \param previousHop the position of the node that sent the packet to this node 100 | * \param nodePos the position of the destination node 101 | * \return Ipv4Address of the next hop, Ipv4Address::GetZero () if no nighbour was found in greedy mode 102 | */ 103 | Ipv4Address BestAngle (Vector previousHop, Vector nodePos); 104 | 105 | //Gives angle between the vector CentrePos-Refpos to the vector CentrePos-node counterclockwise 106 | double GetAngle (Vector centrePos, Vector refPos, Vector node); 107 | 108 | 109 | 110 | private: 111 | Time m_entryLifeTime; 112 | std::map > m_table; 113 | // TX error callback 114 | Callback m_txErrorCallback; 115 | // Process layer 2 TX error notification 116 | void ProcessTxError (WifiMacHeader const&); 117 | 118 | 119 | 120 | 121 | }; 122 | 123 | } // gpsr 124 | } // ns3 125 | #endif /* GPSR_PTABLE_H */ 126 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/model/gpsr-rqueue.cc: -------------------------------------------------------------------------------- 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 | 3 | #include "gpsr-rqueue.h" 4 | #include 5 | #include 6 | #include "ns3/ipv4-route.h" 7 | #include "ns3/socket.h" 8 | #include "ns3/log.h" 9 | 10 | NS_LOG_COMPONENT_DEFINE ("GpsrRequestQueue"); 11 | 12 | namespace ns3 { 13 | namespace gpsr { 14 | uint32_t 15 | RequestQueue::GetSize () 16 | { 17 | Purge (); 18 | return m_queue.size (); 19 | } 20 | 21 | bool 22 | RequestQueue::Enqueue (QueueEntry & entry) 23 | { 24 | Purge (); 25 | for (std::vector::const_iterator i = m_queue.begin (); i 26 | != m_queue.end (); ++i) 27 | { 28 | if ((i->GetPacket ()->GetUid () == entry.GetPacket ()->GetUid ()) 29 | && (i->GetIpv4Header ().GetDestination () 30 | == entry.GetIpv4Header ().GetDestination ())) 31 | { 32 | return false; 33 | } 34 | } 35 | entry.SetExpireTime (m_queueTimeout); 36 | if (m_queue.size () == m_maxLen) 37 | { 38 | Drop (m_queue.front (), "Drop the most aged packet"); // Drop the most aged packet 39 | m_queue.erase (m_queue.begin ()); 40 | } 41 | m_queue.push_back (entry); 42 | return true; 43 | } 44 | 45 | void 46 | RequestQueue::DropPacketWithDst (Ipv4Address dst) 47 | { 48 | NS_LOG_FUNCTION (this << dst); 49 | Purge (); 50 | //const Ipv4Address addr = dst; 51 | for (std::vector::iterator i = m_queue.begin (); i 52 | != m_queue.end (); ++i) 53 | { 54 | if (IsEqual (*i, dst)) 55 | { 56 | Drop (*i, "DropPacketWithDst "); 57 | } 58 | } 59 | m_queue.erase (std::remove_if (m_queue.begin (), m_queue.end (), 60 | std::bind2nd (std::ptr_fun (RequestQueue::IsEqual), dst)), m_queue.end ()); 61 | } 62 | 63 | bool 64 | RequestQueue::Dequeue (Ipv4Address dst, QueueEntry & entry) 65 | { 66 | Purge (); 67 | for (std::vector::iterator i = m_queue.begin (); i != m_queue.end (); ++i) 68 | { 69 | if (i->GetIpv4Header ().GetDestination () == dst) 70 | { 71 | entry = *i; 72 | m_queue.erase (i); 73 | return true; 74 | } 75 | } 76 | return false; 77 | } 78 | 79 | bool 80 | RequestQueue::Find (Ipv4Address dst) 81 | { 82 | for (std::vector::const_iterator i = m_queue.begin (); i 83 | != m_queue.end (); ++i) 84 | { 85 | if (i->GetIpv4Header ().GetDestination () == dst) 86 | { 87 | return true; 88 | } 89 | } 90 | return false; 91 | } 92 | 93 | struct IsExpired 94 | { 95 | bool 96 | operator() (QueueEntry const & e) const 97 | { 98 | return (e.GetExpireTime () < Seconds (0)); 99 | } 100 | }; 101 | 102 | void 103 | RequestQueue::Purge () 104 | { 105 | IsExpired pred; 106 | for (std::vector::iterator i = m_queue.begin (); i 107 | != m_queue.end (); ++i) 108 | { 109 | if (pred (*i)) 110 | { 111 | Drop (*i, "Drop outdated packet "); 112 | } 113 | } 114 | m_queue.erase (std::remove_if (m_queue.begin (), m_queue.end (), pred), 115 | m_queue.end ()); 116 | } 117 | 118 | void 119 | RequestQueue::Drop (QueueEntry en, std::string reason) 120 | { 121 | NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetIpv4Header ().GetDestination ()); 122 | en.GetErrorCallback () (en.GetPacket (), en.GetIpv4Header (), 123 | Socket::ERROR_NOROUTETOHOST); 124 | return; 125 | } 126 | 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/model/gpsr-rqueue.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 | 3 | #ifndef GPSR_RQUEUE_H 4 | #define GPSR_RQUEUE_H 5 | 6 | #include 7 | #include "ns3/ipv4-routing-protocol.h" 8 | #include "ns3/simulator.h" 9 | 10 | 11 | namespace ns3 { 12 | namespace gpsr { 13 | 14 | /** 15 | * \ingroup gpsr 16 | * \brief GPSR Queue Entry 17 | */ 18 | class QueueEntry 19 | { 20 | public: 21 | typedef Ipv4RoutingProtocol::UnicastForwardCallback UnicastForwardCallback; 22 | typedef Ipv4RoutingProtocol::ErrorCallback ErrorCallback; 23 | /// c-tor 24 | QueueEntry (Ptr pa = 0, Ipv4Header const & h = Ipv4Header (), 25 | UnicastForwardCallback ucb = UnicastForwardCallback (), 26 | ErrorCallback ecb = ErrorCallback (), Time exp = Simulator::Now ()) 27 | : m_packet (pa), 28 | m_header (h), 29 | m_ucb (ucb), 30 | m_ecb (ecb), 31 | m_expire (exp + Simulator::Now ()) 32 | { 33 | } 34 | 35 | /** 36 | * Compare queue entries 37 | * \return true if equal 38 | */ 39 | bool operator== (QueueEntry const & o) const 40 | { 41 | return ((m_packet == o.m_packet) && (m_header.GetDestination () == o.m_header.GetDestination ()) && (m_expire == o.m_expire)); 42 | } 43 | ///\name Fields 44 | //\{ 45 | UnicastForwardCallback GetUnicastForwardCallback () const 46 | { 47 | return m_ucb; 48 | } 49 | void SetUnicastForwardCallback (UnicastForwardCallback ucb) 50 | { 51 | m_ucb = ucb; 52 | } 53 | ErrorCallback GetErrorCallback () const 54 | { 55 | return m_ecb; 56 | } 57 | void SetErrorCallback (ErrorCallback ecb) 58 | { 59 | m_ecb = ecb; 60 | } 61 | Ptr GetPacket () const 62 | { 63 | return m_packet; 64 | } 65 | void SetPacket (Ptr p) 66 | { 67 | m_packet = p; 68 | } 69 | Ipv4Header GetIpv4Header () const 70 | { 71 | return m_header; 72 | } 73 | void SetIpv4Header (Ipv4Header h) 74 | { 75 | m_header = h; 76 | } 77 | void SetExpireTime (Time exp) 78 | { 79 | m_expire = exp + Simulator::Now (); 80 | } 81 | Time GetExpireTime () const 82 | { 83 | return m_expire - Simulator::Now (); 84 | } 85 | //\} 86 | private: 87 | /// Data packet 88 | Ptr m_packet; 89 | /// IP header 90 | Ipv4Header m_header; 91 | /// Unicast forward callback 92 | UnicastForwardCallback m_ucb; 93 | /// Error callback 94 | ErrorCallback m_ecb; 95 | /// Expire time for queue entry 96 | Time m_expire; 97 | }; 98 | /** 99 | * \ingroup gpsr 100 | * \brief GPSR route request queue 101 | * 102 | * Since GPSR is an on demand routing we queue requests while looking for route. 103 | */ 104 | class RequestQueue 105 | { 106 | public: 107 | /// Default c-tor 108 | RequestQueue (uint32_t maxLen, Time routeToQueueTimeout) 109 | : m_maxLen (maxLen), 110 | m_queueTimeout (routeToQueueTimeout) 111 | { 112 | } 113 | /// Push entry in queue, if there is no entry with the same packet and destination address in queue. 114 | bool Enqueue (QueueEntry & entry); 115 | /// Return first found (the earliest) entry for given destination 116 | bool Dequeue (Ipv4Address dst, QueueEntry & entry); 117 | /// Remove all packets with destination IP address dst 118 | void DropPacketWithDst (Ipv4Address dst); 119 | /// Finds whether a packet with destination dst exists in the queue 120 | bool Find (Ipv4Address dst); 121 | /// Number of entries 122 | uint32_t GetSize (); 123 | ///\name Fields 124 | //\{ 125 | uint32_t GetMaxQueueLen () const 126 | { 127 | return m_maxLen; 128 | } 129 | void SetMaxQueueLen (uint32_t len) 130 | { 131 | m_maxLen = len; 132 | } 133 | Time GetQueueTimeout () const 134 | { 135 | return m_queueTimeout; 136 | } 137 | void SetQueueTimeout (Time t) 138 | { 139 | m_queueTimeout = t; 140 | } 141 | //\} 142 | 143 | private: 144 | std::vector m_queue; 145 | /// Remove all expired entries 146 | void Purge (); 147 | /// Notify that packet is dropped from queue by timeout 148 | void Drop (QueueEntry en, std::string reason); 149 | /// The maximum number of packets that we allow a routing protocol to buffer. 150 | uint32_t m_maxLen; 151 | /// The maximum period of time that a routing protocol is allowed to buffer a packet for, seconds. 152 | Time m_queueTimeout; 153 | static bool IsEqual (QueueEntry en, const Ipv4Address dst) 154 | { 155 | return (en.GetIpv4Header ().GetDestination () == dst); 156 | } 157 | }; 158 | 159 | 160 | } 161 | } 162 | 163 | #endif /* GPSR_RQUEUE_H */ 164 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/model/gpsr.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 | /* 3 | 4 | */ 5 | #ifndef GPSR_H 6 | #define GPSR_H 7 | 8 | #include "gpsr-ptable.h" 9 | #include "ns3/node.h" 10 | #include "gpsr-packet.h" 11 | #include "ns3/ipv4-routing-protocol.h" 12 | #include "ns3/ipv4-interface.h" 13 | #include "ns3/ipv4-l3-protocol.h" 14 | 15 | #include "ns3/ipv4.h" 16 | #include "ns3/ip-l4-protocol.h" 17 | #include "ns3/icmpv4-l4-protocol.h" 18 | 19 | #include "ns3/mobility-model.h" 20 | #include "gpsr-rqueue.h" 21 | 22 | #include "ns3/ipv4-header.h" 23 | #include "ns3/ipv4-address.h" 24 | #include "ns3/ipv4-route.h" 25 | #include "ns3/location-service.h" 26 | #include "ns3/god.h" 27 | 28 | #include 29 | #include 30 | 31 | namespace ns3 { 32 | namespace gpsr { 33 | /** 34 | * \ingroup gpsr 35 | * 36 | * \brief GPSR routing protocol 37 | */ 38 | class RoutingProtocol : public Ipv4RoutingProtocol 39 | { 40 | public: 41 | static TypeId GetTypeId (void); 42 | static const uint32_t GPSR_PORT; 43 | 44 | /// c-tor 45 | RoutingProtocol (); 46 | virtual ~RoutingProtocol (); 47 | virtual void DoDispose (); 48 | 49 | 50 | ///\name From Ipv4RoutingProtocol 51 | // 52 | // 53 | Ptr RouteOutput (Ptr p, const Ipv4Header &header, Ptr oif, Socket::SocketErrno &sockerr); 54 | bool RouteInput (Ptr p, const Ipv4Header &header, Ptr idev, 55 | UnicastForwardCallback ucb, MulticastForwardCallback mcb, 56 | LocalDeliverCallback lcb, ErrorCallback ecb); 57 | virtual void NotifyInterfaceUp (uint32_t interface); 58 | int GetProtocolNumber (void) const; 59 | virtual void AddHeaders (Ptr p, Ipv4Address source, Ipv4Address destination, uint8_t protocol, Ptr route); 60 | virtual void NotifyInterfaceDown (uint32_t interface); 61 | virtual void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address); 62 | virtual void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address); 63 | virtual void SetIpv4 (Ptr ipv4); 64 | virtual void RecvGPSR (Ptr socket); 65 | virtual void UpdateRouteToNeighbor (Ipv4Address sender, Ipv4Address receiver, Vector Pos); 66 | virtual void SendHello (); 67 | virtual bool IsMyOwnAddress (Ipv4Address src); 68 | 69 | Ptr m_ipv4; 70 | /// Raw socket per each IP interface, map socket -> iface address (IP + mask) 71 | std::map< Ptr, Ipv4InterfaceAddress > m_socketAddresses; 72 | /// Loopback device used to defer RREQ until packet will be fully formed 73 | Ptr m_lo; 74 | 75 | Ptr GetLS (); 76 | void SetLS (Ptr locationService); 77 | 78 | /// Broadcast ID 79 | uint32_t m_requestId; 80 | /// Request sequence number 81 | uint32_t m_seqNo; 82 | 83 | 84 | 85 | /// Number of RREQs used for RREQ rate control 86 | uint16_t m_rreqCount; 87 | Time HelloInterval; 88 | 89 | void SetDownTarget (IpL4Protocol::DownTargetCallback callback); 90 | IpL4Protocol::DownTargetCallback GetDownTarget (void) const; 91 | 92 | virtual void PrintRoutingTable (ns3::Ptr) const 93 | { 94 | return; 95 | } 96 | 97 | 98 | private: 99 | /// Start protocol operation 100 | void Start (); 101 | /// Queue packet and send route request 102 | void DeferredRouteOutput (Ptr p, const Ipv4Header & header, UnicastForwardCallback ucb, ErrorCallback ecb); 103 | /// If route exists and valid, forward packet. 104 | void HelloTimerExpire (); 105 | 106 | /// Queue packet and send route request 107 | Ptr LoopbackRoute (const Ipv4Header & header, Ptr oif); 108 | 109 | /// If route exists and valid, forward packet. 110 | bool Forwarding (Ptr p, const Ipv4Header & header, UnicastForwardCallback ucb, ErrorCallback ecb); 111 | 112 | /// Find socket with local interface address iface 113 | Ptr FindSocketWithInterfaceAddress (Ipv4InterfaceAddress iface) const; 114 | 115 | //Check packet from deffered route output queue and send if position is already available 116 | //returns true if the IP should be erased from the list (was sent/droped) 117 | bool SendPacketFromQueue (Ipv4Address dst); 118 | 119 | //Calls SendPacketFromQueue and re-schedules 120 | void CheckQueue (); 121 | 122 | void RecoveryMode(Ipv4Address dst, Ptr p, UnicastForwardCallback ucb, Ipv4Header header); 123 | 124 | uint32_t MaxQueueLen; ///< The maximum number of packets that we allow a routing protocol to buffer. 125 | Time MaxQueueTime; ///< The maximum period of time that a routing protocol is allowed to buffer a packet for. 126 | RequestQueue m_queue; 127 | 128 | Timer HelloIntervalTimer; 129 | Timer CheckQueueTimer; 130 | uint8_t LocationServiceName; 131 | PositionTable m_neighbors; 132 | bool PerimeterMode; 133 | std::list m_queuedAddresses; 134 | Ptr m_locationService; 135 | 136 | IpL4Protocol::DownTargetCallback m_downTarget; 137 | 138 | 139 | 140 | }; 141 | } 142 | } 143 | #endif /* GPSRROUTINGPROTOCOL_H */ 144 | -------------------------------------------------------------------------------- /project_5/gpsr-edited/gpsr/wscript: -------------------------------------------------------------------------------- 1 | # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- 2 | 3 | # def options(opt): 4 | # pass 5 | 6 | # def configure(conf): 7 | # conf.check_nonfatal(header_name='stdint.h', define_name='HAVE_STDINT_H') 8 | 9 | def build(bld): 10 | module = bld.create_ns3_module('gpsr', ['location-service', 'internet', 'wifi', 'applications', 'mesh', 'point-to-point', 'virtual-net-device']) 11 | module.source = [ 12 | 'model/gpsr-ptable.cc', 13 | 'model/gpsr-rqueue.cc', 14 | 'model/gpsr-packet.cc', 15 | 'model/gpsr.cc', 16 | 'helper/gpsr-helper.cc', 17 | ] 18 | 19 | headers = bld(features=['ns3header']) 20 | headers.module = 'gpsr' 21 | headers.source = [ 22 | 'model/gpsr-ptable.h', 23 | 'model/gpsr-rqueue.h', 24 | 'model/gpsr-packet.h', 25 | 'model/gpsr.h', 26 | 'helper/gpsr-helper.h', 27 | ] 28 | 29 | 30 | # bld.ns3_python_bindings() 31 | 32 | --------------------------------------------------------------------------------