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" |
| 22 | #include <fstream> |
| 23 | #include <cstdlib> |
| 24 | #include <iostream> |
| 25 | #include <sstream> |
| 26 | #include "ns3/log.h" |
| 27 | |
| 28 | using namespace std; |
| 29 | |
| 30 | namespace ns3 |
| 31 | { |
| 32 | NS_LOG_COMPONENT_DEFINE ("AnnotatedTopologyReader"); |
| 33 | |
| 34 | NS_OBJECT_ENSURE_REGISTERED (AnnotatedTopologyReader); |
| 35 | |
| 36 | TypeId AnnotatedTopologyReader::GetTypeId (void) |
| 37 | { |
| 38 | static TypeId tid = TypeId ("ns3::AnnotatedTopologyReader") |
| 39 | .SetParent<Object> () |
| 40 | ; |
| 41 | return tid; |
| 42 | } |
| 43 | |
| 44 | AnnotatedTopologyReader::AnnotatedTopologyReader () |
| 45 | { |
| 46 | NS_LOG_FUNCTION (this); |
| 47 | } |
| 48 | |
| 49 | AnnotatedTopologyReader::~AnnotatedTopologyReader () |
| 50 | { |
| 51 | NS_LOG_FUNCTION (this); |
| 52 | } |
| 53 | |
| 54 | NodeContainer |
| 55 | AnnotatedTopologyReader::Read (void) |
| 56 | { |
| 57 | ifstream topgen; |
| 58 | topgen.open (GetFileName ().c_str ()); |
| 59 | map<string, Ptr<Node> > nodeMap; |
| 60 | NodeContainer nodes; |
| 61 | |
| 62 | if ( !topgen.is_open () ) |
| 63 | { |
| 64 | return nodes; |
| 65 | } |
| 66 | |
| 67 | string from; |
| 68 | string to; |
| 69 | string linkAttr; |
| 70 | |
| 71 | int linksNumber = 0; |
| 72 | int nodesNumber = 0; |
| 73 | |
| 74 | int totnode = 0; |
| 75 | int totlink = 0; |
| 76 | |
| 77 | istringstream lineBuffer; |
| 78 | string line; |
| 79 | |
| 80 | getline (topgen,line); |
| 81 | lineBuffer.str (line); |
| 82 | |
| 83 | lineBuffer >> totnode; |
| 84 | lineBuffer >> totlink; |
| 85 | NS_LOG_INFO ("Annotated topology should have " << totnode << " nodes and " << totlink << " links"); |
| 86 | |
| 87 | if(!topgen.eof ()) |
| 88 | NS_LOG_INFO("!EOF"); |
| 89 | |
| 90 | /*for (int i = 0; i <= totnode; i++) |
| 91 | { |
| 92 | getline (topgen,line); |
| 93 | }*/ |
| 94 | |
| 95 | for (int i = 0; i < totlink && !topgen.eof (); i++) |
| 96 | { |
| 97 | //NS_LOG_INFO("Line #" <<i); |
| 98 | getline (topgen,line); |
| 99 | lineBuffer.clear (); |
| 100 | lineBuffer.str (line); |
| 101 | |
| 102 | lineBuffer >> from; |
| 103 | lineBuffer >> to; |
| 104 | |
| 105 | |
| 106 | if ( (!from.empty ()) && (!to.empty ()) ) |
| 107 | { |
| 108 | NS_LOG_INFO ( linksNumber << " From: " << from << " to: " << to ); |
| 109 | |
| 110 | if ( nodeMap[from] == 0 ) |
| 111 | { |
| 112 | Ptr<Node> tmpNode = CreateObject<Node> (); |
| 113 | nodeMap[from] = tmpNode; |
| 114 | nodes.Add (tmpNode); |
| 115 | nodesNumber++; |
| 116 | } |
| 117 | |
| 118 | if (nodeMap[to] == 0) |
| 119 | { |
| 120 | Ptr<Node> tmpNode = CreateObject<Node> (); |
| 121 | nodeMap[to] = tmpNode; |
| 122 | nodes.Add (tmpNode); |
| 123 | nodesNumber++; |
| 124 | } |
| 125 | |
| 126 | Link link ( nodeMap[from], from, nodeMap[to], to ); |
| 127 | |
| 128 | lineBuffer >> linkAttr; |
| 129 | if ( !linkAttr.empty () ) |
| 130 | { |
| 131 | link.SetAttribute ("DataRate", linkAttr); |
| 132 | } |
| 133 | |
| 134 | lineBuffer >> linkAttr; |
| 135 | if ( !linkAttr.empty () ) |
| 136 | { |
| 137 | link.SetAttribute ("Delay", linkAttr); |
| 138 | } |
| 139 | |
| 140 | lineBuffer >> linkAttr; |
| 141 | if ( !linkAttr.empty () ) |
| 142 | { |
| 143 | link.SetAttribute ("QueueSizeNode1", linkAttr); |
| 144 | } |
| 145 | |
| 146 | lineBuffer >> linkAttr; |
| 147 | if ( !linkAttr.empty () ) |
| 148 | { |
| 149 | link.SetAttribute ("QueueSizeNode2", linkAttr); |
| 150 | } |
| 151 | |
| 152 | AddLink (link); |
| 153 | |
| 154 | linksNumber++; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | NS_LOG_INFO ("Annotated topology created with " << nodesNumber << " nodes and " << linksNumber << " links"); |
| 159 | topgen.close (); |
| 160 | |
| 161 | return nodes; |
| 162 | } |
| 163 | |
| 164 | } /* namespace ns3 */ |
| 165 | |