Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -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 "annotated-topology-reader.h" |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 22 | |
| 23 | using namespace std; |
| 24 | |
| 25 | namespace ns3 |
| 26 | { |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 27 | |
| 28 | NS_LOG_COMPONENT_DEFINE ("AnnotatedTopologyReader"); |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 29 | |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 30 | NS_OBJECT_ENSURE_REGISTERED (AnnotatedTopologyReader); |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 31 | |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 32 | TypeId AnnotatedTopologyReader::GetTypeId (void) |
| 33 | { |
| 34 | static TypeId tid = TypeId ("ns3::AnnotatedTopologyReader") |
| 35 | .SetParent<Object> () |
| 36 | ; |
| 37 | return tid; |
| 38 | } |
| 39 | |
| 40 | AnnotatedTopologyReader::AnnotatedTopologyReader () |
| 41 | { |
| 42 | NS_LOG_FUNCTION (this); |
| 43 | } |
| 44 | |
| 45 | AnnotatedTopologyReader::~AnnotatedTopologyReader () |
| 46 | { |
| 47 | NS_LOG_FUNCTION (this); |
| 48 | } |
| 49 | |
| 50 | NodeContainer |
| 51 | AnnotatedTopologyReader::Read (void) |
| 52 | { |
| 53 | ifstream topgen; |
| 54 | topgen.open (GetFileName ().c_str ()); |
| 55 | map<string, Ptr<Node> > nodeMap; |
| 56 | NodeContainer nodes; |
| 57 | |
| 58 | if ( !topgen.is_open () ) |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 59 | { |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 60 | return nodes; |
| 61 | } |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 62 | |
| 63 | string from; |
| 64 | string to; |
| 65 | string linkAttr; |
| 66 | |
| 67 | int linksNumber = 0; |
| 68 | int nodesNumber = 0; |
| 69 | |
| 70 | int totnode = 0; |
| 71 | int totlink = 0; |
| 72 | |
| 73 | istringstream lineBuffer; |
| 74 | string line; |
| 75 | |
| 76 | getline (topgen,line); |
| 77 | lineBuffer.str (line); |
| 78 | |
| 79 | lineBuffer >> totnode; |
| 80 | lineBuffer >> totlink; |
| 81 | NS_LOG_INFO ("Annotated topology should have " << totnode << " nodes and " << totlink << " links"); |
| 82 | |
| 83 | if(!topgen.eof ()) |
| 84 | NS_LOG_INFO("!EOF"); |
| 85 | |
| 86 | |
| 87 | for (int i = 0; i < totlink && !topgen.eof (); i++) |
| 88 | { |
| 89 | //NS_LOG_INFO("Line #" <<i); |
| 90 | getline (topgen,line); |
| 91 | lineBuffer.clear (); |
| 92 | lineBuffer.str (line); |
| 93 | |
| 94 | lineBuffer >> from; |
| 95 | lineBuffer >> to; |
| 96 | |
| 97 | |
| 98 | if ( (!from.empty ()) && (!to.empty ()) ) |
| 99 | { |
| 100 | NS_LOG_INFO ( linksNumber << " From: " << from << " to: " << to ); |
| 101 | |
| 102 | if ( nodeMap[from] == 0 ) |
| 103 | { |
| 104 | Ptr<Node> tmpNode = CreateObject<Node> (); |
| 105 | nodeMap[from] = tmpNode; |
| 106 | nodes.Add (tmpNode); |
| 107 | nodesNumber++; |
| 108 | } |
| 109 | |
| 110 | if (nodeMap[to] == 0) |
| 111 | { |
| 112 | Ptr<Node> tmpNode = CreateObject<Node> (); |
| 113 | nodeMap[to] = tmpNode; |
| 114 | nodes.Add (tmpNode); |
| 115 | nodesNumber++; |
| 116 | } |
| 117 | |
| 118 | Link link ( nodeMap[from], from, nodeMap[to], to ); |
| 119 | |
| 120 | lineBuffer >> linkAttr; |
| 121 | if ( !linkAttr.empty () ) |
| 122 | { |
| 123 | link.SetAttribute ("DataRate", linkAttr); |
| 124 | } |
| 125 | |
| 126 | lineBuffer >> linkAttr; |
| 127 | if ( !linkAttr.empty () ) |
| 128 | { |
| 129 | link.SetAttribute ("Delay", linkAttr); |
| 130 | } |
| 131 | |
| 132 | lineBuffer >> linkAttr; |
| 133 | if ( !linkAttr.empty () ) |
| 134 | { |
| 135 | link.SetAttribute ("QueueSizeNode1", linkAttr); |
| 136 | } |
| 137 | |
| 138 | lineBuffer >> linkAttr; |
| 139 | if ( !linkAttr.empty () ) |
| 140 | { |
| 141 | link.SetAttribute ("QueueSizeNode2", linkAttr); |
| 142 | } |
| 143 | |
| 144 | AddLink (link); |
| 145 | |
| 146 | linksNumber++; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | NS_LOG_INFO ("Annotated topology created with " << nodesNumber << " nodes and " << linksNumber << " links"); |
| 151 | topgen.close (); |
| 152 | |
| 153 | return nodes; |
| 154 | } |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 155 | |
Ilya Moiseenko | 7dd43be | 2011-08-18 18:57:12 -0700 | [diff] [blame] | 156 | void |
| 157 | AnnotatedTopologyReader::ApplySettings(NetDeviceContainer* ndc, NodeContainer* nc) |
| 158 | { |
| 159 | PointToPointHelper p2p; |
| 160 | TopologyReader::ConstLinksIterator iter2; |
| 161 | int i = 0; |
| 162 | for ( iter2 = this->LinksBegin (); iter2 != this->LinksEnd (); iter2++, i++ ) |
| 163 | { |
| 164 | /*std::string dataRate = iter2->GetAttribute("DataRate"); |
| 165 | NS_LOG_INFO("dataRate = "<<dataRate); |
| 166 | dataRate += "Kbps"; |
| 167 | std::string delay = iter2->GetAttribute("Delay"); |
| 168 | NS_LOG_INFO("delay = "<<delay); |
| 169 | delay += "ms";*/ |
| 170 | |
| 171 | p2p.SetDeviceAttribute("DataRate", StringValue(iter2->GetAttribute("DataRate")+"Kbps")); |
| 172 | NS_LOG_INFO("DataRate = " + iter2->GetAttribute("DataRate")+"Kbps"); |
| 173 | p2p.SetChannelAttribute("Delay", StringValue(iter2->GetAttribute("Delay")+"ms")); |
| 174 | NS_LOG_INFO("Delay = " + iter2->GetAttribute("Delay")+"ms"); |
| 175 | p2p.SetQueue("ns3::DropTailQueue","MaxPackets",StringValue("100")); |
| 176 | ndc[i] = p2p.Install(nc[i]); |
| 177 | |
| 178 | |
| 179 | NodeContainer twoNodes = nc[i]; |
| 180 | |
| 181 | Ptr<Node> nd = twoNodes.Get(0); |
| 182 | if(nd==NULL) |
| 183 | NS_LOG_INFO("nd = null"); |
| 184 | |
| 185 | Ptr<Node> nd2 = twoNodes.Get(1); |
| 186 | if(nd2==NULL) |
| 187 | NS_LOG_INFO("nd2 = null"); |
| 188 | |
| 189 | //NS_LOG_INFO("1"); |
| 190 | NS_LOG_INFO("#netdevices = " << nd->GetNDevices()); |
| 191 | NS_LOG_INFO("#netdevices = " << nd2->GetNDevices()); |
| 192 | |
| 193 | Ptr<PointToPointNetDevice> device = nd->GetDevice(nd->GetNDevices()-1)->GetObject<PointToPointNetDevice> (); |
| 194 | |
| 195 | if(device==NULL) |
| 196 | NS_LOG_INFO("device = 0"); |
| 197 | |
| 198 | //NS_LOG_INFO("2"); |
| 199 | |
| 200 | Ptr<PointToPointNetDevice> device2 = nd2->GetDevice(nd2->GetNDevices()-1)->GetObject<PointToPointNetDevice> (); |
| 201 | |
| 202 | if(device2==NULL) |
| 203 | NS_LOG_INFO("device2 = 0"); |
| 204 | |
| 205 | PointerValue tmp1; |
| 206 | device->GetAttribute ("TxQueue", tmp1); |
| 207 | //NS_LOG_INFO("2.5"); |
| 208 | Ptr<Object> txQueue1 = tmp1.GetObject (); |
| 209 | |
| 210 | PointerValue tmp2; |
| 211 | device2->GetAttribute ("TxQueue", tmp2); |
| 212 | Ptr<Object> txQueue2 = tmp2.GetObject (); |
| 213 | //NS_LOG_INFO("3"); |
| 214 | Ptr<DropTailQueue> dtq1 = txQueue1->GetObject <DropTailQueue> (); |
| 215 | NS_ASSERT (dtq1 != 0); |
| 216 | |
| 217 | Ptr<DropTailQueue> dtq2 = txQueue2->GetObject <DropTailQueue> (); |
| 218 | NS_ASSERT (dtq2 != 0); |
| 219 | |
| 220 | std::string queuesize1 = iter2->GetAttribute("QueueSizeNode1"); |
| 221 | std::string queuesize2 = iter2->GetAttribute("QueueSizeNode2"); |
| 222 | //NS_LOG_INFO("4"); |
| 223 | txQueue1->SetAttribute("MaxPackets", UintegerValue (atoi(queuesize1.c_str()))); |
| 224 | txQueue2->SetAttribute("MaxPackets", UintegerValue (atoi(queuesize2.c_str()))); |
| 225 | |
| 226 | UintegerValue limit; |
| 227 | txQueue1->GetAttribute ("MaxPackets", limit); |
| 228 | NS_LOG_INFO ("NetDevice #"<< device->GetIfIndex() << "has queue limit " << limit.Get () << " packets"); |
| 229 | |
| 230 | txQueue2->GetAttribute ("MaxPackets", limit); |
| 231 | NS_LOG_INFO ("NetDevice #"<< device2->GetIfIndex() << "has queue limit " << limit.Get () << " packets"); |
| 232 | } |
| 233 | } |
| 234 | } |
Ilya Moiseenko | 1eff17d | 2011-08-17 10:55:53 -0700 | [diff] [blame] | 235 | |