1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#include <iostream>
#include <fstream>
#include <string>
#include <stdint.h>
#include <sstream>
#include <cmath>
#include <map>
#include <iterator>
#include <list>
using namespace std;
int main()
{
std::map<int,double_t> Pkt_arrivalmap; // identify the map that contain the arrival information of packets.
std::map<int,double_t> Pkt_creatingmap; // identify the map that contain the creating information of packets.
std::list<double_t> TotalTrafficETED;
int pos,pos2,pos3;
double_t ArrivingID,CreatingID,ArrivingTime,CreatingTime,Pkt_ETED,TTETED=0;
string line,search,word1="ns",word2="Pkt_create", word3="Pkt_recv"; // create an object from string use to read each line in file.
ifstream inf; // create an object from class of input file stream for reading only
//// to store columns values of 123.txt file (receiving) into variables, then store variables in container arrival map:
// search each line of file 123.txt on Pkt_create and print this line (okay)
inf.open ("123.txt"); // open file “123.txt” for reading
if (inf.is_open()) // condition to check if it is possible to open file or not. (if it exist), then open it and do following prcess.
{
while (getline(inf,line)) // loop continue until reaching the end of file.
{
if (line.find("Pkt_recv") != string::npos) // search on "Pkt_recv" each line
{
pos=line.find(word1); // take the length of "ns" in string line.
pos3=line.find(word3);// take the length of "Pkt_recv" in string line.
line.replace(pos,word1.length()," "); // to remove "ns"
line.replace(pos3,word3.length()," "); // to remove "Pkt_recv"
istringstream iss (line);
double_t column1;
//double_t column3;
double_t column2;
iss >> column1 >> column2;
//cout << column1 <<" "<< column2 <<" "<< " "<< endl;
Pkt_arrivalmap[column1]=column2;
}
}
inf.close();
}
else cout << "Unable to open file inf \n";
/*for (std::map<int,double_t>::iterator it = Pkt_arrivalmap.begin(); it != Pkt_arrivalmap.end(); it++)
{
cout << "Arrival info." <<" "<< it->first <<" "<< it->second << " "<< Pkt_arrivalmap.size()<< endl;
}*/
//// to store columns values of 123.txt file (creating) into variables, then store variables in container creatingmap:
// search each line of file 123.txt on Pkt_create and print this line (okay)
inf.open ("123.txt"); // open file “123.txt” for reading
if (inf.is_open()) // condition to check if it is possible to open file or not. (if it exist), then open it and do following prcess.
{
while (getline(inf,line)) // loop continue until reaching the end of file.
{
if (line.find("Pkt_create") != string::npos) // search on "Pkt_create" each line
{
pos=line.find(word1); // take the length of "ns" in string line.
pos2=line.find(word2);// take the length of "Pkt_create" in string line.
line.replace(pos,word1.length()," "); // to remove "ns"
line.replace(pos2,word2.length()," "); // to remove "Pkt_create"
istringstream iss (line);
double_t column1;
//double_t column3;
double_t column2;
iss >> column1 >> column2;
//cout << column1 <<" "<< column2 <<" "<< " "<< endl;
Pkt_creatingmap[column1]=column2;
}
}
inf.close();
}
else cout << "Unable to open file inf \n";
for (std::map<int,double_t>::iterator it = Pkt_creatingmap.begin(); it != Pkt_creatingmap.end(); it++)
{
cout << "Creating info." <<" "<< it->first <<" "<< it->second << " "<< Pkt_creatingmap.size()<< endl;
}
////////////////////////////////////////
auto Pkt_arrivalmap_it = Pkt_arrivalmap.begin();
auto Pkt_arrivalmap_end = Pkt_arrivalmap.end();
const int lowest_key=Pkt_arrivalmap_it->first;
auto Pkt_creatingmap_it = Pkt_creatingmap.lower_bound(lowest_key);
auto Pkt_creatingmap_end = Pkt_creatingmap.end();
while (Pkt_arrivalmap_it != Pkt_arrivalmap_end && Pkt_creatingmap_it != Pkt_creatingmap_end)
{
if (Pkt_arrivalmap_it ->first < Pkt_creatingmap_it ->first)
++ Pkt_arrivalmap_it;
else
if (Pkt_arrivalmap_it ->first == Pkt_creatingmap_it ->first)
{
TotalTrafficETED.push_back(Pkt_arrivalmap_it ->second - Pkt_creatingmap_it ->second);
Pkt_arrivalmap_it=Pkt_arrivalmap.erase(Pkt_arrivalmap_it);
Pkt_creatingmap_it = Pkt_creatingmap.erase(Pkt_creatingmap_it);
}
else ++Pkt_creatingmap_it;
}
/////////////////////////////////////////
for (std::list<double_t>::iterator it = TotalTrafficETED.begin(); it != TotalTrafficETED.end(); it++)
{
TTETED+=(*it/TotalTrafficETED.size())/1000000000;
}
cout << "Total Traffic ETED" << " "<< TTETED <<"Sec."<< endl;
return 0;
}
|