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 Entry[maxIf+1];
18  ifaces = new ListPair(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  Entry& e = getEntry(iface);
37  e.ipa = ipa; e.port = ipp;
38  e.rates = e.availRates = rs;
39  return true;
40 }
41 
46 void IfaceTable::removeEntry(int iface) {
47  if (ifaces->isIn(iface)) ifaces->swap(iface);
48  if (iface == defaultIf) defaultIf = 0;
49 }
50 
65 int IfaceTable::readEntry(istream& in) {
66  int ifnum; ipa_t ipa; int port; RateSpec rs;
67 
68  Util::skipBlank(in);
69  if (!Util::readInt(in,ifnum) || !Np4d::readIpAdr(in,ipa) ||
70  !Util::readInt(in,port) || !rs.read(in)) {
71  return 0;
72  }
73  Util::nextLine(in);
74 
75  if (!addEntry(ifnum,ipa,port,rs)) return 0;
76  return ifnum;
77 }
78 
89 bool IfaceTable::read(istream& in) {
90  int num;
91  Util::skipBlank(in);
92  if (!Util::readInt(in,num)) return false;
93  Util::nextLine(in);
94  for (int i = 1; i <= num; i++) {
95  if (readEntry(in) == 0) {
96  cerr << "IfaceTable::read: Error in "
97  << i << "-th entry read from input\n";
98  return false;
99  }
100  }
101  return true;
102 }
103 
108 string IfaceTable::entry2string(int iface) const {
109  stringstream ss;
110  ss << setw(5) << iface << " " << Np4d::ip2string(ift[iface].ipa)
111  << " " << ift[iface].port << " "
112  << ift[iface].rates.toString() << endl;
113  return ss.str();
114 }
115 
120 string IfaceTable::toString() const {
121  stringstream ss;
122  ss << ifaces->getNumIn() << endl;
123  ss << "# iface ipAddress bitRate pktRate\n";
124  for (int i = firstIface(); i != 0; i = nextIface(i))
125  ss << entry2string(i);
126  return ss.str();
127 }
128 
129 } // ends namespace
130