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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
| #include "udp_test_transmiter.h"
udpTestTransmiter::udpTestTransmiter()
{
setReady(false);
setDone(false);
timeout=0;
_port=0;
ip=0;
}
udpTestTransmiter::udpTestTransmiter(statisticsHandler *_stat, const char * _ip, int16_t port, int32_t speed)
: _port(port)
, stat(_stat)
, ip(_ip)
, abs_speed(speed)
, aru(100000)
, aru_type(0)
{
timeout=1;
//_buflen=speed/39;
if(speed>=12000 && speed<16000)
{
_buflen=340;
aru_type=1;
}
else if(speed>=16000 && speed<=20000)
{
_buflen=456;
aru_type=1;
}
else if(speed>8000 && speed<12000)
{
float tk=speed-8000;
tk/=1000;
tk=pow(1.5, abs(tk));
float tg=0.5+2*(abs(tk)/10);
_buflen=(int)lrint(((float)speed)/(38.5-tg*tk));
}
else if(speed>=5000 && speed<=8000)
{
float tk=8000-speed;
tk/=1000;
tk=pow(1.5, tk);
float tg=0.5-2*(abs(tk)/10);
_buflen=(int)lrint(((float)speed)/(38.5+tg*tk));
}
else if (speed<=5000)
{
float tk=8000-speed;
tk/=1000;
tk=pow(1.5, tk);
float tg=0.5+4*(abs(tk)/10);
_buflen=(int)lrint(((float)speed)/(38.5+tg*tk));
aru_type=2;
}
bufChange=false;
setReady(true);
setDone(false);
}
udpTestTransmiter::~udpTestTransmiter()
{
}
void udpTestTransmiter::configureClocks(std::string _takty)
{
int32_t speed=boost::lexical_cast<int32_t>(_takty);
abs_speed=speed;
if(speed>=12000 && speed<16000)
{
_buflen=340;
aru_type=1;
}
else if(speed>=16000 && speed<=20000)
{
_buflen=456;
aru_type=1;
}
else if(speed>8000 && speed<12000)
{
float tk=speed-8000;
tk/=1000;
tk=pow(1.5, abs(tk));
float tg=0.5+2*(abs(tk)/10);
_buflen=(int)lrint(((float)speed)/(38.5-tg*tk));
}
else if(speed>=5000 && speed<=8000)
{
float tk=8000-speed;
tk/=1000;
tk=pow(1.5, tk);
float tg=0.5-2*(abs(tk)/10);
_buflen=(int)lrint(((float)speed)/(38.5+tg*tk));
}
else if (speed<=5000)
{
float tk=8000-speed;
tk/=1000;
tk=pow(1.5, tk);
float tg=0.5+4*(abs(tk)/10);
_buflen=(int)lrint(((float)speed)/(38.5+tg*tk));
aru_type=2;
}
bufChange=true;
}
void udpTestTransmiter::run()
{
mutex.lock();
this->stopped=false;
mutex.unlock();
transmit();
}
void udpTestTransmiter::stop()
{
mutex.lock();
//cout<<"Try to stop!"<<endl;
this->stopped=true;
mutex.unlock();
setDone(true);
}
void udpTestTransmiter::transmit()
{
//struct sockaddr_in si_other;
int s, i=0, sen;
printf("Buffer length: %d \n", _buflen);
char *buf=static_cast<char *>(malloc(sizeof(char)*_buflen));
if((s=socket(AF_INET, SOCK_DGRAM, 0))<0)
setReady(false);
int tm=1;
if(setsockopt(s, SOL_SOCKET, SO_BROADCAST, &tm, sizeof(tm))==-1)
cout<<"Error socket option"<<endl;
si_other.sin_family=AF_INET;
si_other.sin_port=htons(_port);
si_other.sin_addr.s_addr=inet_addr(ip);
//if(inet_aton(ip,(in_addr *)&si_other.sin_addr.s_addr)==0) //!!!!
//cout<<"INET_ATON ERROR"<<endl;
cout<<"TestTransmiter::Started, num_packets:"<<i<<endl;
cout<<"IP "<<ip<<"port "<<_port<<endl;
cout<<"Timeout: "<<timeout<<endl;
cout<<"Buflen "<<_buflen<<endl;
time_t start, end, runtime;
time_t n_start, n_end, n_runtime;
int64_t bytes=0, bytes_real=0;
int64_t n_bytes=0;
int32_t n_speed;
float unused_num=0;
sleep(3);
time(&start);
time(&n_start);
while(!this->stopped && i<10000000)
{
i++;
boost::this_thread::sleep(boost::posix_time::microseconds(timeout));
if (bufChange) //Размер буфера изменился
{
cout<<"Buffer changed"<<endl;
free(buf);
buf=static_cast<char *>(malloc(sizeof(char)*_buflen));
bufChange=false;
}
memset(buf, 0, sizeof(char)*_buflen);
sprintf(buf,"%d",i);
if((sen=sendto(s, (void*)buf, sizeof(char)*_buflen, MSG_DONTWAIT, (struct sockaddr *)&si_other, sizeof(si_other)))<0)
{
perror("SendTo");
printf("Error sending message %d, To %s\n", sen, inet_ntoa(si_other.sin_addr));
/* Error */ --i;
continue;
}
n_bytes+=_buflen+40;
if(modf(((float)i)/aru, &unused_num)==0.0)
{
time(&n_end);
n_runtime=n_end-n_start;
n_speed=(int)lrint(((float)8*n_bytes/(1024))/n_runtime);
int32_t n_delta=abs_speed-n_speed;
cout<<"Delta is "<<n_delta<<" n_speed "<<n_speed<<endl;
if(abs(n_delta)>100)
{
if(n_delta<0)
{
//_buflen=(int)lrint(((float)abs_speed)/(38.5+0.5*(abs(n_delta)/100)));
float tmp=((float)abs_speed)/_buflen;
if(aru_type==0)//5000 - 12000
tmp+=0.7*(abs(n_delta)/100);
else if(aru_type==1) //12000 - 20000
tmp+=0.1*(abs(n_delta)/100);
else //1000 - 5000
tmp+=0.7*(abs(n_delta)/100);
_buflen=(int)lrint(abs_speed/tmp);
}
else
{
//_buflen=(int)lrint(((float)abs_speed)/(38.5-0.5*(abs(n_delta)/100)));
float tmp=((float)abs_speed)/_buflen;
if(aru_type==0)//5000 - 12000
tmp-=0.7*(abs(n_delta)/100);
else if(aru_type==1) //12000 - 20000
tmp-=0.1*(abs(n_delta)/100);
else //1000 - 5000
tmp-=0.7*(abs(n_delta)/100);
_buflen=(int)lrint(abs_speed/tmp);
}
bufChange=true;
cout<<"New buffer: "<<_buflen<<endl;
}
n_bytes=0;
time(&n_start);
}
bytes+=_buflen+40;
bytes_real+=sen;
if(!mode)
stat->updateStatistics(true, i);
}
time(&end);
runtime=end-start;
printf("Time elapsed runtime: %d sec\n", runtime);
printf("Bits send: %d \n", bytes*8);
printf("Bits real send: %d \n", bytes_real*8);
printf("KBits per second: %f Kb/s\n", ((float)8*bytes/(1024))/runtime);
printf("KBits per second real: %f Kb/s\n", ((float)8*bytes_real/(1024))/runtime);
sprintf(buf,"%d",-1);
usleep(400);
if((sen=sendto(s, ((void*)buf), _buflen, /*MSG_DONTWAIT*/ 0, (struct sockaddr *)&si_other, sizeof(struct sockaddr)))<0)
{
printf("Error sending message %d, To %s\n", sen, inet_ntoa(si_other.sin_addr));
/* Error */
}
setDone(true);
free(buf);
close (s);
}
void udpTestTransmiter::setTestingMode(bool _mode)
{
if(_mode)
{
cout<<"Switching on testing mode"<<endl;
//configureClocks("4000");
si_other.sin_addr.s_addr=htonl(INADDR_BROADCAST);
si_other.sin_port=htons(ETH_PORT_CONTROL);
cout<<"testing mode on"<<endl;
}
else
{
cout<<"Switching off testing mode"<<endl;
si_other.sin_addr.s_addr=inet_addr(ip);
si_other.sin_port=htons(_port);
}
this->mode=_mode;
} |