Ilya Moiseenko | f9a4fb4 | 2011-08-17 10:56:29 -0700 | [diff] [blame^] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 University of California, Los Angeles |
| 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 | * Author: Ilya Moiseenko <iliamo@cs.ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #include <ctime> |
| 22 | #include <sstream> |
| 23 | |
| 24 | #include "ns3/core-module.h" |
| 25 | #include "ns3/network-module.h" |
| 26 | #include "ns3/internet-module.h" |
| 27 | #include "ns3/point-to-point-module.h" |
| 28 | #include "ns3/applications-module.h" |
| 29 | #include "ns3/ipv4-static-routing-helper.h" |
| 30 | #include "ns3/ipv4-list-routing-helper.h" |
| 31 | #include "ns3/annotated-topology-reader.h" |
| 32 | #include <list> |
| 33 | #include "ns3/visualizer-module.h" |
| 34 | |
| 35 | using namespace ns3; |
| 36 | using namespace std; |
| 37 | |
| 38 | NS_LOG_COMPONENT_DEFINE ("AnnotatedTopologyReadingExample"); |
| 39 | |
| 40 | int main (int argc, char *argv[]) |
| 41 | { |
| 42 | //Packet::EnablePrinting(); |
| 43 | GlobalValue::Bind ("SimulatorImplementationType", StringValue |
| 44 | ("ns3::VisualSimulatorImpl")); |
| 45 | |
| 46 | string input ("/Users/iliamo/ns3-abstract-ndn/ns-3.11/src/NDNabstraction/examples/simpletopology.txt"); |
| 47 | |
| 48 | // Set up command line parameters used to control the experiment. |
| 49 | //CommandLine cmd; |
| 50 | //cmd.AddValue ("input", "Name of the input file.", |
| 51 | // input); |
| 52 | //cmd.Parse (argc, argv); |
| 53 | |
| 54 | |
| 55 | // ------------------------------------------------------------ |
| 56 | // -- Read topology data. |
| 57 | // -------------------------------------------- |
| 58 | NodeContainer nodes; |
| 59 | |
| 60 | Ptr<AnnotatedTopologyReader> inFile = CreateObject<AnnotatedTopologyReader> (); |
| 61 | inFile->SetFileName (input); |
| 62 | |
| 63 | if (inFile != 0) |
| 64 | { |
| 65 | nodes = inFile->Read (); |
| 66 | } |
| 67 | |
| 68 | if (inFile->LinksSize () == 0) |
| 69 | { |
| 70 | NS_LOG_ERROR ("Problems reading the topology file. Failing."); |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | // ------------------------------------------------------------ |
| 75 | // -- Create nodes and network stacks |
| 76 | // -------------------------------------------- |
| 77 | NS_LOG_INFO ("creating internet stack"); |
| 78 | InternetStackHelper stack; |
| 79 | |
| 80 | |
| 81 | //routing |
| 82 | Ipv4StaticRoutingHelper staticRouting; |
| 83 | Ipv4ListRoutingHelper listRH; |
| 84 | listRH.Add (staticRouting, 0); |
| 85 | stack.SetRoutingHelper (listRH); // has effect on the next Install () |
| 86 | stack.Install (nodes); |
| 87 | |
| 88 | NS_LOG_INFO ("creating ip4 addresses"); |
| 89 | Ipv4AddressHelper address; |
| 90 | address.SetBase ("10.0.0.0", "255.255.255.252"); |
| 91 | |
| 92 | int totlinks = inFile->LinksSize (); |
| 93 | |
| 94 | NS_LOG_INFO ("creating node containers"); |
| 95 | NodeContainer* nc = new NodeContainer[totlinks]; |
| 96 | TopologyReader::ConstLinksIterator iter; |
| 97 | int i = 0; |
| 98 | for ( iter = inFile->LinksBegin (); iter != inFile->LinksEnd (); iter++, i++ ) |
| 99 | { |
| 100 | nc[i] = NodeContainer (iter->GetFromNode (), iter->GetToNode ()); |
| 101 | } |
| 102 | |
| 103 | NS_LOG_INFO ("creating net device containers"); |
| 104 | ObjectFactory m_queueFactory; |
| 105 | m_queueFactory.SetTypeId("ns3::DropTailQueue"); |
| 106 | //m_queueFactory.Set("Mode", |
| 107 | NetDeviceContainer* ndc = new NetDeviceContainer[totlinks]; |
| 108 | PointToPointHelper p2p; |
| 109 | TopologyReader::ConstLinksIterator iter2; |
| 110 | i = 0; |
| 111 | for ( iter2 = inFile->LinksBegin (); iter2 != inFile->LinksEnd (); iter2++, i++ ) |
| 112 | { |
| 113 | std::string dataRate = iter2->GetAttribute("DataRate"); |
| 114 | NS_LOG_INFO("dataRate = "<<dataRate); |
| 115 | dataRate += "Kbps"; |
| 116 | std::string delay = iter2->GetAttribute("Delay"); |
| 117 | NS_LOG_INFO("delay = "<<delay); |
| 118 | delay += "ms"; |
| 119 | p2p.SetDeviceAttribute("DataRate", StringValue(dataRate)); |
| 120 | p2p.SetChannelAttribute("Delay", StringValue(delay)); |
| 121 | p2p.SetQueue("ns3::DropTailQueue","MaxPackets",StringValue("100")); |
| 122 | ndc[i] = p2p.Install(nc[i]); |
| 123 | |
| 124 | NodeContainer twoNodes = nc[i]; |
| 125 | |
| 126 | |
| 127 | Ptr<Node> nd = twoNodes.Get(0); |
| 128 | if(nd==NULL) |
| 129 | NS_LOG_INFO("nd = null"); |
| 130 | Ptr<Node> nd2 = twoNodes.Get(1); |
| 131 | if(nd2==NULL) |
| 132 | NS_LOG_INFO("nd2 = null"); |
| 133 | //NS_LOG_INFO("1"); |
| 134 | NS_LOG_INFO("#netdevices = " << nd->GetNDevices()); |
| 135 | NS_LOG_INFO("#netdevices = " << nd2->GetNDevices()); |
| 136 | |
| 137 | Ptr<PointToPointNetDevice> device = nd->GetDevice(nd->GetNDevices()-1)->GetObject<PointToPointNetDevice> (); |
| 138 | if(device==NULL) |
| 139 | NS_LOG_INFO("device = 0"); |
| 140 | |
| 141 | //NS_LOG_INFO("2"); |
| 142 | |
| 143 | Ptr<PointToPointNetDevice> device2 = nd2->GetDevice(nd2->GetNDevices()-1)->GetObject<PointToPointNetDevice> (); |
| 144 | ` |
| 145 | if(device2==NULL) |
| 146 | NS_LOG_INFO("device2 = 0"); |
| 147 | |
| 148 | PointerValue tmp1; |
| 149 | device->GetAttribute ("TxQueue", tmp1); |
| 150 | //NS_LOG_INFO("2.5"); |
| 151 | Ptr<Object> txQueue1 = tmp1.GetObject (); |
| 152 | |
| 153 | PointerValue tmp2; |
| 154 | device2->GetAttribute ("TxQueue", tmp2); |
| 155 | Ptr<Object> txQueue2 = tmp2.GetObject (); |
| 156 | //NS_LOG_INFO("3"); |
| 157 | Ptr<DropTailQueue> dtq1 = txQueue1->GetObject <DropTailQueue> (); |
| 158 | NS_ASSERT (dtq1 != 0); |
| 159 | |
| 160 | Ptr<DropTailQueue> dtq2 = txQueue2->GetObject <DropTailQueue> (); |
| 161 | NS_ASSERT (dtq2 != 0); |
| 162 | |
| 163 | std::string queuesize1 = iter2->GetAttribute("QueueSizeNode1"); |
| 164 | std::string queuesize2 = iter2->GetAttribute("QueueSizeNode2"); |
| 165 | //NS_LOG_INFO("4"); |
| 166 | txQueue1->SetAttribute("MaxPackets", UintegerValue (atoi(queuesize1.c_str()))); |
| 167 | txQueue2->SetAttribute("MaxPackets", UintegerValue (atoi(queuesize2.c_str()))); |
| 168 | |
| 169 | UintegerValue limit; |
| 170 | txQueue1->GetAttribute ("MaxPackets", limit); |
| 171 | NS_LOG_INFO ("txQueue1 limit changed: " << limit.Get () << " packets"); |
| 172 | |
| 173 | txQueue2->GetAttribute ("MaxPackets", limit); |
| 174 | NS_LOG_INFO ("txQueue2 limit changed: " << limit.Get () << " packets"); |
| 175 | |
| 176 | |
| 177 | /* //bauman way of doing things |
| 178 | m_queueFactory.Set("MaxPackets",StringValue(iter2->GetAttribute("QueueSizeNode1"))); |
| 179 | NS_LOG_INFO("2.5"); |
| 180 | Ptr<DropTailQueue> queue = m_queueFactory.Create<DropTailQueue>(); |
| 181 | queue->SetMode(ns3::DropTailQueue::PACKETS); |
| 182 | NS_LOG_INFO("2,8"); |
| 183 | device->SetQueue(queue); |
| 184 | NS_LOG_INFO("3"); |
| 185 | m_queueFactory.Set("MaxPackets", StringValue(iter2->GetAttribute("QueueSizeNode2"))); |
| 186 | |
| 187 | Ptr<DropTailQueue> queue2 = m_queueFactory.Create<DropTailQueue>(); |
| 188 | queue2->SetMode(ns3::DropTailQueue::PACKETS); |
| 189 | device2->SetQueue(queue2); |
| 190 | */ |
| 191 | } |
| 192 | |
| 193 | // it creates little subnets, one for each couple of nodes. |
| 194 | NS_LOG_INFO ("creating ipv4 interfaces"); |
| 195 | Ipv4InterfaceContainer* ipic = new Ipv4InterfaceContainer[totlinks]; |
| 196 | for (int i = 0; i < totlinks; i++) |
| 197 | { |
| 198 | ipic[i] = address.Assign (ndc[i]); |
| 199 | address.NewNetwork (); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | /* |
| 208 | NS_LOG_INFO ("Create Applications."); |
| 209 | uint16_t port = 9; // Discard port (RFC 863) |
| 210 | |
| 211 | std::string sendsizeattr = "SendSize"; |
| 212 | //flow2 7-->2 |
| 213 | BulkSendHelper bulksend0 ("ns3::TcpSocketFactory", InetSocketAddress (ipic[1].GetAddress (0), port)); |
| 214 | //bulksend0.SetAttribute(sendsizeattr, AttributeValue(ConstantVariable(2560))); |
| 215 | bulksend0.SetAttribute("MaxBytes", UintegerValue(2560)); |
| 216 | ApplicationContainer apps = bulksend0.Install(nc[6]); |
| 217 | apps.Start(Seconds (1.0)); |
| 218 | apps.Stop(Seconds (10.0)); |
| 219 | |
| 220 | // Create a packet sink to receive these packets |
| 221 | PacketSinkHelper sink0 ("ns3::UdpSocketFactory", InetSocketAddress(Ipv4Address::GetAny (), port)); |
| 222 | apps = sink0.Install(nc[1]); |
| 223 | apps.Start(Seconds(0.0)); |
| 224 | apps.Stop(Seconds(20.0)); |
| 225 | |
| 226 | //flow1 1-->6 |
| 227 | BulkSendHelper bulksend ("ns3::TcpSocketFactory", InetSocketAddress (ipic[5].GetAddress (1), port)); |
| 228 | //bulksend.SetAttribute(sendsizeattr, AttributeValue( ConstantVariable(2560))); |
| 229 | bulksend0.SetAttribute("MaxBytes", UintegerValue(2560)); |
| 230 | apps = bulksend.Install (nc[0]); |
| 231 | apps.Start (Seconds (6.0)); |
| 232 | apps.Stop (Seconds (20.0)); |
| 233 | |
| 234 | // Create a packet sink to receive these packets |
| 235 | PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port)); |
| 236 | apps = sink.Install (nc[5]); |
| 237 | apps.Start(Seconds(0.0)); |
| 238 | apps.Stop(Seconds(20.0)); |
| 239 | |
| 240 | AsciiTraceHelper ascii; |
| 241 | p2p.EnableAsciiAll (ascii.CreateFileStream ("annotated-topology-read.tr")); |
| 242 | p2p.EnablePcapAll ("annotated-topology-read"); |
| 243 | |
| 244 | */ |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | // ------------------------------------------------------------ |
| 254 | // -- Run the simulation |
| 255 | // -------------------------------------------- |
| 256 | NS_LOG_INFO ("Run Simulation."); |
| 257 | Simulator::Stop (Seconds (20)); |
| 258 | Simulator::Run (); |
| 259 | Simulator::Destroy (); |
| 260 | |
| 261 | delete[] ipic; |
| 262 | delete[] ndc; |
| 263 | delete[] nc; |
| 264 | |
| 265 | NS_LOG_INFO ("Done."); |
| 266 | |
| 267 | return 0; |
| 268 | |
| 269 | // end main |
| 270 | } |