RINASim  October 2016
Documentation of framework for OMNeT++
Stats.cc
Go to the documentation of this file.
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2015-2016 TSSG, Waterford Institute of Technology, PRISTINE project
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 
23 #include "Stats.h"
24 
25 
27  {
28  m_nCols = 0; m_nRows = 0;
29 
30  filename = "stats.txt";
31  delim = ',';
32  this->open( );
33  }
34 
36  {
37  clear();
38  }
39 
40  bool Stats::open()
41  {
42  std::ifstream file( filename );
43 
44  clear();
45  if ( file.is_open() )
46  {
47  open_now( file, delim );
48  file.close();
49  return true;
50  }
51 
52  return false;
53  }
54 
55  /* Load the file in MAP data structure */
56  void Stats::open_now( std::istream& istream, char delim = ',' )
57  {
58  std::string line;
59 
60  clear();
61  while ( std::getline( istream, line ) )
62  {
63  unsigned int nCol = 0;
64 
65  std::istringstream lineStream(line);
66  std::string cell;
67 
68  while( std::getline( lineStream, cell, delim ) )
69  {
70  m_oData[std::make_pair( nCol, m_nRows )] = trim( cell );
71  nCol++;
72  }
73  m_nCols = std::max( m_nCols, nCol );
74  m_nRows++;
75  }
76  }
77 
78  bool Stats::save()
79  {
80  std::ofstream ofile( filename );
81  if ( ofile.is_open() )
82  {
83  save_now(ofile, delim );
84  ofile.close();
85  return true;
86  }
87  return false;
88  }
89 
90  /* save the contents of MAP data structure into a txt file */
91  void Stats::save_now( std::ostream& ostream, char delim = ',' )
92  {
93  for ( unsigned int nRow = 0; nRow < m_nRows; nRow++ )
94  {
95  for ( unsigned int nCol = 0; nCol < m_nCols; nCol++ )
96  {
97  ostream << trim( m_oData[std::make_pair( nCol, nRow )] );
98  if ( (nCol+1) < m_nCols )
99  {
100  ostream << delim;
101  }
102  else
103  {
104  ostream << std::endl;
105  }
106  }
107  }
108  }
109 
111  {
112  m_oData.clear();
113  m_nRows = m_nCols = 0;
114  }
115 
116  int Stats::getLoad(std::string apn)
117  {
118  std::pair<unsigned int, unsigned int> p = searchPair(apn);
119  int col = p.first;
120  int row = p.second;
121  if(col == 0 && row != 0) // App name found, update the load info
122  {
123  //EV << "Ehsanz: App found";
124  return toInt(m_oData[std::make_pair(4, row)]);
125  }
126  else // App name not available, create a new row
127  {
128  //EV << "Ehsanz: App Not found";
129  return 0;
130  }
131  }
132 
133  void Stats::updateLoad(std::string apn, std::string api, std::string aen, std::string aei, bool increment)
134  {
135  std::pair<unsigned int, unsigned int> p = searchPair(apn);
136  int col = p.first;
137  int row = p.second;
138  int load = 0;
139  if(col == 0 && row != 0) // // This Server App already having more than one connections, update the load info
140  {
141  EV << "Ehsanz: App found"<<std::endl;
142  load = toInt(m_oData[std::make_pair(4, row)]);
143  if(increment)
144  {
145  EV << "Ehsanz: Incrementing "<<load<<std::endl;
146  m_oData[std::make_pair(4, row)] = this->toString(++load);
147  }
148  else
149  {
150  if(--load==0)
151  {
152  EV << "Ehsanz: Erasing "<<std::endl;
153  //this->rowErase(row);
154  }
155  else
156  {
157  EV << "Ehsanz: Decremented "<<load<<std::endl;
158  m_oData[std::make_pair(4, row)] = this->toString(load);
159  }
160  }
161  }
162 
163  else // this server app does not have any connection at the moment, create a new entry for it in the Stats.txt
164  {
165  if(increment)
166  {
167  row = this->GetRows();
168  m_oData[std::make_pair(0,row)]=apn;
169  m_oData[std::make_pair(1,row)]=api;
170  m_oData[std::make_pair(2,row)]=aen;
171  m_oData[std::make_pair(3,row)]=aei;
172  m_oData[std::make_pair(4,row)]="1";
173  m_nRows++;
174  }
175  }
176  this->save();
177  }
178 
179  /* looks for the column and row number of the APN */
180  std::pair<unsigned int, unsigned int> Stats::searchPair(std::string str)
181  {
182  std::map<std::pair<unsigned int, unsigned int>, std::string>::iterator it = m_oData.begin();
183  while(it != m_oData.end())
184  {
185  if(it->second == str && it->first.first==0)
186  {
187  return it->first;
188  }
189  it++;
190  }
191  std::pair<unsigned int, unsigned int> p = std::make_pair(0, 0);
192  return p;
193  }
194 
195 
196  /* Erase a particular row if the APN is no more in use by any client App */
197  void Stats::rowErase(unsigned int row)
198  {
199  if (row < m_nRows)
200  {
201  for ( unsigned int nRow = row+1; nRow < m_nRows; nRow++ )
202  {
203  for ( unsigned int nCol = 0; nCol < m_nCols; nCol++ )
204  {
205  m_oData[std::make_pair( nCol, nRow-1)] = m_oData[std::make_pair(nCol, nRow)];
206  }
207  }
208 
209  for (unsigned int i=0; i<m_nCols; i++)
210  {
211  m_oData.erase(std::make_pair(i, m_nRows-1));
212  }
213  m_nRows--;
214  }
215  }
216 
218  {
219  int rows=0;
220  std::map<std::pair<unsigned int, unsigned int>, std::string>::iterator it;
221  for (it=m_oData.begin(); it!=m_oData.end(); it++)
222  {
223 
224  }
225  it--;
226  rows = it->first.second;
227  return ++rows;
228  }
229 
230  std::string Stats::toString(int i)
231  {
232  std::stringstream stm;
233  stm<<i;
234  return stm.str();
235  }
236 
237  int Stats::toInt (std::string str)
238  {
239  return atoi(str.c_str());
240  }
241 
242  std::string Stats::getBestApp(std::string srcApp, std::string dstApp, std::string allApps)
243  {
244  if(!allApps.compare("AppErr"))
245  return dstApp;
246  std::istringstream lineStream(allApps);
247  std::string app, availableApp = dstApp;
248  //std::map<std::string, int> appsMap;
249  int minLoad = MAX_LOAD;
250  int appLoad = 0;
251  int appCount = 0;
252  //int dstLoad = 0;
253 
254  while( std::getline( lineStream, app, ',' ) )
255  {
256  app = trim(app);
257  if (!srcApp.compare(app))
258  {
259  continue;
260  }
261  else
262  {
263  appLoad = getLoad(app);
264  //appsMap[app] = appLoad;
265  appCount++;
266  EV <<"Ehsanz: Selection of App: " << app << "Load:" << appLoad;
267  if(minLoad > appLoad)
268  {
269  minLoad = appLoad;
270  availableApp = app;
271  }
272  }
273  }
274  EV << " Min load:" << minLoad << " App:"<< availableApp<<std::endl;
275  return availableApp;
276  }
void rowErase(unsigned int row)
Definition: Stats.cc:197
virtual ~Stats()
Definition: Stats.cc:35
std::string getBestApp(std::string srcApp, std::string dstApp, std::string allApps)
Definition: Stats.cc:242
int toInt(std::string str)
Definition: Stats.cc:237
const int MAX_LOAD
Definition: Stats.h:47
std::string & trim(std::string &s)
Definition: Stats.h:34
std::map< std::pair< unsigned int, unsigned int >, std::string > m_oData
Definition: Stats.h:43
Stats()
Definition: Stats.cc:26
int rowCount()
Definition: Stats.cc:217
void open_now(std::istream &istream, char delim)
Definition: Stats.cc:56
void save_now(std::ostream &ostream, char delim)
Definition: Stats.cc:91
bool save()
Definition: Stats.cc:78
char delim
Definition: Stats.h:61
unsigned int GetRows()
Definition: Stats.h:76
int getLoad(std::string apn)
Definition: Stats.cc:116
void updateLoad(std::string apn, std::string api, std::string aen, std::string aei, bool increment)
Definition: Stats.cc:133
unsigned int m_nRows
Definition: Stats.h:46
unsigned int m_nCols
Definition: Stats.h:45
void clear()
Definition: Stats.cc:110
std::string toString(int i)
Definition: Stats.cc:230
const char * filename
Definition: Stats.h:60
std::pair< unsigned int, unsigned int > searchPair(std::string str)
Definition: Stats.cc:180
bool open()
Definition: Stats.cc:40