forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
IfaceTable.cpp
1 
9 #include "IfaceTable.h"
10 
11 namespace forest {
12 
13 
16 IfaceTable::IfaceTable(int maxIf1) : maxIf(maxIf1) {
17  ift = new IfaceInfo[maxIf+1];
18  ifaces = new UiSetPair(maxIf);
19  defaultIf = 0;
20 }
21 
23 IfaceTable::~IfaceTable() { delete [] ift; delete ifaces; }
24 
31 bool IfaceTable::addEntry(int iface, ipa_t ipa, ipp_t ipp, RateSpec& rs) {
32  if (!ifaces->isOut(iface)) return false;
33  if (ifaces->firstIn() == 0) // this is the first iface
34  defaultIf = iface;
35  ifaces->swap(iface);
36  ift[iface].ipa = ipa; ift[iface].port = ipp;
37  ift[iface].rates = ift[iface].availRates = rs;
38  return true;
39 }
40 
45 void IfaceTable::removeEntry(int iface) {
46  if (ifaces->isIn(iface)) ifaces->swap(iface);
47  if (iface == defaultIf) defaultIf = 0;
48 }
49 
64 int IfaceTable::readEntry(istream& in) {
65  int ifnum; ipa_t ipa; RateSpec rs;
66 
67  Misc::skipBlank(in);
68  if (!Misc::readNum(in,ifnum) || !Np4d::readIpAdr(in,ipa) ||
69  !rs.read(in)) {
70  return 0;
71  }
72  Misc::cflush(in,'\n');
73 
74  if (!addEntry(ifnum,ipa,0,rs)) return 0;
75  return ifnum;
76 }
77 
88 bool IfaceTable::read(istream& in) {
89  int num;
90  Misc::skipBlank(in);
91  if (!Misc::readNum(in,num)) return false;
92  Misc::cflush(in,'\n');
93  for (int i = 1; i <= num; i++) {
94  if (readEntry(in) == 0) {
95  cerr << "IfaceTable::read: Error in "
96  << i << "-th entry read from input\n";
97  return false;
98  }
99  }
100  return true;
101 }
102 
108 string& IfaceTable::entry2string(int iface, string& s) const {
109  stringstream ss;
110  ss << setw(5) << iface << " "
111  << Np4d::ip2string(ift[iface].ipa,s) << " ";
112  ss << ift[iface].rates.toString(s) << endl;
113  s = ss.str();
114  return s;
115 }
116 
122 string& IfaceTable::toString(string& s) const {
123  stringstream ss;
124  ss << ifaces->getNumIn() << endl;
125  ss << "# iface ipAddress bitRate pktRate\n";
126  for (int i = firstIface(); i != 0; i = nextIface(i))
127  ss << entry2string(i,s);
128  s = ss.str();
129  return s;
130 }
131 
132 } // ends namespace
133