00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <unistd.h>
00021
00022
00023
00024 #include <sys/socket.h>
00025 #include <netinet/in.h>
00026 #include <arpa/inet.h>
00027
00028
00029 #include <sys/syscall.h>
00030
00031 #include "StreamControl.h"
00032
00033
00034
00035
00036
00037
00038
00039 CFragmentQueue::CFragmentQueue()
00040 {
00041 #ifdef RIO_DEBUG1
00042 RioErr << "[CFragmentQueue - Construtor] Start" << endl;
00043 #endif
00044
00045
00046 m_Start = NULL;
00047 m_End = NULL;
00048
00049 #ifdef RIO_DEBUG2
00050
00051
00052 FragmentQueueSize = 0;
00053 #endif
00054
00055 #ifdef RIO_DEBUG1
00056 RioErr << "[CFragmentQueue - Construtor] Finish" << endl;
00057 #endif
00058 }
00059
00060
00061
00062 CFragmentQueue::~CFragmentQueue()
00063 {
00064 #ifdef RIO_DEBUG1
00065 RioErr << "[CFragmentQueue - Destrutor] Start" << endl;
00066 #endif
00067
00068
00069 DeleteFragmentQueue();
00070
00071 #ifdef RIO_DEBUG1
00072 RioErr << "[CFragmentQueue - Destrutor] Finish" << endl;
00073 #endif
00074 }
00075
00076
00077
00078 void CFragmentQueue::DeleteFragmentQueue()
00079 {
00080 FragmentInfoList *Object, *NextObject;
00081
00082 #ifdef RIO_DEBUG1
00083 RioErr << "[CFragmentQueue - DeleteFragmentQueue] Start" << endl;
00084 #endif
00085
00086
00087 Object = m_Start;
00088 while( Object != NULL )
00089 {
00090
00091 NextObject = Object->NextObject;
00092
00093 delete Object->fip->FragmentData;
00094
00095 delete Object->fip;
00096
00097 delete Object;
00098
00099 Object = NextObject;
00100 }
00101
00102 m_Start = NULL;
00103 m_End = NULL;
00104
00105 #ifdef RIO_DEBUG2
00106 FragmentQueueSize = 0;
00107 #endif
00108
00109 #ifdef RIO_DEBUG1
00110 RioErr << "[CFragmentQueue - DeleteFragmentQueue] Finish" << endl;
00111 #endif
00112 }
00113
00114
00115 int CFragmentQueue::Insert( FragmentInfo *fip )
00116 {
00117 FragmentInfoList *Object;
00118
00119 #ifdef RIO_DEBUG1
00120 RioErr << "[CFragmentQueue - Insert] Start" << endl;
00121 #endif
00122
00123
00124 Object = new FragmentInfoList;
00125 if( Object == NULL )
00126 {
00127
00128 #ifdef RIO_DEBUG1
00129 RioErr << "[CFragmentQueue - Insert] Finish1" << endl;
00130 #endif
00131
00132 return ERROR_STREAMCONTROL + ERROR_MEMORY;
00133 }
00134
00135 Object->fip = fip;
00136
00137 Object->NextObject = NULL;
00138 if( m_End == NULL )
00139 m_Start = Object;
00140 else
00141 m_End->NextObject = Object;
00142 m_End = Object;
00143
00144 #ifdef RIO_DEBUG2
00145
00146
00147 FragmentQueueSize++;
00148 #endif
00149
00150 #ifdef RIO_DEBUG1
00151 RioErr << "[CFragmentQueue - Insert] Finish2" << endl;
00152 #endif
00153
00154 return S_OK;
00155 }
00156
00157
00158 FragmentInfo *CFragmentQueue::Remove()
00159 {
00160 FragmentInfoList *Object;
00161 FragmentInfo *fip;
00162
00163 #ifdef RIO_DEBUG1
00164 RioErr << "[CFragmentQueue - Remove] Start" << endl;
00165 #endif
00166
00167
00168 if( m_Start == NULL )
00169 {
00170
00171 #ifdef RIO_DEBUG1
00172 RioErr << "[CFragmentQueue - Remove] Finish1" << endl;
00173 #endif
00174
00175 return NULL;
00176 }
00177
00178 Object = m_Start;
00179
00180 m_Start = m_Start->NextObject;
00181 if( m_Start == NULL )
00182 m_End = NULL;
00183
00184 fip = Object->fip;
00185
00186 delete Object;
00187
00188 #ifdef RIO_DEBUG2
00189
00190
00191 FragmentQueueSize--;
00192 #endif
00193
00194
00195
00196 #ifdef RIO_DEBUG1
00197 RioErr << "[CFragmentQueue - Remove] Finish2" << endl;
00198 #endif
00199
00200 return fip;
00201 }
00202
00203
00204
00205 bool CFragmentQueue::IsEmpty()
00206 {
00207 #ifdef RIO_DEBUG1
00208 RioErr << "[CFragmentQueue - Remove] Single" << endl;
00209 #endif
00210
00211
00212
00213 return m_Start == NULL;
00214 }
00215
00216 #ifdef RIO_DEBUG2
00217
00218
00219
00220 void CFragmentQueue::PrintFragmentQueue( const char *QueueInfo )
00221 {
00222 #ifdef RIO_DEBUG1
00223 RioErr << "[CFragmentQueue - PrintFragmentQueue] Start" << endl;
00224 #endif
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 RioErr << "- Numero de objetos FragmentInfo (" << QueueInfo << "): ";
00258 if ( FragmentQueueSize == 0 )
00259 RioErr << " nenhum objeto armazenado." << endl;
00260 else
00261 RioErr << FragmentQueueSize << " objeto(s) armazenado(s)." << endl;
00262
00263 #ifdef RIO_DEBUG1
00264 RioErr << "[CFragmentQueue - PrintFragmentQueue] Finish" << endl;
00265 #endif
00266 }
00267 #endif
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 CLeakBucket::CLeakBucket( unsigned int MaxCredits,
00278 unsigned long long int TimeBetweenCredits )
00279 {
00280 struct timeval CreateTime;
00281
00282 #ifdef RIO_DEBUG1
00283 RioErr << "[CLeakBucket - Construtor] Start" << endl;
00284 #endif
00285
00286
00287 SetMaxCredits( MaxCredits );
00288 SetTimeBetweenCredits( TimeBetweenCredits );
00289 m_Credits = 1;
00290 gettimeofday( &CreateTime, NULL );
00291 m_LastUpdateCredits = ( ( unsigned long long int ) CreateTime.tv_sec )
00292 * 1000000ull +
00293 ( ( unsigned long long int ) CreateTime.tv_usec );
00294 #ifdef RIO_DEBUG2
00295 PrintLeakBucketInfo( "Construtor da classe" );
00296 #endif
00297
00298 #ifdef RIO_DEBUG1
00299 RioErr << "[CLeakBucket - Construtor] Finish" << endl;
00300 #endif
00301 }
00302
00303
00304 CLeakBucket::~CLeakBucket()
00305 {
00306 #ifdef RIO_DEBUG1
00307 RioErr << "[CLeakBucket - Destrutor] Single" << endl;
00308 #endif
00309 }
00310
00311
00312 void CLeakBucket::SetMaxCredits( unsigned int Value )
00313 {
00314 #ifdef RIO_DEBUG1
00315 RioErr << "[CLeakBucket - SetMaxCredits] Start" << endl;
00316 #endif
00317
00318
00319 m_MaxCredits = Value;
00320
00321 if( m_Credits > m_MaxCredits )
00322 m_Credits = m_MaxCredits;
00323
00324 #ifdef RIO_DEBUG1
00325 RioErr << "[CLeakBucket - SetMaxCredits] Finish" << endl;
00326 #endif
00327 }
00328
00329
00330 void CLeakBucket::SetTimeBetweenCredits( unsigned long long int Value )
00331 {
00332 #ifdef RIO_DEBUG1
00333 RioErr << "[CLeakBucket - SetTimeBetweenCredits] Single" << endl;
00334 #endif
00335
00336
00337 m_TimeBetweenCredits = Value;
00338 }
00339
00340 #ifdef RIO_DEBUG2
00341
00342
00343
00344 void CLeakBucket::PrintLeakBucketInfo( const char *LeakBucketInfo )
00345 {
00346 #ifdef RIO_DEBUG1
00347 RioErr << "[CLeakBucket - PrintLeakBucketInfo] Single" << endl;
00348 #endif
00349
00350 RioErr << "- Informacoes sobre o objeto LeakBucket (" << LeakBucketInfo
00351 << "): MaxCredits = " << m_MaxCredits << ", Credits = " << m_Credits
00352 << ", TimeBetweenCredits = " << m_TimeBetweenCredits
00353 << ", LastUpdateCredits = " << m_LastUpdateCredits << "." << endl;
00354 }
00355 #endif
00356
00357
00358 unsigned int CLeakBucket::GetCredits()
00359 {
00360 #ifdef RIO_DEBUG1
00361 RioErr << "[CLeakBucket - GetCredits] Single" << endl;
00362 #endif
00363
00364 return m_Credits;
00365 }
00366
00367
00368 unsigned int CLeakBucket::GetMaxCredits()
00369 {
00370 #ifdef RIO_DEBUG1
00371 RioErr << "[CLeakBucket - GetMaxCredits] Single" << endl;
00372 #endif
00373
00374 return m_MaxCredits;
00375 }
00376
00377
00378 void CLeakBucket::AddCredits( unsigned int Value )
00379 {
00380 #ifdef RIO_DEBUG1
00381 RioErr << "[CLeakBucket - AddCredits] Start" << endl;
00382 #endif
00383
00384 if( m_Credits < m_MaxCredits )
00385 {
00386
00387 m_Credits = m_Credits + Value;
00388
00389 if( m_Credits > m_MaxCredits )
00390 m_Credits = m_MaxCredits;
00391 #ifdef RIO_DEBUG2
00392
00393 PrintLeakBucketInfo( "Ao adicionar creditos" );
00394 #endif
00395 }
00396
00397 #ifdef RIO_DEBUG1
00398 RioErr << "[CLeakBucket - AddCredits] Finish" << endl;
00399 #endif
00400 }
00401
00402
00403 void CLeakBucket::DecCredits()
00404 {
00405 #ifdef RIO_DEBUG1
00406 RioErr << "[CLeakBucket - DecCredits] Start" << endl;
00407 #endif
00408
00409
00410 if( m_Credits > 0 )
00411 {
00412 m_Credits = m_Credits - 1;
00413 #ifdef RIO_DEBUG2
00414
00415 PrintLeakBucketInfo( "Ao decrementar creditos" );
00416 #endif
00417 }
00418
00419 #ifdef RIO_DEBUG1
00420 RioErr << "[CLeakBucket - DecCredits] Finish" << endl;
00421 #endif
00422 }
00423
00424
00425 bool CLeakBucket::VerifyCredits()
00426 {
00427 #ifdef RIO_DEBUG1
00428 RioErr << "[CLeakBucket - VerifyCredits] Single" << endl;
00429 #endif
00430
00431 return m_Credits > 0;
00432 }
00433
00434
00435
00436
00437
00438 FragmentInfo *CLeakBucket::Remove()
00439 {
00440 #ifdef RIO_DEBUG1
00441 RioErr << "[CLeakBucket - Remove] Start" << endl;
00442 #endif
00443
00444 if( ( !IsEmpty() ) && ( VerifyCredits() ) )
00445 {
00446
00447
00448 DecCredits();
00449
00450 #ifdef RIO_DEBUG1
00451 RioErr << "[CLeakBucket - Remove] Finish1" << endl;
00452 #endif
00453
00454
00455 return CFragmentQueue::Remove();
00456 }
00457 else
00458 {
00459
00460 #ifdef RIO_DEBUG1
00461 RioErr << "[CLeakBucket - Remove] Finish2" << endl;
00462 #endif
00463
00464
00465
00466 return NULL;
00467 }
00468 }
00469
00470
00471
00472
00473
00474
00475 void CLeakBucket::UpdateCredits()
00476 {
00477 struct timeval tvUpdateTime;
00478 unsigned long long int UpdateTime, IntervalTime, Credits, RestTime;
00479
00480 #ifdef RIO_DEBUG1
00481 RioErr << "[CLeakBucket - UpdateCredits] Start" << endl;
00482 #endif
00483
00484
00485 gettimeofday( &tvUpdateTime, NULL );
00486
00487 UpdateTime = ( ( unsigned long long int ) tvUpdateTime.tv_sec )
00488 * 1000000ull +
00489 ( ( unsigned long long int ) tvUpdateTime.tv_usec );
00490
00491 IntervalTime = UpdateTime - m_LastUpdateCredits;
00492
00493 if( IntervalTime >= m_TimeBetweenCredits ) {
00494
00495
00496
00497
00498 Credits = IntervalTime / m_TimeBetweenCredits;
00499 RestTime = IntervalTime % m_TimeBetweenCredits;
00500 #ifdef RIO_DEBUG2
00501 RioErr << "CLeakBucket::UpdateCredits Intervalo = " << IntervalTime
00502 << ", m_TimeBetweenCredits = " << m_TimeBetweenCredits
00503 << ", Credits = " << Credits << ", UpdateTime = "
00504 << UpdateTime << ", RestTime = " << RestTime
00505 << ", m_LastUpdateCredits = " << m_LastUpdateCredits << endl;
00506 #endif
00507 AddCredits( Credits );
00508 m_LastUpdateCredits = UpdateTime - RestTime;
00509 }
00510
00511 #ifdef RIO_DEBUG1
00512 RioErr << "[CLeakBucket - UpdateCredits] Finish" << endl;
00513 #endif
00514 }
00515
00516
00517
00518 unsigned long long int CLeakBucket::NextTimeToUpdateCredits()
00519 {
00520 #ifdef RIO_DEBUG1
00521 RioErr << "[CLeakBucket - NextTimeToUpdateCredits] Single" << endl;
00522 #endif
00523
00524 return m_LastUpdateCredits + m_TimeBetweenCredits;
00525 }
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538 CSendData::CSendData( unsigned int MaxCredits,
00539 unsigned long long int TimeBetweenCredits )
00540 :CLeakBucket( MaxCredits, TimeBetweenCredits )
00541 {
00542 #ifdef RIO_DEBUG1
00543 RioErr << "[CSendData - Construtor] Start" << endl;
00544 #endif
00545
00546
00547 pthread_mutex_init( &m_AccessFragmentQueue, NULL );
00548
00549
00550 pthread_mutex_init( &m_AccessLeakBucket, NULL );
00551
00552 pthread_cond_init( &m_WaitObject, NULL );
00553
00554 pthread_cond_init( &m_WaitCredits, NULL );
00555
00556
00557 pthread_cond_init( &m_WaitUseCredits, NULL );
00558
00559 #ifdef RIO_DEBUG1
00560 RioErr << "[CSendData - Construtor] Finish" << endl;
00561 #endif
00562 }
00563
00564
00565 CSendData::~CSendData()
00566 {
00567 #ifdef RIO_DEBUG1
00568 RioErr << "[CSendData - Destrutor] Start" << endl;
00569 #endif
00570
00571
00572 pthread_mutex_destroy( &m_AccessFragmentQueue );
00573
00574
00575 pthread_mutex_destroy( &m_AccessLeakBucket );
00576
00577 pthread_mutex_destroy( &m_AccessFragmentQueue );
00578
00579
00580 pthread_cond_destroy( &m_WaitObject );
00581
00582
00583 pthread_cond_destroy( &m_WaitCredits );
00584
00585
00586 pthread_cond_destroy( &m_WaitUseCredits );
00587
00588 #ifdef RIO_DEBUG1
00589 RioErr << "[CSendData - Destrutor] Finish" << endl;
00590 #endif
00591 }
00592
00593
00594 void CSendData::DecCredits()
00595 {
00596 bool SendSignal;
00597
00598 #ifdef RIO_DEBUG1
00599 RioErr << "[CSendData - DecCredits] Start" << endl;
00600 #endif
00601
00602
00603
00604 CLeakBucket::DecCredits();
00605 SendSignal = ( GetCredits() == ( GetMaxCredits() - 1 ) );
00606
00607
00608 if( SendSignal )
00609 pthread_cond_signal( &m_WaitUseCredits );
00610
00611 #ifdef RIO_DEBUG1
00612 RioErr << "[CSendData - DecCredits] Finish" << endl;
00613 #endif
00614 }
00615
00616
00617
00618
00619
00620
00621 int CSendData::Insert( FragmentInfo *fip )
00622 {
00623 int result;
00624 bool SendSignal;
00625
00626 #ifdef RIO_DEBUG1
00627 RioErr << "[CSendData - Insert] Start" << endl;
00628 #endif
00629
00630
00631
00632 pthread_mutex_lock( &m_AccessFragmentQueue );
00633 SendSignal = IsEmpty();
00634 result = CLeakBucket::Insert( fip );
00635 #ifdef RIO_DEBUG2
00636
00637 CLeakBucket::PrintFragmentQueue( "Insercao em CSendData" );
00638 #endif
00639 if( ( result == S_OK ) && ( SendSignal ) )
00640 pthread_cond_signal( &m_WaitObject );
00641
00642 pthread_mutex_unlock( &m_AccessFragmentQueue );
00643
00644 #ifdef RIO_DEBUG1
00645 RioErr << "[CSendData - Insert] Finish" << endl;
00646 #endif
00647
00648 return result;
00649 }
00650
00651
00652
00653
00654
00655
00656
00657 FragmentInfo *CSendData::Remove()
00658 {
00659 FragmentInfo *fip;
00660
00661 #ifdef RIO_DEBUG1
00662 RioErr << "[CSendData - Remove] Start" << endl;
00663 #endif
00664
00665
00666
00667
00668
00669 pthread_mutex_lock( &m_AccessLeakBucket );
00670 if( !VerifyCredits() )
00671 {
00672 #ifdef RIO_DEBUG2
00673 RioErr << "CSendData::Remove esperando por creditos." << endl;
00674 #endif
00675 pthread_cond_wait( &m_WaitCredits, &m_AccessLeakBucket );
00676 }
00677
00678
00679 DecCredits();
00680
00681 pthread_mutex_unlock( &m_AccessLeakBucket );
00682
00683 pthread_mutex_lock( &m_AccessFragmentQueue );
00684
00685
00686
00687 if( IsEmpty() )
00688 {
00689 #ifdef RIO_DEBUG2
00690 RioErr << "CSendData::Remove esperando por um objeto." << endl;
00691 #endif
00692 pthread_cond_wait( &m_WaitObject, &m_AccessFragmentQueue );
00693 }
00694
00695
00696
00697
00698 fip = CFragmentQueue::Remove();
00699 #ifdef RIO_DEBUG2
00700
00701 CLeakBucket::PrintFragmentQueue( "Remocao em CSendData" );
00702 #endif
00703
00704 pthread_mutex_unlock( &m_AccessFragmentQueue );
00705
00706 #ifdef RIO_DEBUG1
00707 RioErr << "[CSendData - Remove] Finish" << endl;
00708 #endif
00709
00710 return fip;
00711 }
00712
00713
00714
00715
00716
00717
00718 void CSendData::UpdateCredits()
00719 {
00720 bool SendSignal, Wait;
00721
00722 #ifdef RIO_DEBUG1
00723 RioErr << "[CSendData - UpdateCredits] Start" << endl;
00724 #endif
00725
00726
00727
00728
00729 pthread_mutex_lock( &m_AccessLeakBucket );
00730
00731
00732 Wait = ( GetCredits() == GetMaxCredits() );
00733 if( Wait )
00734 pthread_cond_wait( &m_WaitUseCredits, &m_AccessLeakBucket );
00735 SendSignal = !VerifyCredits();
00736
00737 CLeakBucket::UpdateCredits();
00738
00739
00740
00741 if( SendSignal )
00742 pthread_cond_signal( &m_WaitCredits );
00743
00744 pthread_mutex_unlock( &m_AccessLeakBucket );
00745
00746 #ifdef RIO_DEBUG1
00747 RioErr << "[CSendData - UpdateCredits] Finish" << endl;
00748 #endif
00749 }
00750
00751
00752
00753
00754
00755
00756
00757 CFPool::CFPool()
00758 {
00759 #ifdef RIO_DEBUG1
00760 RioErr << "[CFPool - Construtor] Start" << endl;
00761 #endif
00762
00763
00764 pthread_cond_init( &m_WaitObject, NULL );
00765
00766 pthread_mutex_init( &m_AccessFragmentQueue, NULL );
00767
00768 #ifdef RIO_DEBUG1
00769 RioErr << "[CFPool - Construtor] Finish" << endl;
00770 #endif
00771 }
00772
00773
00774 CFPool::~CFPool()
00775 {
00776 #ifdef RIO_DEBUG1
00777 RioErr << "[CFPool - Destrutor] Start" << endl;
00778 #endif
00779
00780
00781 pthread_cond_destroy( &m_WaitObject );
00782
00783 pthread_mutex_destroy( &m_AccessFragmentQueue );
00784
00785 #ifdef RIO_DEBUG1
00786 RioErr << "[CFPool - Destrutor] Finish" << endl;
00787 #endif
00788 }
00789
00790
00791
00792
00793 int CFPool::Insert( FragmentInfo *fip )
00794 {
00795 bool SendSignal;
00796 int result;
00797
00798 #ifdef RIO_DEBUG1
00799 RioErr << "[CFPool - Insert] Start" << endl;
00800 #endif
00801
00802
00803
00804
00805 pthread_mutex_lock( &m_AccessFragmentQueue );
00806 SendSignal = IsEmpty();
00807 result = CFragmentQueue::Insert( fip );
00808 #ifdef RIO_DEBUG2
00809
00810 CFragmentQueue::PrintFragmentQueue( "Insercao em CFPool" );
00811 #endif
00812
00813
00814
00815 if( ( result == S_OK ) && ( SendSignal ) )
00816
00817
00818
00819 pthread_cond_signal( &m_WaitObject );
00820
00821 pthread_mutex_unlock( &m_AccessFragmentQueue );
00822
00823 #ifdef RIO_DEBUG1
00824 RioErr << "[CFPool - Insert] Finish" << endl;
00825 #endif
00826
00827 return result;
00828 }
00829
00830
00831
00832
00833
00834 void CFPool::WaitObject()
00835 {
00836 #ifdef RIO_DEBUG1
00837 RioErr << "[CFPool - WaitObject] Start" << endl;
00838 #endif
00839
00840
00841
00842
00843 pthread_mutex_lock( &m_AccessFragmentQueue );
00844 if( IsEmpty() )
00845 {
00846
00847
00848
00849
00850 #ifdef RIO_DEBUG2
00851 RioErr << "CFPool::WaitObject esperando por um objeto." << endl;
00852 #endif
00853 pthread_cond_wait( &m_WaitObject, &m_AccessFragmentQueue );
00854 }
00855
00856 pthread_mutex_unlock( &m_AccessFragmentQueue );
00857
00858 #ifdef RIO_DEBUG1
00859 RioErr << "[CFPool - WaitObject] Finish" << endl;
00860 #endif
00861 }
00862
00863
00864
00865 FragmentInfo *CFPool::Remove()
00866 {
00867 FragmentInfo *fip;
00868
00869 #ifdef RIO_DEBUG1
00870 RioErr << "[CFPool - Remove] Start" << endl;
00871 #endif
00872
00873
00874
00875
00876 pthread_mutex_lock( &m_AccessFragmentQueue );
00877 fip = CFragmentQueue::Remove();
00878 #ifdef RIO_DEBUG2
00879
00880 CFragmentQueue::PrintFragmentQueue( "Remocao em CFPool" );
00881 #endif
00882
00883 pthread_mutex_unlock( &m_AccessFragmentQueue );
00884
00885 #ifdef RIO_DEBUG1
00886 RioErr << "[CFPool - Remove] Finish" << endl;
00887 #endif
00888
00889 return fip;
00890 }
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904 CClientInfo::CClientInfo( unsigned int MaxCredits,
00905 unsigned long long int TimeBetweenCredits,
00906 int ip, int port )
00907 :CLeakBucket( MaxCredits, TimeBetweenCredits )
00908 {
00909 #ifdef RIO_DEBUG1
00910 RioErr << "[CClientInfo - Construtor] Start" << endl;
00911 #endif
00912
00913
00914 IP = ip;
00915 Port = port;
00916 IdleCount = MAXIDLECOUNT;
00917
00918 pthread_mutex_init( &m_AccessFragmentQueue, NULL );
00919
00920 #ifdef RIO_DEBUG1
00921 RioErr << "[CClientInfo - Construtor] Finish" << endl;
00922 #endif
00923 }
00924
00925
00926 CClientInfo::~CClientInfo()
00927 {
00928 #ifdef RIO_DEBUG1
00929 RioErr << "[CClientInfo - Destrutor] Start" << endl;
00930 #endif
00931
00932
00933 pthread_mutex_destroy( &m_AccessFragmentQueue );
00934
00935 #ifdef RIO_DEBUG1
00936 RioErr << "[CClientInfo - Destrutor] Finish" << endl;
00937 #endif
00938 }
00939
00940
00941
00942
00943 int CClientInfo::Insert( FragmentInfo *fip )
00944 {
00945 int result;
00946
00947 #ifdef RIO_DEBUG1
00948 RioErr << "[CClientInfo - Insert] Start" << endl;
00949 #endif
00950
00951
00952
00953 pthread_mutex_lock( &m_AccessFragmentQueue );
00954
00955 IdleCount = MAXIDLECOUNT;
00956
00957 result = CLeakBucket::Insert( fip );
00958
00959 pthread_mutex_unlock( &m_AccessFragmentQueue );
00960
00961 #ifdef RIO_DEBUG1
00962 RioErr << "[CClientInfo - Insert] Finish" << endl;
00963 #endif
00964
00965 return result;
00966 }
00967
00968
00969
00970 FragmentInfo *CClientInfo::Remove()
00971 {
00972 FragmentInfo *fip;
00973
00974 #ifdef RIO_DEBUG1
00975 RioErr << "[CClientInfo - Remove] Start" << endl;
00976 #endif
00977
00978
00979
00980 pthread_mutex_lock( &m_AccessFragmentQueue );
00981
00982 fip = CLeakBucket::Remove();
00983
00984 pthread_mutex_unlock( &m_AccessFragmentQueue );
00985
00986 #ifdef RIO_DEBUG1
00987 RioErr << "[CClientInfo - Remove] Finish" << endl;
00988 #endif
00989
00990 return fip;
00991 }
00992
00993
00994
00995
00996 void CClientInfo::UpdateCredits()
00997 {
00998 #ifdef RIO_DEBUG1
00999 RioErr << "[CClientInfo - UpdateCredits] Start" << endl;
01000 #endif
01001
01002
01003
01004 pthread_mutex_lock( &m_AccessFragmentQueue );
01005
01006
01007 if( ( IsEmpty() ) && ( IdleCount > 0 ) )
01008 IdleCount--;
01009
01010 pthread_mutex_unlock( &m_AccessFragmentQueue );
01011
01012 CLeakBucket::UpdateCredits();
01013
01014 #ifdef RIO_DEBUG1
01015 RioErr << "[CClientInfo - UpdateCredits] Finish" << endl;
01016 #endif
01017 }
01018
01019
01020
01021 bool CClientInfo::MustRemoveClient()
01022 {
01023 #ifdef RIO_DEBUG1
01024 RioErr << "[CClientInfo - MustRemoveClient] Single" << endl;
01025 #endif
01026
01027 return ( IdleCount == 0 );
01028
01029
01030 }
01031
01032
01033 int CClientInfo::GetIPAddress()
01034 {
01035 return IP;
01036 }
01037
01038
01039 int CClientInfo::GetPort()
01040 {
01041 #ifdef RIO_DEBUG1
01042 RioErr << "[CClientInfo - GetPort] Single" << endl;
01043 #endif
01044
01045 return Port;
01046 }
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064 CClientHeap::CClientHeap( unsigned int MaxClientsCredits )
01065 {
01066 #ifdef RIO_DEBUG1
01067 RioErr << "[CClientHeap - Construtor] Start" << endl;
01068 #endif
01069
01070
01071
01072
01073 m_MaxClientsCredits = MaxClientsCredits;
01074
01075 m_LastClient = NULL;
01076
01077
01078 pthread_mutex_init( &m_AccessClients, NULL );
01079
01080 pthread_mutex_init( &m_AccessClientsHeap, NULL );
01081
01082 pthread_cond_init( &m_WaitClients, NULL );
01083
01084 #ifdef RIO_DEBUG1
01085 RioErr << "[CClientHeap - Construtor] Finish" << endl;
01086 #endif
01087 }
01088
01089
01090 CClientHeap::~CClientHeap()
01091 {
01092 #ifdef RIO_DEBUG1
01093 RioErr << "[CClientHeap - Destrutor] Start" << endl;
01094 #endif
01095
01096
01097
01098 pthread_mutex_destroy( &m_AccessClients );
01099
01100 pthread_mutex_destroy( &m_AccessClientsHeap );
01101
01102 pthread_cond_destroy( &m_WaitClients );
01103
01104 #ifdef RIO_DEBUG1
01105 RioErr << "[CClientHeap - Destrutor] Finish" << endl;
01106 #endif
01107 }
01108
01109
01110
01111
01112
01113 CClientInfo *CClientHeap::FindClient( int ip, int port )
01114 {
01115 unsigned long long int ClientMapId;
01116
01117
01118 TClientsMap::iterator ClientIt, EndIt;
01119
01120 #ifdef RIO_DEBUG1
01121 RioErr << "[CClientHeap - FindClient] Start" << endl;
01122 #endif
01123
01124 #ifdef RIO_DEBUG2
01125 struct in_addr clientaddr;
01126 clientaddr.s_addr = ip;
01127 RioErr << "CClientList::FindClient procurando pelo cliente com o IP "
01128 << inet_ntoa( clientaddr ) << " e a porta "
01129 << ntohs( port ) << endl;
01130 #endif
01131
01132
01133 ClientMapId = ( unsigned int ) port;
01134 ClientMapId = ( ClientMapId << 32 ) | ( ( unsigned int ) ip );
01135
01136 ClientIt = m_ClientsMap.find( ClientMapId );
01137 EndIt = m_ClientsMap.end();
01138 if( ClientIt != EndIt )
01139 {
01140 #ifdef RIO_DEBUG2
01141 RioErr << "CClientList::FindClient cliente com a ID " << ClientMapId
01142 << " achado no mapa." << endl;
01143 #endif
01144
01145 #ifdef RIO_DEBUG1
01146 RioErr << "[CClientHeap - FindClient] Finish1" << endl;
01147 #endif
01148
01149
01150
01151
01152 return ClientIt->second;
01153 }
01154 else
01155 {
01156 #ifdef RIO_DEBUG2
01157 RioErr << "CClientList::FindClient cliente com a ID " << ClientMapId
01158 << " nao achado no mapa." << endl;
01159 #endif
01160
01161 #ifdef RIO_DEBUG1
01162 RioErr << "[CClientHeap - FindClient] Finish2" << endl;
01163 #endif
01164
01165
01166 return NULL;
01167 }
01168 }
01169
01170
01171
01172
01173 CClientInfo *CClientHeap::CreateClient( int ip, int port,
01174 unsigned long long int TimeBetweenCredits )
01175 {
01176 unsigned long long int ClientMapId;
01177 CClientInfo *Client;
01178
01179 #ifdef RIO_DEBUG1
01180 RioErr << "[CClientHeap - CreateClient] Start" << endl;
01181 #endif
01182
01183 #ifdef RIO_DEBUG2
01184 struct in_addr clientaddr;
01185 clientaddr.s_addr = ip;
01186 RioErr << "CClientList::CreateClient criando uma lista para o cliente com "
01187 << "o IP " << inet_ntoa( clientaddr ) << " e a porta "
01188 << ntohs( port ) << endl;
01189 #endif
01190
01191 Client = new CClientInfo( m_MaxClientsCredits, TimeBetweenCredits, ip,
01192 port );
01193 if( Client == NULL )
01194 {
01195
01196 #ifdef RIO_DEBUG1
01197 RioErr << "[CClientHeap - CreateClient] Finish1" << endl;
01198 #endif
01199
01200 return NULL;
01201 }
01202
01203
01204 pthread_mutex_lock( &m_AccessClientsHeap );
01205
01206
01207
01208 if( m_ClientsHeap.empty() )
01209 pthread_cond_signal( &m_WaitClients );
01210 m_ClientsHeap.push( Client );
01211
01212 pthread_mutex_unlock( &m_AccessClientsHeap );
01213
01214
01215 ClientMapId = ( unsigned int ) port;
01216 ClientMapId = ( ClientMapId << 32 ) | ( ( unsigned int ) ip );
01217
01218 m_ClientsMap.insert( pair< unsigned long long int, CClientInfo * >
01219 ( ClientMapId, Client ) );
01220 #ifdef RIO_DEBUG2
01221 RioErr << "CClientList::CreateClient inserindo no mapa o cliente com o IP "
01222 << inet_ntoa( clientaddr ) << ", a porta " << ntohs( port )
01223 << ", e a ID " << ClientMapId << endl;
01224 #endif
01225
01226 #ifdef RIO_DEBUG1
01227 RioErr << "[CClientHeap - CreateClient] Finish2" << endl;
01228 #endif
01229
01230 return Client;
01231 }
01232
01233
01234
01235
01236
01237 bool CClientHeap::RemoveClient( CClientInfo *Client )
01238 {
01239 unsigned long long int ClientMapId;
01240 bool result;
01241
01242 #ifdef RIO_DEBUG1
01243 RioErr << "[CClientHeap - RemoveClient] Start" << endl;
01244 #endif
01245
01246
01247
01248
01249
01250 pthread_mutex_lock( &m_AccessClients );
01251 if( ( Client->MustRemoveClient() ) && ( m_LastClient != Client ) )
01252 {
01253 #ifdef RIO_DEBUG2
01254 struct in_addr clientaddr;
01255 clientaddr.s_addr = Client->GetIPAddress();
01256 RioErr << "CClientList::RemoveClient removendo a lista do cliente com o "
01257 << "IP " << inet_ntoa( clientaddr ) << " e a porta "
01258 << ntohs( Client->GetPort() ) << endl;
01259 #endif
01260
01261
01262 ClientMapId = ( unsigned int ) Client->GetPort();
01263 ClientMapId = ( ClientMapId << 32 ) |
01264 ( ( unsigned int ) Client->GetIPAddress() );
01265
01266 m_ClientsMap.erase( ClientMapId );
01267
01268 if( m_LastClient == Client )
01269 m_LastClient = NULL;
01270 #ifdef RIO_DEBUG2
01271 RioErr << "CClientList::RemoveClient removendo do mapa o cliente com "
01272 << "o IP " << inet_ntoa( clientaddr ) << ", a porta "
01273 << ntohs( Client->GetPort() ) << ", e a ID " << ClientMapId
01274 << endl;
01275 #endif
01276
01277 delete Client;
01278 result = true;
01279 }
01280 else
01281 result = false;
01282
01283 pthread_mutex_unlock( &m_AccessClients );
01284
01285 #ifdef RIO_DEBUG1
01286 RioErr << "[CClientHeap - RemoveClient] Finish" << endl;
01287 #endif
01288
01289 return result;
01290 }
01291
01292
01293
01294 int CClientHeap::Insert( FragmentInfo *fip )
01295 {
01296 int result;
01297 CClientInfo *Client;
01298
01299 #ifdef RIO_DEBUG1
01300 RioErr << "[CClientHeap - Insert] Start" << endl;
01301 #endif
01302
01303
01304 pthread_mutex_lock( &m_AccessClients );
01305
01306
01307
01308
01309
01310 if( ( m_LastClient == NULL ) || ( m_LastClient->GetIPAddress() != fip->IP ) ||
01311 ( m_LastClient->GetPort() != fip->Port ) )
01312 {
01313
01314
01315
01316 Client = FindClient( fip->IP, fip->Port );
01317 if( Client == NULL )
01318 {
01319
01320
01321 Client = CreateClient( fip->IP, fip->Port,
01322 fip->TimeBetweenCredits );
01323 if( Client == NULL )
01324 {
01325
01326 #ifdef RIO_DEBUG1
01327 RioErr << "[CClientHeap - Insert] Finish1" << endl;
01328 #endif
01329
01330 return ERROR_STREAMCONTROL + ERROR_MEMORY;
01331 }
01332 }
01333 m_LastClient = Client;
01334 }
01335 else
01336 Client = m_LastClient;
01337
01338
01339
01340
01341 pthread_mutex_unlock( &m_AccessClients );
01342
01343
01344 result = Client->Insert( fip );
01345 if( result == S_OK )
01346 {
01347
01348
01349 #ifdef RIO_DEBUG2
01350 struct in_addr clientaddr;
01351 clientaddr.s_addr = Client->GetIPAddress();
01352 char StrAux[ 256 ];
01353 sprintf( StrAux, "Insercao em CClientList [IP=%s,Port=%u]",
01354 inet_ntoa( clientaddr ), ntohs( Client->GetPort() ) );
01355 Client->PrintFragmentQueue( StrAux );
01356 #endif
01357
01358 }
01359
01360 #ifdef RIO_DEBUG1
01361 RioErr << "[CClientHeap - Insert] Finish2" << endl;
01362 #endif
01363
01364 return result;
01365 }
01366
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378 void CClientHeap::GetFragments( TFragments &Fragments )
01379 {
01380 unsigned long long int CurrentTime, UpdateTime;
01381 bool InsertClient;
01382 FragmentInfo *fip;
01383 CClientInfo *Client;
01384 struct timeval tvCurrentTime;
01385
01386 #ifdef RIO_DEBUG1
01387 RioErr << "[CClientHeap - GetFragments] Start" << endl;
01388 #endif
01389
01390
01391
01392 pthread_mutex_lock( &m_AccessClientsHeap );
01393
01394
01395 if( m_ClientsHeap.empty() )
01396 pthread_cond_wait( &m_WaitClients, &m_AccessClientsHeap );
01397
01398
01399
01400 Client = m_ClientsHeap.top();
01401
01402 m_ClientsHeap.pop();
01403
01404 pthread_mutex_unlock( &m_AccessClientsHeap );
01405
01406 UpdateTime = Client->NextTimeToUpdateCredits();
01407
01408 gettimeofday( &tvCurrentTime, NULL );
01409
01410 CurrentTime = ( ( unsigned long long int ) tvCurrentTime.tv_sec )
01411 * 1000000ull +
01412 ( ( unsigned long long int ) tvCurrentTime.tv_usec );
01413
01414
01415
01416
01417 if( UpdateTime > CurrentTime )
01418 usleep( UpdateTime - CurrentTime );
01419
01420
01421 Client->UpdateCredits();
01422
01423
01424 InsertClient = true;
01425
01426
01427
01428 Fragments.clear();
01429 while ( ( fip = Client->Remove() ) != NULL )
01430 {
01431
01432
01433 Fragments.insert( Fragments.end(), fip );
01434 }
01435 if( Fragments.size() > 0 )
01436 {
01437 #ifdef RIO_DEBUG2
01438 struct in_addr clientaddr;
01439 char StrAux[ 256 ];
01440 unsigned int f;
01441 for( f = 0; f < Fragments.size(); f++ )
01442 {
01443 clientaddr.s_addr = Fragments[ f ]->IP;
01444 sprintf( StrAux,
01445 "enviando fragmento %u para o cliente [IP=%s,Port=%u]",
01446 f+1, inet_ntoa( clientaddr ),
01447 ntohs( Fragments[ f ]->Port ) );
01448 RioErr << "CClientHeap::GetNextFragment " << StrAux << endl;
01449 }
01450 #endif
01451 }
01452 else
01453 {
01454
01455
01456
01457
01458
01459
01460
01461
01462 InsertClient = !RemoveClient( Client );
01463 }
01464
01465 pthread_mutex_lock( &m_AccessClientsHeap );
01466
01467 if( InsertClient )
01468 m_ClientsHeap.push( Client );
01469
01470 pthread_mutex_unlock( &m_AccessClientsHeap );
01471
01472 #ifdef RIO_DEBUG1
01473 RioErr << "[CClientHeap - GetFragments] Finish" << endl;
01474 #endif
01475 }
01476
01477
01478
01479
01480
01481
01482
01483
01484
01485 CStreamControl::CStreamControl()
01486 {
01487 #ifdef RIO_DEBUG1
01488 RioErr << "[CStreamControl - Construtor] Start" << endl;
01489 #endif
01490
01491
01492 m_SendData = NULL;
01493 m_FPool = NULL;
01494 m_ClientHeap = NULL;
01495 m_SendThread = 0;
01496 m_PoolThread = 0;
01497 m_ClientsThread = 0;
01498 m_UpdateThread = 0;
01499 m_RioNeti = NULL;
01500 m_Started = false;
01501
01502 #ifdef RIO_DEBUG1
01503 RioErr << "[CStreamControl - Construtor] Finish" << endl;
01504 #endif
01505 }
01506
01507
01508 CStreamControl::~CStreamControl()
01509 {
01510 #ifdef RIO_DEBUG1
01511 RioErr << "[CStreamControl - Destrutor] Start" << endl;
01512 #endif
01513
01514 Stop();
01515
01516 #ifdef RIO_DEBUG1
01517 RioErr << "[CStreamControl - Destrutor] Finish" << endl;
01518 #endif
01519 }
01520
01521
01522 void *CStreamControl::PoolThread( void *Param )
01523 {
01524 CStreamControl *psc;
01525 FragmentInfo *fip;
01526 int result;
01527
01528 #ifdef RIO_DEBUG1
01529 RioErr << "[CStreamControl - PoolThread] Start" << endl;
01530 #endif
01531
01532 psc = (CStreamControl *) Param;
01533
01534 RioErr << "CStreamControl::PoolThread thread iniciada." << endl;
01535
01536 RioErr << "POOLTHREADID " << syscall( SYS_gettid ) << endl;
01537
01538 for( ; ; )
01539
01540 {
01541
01542
01543 psc->m_FPool->WaitObject();
01544
01545 while( ( fip = psc->m_FPool->Remove() ) != NULL )
01546 {
01547
01548
01549 while ( ( result = psc->m_ClientHeap->Insert( fip ) )
01550 != S_OK )
01551 {
01552 RioErr << "StreamControl::PoolThread erro de alocacao "
01553 << "de memoria ao inserir um fragmento na lista "
01554 << "de um cliente da classe CClientHeap. Tentando "
01555 << "novamente." << endl;
01556 }
01557 }
01558 }
01559
01560 #ifdef RIO_DEBUG1
01561 RioErr << "[CStreamControl - PoolThread] Finish" << endl;
01562 #endif
01563
01564 return NULL;
01565 }
01566
01567
01568
01569 void *CStreamControl::ClientsThread( void *Param )
01570 {
01571 CStreamControl *psc;
01572 int result;
01573 unsigned int f;
01574 TFragments Fragments;
01575
01576 #ifdef RIO_DEBUG1
01577 RioErr << "[CStreamControl - ClientsThread] Start" << endl;
01578 #endif
01579
01580 psc = (CStreamControl *) Param;
01581
01582 RioErr << "CStreamControl::ClientsThread thread iniciada." << endl;
01583
01584 RioErr << "CLIENTSTHREADID " << syscall( SYS_gettid ) << endl;
01585
01586 for( ; ; )
01587 {
01588
01589
01590
01591
01592
01593
01594
01595
01596 psc->m_ClientHeap->GetFragments( Fragments );
01597 for( f = 0; f < Fragments.size(); f++ )
01598 {
01599
01600
01601 while ( ( result = psc->m_SendData->Insert( Fragments[ f ] ) )
01602 != S_OK )
01603 {
01604 RioErr << "StreamControl::ClientsThread erro de alocacao "
01605 << "de memoria ao criar um objeto da classe "
01606 << "CSendData. Tentando novamente." << endl;
01607 }
01608 }
01609 }
01610
01611 #ifdef RIO_DEBUG1
01612 RioErr << "[CStreamControl - ClientsThread] Finish" << endl;
01613 #endif
01614
01615 return NULL;
01616 }
01617
01618
01619
01620 void *CStreamControl::SendDataThread( void *Param )
01621 {
01622 CStreamControl *psc;
01623 FragmentInfo *fip;
01624 int result;
01625
01626 #ifdef RIO_DEBUG1
01627 RioErr << "[CStreamControl - SendDataThread] Start" << endl;
01628 #endif
01629
01630 psc = (CStreamControl *) Param;
01631
01632 RioErr << "CStreamControl::SendDataThread thread iniciada." << endl;
01633
01634 RioErr << "SENDDATATHREADID " << syscall( SYS_gettid ) << endl;
01635
01636 for( ; ; )
01637 {
01638
01639
01640
01641
01642 fip = psc->m_SendData->Remove();
01643 if( fip != NULL )
01644 {
01645
01646 result = psc->m_RioNeti->nbSend( NULL, fip->FragmentData,
01647 fip->FragmentSize, fip->IP,
01648 fip->Port);
01649
01650
01651 #ifdef RIO_DEBUG2
01652 psc->TotalSendFragments++;
01653 RioErr << "CStreamControl::SendDataThread enviado o fragmento "
01654 << "numero " << psc->TotalSendFragments << endl;
01655 #endif
01656 delete fip->FragmentData;
01657 delete fip;
01658 }
01659 else
01660 RioErr << "CStreamControl::SendDataThread erro - fip = NULL"
01661 << endl;
01662 }
01663
01664 #ifdef RIO_DEBUG1
01665 RioErr << "[CStreamControl - SendDataThread] Finish" << endl;
01666 #endif
01667
01668 return NULL;
01669 }
01670
01671
01672 void *CStreamControl::UpdateThread( void *Param )
01673 {
01674 CStreamControl *psc;
01675
01676 #ifdef RIO_DEBUG1
01677 RioErr << "[CStreamControl - UpdateThread] Start" << endl;
01678 #endif
01679
01680 psc = (CStreamControl *) Param;
01681
01682 RioErr << "CStreamControl::UpdateThread thread iniciada." << endl;
01683
01684 RioErr << "UPDATETHREADID " << syscall( SYS_gettid ) << endl;
01685
01686 for( ; ; )
01687 {
01688 psc->m_SendData->UpdateCredits();
01689 }
01690
01691 #ifdef RIO_DEBUG1
01692 RioErr << "[CStreamControl - UpdateThread] Finish" << endl;
01693 #endif
01694
01695 return NULL;
01696 }
01697
01698
01699
01700
01701
01702
01703
01704
01705
01706 int CStreamControl::Start( RioNeti *rn, unsigned int MaxClientsCredits,
01707 unsigned int MaxNetworkCredits,
01708 unsigned long long int TimeBetweenCredits )
01709 {
01710
01711 pthread_attr_t attribSendThread;
01712 pthread_attr_t attribPoolThread;
01713 pthread_attr_t attribClientsThread;
01714 pthread_attr_t attribUpdateThread;
01715
01716 #ifdef RIO_DEBUG1
01717 RioErr << "[CStreamControl - Start] Start" << endl;
01718 #endif
01719
01720
01721 if( m_Started )
01722 {
01723
01724 #ifdef RIO_DEBUG1
01725 RioErr << "[CStreamControl - Start] Finish1" << endl;
01726 #endif
01727
01728 return ERROR_STREAMCONTROL + ERROR_STARTED;
01729 }
01730
01731
01732
01733
01734
01735 m_SendData = new CSendData( MaxNetworkCredits, TimeBetweenCredits );
01736 if( m_SendData == NULL )
01737 {
01738
01739 #ifdef RIO_DEBUG1
01740 RioErr << "[CStreamControl - Start] Finish2" << endl;
01741 #endif
01742
01743 return ERROR_STREAMCONTROL + ERROR_MEMORY;
01744 }
01745
01746
01747 m_FPool = new CFPool;
01748 if( m_FPool == NULL )
01749 {
01750
01751 #ifdef RIO_DEBUG1
01752 RioErr << "[CStreamControl - Start] Finish3" << endl;
01753 #endif
01754
01755 delete m_SendData;
01756 return ERROR_STREAMCONTROL + ERROR_MEMORY;
01757 }
01758
01759
01760 m_ClientHeap = new CClientHeap( MaxClientsCredits );
01761 if ( m_ClientHeap == NULL )
01762 {
01763
01764 #ifdef RIO_DEBUG1
01765 RioErr << "[CStreamControl - Start] Finish4" << endl;
01766 #endif
01767
01768 delete m_SendData;
01769 delete m_FPool;
01770 return ERROR_STREAMCONTROL + ERROR_MEMORY;
01771 }
01772
01773 pthread_attr_init( &attribSendThread );
01774 pthread_attr_init( &attribPoolThread );
01775 pthread_attr_init( &attribClientsThread );
01776 pthread_attr_init( &attribUpdateThread );
01777 pthread_attr_setstacksize( &attribSendThread, 2*PTHREAD_STACK_MIN );
01778 pthread_attr_setstacksize( &attribPoolThread, 2*PTHREAD_STACK_MIN );
01779 pthread_attr_setstacksize( &attribClientsThread, 2*PTHREAD_STACK_MIN );
01780 pthread_attr_setstacksize( &attribUpdateThread, 2*PTHREAD_STACK_MIN );
01781
01782 if ( pthread_create( &m_PoolThread, &attribPoolThread, PoolThread, this )
01783 != 0 )
01784 {
01785
01786 #ifdef RIO_DEBUG1
01787 RioErr << "[CStreamControl - Start] Finish5" << endl;
01788 #endif
01789
01790 delete m_SendData;
01791 delete m_FPool;
01792 delete m_ClientHeap;
01793 return ERROR_STREAMCONTROL + ERROR_CREATE_THREAD;
01794 }
01795 if ( pthread_create( &m_ClientsThread, &attribClientsThread, ClientsThread,
01796 this ) != 0 )
01797 {
01798
01799 #ifdef RIO_DEBUG1
01800 RioErr << "[CStreamControl - Start] Finish6" << endl;
01801 #endif
01802
01803 delete m_SendData;
01804 delete m_FPool;
01805 delete m_ClientHeap;
01806 pthread_cancel( m_PoolThread );
01807 return ERROR_STREAMCONTROL + ERROR_CREATE_THREAD;
01808 }
01809 if ( pthread_create( &m_SendThread, &attribSendThread, SendDataThread,
01810 this ) != 0 )
01811 {
01812
01813 #ifdef RIO_DEBUG1
01814 RioErr << "[CStreamControl - Start] Finish7" << endl;
01815 #endif
01816
01817 delete m_SendData;
01818 delete m_FPool;
01819 delete m_ClientHeap;
01820 pthread_cancel( m_PoolThread );
01821 pthread_cancel( m_ClientsThread );
01822 return ERROR_STREAMCONTROL + ERROR_CREATE_THREAD;
01823 }
01824 if ( pthread_create( &m_UpdateThread, &attribUpdateThread, UpdateThread,
01825 this ) != 0 )
01826 {
01827
01828 #ifdef RIO_DEBUG1
01829 RioErr << "[CStreamControl - Start] Finish8" << endl;
01830 #endif
01831
01832 delete m_SendData;
01833 delete m_FPool;
01834 delete m_ClientHeap;
01835 pthread_cancel( m_PoolThread );
01836 pthread_cancel( m_ClientsThread );
01837 pthread_cancel( m_SendThread );
01838 return ERROR_STREAMCONTROL + ERROR_CREATE_THREAD;
01839 }
01840
01841 m_RioNeti = rn;
01842 m_Started = true;
01843
01844 #ifdef RIO_DEBUG2
01845
01846 TotalSendFragments = 0;
01847 #endif
01848
01849 #ifdef RIO_DEBUG1
01850 RioErr << "[CStreamControl - Start] Finish9" << endl;
01851 #endif
01852
01853 return S_OK;
01854 }
01855
01856
01857 void CStreamControl::Stop()
01858 {
01859 #ifdef RIO_DEBUG1
01860 RioErr << "[CStreamControl - Stop] Start" << endl;
01861 #endif
01862
01863 if( m_SendThread != 0 )
01864 pthread_cancel( m_SendThread );
01865 if( m_PoolThread != 0 )
01866 pthread_cancel( m_PoolThread );
01867 if( m_ClientsThread != 0 )
01868 pthread_cancel( m_ClientsThread );
01869 if( m_UpdateThread != 0 )
01870 pthread_cancel( m_UpdateThread );
01871 if( m_SendData != NULL )
01872 delete m_SendData;
01873 if( m_FPool != NULL )
01874 delete m_FPool;
01875 if( m_ClientHeap != NULL )
01876 delete m_ClientHeap;
01877
01878 m_SendData = NULL;
01879 m_FPool = NULL;
01880 m_ClientHeap = NULL;
01881 m_SendThread = 0;
01882 m_PoolThread = 0;
01883 m_ClientsThread = 0;
01884 m_UpdateThread = 0;
01885 m_RioNeti = NULL;
01886 m_Started = false;
01887
01888 #ifdef RIO_DEBUG2
01889 TotalSendFragments = 0;
01890 #endif
01891
01892 #ifdef RIO_DEBUG1
01893 RioErr << "[CStreamControl - Stop] Finish" << endl;
01894 #endif
01895 }
01896
01897
01898
01899
01900 int CStreamControl::Put( NetBuf *nbp )
01901 {
01902 FragmentInfo *fip;
01903
01904 #ifdef RIO_DEBUG1
01905 RioErr << "[CStreamControl - Put] Start" << endl;
01906 #endif
01907
01908
01909
01910
01911
01912 if( m_Started )
01913 {
01914
01915
01916 fip = new FragmentInfo;
01917 if( fip == NULL )
01918 {
01919
01920 #ifdef RIO_DEBUG1
01921 RioErr << "[CStreamControl - Put] Finish1" << endl;
01922 #endif
01923
01924 return ERROR_STREAMCONTROL + ERROR_MEMORY;
01925 }
01926
01927
01928 if( nbp->GetFragmentInfo( &fip->FragmentData, &fip->FragmentSize ) !=
01929 S_OK )
01930 {
01931
01932 #ifdef RIO_DEBUG1
01933 RioErr << "[CStreamControl - Put] Finish2" << endl;
01934 #endif
01935
01936 delete fip;
01937 return ERROR_STREAMCONTROL + ERROR_MEMORY;
01938 }
01939
01940 nbp->GetIPAndPort( &fip->IP, &fip->Port );
01941
01942 fip->TimeBetweenCredits = nbp->GetTimeBetweenCredits();
01943
01944 #ifdef RIO_DEBUG1
01945 RioErr << "[CStreamControl - Put] Finish3" << endl;
01946 #endif
01947
01948 return m_FPool->Insert( fip );
01949 }
01950 else
01951 {
01952
01953 #ifdef RIO_DEBUG1
01954 RioErr << "[CStreamControl - Put] Finish4" << endl;
01955 #endif
01956
01957 return ERROR_STREAMCONTROL + ERROR_NOT_STARTED;
01958 }
01959 }