-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHost.cpp
More file actions
204 lines (194 loc) · 6.32 KB
/
Host.cpp
File metadata and controls
204 lines (194 loc) · 6.32 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "Host.h"
#include <fstream>
using namespace std;
Host::Host()
{
std::string address1;
/*this->address = "aa:bb:cc:dd:ee:ff";*/
for (int i = 0; i <17; ++i){
if (i == 2 || i == 5 || i == 8 || i == 11 || i == 14){
address1.push_back(':');
}
else {
address1.push_back('a' + rand()%26);
}
}
this->address = address1;
}
Host::Host(std::string name, Port * p1)
{
this->name = name;
//this->destAddress = destAddress;
myport = p1;
std::string address1;
/*this->address = "aa:bb:cc:dd:ee:ff";*/
for (int i = 0; i <17; ++i){
if (i == 2 || i == 5 || i == 8 || i == 11 || i == 14){
address1.push_back(':');
}
else {
address1.push_back('a' + rand()%26);
}
}
this->address = address1;
//this->setPort(& p1);
}
void Host::setPort(Port * p1)
{
//myport = p1;
}
std::string Host::getAddress()
{
return this->address;
}
std::string Host::getName()
{
return this->name;
}
void Host::setdesAddress(std::string destAddress)
{
this->destAddress = destAddress;
}
void Host::setPayload(std::string payload)
{
this->payload = payload;
}
std::string Host::frameCreator()
{
srand (time(NULL));
std::string payload;
double lambda = (1/5.0);
double x = (double) rand()/(RAND_MAX);
std::cout << "x = " << x << std::endl;
int interArrival = (-1/lambda)*log(x);
std::cout << interArrival << std::endl; /* The exponential random variable based on lambda and the uniform rand variable x*/
int randlength = 1 + (10-1)*x;
/*usleep(randlength*1000000);*/
/*cout << "randlength = " << randlength << endl;*/
for (int i = 0; i < randlength; ++i){
payload.push_back('a' + rand()%26);
}
time_t start;
start = time(NULL);
cout << "Current seconds is: " << start << endl;
time_t nextTime = start + interArrival;
//sleep(5);
while(time(NULL) < nextTime){ }
time_t end;
end = time(NULL);
cout << "Updated time now is: " << end << endl;
cout << "Elapsed time is: " << difftime(end, start) << endl;
/*cout << payload << endl;*/
return payload;
}
std::string Host::frameCreatorClock()
{
srand (time(NULL));
std::string payload;
double lambda = (1/5.0);
double x = (double) rand()/(RAND_MAX);
std::cout << "x = " << x << std::endl;
int interArrival = (-1/lambda)*log(x);
std::cout << interArrival << std::endl; /* The exponential random variable based on lambda and the uniform rand variable x*/
int randlength = 1 + (10-1)*x;
/*usleep(randlength*1000000);*/
/*cout << "randlength = " << randlength << endl;*/
for (int i = 0; i < randlength; ++i)
{
payload.push_back('a' + rand()%26);
}
clock_t start;
start = clock();
cout << "Current ticks is: " << start << endl;
clock_t nextTime = start + interArrival*CLOCKS_PER_SEC;
//sleep(5);
while(clock() < nextTime){ }
clock_t end;
end = clock();
cout << "Updated ticks now is: " << end << endl;
cout << "Elapsed time is: " << (double) (end - start)/CLOCKS_PER_SEC << endl;
/*cout << payload << endl;*/
return payload;
}
std::string Host::frameCreatorChrono(int randomN)
{
std::string payload;
double x = (randomN / (double) RAND_MAX);
//long y = 1000000;
//double x = (randomN % y) /double(y);
//std::cout << "x = " << x << std::endl;
int randlength = 1 + (10-1)*x;
/*usleep(randlength*10000000);*/
/*cout << "randlength = " << randlength << endl;*/
for (int i = 0; i < randlength; ++i)
{
payload.push_back('a' + rand()%26);
}
return payload;
}
int Host::poissonDelay(int randomN)
{
double lambda = (1/20.0);
long y = 100000;
double x = (randomN % y) /double(y);
int interArrival = (-1/lambda)*log(x);
return interArrival;
}
void Host::setFrame(Frame * f1, std::string payload)
{
f1->setPayload(this->address, this->destAddress, payload);
std::ofstream myfile;
myfile.open(this->getName() + "Tx.txt", std::ios::app);
myfile << this->getName() << " : " << "Payload sent: \" " << payload << " \" to link " << f1->getName() << std::endl;
std::cout << this->getName() << " : " << "Payload sent: \" " << payload << " \" to link " << f1->getName() << std::endl;
myfile.close();
}
void Host::processFrame(Frame * f1)
{
if (f1->getDestAdd() == this->address)
{
this->setPayload(f1->getPayload());
std::string srcAdd = f1->getsourceAdd();
std::string destAdd = f1->getDestAdd();
f1->clearPayload();
std::ofstream myfile;
myfile.open(this->getName() + "Rx.txt", std::ios::app);
myfile << this->getName() << " : " << "Payload \" " << this->payload << " \" received..." << " from link " << f1->getName() << std::endl;
std::cout << this->getName() << " : " << "Payload \" " << this->payload << " \" received..." << " from link " << f1->getName() << std::endl;
myfile.close();
}
}
void Host::run(int randomN)
{
while (1)
{
int interArrival = this->poissonDelay(rand());
//std::cout << interArrival << endl;
auto start_time = std::chrono::high_resolution_clock::now().time_since_epoch();
//std::cout << "Current seconds is : " << (std::chrono::duration_cast<std::chrono::microseconds>(start_time).count())/1000000 << endl << endl << "Waiting..." << endl;
auto intv = std::chrono::microseconds(interArrival*10000);
auto totTime = start_time + intv;
while(std::chrono::high_resolution_clock::now().time_since_epoch() < totTime)
{
if (!myport->incomingFrame->isempty())
{
if (myport->incomingFrame->getDestAdd() == this->address)
{
this->processFrame(myport->incomingFrame);
}
}
}
while(1)
{
if (myport->outgoingFrame->isempty())
{
this->setFrame(myport->outgoingFrame, this->frameCreatorChrono(randomN));
break;
}
}
}
}
Host::~Host()
{
//dtor
}