-
Notifications
You must be signed in to change notification settings - Fork 3
/
peerthread.cpp
722 lines (568 loc) · 20.4 KB
/
peerthread.cpp
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
#include "peerthread.h"
PeerThread::PeerThread(QObject *parent) : QThread(parent)
{
peerobj = new p2psp::peer();
//logger = new Logger();
player_port = peerobj->GetDefaultPlayerPort();
splitter_addr = p2psp::Peer_core::GetDefaultSplitterAddr().to_string();
splitter_port = p2psp::Peer_core::GetDefaultSplitterPort();
#ifndef __IMS__
max_chunk_debt = p2psp::Peer_DBS::GetDefaultMaxChunkDebt();
team_port = p2psp::Peer_core::GetDefaultTeamPort();
#endif
#if defined __NTS__ && not defined __monitor__
source_port_step = 0;
#endif
}
void PeerThread::Play(runDialogStorage runParameters)
{
runtimeParameters.setPlayerPort(runParameters.getPlayerPort());
runtimeParameters.setSplitterPort(runParameters.getSplitterPort());
runtimeParameters.setSplitterAddress(runParameters.getSplitterAddress());
#ifndef __IMS__
runtimeParameters.setMaxChunk(runParameters.getMaxChunk());
runtimeParameters.setLocalHost(runParameters.getLocalHost());
runtimeParameters.setTeamPort(runParameters.getTeamPort());
#endif
#ifdef __NTS__
#ifndef __monitor__
runtimeParameters.setSourcePortStep(runParameters.getSourcePortStep());
#endif
#endif
if (!isRunning())
{
if (isStopped())
{
stop1 = false;
}
start(LowPriority);
}
}
void PeerThread::run()
{
usingHeaders();
setParameters();
peerInitialization();
calcBufferingTime();
peerobj->Start();
setLocalVariables();
while(peerobj->IsPlayerAlive())
{
boost::this_thread::sleep(boost::posix_time::seconds(1));
// delay(1000);
process();
printCurrentTime();
}
}
void PeerThread::usingHeaders()
{
#if defined __IMS__
//logger->log(LOG_INFO,"Using Peer_IMS");
#endif
#if defined __DBS__ || defined __ACS__
#if defined __monitor__
//emit eventLog("Using Monitor_DBS");
//std::cout << "Using Monitor_DBS" << std::endl;
#else
//logger->log(LOG_INFO,"Using Peer_DBS");
//std::cout << "Using Peer_DBS" << std::endl;
#endif /* __monitor__ */
#endif /* __DBS__ || __ACS__ */
#if defined __LRS__
#if defined __monitor__
emit eventLog("Using Monitor_LRS");
//std::cout << "Using Monitor_LRS" << std::endl;
#else
emit eventLog("Using Peer_DBS");
//std::cout << "Using Peer_DBS" << std::endl;
#endif
#endif /* __LRS__ */
#if defined __NTS__
#if defined __monitor__
std::cout << "Using Monitor_NTS" << std::endl;
#else
std::cout << "Using Peer_NTS" << std::endl;
#endif /* __monitor__ */
#endif /* __NTS__ */
#if defined __EMS__
#if defined __monitor__
std::cout << "Using Monitor_EMS" << std::endl;
#else
std::cout << "Using Peer_EMS" << std::endl;
#endif /* __monitor__ */
#endif /* __EMS__ */
}
void PeerThread::setParameters()
{
// cout<<"Default Player Port"<<player_port<<endl;
// cout<<"Player port from UI "<<runtimeParameters.getPlayerPort();
if(runtimeParameters.getPlayerPort()!=0)
{
player_port=runtimeParameters.getPlayerPort();
}
peerobj->SetPlayerPort(player_port);
peerobj->WaitForThePlayer();
if(runtimeParameters.getSplitterAddress()!="")
{
splitter_addr=runtimeParameters.getSplitterAddress();
peerobj->SetSplitterAddr(ip::address::from_string(splitter_addr));
}
if(runtimeParameters.getSplitterPort()!=0)
{
splitter_port=runtimeParameters.getSplitterPort();
peerobj->SetSplitterPort(splitter_port);
}
peerobj->ConnectToTheSplitter();
peerobj->ReceiveSourceEndpoint();
peerobj->ConnectToTheSource();
peerobj->ReceiveChannel();
peerobj->ReceiveHeaderSize();
peerobj->RequestHeader();
peerobj->RelayHeader();
peerobj->ReceiveChunkSize();
peerobj->ReceiveBufferSize();
#ifdef __IMS__
peerobj->ReceiveMcastGroup();
#else
if(runtimeParameters.getMaxChunk()!=0)
{
max_chunk_debt=runtimeParameters.getMaxChunk();
peerobj->SetMaxChunkDebt(max_chunk_debt);
}
if(runtimeParameters.getTeamPort()!=0)
{
team_port=runtimeParameters.getTeamPort();
peerobj->SetTeamPort(team_port);
}
if(runtimeParameters.getLocalHost())
{
peerobj->SetUseLocalHost(true);
}
#endif
#ifdef __NTS__
#ifndef __monitor__
if(runtimeParameters.getSourcePortStep()!=0)
{
sourcePortStep=runtimeParameters.getSourcePortStep();
peerobj->SetPortStep(sourcePortStep);
}
#endif
#endif
}
void PeerThread::peerInitialization()
{
peerobj->Init();
peerobj->ListenToTheTeam();
#ifndef __IMS__
peerobj->ReceiveTheListOfPeers();
// TRACE("List of peers received");
// TRACE("Number of peers in the team (excluding me) = "
// << std::to_string(peerobj->GetNumberOfPeers()));
#endif
peerobj->SendReadyForReceivingChunks();
peerobj->DisconnectFromTheSplitter();
}
void PeerThread::calcBufferingTime()
{
std::cout<< "Buffering ... "<< std::endl << std::flush;
//time_t start_time = time(NULL);
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
//auto start = std::chrono::steady_clock::now();
peerobj->BufferData();
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
/*auto duration = std::chrono::duration_cast<std::chrono::milliseconds>
(std::chrono::steady_clock::now() - start);*/
std::cout << "done" << std::endl;
std::cout<< "Buffering time = "
<< std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() / 1000000.0
<< " seconds" << std::endl;
}
void PeerThread::setLocalVariables()
{
kbps_recvfrom = 0;
kbps_sendto = 0;
last_sendto_counter = -1;
last_recvfrom_counter = peerobj->GetRecvfromCounter();
#ifndef __IMS__
last_chunk_number = peerobj->GetPlayedChunk();
kbps_expected_recv = 0;
if (peerobj->GetSendtoCounter() < 0) {
last_sendto_counter = 0;
} else {
//peer->SetSendtoCounter(0);
last_sendto_counter = 0;
}
team_ratio = 0.0f;
kbps_expected_sent = 0;
counter = 0;
#endif
}
void PeerThread::process()
{
kbps_sendto = int(((peerobj->GetSendtoCounter() - last_sendto_counter) *
peerobj->GetChunkSize() * 8) / 1000.0f);
last_sendto_counter = peerobj->GetSendtoCounter();
kbps_recvfrom = int(((peerobj->GetRecvfromCounter() - last_recvfrom_counter) *
peerobj->GetChunkSize() * 8) / 1000.0f);
last_recvfrom_counter = peerobj->GetRecvfromCounter();
#ifndef __IMS__
kbps_expected_recv = int(((peerobj->GetPlayedChunk() - last_chunk_number) *
peerobj->GetChunkSize() * 8) / 1000.0f);
last_chunk_number = peerobj->GetPlayedChunk();
{
team_ratio = peerobj->GetPeerList()->size() / (peerobj->GetPeerList()->size() + 1.0f);
}
kbps_expected_sent = (int)(kbps_expected_recv * team_ratio);
if (kbps_expected_recv < kbps_recvfrom) {
// cout << _SET_COLOR(_GREEN);
} else if (kbps_expected_recv > kbps_recvfrom) {
// cout << _SET_COLOR(_RED);
}
#endif /* not defined __IMS__ */
// std::cout<< kbps_recvfrom<< _RESET_COLOR();
#ifndef __IMS__
// std::cout<<kbps_expected_recv;
if (kbps_expected_sent < kbps_sendto) {
// std::cout << _SET_COLOR(_GREEN);
} else if (kbps_expected_sent > kbps_sendto) {
// std::cout << _SET_COLOR(_RED);
}
#endif /* not defined __IMS__ */
// std::cout<< kbps_sendto<< _RESET_COLOR();
#ifndef __IMS__
// std::cout<< kbps_expected_sent;
#endif /* not defined __IMS__ */
/*Used for Displaying PeerList with ports*/
//#ifndef __IMS__
// counter = 0;
// for (std::vector<boost::asio::ip::udp::endpoint>::iterator p = peer->GetPeerList()->begin();
// p != peer->GetPeerList()->end();
// ++p) {
// if (counter < 5) {
// std::cout << p->address().to_string()
// << ","
// << std::to_string(p->port())
// << " ";
// counter++;
// } else {
// break;
// std::cout << "";
// }
// }
//#endif
// cout<<"Download"<<kbps_recvfrom<<endl;
// cout<<"Upload"<<kbps_sendto<<endl;
#ifndef __IMS__
emit stats(kbps_recvfrom,kbps_sendto,peerobj->GetNumberOfPeers());
#else
emit stats(kbps_recvfrom,kbps_sendto,0);
#endif
}
void PeerThread::printCurrentTime()
{
// using boost::posix_time::ptime;
// using boost::posix_time::second_clock;
// using boost::posix_time::to_simple_string;
// using boost::gregorian::day_clock;
// ptime todayUtc(day_clock::universal_day(), second_clock::universal_time().time_of_day());
// std::cout << to_simple_string(todayUtc);
}
void PeerThread::delay(int msecs)
{
QTime dieTime= QTime::currentTime().addMSecs(msecs);
while (QTime::currentTime() < dieTime)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
PeerThread::~PeerThread()
{
mutex.lock();
stop1 = true;
//delete pBgs;
condition.wakeOne();
mutex.unlock();
wait();
}
void PeerThread::msleep(int ms)
{
struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
nanosleep(&ts, NULL);
}
void PeerThread::Stop()
{
stop1 = true;
}
bool PeerThread::isStopped() const
{
return this->stop1;
}
//int run(int argc, const char *argv[]) throw(boost::system::system_error)
//{
//// Argument Parsing
// const char description[80] = "This is a peer node of a P2PSP team.\n"
// "Parameters";
// boost::program_options::options_description desc(description);
// {
// // TODO: strpe option should expect a list of arguments, not bool
// desc.add_options()
// ("help,h", "Produce this help message and exits.")
//#if not defined __IMS__
// ("max_chunk_debt", boost::program_options::value<int>()->default_value(max_chunk_debt), "Maximum number of times that other peer can not send a chunk to this peer.")
//#endif
// ("player_port", boost::program_options::value<uint16_t>()->default_value(player_port), "Port to communicate with the player.")
//#if defined __NTS__ && not defined __monitor__
// ("source_port_step", boost::program_options::value<int>()->default_value(source_port_step), "Source port step forced when behind a sequentially port allocating NAT (conflicts with --chunk_loss_period).")
//#endif
// ("splitter_addr", boost::program_options::value<std::string>()->default_value(splitter_addr), "IP address or hostname of the splitter.")
// ("splitter_port", boost::program_options::value<uint16_t>()->default_value(splitter_port), "Listening port of the splitter.")
//#if not defined __IMS__
// ("team_port", boost::program_options::value<uint16_t>()->default_value(team_port), "Port to communicate with the peers. By default the OS will chose it.")
// ("use_localhost", "Forces the peer to use localhost instead of the IP of the adapter to connect to the splitter." "Notice that in this case, peers that run outside of the host will not be able to communicate with this peer.")
//#endif
// ;
// }
// boost::program_options::variables_map vm;
// try {
// boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
// } catch (std::exception &e) {
// // If the argument passed is unknown, print the list of available arguments
// std::cout << desc << "\n";
// return 1;
// }
// boost::program_options::notify(vm);
// if (vm.count("help")) {
// std::cout << desc << "\n";
// return 1;
// }
// Peer instantiation
// class Console *peer = new Console();
// if (vm.count("player_port")) {
// peer->SetPlayerPort(vm["player_port"].as<uint16_t>());
// TRACE("Player port = "
// << peer->GetPlayerPort());
// }
// peer->WaitForThePlayer();
// std::cout
// << "Player connected"
// << std::endl;
// if (vm.count("splitter_addr")) {
// peer->SetSplitterAddr(ip::address::from_string(vm["splitter_addr"].as<std::string>()));
// TRACE("Splitter address = "
// << peer->GetSplitterAddr());
// }
// if (vm.count("splitter_port")) {
// peer->SetSplitterPort(vm["splitter_port"].as<uint16_t>());
// TRACE("Splitter port = "
// << peer->GetSplitterPort());
// }
// peer->ConnectToTheSplitter();
// TRACE("Connected to the splitter");
// /*std::cout
// << "Real splitter port = "
// << peer->GetRealSplitterPort()
// << std::endl;*/
// peer->ReceiveSourceEndpoint();
// TRACE("Source = ("
// << peer->GetSourceAddr()
// << ","
// << std::to_string(peer->GetSourcePort())
// << ")");
// peer->ConnectToTheSource();
// TRACE("Connected to the source");
// peer->ReceiveChannel();
// TRACE("channel = "
// << peer->GetChannel());
// peer->ReceiveHeaderSize();
// TRACE("Header size = "
// << peer->GetHeaderSize());
// peer->RequestHeader();
// TRACE("Header requested");
// std::cout << "Relaying the header from the source to the player ... " << std::flush;
// peer->RelayHeader();
// std::cout << "done" << std::endl;
// peer->ReceiveChunkSize();
// TRACE("Chunk size = "
// << peer->GetChunkSize());
// peer->ReceiveBufferSize();
// TRACE("Buffer size = "
// << peer->GetBufferSize());
//#if defined __IMS__
// peer->ReceiveMcastGroup();
// TRACE("Using IP multicast group = ("
// << peer->GetMcastAddr().to_string()
// << ","
// << peer->GetMcastPort()
// << ")");
//#else /* __IMS__ */
// if (vm.count("max_chunk_debt")) {
// peer->SetMaxChunkDebt(vm["max_chunk_debt"].as<int>());
// TRACE("Maximum chunk debt = "
// << peer->GetMaxChunkDebt());
// }
// if (vm.count("team_port")) {
// peer->SetTeamPort(vm["team_port"].as<uint16_t>());
// TRACE("team_port = "
// << peer->GetTeamPort());
// }
// if (vm.count("use_localhost")) {
// peer->SetUseLocalHost(true);
// TRACE("use_localhost = "
// << peer->GetUseLocalHost());
// }
//#endif /* __IMS__*/
//#if defined __NTS__
//# if not defined __monitor__
// if (vm.count("source_port_step")) {
// peer->SetPortStep(vm["source_port_step"].as<int>());
// }
// TRACE("Source port step = "
// << peer->GetPortStep());
//#endif
//#endif
// peer->Init();
// peer->ListenToTheTeam();
// TRACE("Listening to the team");
//#if not defined __IMS__
// TRACE("Receiving the list of peers ... ");
// peer->ReceiveTheListOfPeers();
// std::cout << "done" << std::endl;
// TRACE("List of peers received");
// TRACE("Number of peers in the team (excluding me) = "
// << std::to_string(peer->GetNumberOfPeers()));
//#endif
// peer->SendReadyForReceivingChunks();
// peer->DisconnectFromTheSplitter();
// TRACE("Recived the configuration from the splitter.");
// TRACE("Clossing the connection");
// std::cout
// << "Buffering ... "
// << std::endl << std::flush;
// {
// //time_t start_time = time(NULL);
// std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
// //auto start = std::chrono::steady_clock::now();
// peer->BufferData();
// std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
// /*auto duration = std::chrono::duration_cast<std::chrono::milliseconds>
// (std::chrono::steady_clock::now() - start);*/
// std::cout << "done" << std::endl;
// std::cout
// << "Buffering time = "
// << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() / 1000000.0
// << " seconds" << std::endl;
// }
// peer->Start();
// //LOG("Peer running in a thread");
// std::cout << _RESET_COLOR();
//#if defined __IMS__
// std::cout << " | Received | Sent |" << std::endl;
// std::cout << " Time | (kbps) | (kbps) |" << std::endl;
// std::cout << "---------------------+----------+----------+" << std::endl;
//#else
// std::cout << std::endl;
// std::cout << " | Received Expected | Sent Expected | Team |" << std::endl;
// std::cout << " Time | (kbps) (kbps) | (kbps) (kbps) | size | Peer list" << std::endl;
// std::cout << "---------------------+-------------------+-------------------+------+----------..." << std::endl;
//#endif
// int kbps_recvfrom = 0;
// int kbps_sendto = 0;
// int last_sendto_counter = -1;
// int last_recvfrom_counter = peer->GetRecvfromCounter();
//#if not defined __IMS__
// int last_chunk_number = peer->GetPlayedChunk();
// int kbps_expected_recv = 0;
// if (peer->GetSendtoCounter() < 0) {
// last_sendto_counter = 0;
// } else {
// //peer->SetSendtoCounter(0);
// last_sendto_counter = 0;
// }
// float team_ratio = 0.0f;
// int kbps_expected_sent = 0;
// int counter = 0;
//#endif
// while (peer->IsPlayerAlive()) {
// boost::this_thread::sleep(boost::posix_time::seconds(1));
// { /* Print current time */
// using boost::posix_time::ptime;
// using boost::posix_time::second_clock;
// using boost::posix_time::to_simple_string;
// using boost::gregorian::day_clock;
// ptime todayUtc(day_clock::universal_day(), second_clock::universal_time().time_of_day());
// std::cout << to_simple_string(todayUtc);
// }
// std::cout
// << " |";
// kbps_sendto = int(((peer->GetSendtoCounter() - last_sendto_counter) *
// peer->GetChunkSize() * 8) / 1000.0f);
// last_sendto_counter = peer->GetSendtoCounter();
// kbps_recvfrom = int(((peer->GetRecvfromCounter() - last_recvfrom_counter) *
// peer->GetChunkSize() * 8) / 1000.0f);
// last_recvfrom_counter = peer->GetRecvfromCounter();
//#if not defined __IMS__
// kbps_expected_recv = int(((peer->GetPlayedChunk() - last_chunk_number) *
// peer->GetChunkSize() * 8) / 1000.0f);
// last_chunk_number = peer->GetPlayedChunk();
// {
// team_ratio = peer->GetPeerList()->size() / (peer->GetPeerList()->size() + 1.0f);
// }
// kbps_expected_sent = (int)(kbps_expected_recv * team_ratio);
// if (kbps_expected_recv < kbps_recvfrom) {
// std::cout << _SET_COLOR(_GREEN);
// } else if (kbps_expected_recv > kbps_recvfrom) {
// std::cout << _SET_COLOR(_RED);
// }
//#endif /* not defined __IMS__ */
// std::cout
// << std::setw(9)
// << kbps_recvfrom
// << _RESET_COLOR();
//#if not defined __IMS__
// std::cout
// << std::setw(9)
// << kbps_expected_recv;
// std::cout
// << " |";
// if (kbps_expected_sent < kbps_sendto) {
// std::cout << _SET_COLOR(_GREEN);
// } else if (kbps_expected_sent > kbps_sendto) {
// std::cout << _SET_COLOR(_RED);
// }
//#endif /* not defined __IMS__ */
// std::cout
// << std::setw(9)
// << kbps_sendto
// << _RESET_COLOR();
//#if not defined __IMS__
// std::cout
// << std::setw(9)
// << kbps_expected_sent;
//#endif /* not defined __IMS__ */
// std::cout << " |";
//#ifndef __IMS__
// {
// std::cout
// << std::setw(5)
// << peer->GetPeerList()->size()
// << " | ";
// counter = 0;
// for (std::vector<boost::asio::ip::udp::endpoint>::iterator p = peer->GetPeerList()->begin();
// p != peer->GetPeerList()->end();
// ++p) {
// if (counter < 5) {
// std::cout << p->address().to_string()
// << ","
// << std::to_string(p->port())
// << " ";
// counter++;
// } else {
// break;
// std::cout << "";
// }
// }
// }
//#endif
// std::cout
// << std::endl;
// }
// return 0;
//}