28 #include "ns3/unused.h"
29 #include "rocketfuel-topology-reader.h"
35 NS_OBJECT_ENSURE_REGISTERED (RocketfuelTopologyReader);
37 TypeId RocketfuelTopologyReader::GetTypeId (
void)
39 static TypeId tid = TypeId (
"ns3::RocketfuelTopologyReader")
45 RocketfuelTopologyReader::RocketfuelTopologyReader ()
50 RocketfuelTopologyReader::~RocketfuelTopologyReader ()
57 #define REGMATCH_MAX 16
61 #define SPACE "[ \t]+"
62 #define MAYSPACE "[ \t]*"
64 #define ROCKETFUEL_MAPS_LINE \
65 START "(-*[0-9]+)" SPACE "(@[?A-Za-z0-9,+]+)" SPACE \
66 "(\\+)*" MAYSPACE "(bb)*" MAYSPACE \
67 "\\(([0-9]+)\\)" SPACE "(&[0-9]+)*" MAYSPACE \
68 "->" MAYSPACE "(<[0-9 \t<>]+>)*" MAYSPACE \
69 "(\\{-[0-9\\{\\} \t-]+\\})*" SPACE \
70 "=([A-Za-z0-9.!-]+)" SPACE "r([0-9])" \
73 #define ROCKETFUEL_WEIGHTS_LINE \
74 START "([^ \t]+)" SPACE "([^ \t]+)" SPACE "([0-9.]+)" MAYSPACE END
78 std::map<std::string, Ptr<Node> > nodeMap;
81 PrintNodeInfo (std::string & uid, std::string & loc,
bool dns,
bool bb,
82 std::vector <std::string>::size_type neighListSize,
83 std::string & name,
int radius)
86 NS_LOG_INFO (
"Load Node[" << uid <<
"]: location: " << loc <<
" dns: " << dns
87 <<
" bb: " << bb <<
" neighbors: " << neighListSize
88 <<
"(" <<
"%d" <<
") externals: \"%s\"(%d) "
89 <<
"name: " << name <<
" radius: " << radius);
93 RocketfuelTopologyReader::GenerateFromMapsFile (
int argc,
char *argv[])
103 unsigned int num_neigh = 0;
105 std::vector <std::string> neigh_list;
121 num_neigh_s = ::atoi (argv[4]);
125 NS_LOG_WARN (
"Negative number of neighbors given");
129 num_neigh = num_neigh_s;
136 char *stringp = argv[6];
137 while ((nbr = strsep (&stringp,
" \t")) != NULL)
139 nbr[strlen (nbr) - 1] =
'\0';
140 neigh_list.push_back (nbr + 1);
143 if (num_neigh != neigh_list.size ())
145 NS_LOG_WARN (
"Given number of neighbors = " << num_neigh <<
" != size of neighbors list = " << neigh_list.size ());
160 radius = ::atoi (&argv[9][1]);
166 PrintNodeInfo (uid, loc, dns, bb, neigh_list.size (), name, radius);
171 if (nodeMap[uid] == 0)
173 Ptr<Node> tmpNode = CreateObject<Node> ();
174 nodeMap[uid] = tmpNode;
179 for (uint32_t i = 0; i < neigh_list.size (); ++i)
181 nuid = neigh_list[i];
188 if (nodeMap[nuid] == 0)
190 Ptr<Node> tmpNode = CreateObject<Node> ();
191 nodeMap[nuid] = tmpNode;
195 NS_LOG_INFO (linksNumber <<
":" << nodesNumber <<
" From: " << uid <<
" to: " << nuid);
196 Link link (nodeMap[uid], uid, nodeMap[nuid], nuid);
205 RocketfuelTopologyReader::GenerateFromWeightsFile (
int argc,
char *argv[])
215 double v = strtod (argv[2], &endptr);
224 if (!sname.empty () && !tname.empty ())
226 if (nodeMap[sname] == 0)
228 Ptr<Node> tmpNode = CreateObject<Node> ();
229 nodeMap[sname] = tmpNode;
234 if (nodeMap[tname] == 0)
236 Ptr<Node> tmpNode = CreateObject<Node> ();
237 nodeMap[tname] = tmpNode;
241 NS_LOG_INFO (linksNumber <<
":" << nodesNumber <<
" From: " << sname <<
" to: " << tname);
246 if ((iter->GetFromNode () == nodeMap[tname])
247 && (iter->GetToNode () == nodeMap[sname]))
256 Link link (nodeMap[sname], sname, nodeMap[tname], tname);
264 enum RocketfuelTopologyReader::RF_FileType
265 RocketfuelTopologyReader::GetFileType (
const char *line)
268 regmatch_t regmatch[REGMATCH_MAX];
273 ret = regcomp (®ex, ROCKETFUEL_MAPS_LINE, REG_EXTENDED | REG_NEWLINE);
276 regerror (ret, ®ex, errbuf,
sizeof (errbuf));
279 ret = regexec (®ex, line, REGMATCH_MAX, regmatch, 0);
280 if (ret != REG_NOMATCH)
288 ret = regcomp (®ex, ROCKETFUEL_WEIGHTS_LINE, REG_EXTENDED | REG_NEWLINE);
291 regerror (ret, ®ex, errbuf,
sizeof (errbuf));
294 ret = regexec (®ex, line, REGMATCH_MAX, regmatch, 0);
295 if (ret != REG_NOMATCH)
309 std::ifstream topgen;
313 std::istringstream lineBuffer;
316 enum RF_FileType ftype = RF_UNKNOWN;
319 if (!topgen.is_open ())
325 while (!topgen.eof ())
329 char *argv[REGMATCH_MAX];
336 getline (topgen, line);
337 buf = (
char *)line.c_str ();
341 ftype = GetFileType (buf);
342 if (ftype == RF_UNKNOWN)
349 regmatch_t regmatch[REGMATCH_MAX];
352 if (ftype == RF_MAPS)
354 ret = regcomp (®ex, ROCKETFUEL_MAPS_LINE, REG_EXTENDED | REG_NEWLINE);
357 regerror (ret, ®ex, errbuf,
sizeof (errbuf));
362 ret = regexec (®ex, buf, REGMATCH_MAX, regmatch, 0);
363 if (ret == REG_NOMATCH)
365 NS_LOG_WARN (
"match failed (maps file): %s" << buf);
370 else if (ftype == RF_WEIGHTS)
372 ret = regcomp (®ex, ROCKETFUEL_WEIGHTS_LINE, REG_EXTENDED | REG_NEWLINE);
375 regerror (ret, ®ex, errbuf,
sizeof (errbuf));
380 ret = regexec (®ex, buf, REGMATCH_MAX, regmatch, 0);
381 if (ret == REG_NOMATCH)
383 NS_LOG_WARN (
"match failed (weights file): %s" << buf);
393 for (
int i = 1; i < REGMATCH_MAX; i++)
395 if (regmatch[i].rm_so == -1)
401 line[regmatch[i].rm_eo] =
'\0';
402 argv[i - 1] = &line[regmatch[i].rm_so];
407 if (ftype == RF_MAPS)
409 nodes.
Add (GenerateFromMapsFile (argc, argv));
411 else if (ftype == RF_WEIGHTS)
413 nodes.
Add (GenerateFromWeightsFile (argc, argv));
417 NS_LOG_WARN (
"Unsupported file format (only Maps/Weights are supported)");
426 NS_LOG_INFO (
"Rocketfuel topology created with " << nodesNumber <<
" nodes and " << linksNumber <<
" links");
#define NS_LOG_FUNCTION(parameters)
ConstLinksIterator LinksBegin(void) const
Returns an iterator to the the first link in this block.
std::string GetFileName(void) const
Returns the input file name.
#define NS_LOG_COMPONENT_DEFINE(name)
void AddLink(Link link)
Adds a link to the topology.
std::list< Link >::const_iterator ConstLinksIterator
Constant iterator to the list of the links.
keep track of a set of node pointers.
virtual NodeContainer Read(void)
Main topology reading function.
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
ConstLinksIterator LinksEnd(void) const
Returns an iterator to the the last link in this block.