00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "PLSessionManager.h"
00019
00020 #include <limits.h>
00021
00022 InfoClient *PLSessionManager::m_InfoClient = NULL;
00023 InfoClient *PLSessionManager::m_InactiveClient = NULL;
00024 CommunicationStream *PLSessionManager::CommMulticastStream = NULL;
00025
00026 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00027 const char* const LOGFILE = "RIOPLSessionManager.log";
00028 const char* const LOGREQUEST = "RIOPLLogRequest.log";
00029 const char* const LOGCLIENTS = "RIOPLLogClients.log";
00030 #endif
00031
00032
00033
00034
00035
00036
00037 static int MulticastPortInfo[5000][2];
00038
00039
00040
00041 const char CONFIGNAME[] = "RIOPLsystem.cfg";
00042
00043 const char CONFIGSSUBDIR[] = "etc/RIO/";
00044
00045 const char LOGSSUBDIR[] = "var/log/RIO/";
00046
00047 PLSessionManager::PLSessionManager()
00048 {
00049 #ifdef RIO_DEBUG1
00050 RioErr << "### [PLSessionManager - Constructor] Start" << endl;
00051 #endif
00052
00053 BlockSize = 0;
00054 RIOServer = NULL;
00055 StreamManager = NULL;
00056 ObjectManager = NULL;
00057 ObjectRioServer = NULL;
00058 RequestRioServer = NULL;
00059 next_group = 1;
00060
00061 m_TCPconnection = 0;
00062 m_ConnectionThreadId = 0;
00063 m_initialized = false;
00064 m_MulticastIP = strdup( "0.0.0.0" );
00065 m_DeltaAfter = 0;
00066 m_DeltaBefore = 0;
00067 m_num_ports = 100;
00068 m_inicial_multicast_port = 30000;
00069
00070 pthread_mutex_init( &InfoClientMutex , NULL );
00071 pthread_mutex_init( &MoveMutex , NULL );
00072 pthread_mutex_init( &MulticastPortInfoMutex, NULL );
00073
00074 #ifdef RIO_DEBUG1
00075 RioErr << "### [PLSessionManager - Constructor] Finish" << endl;
00076 #endif
00077
00078 }
00079
00080 PLSessionManager::~PLSessionManager()
00081 {
00082 #ifdef RIO_DEBUG1
00083 RioErr << "### [PLSessionManager - Destructor] Start" << endl;
00084 #endif
00085
00086 CleanUp();
00087
00088 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00089 m_log.close();
00090 m_requestlog.close();
00091 m_log_timer.close();
00092 #endif
00093
00094 #ifdef RIO_DEBUG1
00095 RioErr << "### [PLSessionManager - Constructor] Finish" << endl;
00096 #endif
00097
00098
00099
00100
00101 if( m_ConfigsDirectory != NULL )
00102 delete[] m_ConfigsDirectory;
00103
00104 if( m_LogsDirectory != NULL )
00105 delete[] m_LogsDirectory;
00106 }
00107
00108
00109 int PLSessionManager::ReadConfiguration()
00110 {
00111 #ifdef RIO_DEBUG1
00112 RioErr << "### [PLSessionManager - ReadConfiguration] Start" << endl;
00113 #endif
00114
00115 enum keys
00116 {
00117 RIOSERVER,
00118 INICIALMULTICASTPORT,
00119 TOTALPORTS,
00120 DELTAWINDOW,
00121 MULTICASTIP,
00122 DELTABEFORE,
00123 DELTAAFTER
00124 };
00125
00126 static struct token_kw mykeys[] =
00127 {
00128 {"RioServer" , RIOSERVER },
00129 {"InicialMulticastPort" , INICIALMULTICASTPORT},
00130 {"TotalPorts" , TOTALPORTS },
00131 {"DeltaWindow" , DELTAWINDOW },
00132 {"MulticastIP" , MULTICASTIP },
00133 {"DeltaBefore" , DELTABEFORE },
00134 {"DeltaAfter" , DELTAAFTER },
00135 {NULL , 0 }
00136 };
00137
00138 token tok;
00139
00140
00141 char ConfigFileName[ MaxPathSize ];
00142
00143 strcpy( ConfigFileName, m_ConfigsDirectory );
00144 strcat( ConfigFileName, CONFIGNAME );
00145
00146 if( tok.openfile( ConfigFileName ) )
00147 {
00148 #ifdef RIO_DEBUG1
00149 RioErr << "### [PLSessionManager - ReadConfiguration] Finish 1" << endl;
00150 #endif
00151
00152 return 1;
00153 }
00154
00155 int word;
00156
00157 while( ( word = tok.parseline( mykeys ) ) >= 0 )
00158 {
00159 switch( word )
00160 {
00161 case RIOSERVER:
00162 tok.getstrnew( &RIOServer );
00163 break;
00164
00165 case INICIALMULTICASTPORT:
00166 tok.getnumber( &m_inicial_multicast_port );
00167 break;
00168
00169 case TOTALPORTS:
00170 tok.getnumber( &m_num_ports );
00171 break;
00172
00173 case MULTICASTIP:
00174 if( m_MulticastIP != NULL )
00175 free( m_MulticastIP );
00176 tok.getstrnew( &m_MulticastIP );
00177 break;
00178
00179 case DELTABEFORE:
00180 tok.getnumber( &m_DeltaBefore );
00181 break;
00182
00183 case DELTAAFTER:
00184 tok.getnumber( &m_DeltaAfter );
00185 break;
00186
00187 default:
00188 tok.emsg( "{default} invalid word" );
00189 continue;
00190 }
00191 tok.ckend();
00192 }
00193
00194 #ifdef RIO_DEBUG1
00195 RioErr << "### [PLSessionManager - ReadConfiguration] Finish 2" << endl;
00196 #endif
00197
00198 return CheckConfiguration();
00199 }
00200
00201
00202 int PLSessionManager::CheckConfiguration()
00203 {
00204 #ifdef RIO_DEBUG1
00205 RioErr << "### [PLSessionManager - CheckConfiguration] Start" << endl;
00206 #endif
00207
00208 int status = 1;
00209
00210 if( ( RIOServer == NULL ) or ( strcmp( RIOServer, "" ) == 0 ) )
00211 {
00212 status = 0;
00213 RioErr << "Invalid RioServer" << endl;
00214 }
00215
00216 if( m_inicial_multicast_port <= 1024 )
00217 {
00218 status = 0;
00219 RioErr << "Invalid InicialMulticastPort" << endl;
00220 }
00221
00222 if( m_num_ports <= 0 )
00223 {
00224 status = 0;
00225 RioErr << "Invalid TotalPorts" << endl;
00226 }
00227
00228 #ifdef RIO_DEBUG1
00229 RioErr << "### [PLSessionManager - CheckConfiguration] Finish" << endl;
00230 #endif
00231
00232 return( status );
00233 }
00234
00235
00236 void PLSessionManager::InitializeMulticastPortInfo( )
00237 {
00238 #ifdef RIO_DEBUG1
00239 RioErr << "### [PLSessionManager - InitializeMulticastPortInfo] Start" << endl;
00240 #endif
00241
00242 int i;
00243 int port;
00244
00245 port = m_inicial_multicast_port;
00246
00247 for( i = 0; i < m_num_ports; i++ )
00248 {
00249 MulticastPortInfo[i][0] = port;
00250 MulticastPortInfo[i][1] = 0;
00251 port++;
00252 }
00253
00254 #ifdef RIO_DEBUG1
00255 RioErr << "### [PLSessionManager - InitializeMulticastPortInfo] Finish" << endl;
00256 #endif
00257 }
00258
00259
00260 int PLSessionManager::Initialize( char *PrefixDirectory )
00261 {
00262 #ifdef RIO_DEBUG1
00263 RioErr << "### [PLSessionManager - Initialize] Start" << endl;
00264 #endif
00265
00266 int status;
00267 pthread_t lThread;
00268 pthread_t rThread;
00269 pthread_attr_t attribl;
00270 pthread_attr_t attribr;
00271 pthread_attr_t attribConnection;
00272
00273 pthread_attr_init( &attribl );
00274 pthread_attr_init( &attribr );
00275 pthread_attr_init( &attribConnection );
00276 pthread_attr_setstacksize( &attribl , 3*PTHREAD_STACK_MIN );
00277 pthread_attr_setstacksize( &attribr , 3*PTHREAD_STACK_MIN );
00278 pthread_attr_setstacksize( &attribConnection, 3*PTHREAD_STACK_MIN );
00279
00280
00281 if( m_initialized )
00282 {
00283 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00284 m_log << "Initialize(): Tried to initialize component "
00285 " already initialized" << endl;
00286 #endif
00287
00288 #ifdef RIO_DEBUG1
00289 RioErr << "### [PLSessionManager - Initialize] Finish 1" << endl;
00290 #endif
00291
00292 return ERROR_PLSESSIONMANAGER + ERROR_INITIALIZED;
00293 }
00294
00295
00296
00297 if( PrefixDirectory == NULL )
00298 {
00299
00300
00301
00302
00303 m_ConfigsDirectory = new char[ 1 ];
00304 if( m_ConfigsDirectory == NULL )
00305 {
00306 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00307 RioErr << "Initialize(): Memory allocation error" << endl;
00308 #endif
00309
00310 #ifdef RIO_DEBUG1
00311 RioErr << "### [PLSessionManager - Initialize] Finish 2" << endl;
00312 #endif
00313
00314 return ERROR_PLSESSIONMANAGER + ERROR_MEMORY;
00315 }
00316 m_LogsDirectory = new char[ 1 ];
00317 if( m_LogsDirectory == NULL )
00318 {
00319 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00320 RioErr << "Initialize(): Memory allocation error" << endl;
00321 #endif
00322
00323 #ifdef RIO_DEBUG1
00324 RioErr << "### [PLSessionManager - Initialize] Finish 3" << endl;
00325 #endif
00326
00327 delete[] m_ConfigsDirectory;
00328 return ERROR_PLSESSIONMANAGER + ERROR_MEMORY;
00329 }
00330 m_ConfigsDirectory[ 0 ] = 0;
00331 m_LogsDirectory[ 0 ] = 0;
00332 }
00333 else
00334 {
00335
00336
00337 unsigned int PrefixDirectorySize, ConfigsDirectorySize,
00338 LogsDirectorySize;
00339 PrefixDirectorySize = strlen( PrefixDirectory );
00340 ConfigsDirectorySize = PrefixDirectorySize + strlen( CONFIGSSUBDIR ) +
00341 1;
00342 LogsDirectorySize = PrefixDirectorySize + strlen( LOGSSUBDIR ) + 1;
00343
00344 if( PrefixDirectory[ PrefixDirectorySize - 1 ] != '/' )
00345 {
00346
00347
00348
00349 ConfigsDirectorySize++;
00350 LogsDirectorySize++;
00351 }
00352 m_ConfigsDirectory = new char[ ConfigsDirectorySize ];
00353 if( m_ConfigsDirectory == NULL )
00354 {
00355 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00356 RioErr << "Initialize(): Memory allocation error" << endl;
00357 #endif
00358
00359 #ifdef RIO_DEBUG1
00360 RioErr << "### [PLSessionManager - Initialize] Finish 4" << endl;
00361 #endif
00362
00363 return ERROR_PLSESSIONMANAGER + ERROR_MEMORY;
00364 }
00365 m_LogsDirectory = new char[ LogsDirectorySize ];
00366 if( m_LogsDirectory == NULL )
00367 {
00368 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00369 RioErr << "Initialize(): Memory allocation error" << endl;
00370 #endif
00371
00372 #ifdef RIO_DEBUG1
00373 RioErr << "### [PLSessionManager - Initialize] Finish 5" << endl;
00374 #endif
00375
00376 delete[] m_ConfigsDirectory;
00377 return ERROR_PLSESSIONMANAGER + ERROR_MEMORY;
00378 }
00379 strcpy( m_ConfigsDirectory, PrefixDirectory );
00380 strcpy( m_LogsDirectory, PrefixDirectory );
00381 if( PrefixDirectory[ PrefixDirectorySize - 1 ] != '/' )
00382 {
00383
00384
00385 strcat( m_ConfigsDirectory, "/" );
00386 strcat( m_LogsDirectory, "/" );
00387 }
00388 strcat( m_ConfigsDirectory, CONFIGSSUBDIR );
00389 strcat( m_LogsDirectory, LOGSSUBDIR );
00390 }
00391
00392 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00393 gettimeofday( &time_list , NULL );
00394 gettimeofday( &initial_time, NULL );
00395 gettimeofday( &final_time , NULL );
00396
00397
00398 char LogFileName[ MaxPathSize ];
00399
00400 strcpy( LogFileName, m_LogsDirectory );
00401 strcat( LogFileName, LOGCLIENTS );
00402 m_log_timer.open( LogFileName );
00403 m_log_timer << "Multicast "
00404 << "Patching "
00405 << "Ativos "
00406 << "Inativos "
00407 << "Tempo" << endl;
00408 strcpy( LogFileName, m_LogsDirectory );
00409 strcat( LogFileName, LOGFILE );
00410 m_log.open( LogFileName );
00411 strcpy( LogFileName, m_LogsDirectory );
00412 strcat( LogFileName, LOGREQUEST );
00413 m_requestlog.open( LogFileName );
00414 #endif
00415
00416 m_TCPconnection = new CRioTCP;
00417 if( m_TCPconnection == NULL )
00418 {
00419 RioErr << "Imposs�vel cria��o da m_TCPconnection." << endl;
00420
00421 #ifdef RIO_DEBUG1
00422 RioErr << "### [PLSessionManager - Initialize] Finish 6" << endl;
00423 #endif
00424
00425 return ERROR_PLSESSIONMANAGER;
00426 }
00427
00428 status = ReadConfiguration();
00429 if( !status )
00430 {
00431 #ifdef RIO_DEBUG1
00432 RioErr << "### [PLSessionManager - Initialize] Finish 7" << endl;
00433 #endif
00434
00435 return ERROR_PLSESSIONMANAGER;
00436 }
00437
00438
00439 CommMulticastStream = new CommunicationStream( 0, this, 0, 0, 0, m_LogsDirectory );
00440
00441 if( CommMulticastStream == NULL )
00442 {
00443 RioErr << "Imposs�vel cria��o da CommMulticastStream." << endl;
00444
00445 #ifdef RIO_DEBUG1
00446 RioErr << "### [PLSessionManager - Initialize] Finish 8" << endl;
00447 #endif
00448
00449 return ERROR_PLSESSIONMANAGER;
00450 }
00451
00452 CommunicationStreamThreadParam *p;
00453 p = new CommunicationStreamThreadParam();
00454
00455 if( p == NULL )
00456 {
00457 RioErr << "Imposs�vel cria��o da CommunicationStreamThreadParam."
00458 << endl;
00459 return ERROR_PLSESSIONMANAGER;
00460 }
00461
00462 p->port = COMMPORT;
00463 p->stream = CommMulticastStream;
00464
00465 InitializeMulticastPortInfo();
00466
00467 if( pthread_create( &lThread, &attribl,
00468 CommunicationStream::startListenThread, p ) != 0
00469 )
00470 {
00471 RioErr << "Could not create startlisten thread" << endl;
00472
00473 #ifdef RIO_DEBUG1
00474 RioErr << "### [PLSessionManager - Initialize] Finish 9" << endl;
00475 #endif
00476
00477 return( 0 );
00478 }
00479
00480 if( pthread_create( &rThread, &attribr,
00481 CommunicationStream::startReceiveThread, p ) != 0
00482 )
00483 {
00484 #ifdef RIO_DEBUG1
00485 RioErr << "### [PLSessionManager - Initialize] Finish 10" << endl;
00486 #endif
00487
00488 RioErr << "Could not create startreceive thread" << endl;
00489 return( 0 );
00490 }
00491
00492
00493
00494
00495
00496
00497 StreamManager = new PLStreamManager( m_LogsDirectory );
00498 ObjectManager = new PLObjectManager( m_LogsDirectory );
00499
00500 int hResult = m_TCPconnection->InitServer( RioTCPportPL );
00501
00502 if( hResult )
00503 {
00504 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00505 m_log << "Initialize(): Failed to Initialize server TCP connection, "
00506 << "rc: " << hResult << endl;
00507 #endif
00508
00509 CleanUp();
00510
00511 #ifdef RIO_DEBUG1
00512 RioErr << "### [PLSessionManager - Initialize] Finish 11" << endl;
00513 #endif
00514
00515 return hResult;
00516 }
00517
00518 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00519 m_log << "-----------------------------------------" << endl;
00520 m_log << "Patching Layer - RIO" << endl;
00521
00522 if( RIOServer == NULL )
00523 m_log << "RIO Server: NULL" << endl;
00524 else
00525 m_log << "RIO Server: " << RIOServer << endl;
00526
00527 m_log << "Total Ports " << m_num_ports << endl;
00528 m_log << "Inicial Multicast Port " << m_inicial_multicast_port << endl;
00529 m_log << "Multicast IP " << m_MulticastIP << endl;
00530 m_log << "Delta After: " << m_DeltaAfter << " blocks" << endl;
00531 m_log << "Delta Before: " << m_DeltaBefore << " blocks" << endl;
00532 m_log << "-----------------------------------------" << endl;
00533 #endif
00534
00535 if( pthread_create( &m_ConnectionThreadId, &attribConnection,
00536 &ConnectionThread, this )
00537 )
00538 {
00539 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00540 m_log << "Start(): Failed to create thread (connection thread)" << endl;
00541 m_log << " ERROR: " << strerror(errno) << endl;
00542 #endif
00543
00544 CleanUp();
00545
00546 #ifdef RIO_DEBUG1
00547 RioErr << "### [PLSessionManager - Initialize] Finish 12" << endl;
00548 #endif
00549
00550 return ERROR_PLSESSIONMANAGER + ERROR_CREATE_THREAD;
00551 }
00552
00553 m_initialized = true;
00554
00555 #ifdef RIO_DEBUG1
00556 RioErr << "### [PLSessionManager - Initialize] Finish 13" << endl;
00557 #endif
00558
00559 return S_OK;
00560 }
00561
00562
00563 int PLSessionManager::CleanUp()
00564 {
00565 #ifdef RIO_DEBUG1
00566 RioErr << "### [PLSessionManager - CleanUp] Start" << endl;
00567 #endif
00568
00569 if( m_TCPconnection != 0 )
00570 {
00571 m_TCPconnection->Disconnect();
00572 delete( m_TCPconnection );
00573 }
00574
00575 if( RequestRioServer != 0 )
00576 {
00577 if( RequestRioServer->Buffer != 0 )
00578 delete( (char *)RequestRioServer->Buffer );
00579
00580 delete( RequestRioServer );
00581 }
00582
00583 if( ObjectRioServer != 0 )
00584 {
00585 delete( ObjectRioServer );
00586 }
00587
00588 #ifdef RIO_DEBUG1
00589 RioErr << "### [PLSessionManager - CleanUp] Finish" << endl;
00590 #endif
00591
00592 return S_OK;
00593 }
00594
00595
00596 void *PLSessionManager::ConnectionThread( void * Param )
00597 {
00598 #ifdef RIO_DEBUG1
00599 RioErr << "### [PLSessionManager - ConnectionThread] Start" << endl;
00600 #endif
00601
00602 #ifdef RIO_DEBUG2
00603 RioErr << "PID da ConnectionThread = " << pthread_self() << endl;
00604 #endif
00605
00606 CRioTCP *TCPconnection;
00607 SOCKET ConnectedSocket;
00608
00609
00610 PLSessionManager *SessionManager = (PLSessionManager *) Param;
00611
00612
00613 TCPconnection = SessionManager->m_TCPconnection;
00614 while( true )
00615 {
00616
00617 #ifdef RIO_DEBUG2
00618 RioErr << "CVS tag version: " << (char*) VERSION << endl;
00619 RioErr << "PL Direcionada para " << SessionManager->RIOServer << endl;
00620 #endif
00621
00622 RioErr << "Waiting Connections..." << endl;
00623 HRESULT hResult = TCPconnection->WaitConnection( &ConnectedSocket );
00624
00625
00626 if( FAILED( hResult ) )
00627 {
00628 TCPconnection->Disconnect();
00629
00630 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00631 SessionManager->m_log << "ConnectionThread: "
00632 << "Connection waiting failed" << endl;
00633 #endif
00634
00635 #ifdef RIO_DEBUG1
00636 RioErr << "### [PLSessionManager - ConnectionThread] Finish 1" << endl;
00637 #endif
00638
00639 return(0);
00640 }
00641
00642 else
00643 {
00644 ClientThreadParameter *Param = new ClientThreadParameter;
00645 Param->Socket = ConnectedSocket;
00646 Param->SessionManager = SessionManager;
00647 pthread_t junk;
00648
00649 Param->RemoteAddress = TCPconnection->m_RemoteAddress;
00650
00651 pthread_attr_t attrib;
00652 pthread_attr_init( &attrib );
00653 pthread_attr_setstacksize( &attrib, 3*PTHREAD_STACK_MIN );
00654
00655 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00656 SessionManager->m_log << "Starting Client Thread" << endl;
00657 #endif
00658
00659 if( pthread_create( &junk, &attrib, &ClientThread, (void*) Param ) )
00660 {
00661 delete Param;
00662
00663 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00664 SessionManager->m_log << "ConnectionThread: "
00665 << "Failed to create client thread"
00666 << endl;
00667 #endif
00668
00669 shutdown( ConnectedSocket, 2 );
00670 }
00671 else
00672 pthread_detach( junk );
00673 }
00674 }
00675
00676 #ifdef RIO_DEBUG1
00677 RioErr << "### [PLSessionManager - ConnectionThread] Finish 2" << endl;
00678 #endif
00679
00680 return 0;
00681 }
00682
00683
00684 void receiveAlarm( int i )
00685 {
00686 #ifdef RIO_DEBUG1
00687 RioErr << "### [PLSessionManager - receiveAlarm] Single" << endl;
00688 #endif
00689
00690 }
00691
00692
00693 void PLSessionManager::KillClientThread( CRioSession *SessionRioServer,
00694 CRioStream *StreamRioServer,
00695 CRioObject *ObjectRioServer,
00696 RioRequest *RequestRioServer )
00697 {
00698 #ifdef RIO_DEBUG1
00699 RioErr << "### [PLSessionManager - KillClientThread] Start" << endl;
00700 #endif
00701
00702
00703 DeleteClient( pthread_self( ) );
00704
00705 if( SessionRioServer )
00706 delete( SessionRioServer );
00707
00708 if( StreamRioServer )
00709 delete( StreamRioServer );
00710
00711 if( ObjectRioServer )
00712 delete( ObjectRioServer );
00713
00714 if( RequestRioServer )
00715 {
00716 if( RequestRioServer->Buffer != 0 )
00717 delete( (char *)RequestRioServer->Buffer );
00718
00719 delete( RequestRioServer );
00720 }
00721
00722 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00723 m_log << "Client Deleted" << endl;
00724 #endif
00725
00726 #ifdef RIO_DEBUG1
00727 RioErr << "### [PLSessionManager - KillClientThread] Finish" << endl;
00728 #endif
00729
00730 pthread_exit( 0 );
00731 }
00732
00733
00734 void *PLSessionManager::ClientThread( void * Param )
00735 {
00736 #ifdef RIO_DEBUG1
00737 RioErr << "### [PLSessionManager - ClientThread] Start" << endl;
00738 #endif
00739
00740 ClientThreadParameter *ClientParam = ( ClientThreadParameter * ) Param;
00741 SOCKET sock = ClientParam->Socket;
00742 SOCKADDR_IN remoteAddress = ClientParam->RemoteAddress;
00743 PLSessionManager *SessionManager = ClientParam->SessionManager;
00744
00745 unsigned int Class;
00746 unsigned int Method;
00747 unsigned int Size;
00748 unsigned int ParameterSize = MaxTCPDataSize;
00749 unsigned int ResultSize = MaxTCPDataSize;
00750 char *Parameter = (char*) malloc( MaxTCPDataSize*sizeof(char) );
00751 char *Result = (char*) malloc( MaxTCPDataSize*sizeof(char) );
00752
00753 if( Parameter == NULL or Result == NULL )
00754 {
00755 RioErr << "malloc error ClientThread:" << strerror(errno) << endl;
00756 free( Parameter );
00757 free( Result );
00758
00759 return 0;
00760 }
00761
00762 memset( Parameter, 0, MaxTCPDataSize*sizeof( char ) );
00763 memset( Result , 0, MaxTCPDataSize*sizeof( char ) );
00764
00765
00766
00767
00768
00769
00770
00771 HRESULT Status;
00772 CRioTCP TCPconnection;
00773
00774
00775 Status = TCPconnection.AcceptConnection( sock );
00776 if( FAILED( Status ) )
00777 {
00778 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00779 SessionManager->m_log << "Error accepting TCP connection at "
00780 << "ClienThread. Exiting Session thread."
00781 << endl;
00782 #endif
00783
00784 free( Parameter );
00785 free( Result );
00786
00787 #ifdef RIO_DEBUG1
00788 RioErr << "### [PLSessionManager - ClientThread] Finish 1" << endl;
00789 #endif
00790
00791 return( 0 );
00792 }
00793
00794 sigset_t set;
00795 struct sigaction action;
00796
00797 sigemptyset( &set );
00798
00799 action.sa_handler = receiveAlarm;
00800 action.sa_mask = set;
00801 action.sa_flags = SA_RESTART;
00802 pthread_sigmask( SIG_UNBLOCK, &set, NULL );
00803 sigaction( SIGUSR1, &action, NULL );
00804
00805 CRioSession *SessionRioServer;
00806 SessionRioServer = new CRioSession;
00807
00808 CRioStream *StreamRioServer;
00809 StreamRioServer = new CRioStream;
00810
00811 CRioObject *ObjectRioServer;
00812 ObjectRioServer = new CRioObject;
00813
00814
00815 RioRequest *RequestRioServer;
00816 RequestRioServer = new RioRequest;
00817
00818 while( true )
00819 {
00820
00821 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00822 SessionManager->m_log << "Before Receive Call" << endl;
00823 #endif
00824
00825 Status = TCPconnection.ReceiveCall( &Class,
00826 &Method,
00827 Parameter,
00828 ParameterSize );
00829
00830
00831 if( FAILED( Status ) )
00832 {
00833 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00834 SessionManager->m_log << "ReceiveCall Error" << endl;
00835 #endif
00836
00837 TCPconnection.Disconnect();
00838
00839 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00840 SessionManager->m_log << "[PLSessionManager]: "
00841 << "Retirando cliente da lista" << endl;
00842 #endif
00843
00844 SessionManager->KillClientThread( SessionRioServer,
00845 StreamRioServer,
00846 ObjectRioServer,
00847 RequestRioServer );
00848
00849 delete( SessionManager );
00850
00851 free( Parameter );
00852 free( Result );
00853
00854 #ifdef RIO_DEBUG1
00855 RioErr << "### [PLSessionManager - ClientThread] Finish 2" << endl;
00856 #endif
00857
00858 return( 0 );
00859 }
00860 else
00861 {
00862 Size = ResultSize;
00863
00864 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00865 char *info = myInfo();
00866 SessionManager->m_log << info << "------------New Call Received"
00867 << "-------------" << endl;
00868 free( info );
00869 #endif
00870
00871
00872 Status = SessionManager->ExecuteCall( Class,
00873 Method,
00874 Parameter,
00875 ParameterSize,
00876 Result,
00877 &Size,
00878 SessionRioServer,
00879 StreamRioServer,
00880 ObjectRioServer,
00881 RequestRioServer,
00882 remoteAddress );
00883
00884
00885 if( FAILED( Status ) )
00886 {
00887 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00888 SessionManager->m_log << "[PLSessionManager]: "
00889 << "Sending TCP error message " << endl;
00890 #endif
00891
00892 Status = TCPconnection.SendCallError( Status );
00893 }
00894 else
00895 {
00896 Status = TCPconnection.SendResult( Result, Size );
00897 }
00898
00899 if( ( Class == RioClassSessionManager ) &&
00900 ( Method == RioMethodSessionManagerClose )
00901 )
00902 {
00903 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00904 SessionManager->m_log << "[PLSessionManager]: "
00905 << "Retirando cliente da lista" << endl;
00906 #endif
00907
00908 TCPconnection.Disconnect();
00909 SessionManager->KillClientThread( SessionRioServer,
00910 StreamRioServer,
00911 ObjectRioServer,
00912 RequestRioServer );
00913
00914 delete( SessionManager );
00915
00916 free( Parameter );
00917 free( Result );
00918
00919 #ifdef RIO_DEBUG1
00920 RioErr << "### [PLSessionManager - ClientThread] Finish 3" << endl;
00921 #endif
00922
00923 return( 0 );
00924 }
00925 }
00926 }
00927
00928 free( Parameter );
00929 free( Result );
00930
00931 #ifdef RIO_DEBUG1
00932 RioErr << "### [PLSessionManager - ClientThread] Finish 4" << endl;
00933 #endif
00934
00935 return( 0 );
00936 }
00937
00938 void PLSessionManager::ChangePIDPort( pid_t actualLeader_pid,
00939 pid_t newLeader_pid )
00940 {
00941 #ifdef RIO_DEBUG1
00942 RioErr << "### [PLSessionManager - ChangePIDPort] Start" << endl;
00943 #endif
00944
00945 int i;
00946
00947 MulticastPortInfoMutexLock( "chpidp1" );
00948
00949 for( i = 0; i < m_num_ports; i++ )
00950 {
00951 if( MulticastPortInfo[i][1] == actualLeader_pid )
00952 {
00953 #ifdef RIO_DEBUG2
00954 RioErr << "[ChangePIDPort] Porta " << MulticastPortInfo[i][0]
00955 << " sendo transferida de cliente " << actualLeader_pid
00956 << " para cliente " << newLeader_pid << endl;
00957 #endif
00958
00959 MulticastPortInfo[i][1] = newLeader_pid;
00960 break;
00961 }
00962 }
00963
00964 if( i == m_num_ports )
00965 {
00966 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
00967 m_log << "[PLSessionManager]: No Port Available" << endl;
00968 #endif
00969 }
00970
00971 MulticastPortInfoMutexUnlock( "chpidp1" );
00972
00973 #ifdef RIO_DEBUG1
00974 RioErr << "### [PLSessionManager - ChangePIDPort] Finish 1" << endl;
00975 #endif
00976
00977 }
00978
00979 int PLSessionManager::ChooseMulticastPort( pid_t pid )
00980 {
00981 #ifdef RIO_DEBUG1
00982 RioErr << "### [PLSessionManager - ChooseMulticastPort] Start" << endl;
00983 #endif
00984
00985 int i;
00986 int status = 0;
00987
00988 MulticastPortInfoMutexLock( "chmtcspt1" );
00989
00990 for( i = 0; i < m_num_ports; i++ )
00991 {
00992 if( MulticastPortInfo[i][1] == 0 )
00993 {
00994 #ifdef RIO_DEBUG2
00995 RioErr << "[ChooseMulticastPort] Porta " << MulticastPortInfo[i][0]
00996 << " sendo alocada para cliente " << pid << endl;
00997 #endif
00998
00999 MulticastPortInfo[i][1] = pid;
01000 status = MulticastPortInfo[i][0];
01001 break;
01002 }
01003 }
01004
01005 if( i == m_num_ports )
01006 {
01007 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
01008 m_log << "[PLSessionManager]: No Port Available" << endl;
01009 #endif
01010 }
01011
01012 MulticastPortInfoMutexUnlock( "chmtcspt1" );
01013
01014 #ifdef RIO_DEBUG1
01015 RioErr << "### [PLSessionManager - ChooseMulticastPort] Finish" << endl;
01016 #endif
01017
01018 return( status );
01019 }
01020
01021 void PLSessionManager::DischargeMulticastPort( pid_t pid )
01022 {
01023 #ifdef RIO_DEBUG1
01024 RioErr << "### [PLSessionManager - DischargeMulticastPort] Start" << endl;
01025 #endif
01026
01027 int i;
01028
01029 MulticastPortInfoMutexLock( "dsmtpr1" );
01030
01031 for( i = 0; i < m_num_ports; i++ )
01032 {
01033 if( MulticastPortInfo[i][1] == pid )
01034 {
01035 #ifdef RIO_DEBUG2
01036 RioErr << "[PLSessionManager] Discharging MulticastPort "
01037 << MulticastPortInfo[i][0] << endl;
01038 #endif
01039
01040 MulticastPortInfo[i][1] = 0;
01041 break;
01042 }
01043 }
01044
01045 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
01046 if( i == m_num_ports )
01047 m_log << "[PLSessionManager]: Invalid Port" << endl;
01048 #endif
01049
01050 MulticastPortInfoMutexUnlock( "dsmtpr1" );
01051
01052 #ifdef RIO_DEBUG1
01053 RioErr << "### [PLSessionManager - DischargeMulticastPort] Finish 1" << endl;
01054 #endif
01055
01056 }
01057
01058
01059 int PLSessionManager::ExecuteSessionManager( unsigned int Method,
01060 char *Parameter,
01061 unsigned int ParameterSize,
01062 char *Result,
01063 unsigned int *ResultSize,
01064 CRioSession *SessionRioServer,
01065 CRioStream *StreamRioServer,
01066 RioRequest *RequestRioServer )
01067 {
01068 #ifdef RIO_DEBUG1
01069 RioErr << "### [PLSessionManager - ExecuteSessionManager] Start" << endl;
01070 #endif
01071
01072 int offset = 0;
01073
01074 switch( Method )
01075 {
01076
01077 case RioMethodSessionManagerOpen:
01078 {
01079 char *Server;
01080 char *UserName;
01081 char *Password;
01082 CRioTCP TCPconnection;
01083 HRESULT hresult;
01084
01085 offset = 0;
01086 Server = RIOServer;
01087
01088
01089
01090 if( !GetString( Parameter,ParameterSize,&UserName,&offset ) )
01091 {
01092 #ifdef RIO_DEBUG1
01093 RioErr << "### [PLSessionManager - ExecuteSessionManager] Finish 1" << endl;
01094 #endif
01095
01096 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
01097 }
01098
01099 if( !GetString( Parameter,ParameterSize,&Password,&offset ) )
01100 {
01101 #ifdef RIO_DEBUG1
01102 RioErr << "### [PLSessionManager - ExecuteSessionManager] Finish 2" << endl;
01103 #endif
01104
01105 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
01106 }
01107
01108 hresult = OpenSessionRioServer( Server,
01109 UserName,
01110 Password,
01111 SessionRioServer,
01112 RequestRioServer );
01113
01114 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
01115 if( FAILED( hresult ) )
01116 m_log << "[ExecuteSessionManager] Failed: "
01117 << GetErrorDescription( hresult ) << endl;
01118 #endif
01119
01120
01121 offset = 0;
01122
01123 SetLong( Result, hresult, &offset );
01124
01125 SetLong( Result, 2000, &offset );
01126 SetLong( Result, 1, &offset );
01127
01128 SetULong( Result, BlockSize, &offset );
01129
01130
01131 *ResultSize = offset;
01132
01133 #ifdef RIO_DEBUG1
01134 RioErr << "### [PLSessionManager - ExecuteSessionManager] Finish 3" << endl;
01135 #endif
01136
01137
01138
01139 return S_OK;
01140
01141 }
01142
01143
01144 case RioMethodSessionManagerClose:
01145 {
01146 RioSessionId Session;
01147 HRESULT hresult;
01148
01149 offset = 0;
01150
01151
01152
01153 if( !GetLong( Parameter,ParameterSize,&(Session.Version),
01154 &offset )
01155 )
01156 {
01157 #ifdef RIO_DEBUG1
01158 RioErr << "### [PLSessionManager - ExecuteSessionManager] Finish 4" << endl;
01159 #endif
01160
01161 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
01162 }
01163 if( !GetLong( Parameter,ParameterSize,&(Session.Index),&offset ) )
01164 {
01165 #ifdef RIO_DEBUG1
01166 RioErr << "### [PLSessionManager - ExecuteSessionManager] Finish 5" << endl;
01167 #endif
01168
01169 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
01170 }
01171
01172 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
01173 m_log << "[PLSessionManager]: Closing Session... " << endl;
01174 #endif
01175
01176 hresult = CloseSessionRioServer( SessionRioServer,
01177 ResultSize );
01178
01179 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
01180 if( FAILED( hresult ) )
01181 m_log << "[ExecuteSessionManager] Failed: "
01182 << GetErrorDescription( hresult ) << endl;
01183 #endif
01184
01185 offset = 0;
01186
01187
01188 SetLong( Result, hresult, &offset );
01189
01190
01191 *ResultSize = offset;
01192
01193 #ifdef RIO_DEBUG1
01194 RioErr << "### [PLSessionManager - ExecuteSessionManager] Finish 6" << endl;
01195 #endif
01196
01197
01198
01199 return S_OK;
01200 }
01201
01202
01203 case RioMethodSessionManagerOpenStream:
01204 {
01205 RioStreamTraffic Traffic;
01206 RioSessionId Session;
01207 RioStreamId Stream;
01208 HRESULT hresult;
01209
01210 offset = 0;
01211 Stream.Version = 0;
01212 Stream.Index = 0;
01213
01214
01215
01216 if( !GetLong( Parameter,ParameterSize,&( Session.Version ),
01217 &offset )
01218 )
01219 {
01220 #ifdef RIO_DEBUG1
01221 RioErr << "### [PLSessionManager - ExecuteSessionManager] Finish 7" << endl;
01222 #endif
01223
01224 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
01225 }
01226 if( !GetLong( Parameter,ParameterSize,&( Session.Index ),
01227 &offset )
01228 )
01229 {
01230 #ifdef RIO_DEBUG1
01231 RioErr << "### [PLSessionManager - ExecuteSessionManager] Finish 8" << endl;
01232 #endif
01233
01234 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
01235 }
01236
01237
01238 short shortvalue;
01239 if( !GetShort( Parameter,ParameterSize,&shortvalue,&offset ) )
01240 {
01241 #ifdef RIO_DEBUG1
01242 RioErr << "### [PLSessionManager - ExecuteSessionManager] Finish 9" << endl;
01243 #endif
01244
01245 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
01246 }
01247 Traffic.Type = ( RioTrafficType ) shortvalue;
01248
01249 if( !GetShort( Parameter,ParameterSize,&shortvalue,&offset ) )
01250 {
01251 #ifdef RIO_DEBUG1
01252 RioErr << "### [PLSessionManager - ExecuteSessionManager] Finish 10" << endl;
01253 #endif
01254
01255 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
01256 }
01257 Traffic.Direction = ( RioStreamDirection ) shortvalue;
01258
01259 if( !GetULong( Parameter,ParameterSize,
01260 &( Traffic.LogicalBlockSize ), &offset )
01261 )
01262 {
01263 #ifdef RIO_DEBUG1
01264 RioErr << "### [PLSessionManager - ExecuteSessionManager] Finish 11" << endl;
01265 #endif
01266
01267 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
01268 }
01269 if( !GetLong( Parameter,ParameterSize,&( Traffic.MaxRequests ),
01270 &offset ) )
01271 {
01272 #ifdef RIO_DEBUG1
01273 RioErr << "### [PLSessionManager - ExecuteSessionManager] Finish 12" << endl;
01274 #endif
01275
01276 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
01277 }
01278
01279 if( Traffic.Type == RIO_TRAFFIC_CBR )
01280 {
01281 if( !GetDouble( Parameter,ParameterSize,
01282 &( Traffic.TrafficCBR.Rate ),&offset ) )
01283 {
01284 #ifdef RIO_DEBUG1
01285 RioErr << "### [PLSessionManager - ExecuteSessionManager] Finish 13" << endl;
01286 #endif
01287
01288 return ( ERROR_PLCONNECTIONMANAGER +
01289 ERROR_INVALID_METHOD_PARAM );
01290 }
01291 }
01292 if( Traffic.Type == RIO_TRAFFIC_VBR )
01293 {
01294 if( !GetDouble( Parameter,ParameterSize,
01295 &(Traffic.TrafficVBR.Rate),&offset ) )
01296 {
01297 #ifdef RIO_DEBUG1
01298 RioErr << "### [PLSessionManager - ExecuteSessionManager] "
01299 << "Finish 14" << endl;
01300 #endif
01301
01302 return ( ERROR_PLCONNECTIONMANAGER +
01303 ERROR_INVALID_METHOD_PARAM );
01304 }
01305 }
01306 if( Traffic.Type == RIO_TRAFFIC_NRT )
01307 {
01308 if( !GetULong( Parameter,ParameterSize,
01309 &( Traffic.TrafficNRT.Reserved ),&offset ) )
01310 {
01311 #ifdef RIO_DEBUG1
01312 RioErr << "### [PLSessionManager - ExecuteSessionManager] "
01313 << "Finish 14" << endl;
01314 #endif
01315
01316 return ( ERROR_PLCONNECTIONMANAGER +
01317 ERROR_INVALID_METHOD_PARAM );
01318 }
01319 }
01320
01321 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
01322 m_log << "[PLSessionManager]: Opening stream ..." << endl;
01323 #endif
01324
01325 int maxrequests = 0;
01326 hresult = StreamManager->OpenStreamRioServer( Traffic,
01327 ResultSize,
01328 StreamRioServer,
01329 SessionRioServer,
01330 &maxrequests );
01331
01332 if( FAILED( hresult ) )
01333 {
01334 m_log << "[ExecuteSessionManager] Failed: "
01335 << GetErrorDescription( hresult ) << endl;
01336
01337 #ifdef RIO_DEBUG1
01338 RioErr << "### [PLSessionManager - ExecuteSessionManager] "
01339 << "Finish 16" << endl;
01340 #endif
01341
01342 return( hresult );
01343 }
01344
01345 offset = 0;
01346
01347
01348 SetLong( Result, hresult, &offset );
01349
01350
01351 SetLong( Result, Stream.Version, &offset );
01352 SetLong( Result, Stream.Index, &offset );
01353
01354
01355 SetLong( Result, maxrequests, &offset );
01356
01357
01358 *ResultSize = offset;
01359
01360 #ifdef RIO_DEBUG1
01361 RioErr << "### [PLSessionManager - ExecuteSessionManager] "
01362 << "Finish 17" << endl;
01363 #endif
01364
01365 return S_OK;
01366 }
01367
01368 default:
01369 {
01370 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
01371 m_log << "[PLSessionManager]: RioMethodSessionManagerOpenStream "
01372 << "Invalid Method: " << Method << endl;
01373 #endif
01374
01375 #ifdef RIO_DEBUG1
01376 RioErr << "### [PLSessionManager - ExecuteSessionManager] "
01377 << "Finish 18" << endl;
01378 #endif
01379
01380 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD;
01381 };
01382 }
01383
01384 #ifdef RIO_DEBUG1
01385 RioErr << "### [PLSessionManager - ExecuteSessionManager] Finish 19" << endl;
01386 #endif
01387
01388 }
01389
01390
01391 HRESULT PLSessionManager::CloseSessionRioServer( CRioSession *SessionRioServer,
01392 unsigned int *ResultSize )
01393 {
01394 #ifdef RIO_DEBUG1
01395 RioErr << "### [PLSessionManager - CloseSessionRioServer] Start" << endl;
01396 #endif
01397
01398
01399 HRESULT hresult = SessionRioServer->Disconnect();
01400
01401
01402 unsigned int Size;
01403
01404 Size = MAX_LONG_STRING_SIZE;
01405
01406 if( Size > *ResultSize )
01407 {
01408
01409 #ifdef RIO_DEBUG1
01410 RioErr << "### [PLSessionManager - CloseSessionRioServer] Finish 1" << endl;
01411 #endif
01412
01413 return ERROR_PLCONNECTIONMANAGER + ERROR_RESULT_MESSAGE_OVERFLOW;
01414 }
01415
01416 #ifdef RIO_DEBUG1
01417 RioErr << "### [PLSessionManager - CloseSessionRioServer] Finish 2" << endl;
01418 #endif
01419
01420 return( hresult );
01421 }
01422
01423
01424 HRESULT PLSessionManager::OpenSessionRioServer( char *Server,
01425 char *UserName,
01426 char *Password,
01427 CRioSession *SessionRioServer,
01428 RioRequest* RequestRioServer )
01429 {
01430 #ifdef RIO_DEBUG1
01431 RioErr << "### [PLSessionManager - OpenSessionRioServer] Start" << endl;
01432 #endif
01433
01434 HRESULT hresult;
01435
01436
01437 hresult = SessionRioServer->Connect( Server, UserName, Password, 1 );
01438
01439 if( FAILED(hresult) )
01440 {
01441 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
01442 m_log << "[PLSessionManager] OpenSessionRioServer Error1: "
01443 << GetErrorDescription( hresult ) << endl;
01444 #endif
01445
01446 #ifdef RIO_DEBUG2
01447 RioErr << "[PLSessionManager] Imposs�vel abrir sess�o com servidor:"
01448 << GetErrorDescription( hresult ) << endl;
01449 #endif
01450
01451 #ifdef RIO_DEBUG1
01452 RioErr << "### [PLSessionManager - OpenSessionRioServer] Finish 1" << endl;
01453 #endif
01454
01455 return (hresult);
01456 }
01457
01458 hresult = SessionRioServer->GetBlockSize( &BlockSize );
01459
01460 if( FAILED( hresult ) )
01461 {
01462 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
01463 m_log << "[PLSessionManager] OpenSessionRioServer Error2: "
01464 << GetErrorDescription( hresult ) << endl;
01465 #endif
01466 }
01467 else
01468 {
01469
01470 RequestRioServer->threadid = pthread_self();
01471 RequestRioServer->Size = BlockSize;
01472 RequestRioServer->Buffer = new char[BlockSize];
01473
01474 if( RequestRioServer->Buffer == 0 )
01475 {
01476 hresult = ERROR_USERMANAGER + ERROR_MEMORY;
01477 SessionRioServer->Disconnect();
01478 }
01479
01480 RequestRioServer->CallBackFunction = NULL;
01481 RequestRioServer->User = NULL;
01482 RequestRioServer->BufferStatus = 0;
01483 }
01484
01485 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
01486 m_log << " OK" << endl;
01487 #endif
01488
01489 #ifdef RIO_DEBUG1
01490 RioErr << "### [PLSessionManager - OpenSessionRioServer] Finish 2" << endl;
01491 #endif
01492
01493 return( hresult );
01494 }
01495
01496 InfoClient *PLSessionManager::FindClient( pid_t PIDClientThread )
01497 {
01498 #ifdef RIO_DEBUG1
01499 RioErr << "### [PLSessionManager - FindClient] Start" << endl;
01500 #endif
01501
01502
01503 InfoClient *first = m_InfoClient;
01504 InfoClient *aux;
01505
01506 #ifdef RIO_DEBUG2
01507 RioErr << "[FindClient] Finding client " << PIDClientThread << endl;
01508 #endif
01509
01510 while( first )
01511 {
01512 aux = first;
01513 while( aux )
01514 {
01515 if( aux->PIDClientThread == PIDClientThread )
01516 {
01517 #ifdef RIO_DEBUG1
01518 RioErr << "### [PLSessionManager - FindClient] Finish 1" << endl;
01519 #endif
01520
01521 return aux;
01522 }
01523 aux = aux->NextMember;
01524 }
01525
01526 first = first->NextGroup;
01527 }
01528
01529
01530 aux = m_InactiveClient;
01531 while( aux )
01532 {
01533 if( aux->PIDClientThread == PIDClientThread )
01534 {
01535 #ifdef RIO_DEBUG1
01536 RioErr << "### [PLSessionManager - FindClient] Finish 2" << endl;
01537 #endif
01538
01539 return aux;
01540 }
01541 aux = aux->NextMember;
01542 }
01543
01544 #ifdef RIO_DEBUG1
01545 RioErr << "### [PLSessionManager - FindClient] Finish 3" << endl;
01546 #endif
01547
01548 return NULL;
01549 }
01550
01551 int PLSessionManager::AllocClient( int clientPID, msgMove *moveData )
01552 {
01553 #ifdef RIO_DEBUG1
01554 RioErr << "### [PLSessionManager - AllocClient] Start" << endl;
01555 #endif
01556
01557 int block = (int) moveData->block;
01558 int new_block = -1;
01559
01560 int ModeMsgBlock;
01561
01562 MoveMutexLock( "alloccli1" );
01563
01564 InfoClient *client = FindClient( clientPID );
01565
01566 if( client == NULL )
01567 {
01568 #ifdef RIO_DEBUG2
01569 RioErr << "[AllocClient] Aloca��o imposs�vel: membro n�o foi "
01570 << "encontrado." << endl;
01571 #endif
01572
01573 #ifdef RIO_DEBUG1
01574 RioErr << "### [PLSessionManager - AllocClient] Finish 1" << endl;
01575 #endif
01576
01577 return PL_ERROR;
01578 }
01579
01580
01581
01582 ClientMode clientMode = client->mode;
01583 int clientCurrentBlock = client->CurrentBlock;
01584 int clientTarget = client->target;
01585 int clientPort = client->multicast_port;
01586 char clientIP[1024+1];
01587 strcpy( clientIP, client->multicast_ip );
01588
01589 InfoClient *group = NULL;
01590
01591 if( moveData->action == ACTION_READBUFFER )
01592 {
01593 if( ( clientMode == LEADER ) &&
01594 ( block == (int)client->videoTotalBlocks )
01595 )
01596 {
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606 #ifdef RIO_DEBUG2
01607 RioErr << "[AllocClient1] Enviando IPMsg para cliente "
01608 << "abandonar a lideran�a mas permanecer no grupo. "
01609 << "L�der j� alcan�ou o fim do v�deo com pedidos "
01610 << "(bloco " << client->videoTotalBlocks << ")."
01611 << endl;
01612 #endif
01613
01614 CommMulticastStream->sendIPMsg( clientPID, clientIP, clientPort,
01615 PASSIVE );
01616
01617
01618 SetMode( client, PASSIVE );
01619 }
01620 else if( clientMode == LEADER )
01621 {
01622 if( client->NextMember != NULL )
01623 {
01624
01625
01626
01627
01628
01629
01630
01631
01632
01633
01634
01635
01636
01637
01638
01639
01640 if( client->NextMember->mode == SUBLEADER )
01641 {
01642 ModeMsgBlock = -1;
01643
01644 #ifdef RIO_DEBUG2
01645 RioErr << "[AllocClient2] Enviando IPMsg para submembros"
01646 << " abandonarem fluxo principal." << endl;
01647 #endif
01648
01649 for( InfoClient *aux = client->NextMember;
01650 aux != NULL;
01651 aux = aux->NextMember
01652 )
01653 {
01654 CommMulticastStream->sendIPMsg( aux->PIDClientThread,
01655 clientIP, clientPort,
01656 aux->mode, -1,
01657 LEAVE_THIS );
01658 }
01659 }
01660 else
01661 ModeMsgBlock = clientCurrentBlock;
01662
01663 #ifdef RIO_DEBUG2
01664 RioErr << "[AllocClient3] Enviando ModeMsg pra novo l�der."
01665 << endl;
01666 #endif
01667
01668 CommMulticastStream->sendModeMsg(
01669 client->NextMember->PIDClientThread,
01670 LEADER, ModeMsgBlock );
01671 }
01672
01673
01674
01675 #ifdef RIO_DEBUG2
01676 RioErr << "[AllocClient4] Enviando IPMsg para cliente "
01677 << "abandonar todos os fluxos." << endl;
01678 #endif
01679
01680
01681 CommMulticastStream->sendIPMsg( clientPID, clientIP,
01682 clientPort, INACTIVE, -1,
01683 LEAVE_ALL, INT_MAX );
01684
01685
01686 if( ( RemoveMember( client ) == PL_ERROR ) ||
01687 ( InsertMemberInInactiveGroup( client ) == PL_ERROR )
01688 )
01689 {
01690 #ifdef RIO_DEBUG2
01691 RioErr << "[PLSessionManager - AllocClient] Erro ao tentar "
01692 << "transferir cliente " << clientPID << " para o "
01693 << "grupo de inativos." << endl;
01694 #endif
01695
01696 #ifdef RIO_DEBUG1
01697 RioErr << "### [PLSessionManager - AllocClient] Finish 2" << endl;
01698 #endif
01699
01700 return PL_ERROR;
01701 }
01702 }
01703 else if( clientMode == SUBLEADER )
01704 {
01705
01706
01707
01708
01709 if( client->NextMember != NULL )
01710 {
01711
01712
01713
01714
01715 #ifdef RIO_DEBUG2
01716 RioErr << "[AllocClient5] Enviando ModeMsg pra novo "
01717 << "subl�der." << endl;
01718 #endif
01719
01720 CommMulticastStream->sendModeMsg(
01721 client->NextMember->PIDClientThread,
01722 SUBLEADER, clientCurrentBlock,
01723 clientTarget );
01724 }
01725 else
01726 {
01727 DischargeMulticastPort( clientPID );
01728 }
01729
01730 #ifdef RIO_DEBUG2
01731 RioErr << "[AllocClient6] Enviando IPMsg para subl�der"
01732 << " repassar a sublideran�a e se tornar inativo."
01733 << endl;
01734 #endif
01735
01736 CommMulticastStream->sendIPMsg( clientPID, clientIP,
01737 clientPort, INACTIVE,
01738 -1, LEAVE_ALL, INT_MAX );
01739
01740 if( ( RemoveMember( client ) == PL_ERROR ) ||
01741 ( InsertMemberInInactiveGroup( client ) == PL_ERROR )
01742 )
01743 {
01744 #ifdef RIO_DEBUG2
01745 RioErr << "[PLSessionManager - AllocClient] Erro ao tentar "
01746 << "transferir cliente " << clientPID << " para o "
01747 << "grupo de inativos." << endl;
01748 #endif
01749
01750 #ifdef RIO_DEBUG1
01751 RioErr << "### [PLSessionManager - AllocClient] Finish 3" << endl;
01752 #endif
01753
01754 return PL_ERROR;
01755 }
01756
01757 }
01758 else if( clientMode == PASSIVE )
01759 {
01760
01761
01762 client->isPatching = false;
01763
01764 #ifdef RIO_DEBUG2
01765 RioErr << "[AllocClient7] Cliente " << clientPID << " alcan�ou o "
01766 << "l�der do grupo. Continuar� ouvindo o fluxo multicast"
01767 << " normalmente." << endl;
01768 #endif
01769 }
01770 #ifdef RIO_DEBUG2
01771 else
01772 {
01773 RioErr << "[AllocClient] Erro cr�tico! Cliente inativo ou "
01774 << "indispon�vel enviando ACTION_READBUFFER!"
01775 << endl;
01776 }
01777 #endif
01778 }
01779 else if( moveData->action == ACTION_REACHLEADER )
01780 {
01781 if( clientMode == SUBLEADER )
01782 {
01783
01784
01785
01786
01787 #ifdef RIO_DEBUG2
01788 RioErr << "[AllocClient8] Subl�der alcan�ou o l�der. Enviando IPMsg"
01789 << " para submembros abandonarem fluxo principal." << endl;
01790 #endif
01791
01792 for( InfoClient *aux = client;
01793 aux != NULL;
01794 aux = aux->NextMember
01795 )
01796 {
01797 CommMulticastStream->sendIPMsg( aux->PIDClientThread,
01798 clientIP, clientPort,
01799 PASSIVE, -1, LEAVE_THIS );
01800 }
01801
01802
01803
01804
01805 SetMode( client, PASSIVE );
01806 client->isPatching = false;
01807 client->target = INT_MAX;
01808 DischargeMulticastPort( clientPID );
01809 }
01810 else if( clientMode == PASSIVE )
01811 {
01812
01813
01814 client->isPatching = false;
01815 client->target = INT_MAX;
01816
01817 #ifdef RIO_DEBUG2
01818 RioErr << "[AllocClient9] Cliente " << clientPID << " alcan�ou o "
01819 << "l�der do grupo."
01820 << endl;
01821 #endif
01822 }
01823 #ifdef RIO_DEBUG2
01824 else
01825 {
01826 RioErr << "[AllocClient] Erro cr�tico! L�der, cliente inativo ou "
01827 << "cliente indispon�vel enviando ACTION_REACHLEADER!"
01828 << endl;
01829 }
01830 #endif
01831 }
01832 else if( ( moveData->action == ACTION_PAUSE ) ||
01833 ( moveData->action == ACTION_STOP ) ||
01834 ( block < 0 )
01835 )
01836 {
01837
01838
01839
01840
01841 if( ( clientMode == LEADER ) || ( clientMode == SUBLEADER ) )
01842 {
01843 if( client->NextMember != NULL )
01844 {
01845
01846
01847
01848
01849
01850
01851
01852
01853
01854 if( client->NextMember->mode == SUBLEADER )
01855 {
01856 ModeMsgBlock = -1;
01857
01858 #ifdef RIO_DEBUG2
01859 RioErr << "[AllocClient10] Enviando IPMsg para submembros "
01860 << "abandonarem fluxo principal." << endl;
01861 #endif
01862
01863 for( InfoClient *aux = client->NextMember;
01864 aux != NULL;
01865 aux = aux->NextMember
01866 )
01867 {
01868 CommMulticastStream->sendIPMsg( aux->PIDClientThread,
01869 clientIP, clientPort,
01870 aux->mode, -1,
01871 LEAVE_THIS );
01872 }
01873 }
01874 else
01875 {
01876 ModeMsgBlock = clientCurrentBlock;
01877 }
01878
01879 #ifdef RIO_DEBUG2
01880 RioErr << "[AllocClient11] Enviando ModeMsg pra novo "
01881 << "(sub)l�der." << endl;
01882 #endif
01883
01884 CommMulticastStream->sendModeMsg(
01885 client->NextMember->PIDClientThread,
01886 clientMode, ModeMsgBlock, clientTarget );
01887 }
01888 }
01889
01890 if( clientMode != UNAVAILABLE )
01891 {
01892
01893
01894 #ifdef RIO_DEBUG2
01895 RioErr << "[AllocClient12] Enviando IPMsg para cliente abandonar"
01896 << " todos os fluxos." << endl;
01897 #endif
01898
01899
01900
01901
01902 CommMulticastStream->sendIPMsg( clientPID, clientIP, clientPort,
01903 INACTIVE, -1, LEAVE_ALL, INT_MAX );
01904 }
01905
01906
01907 if( ( RemoveMember( client ) == PL_ERROR ) ||
01908 ( InsertMemberInInactiveGroup( client ) == PL_ERROR )
01909 )
01910 {
01911 #ifdef RIO_DEBUG2
01912 RioErr << "[PLSessionManager - AllocClient] Erro ao tentar "
01913 << "transferir cliente " << clientPID << " para o "
01914 << "grupo de inativos." << endl;
01915 #endif
01916
01917 #ifdef RIO_DEBUG1
01918 RioErr << "### [PLSessionManager - AllocClient] Finish 4" << endl;
01919 #endif
01920
01921 return PL_ERROR;
01922 }
01923 #ifdef RIO_DEBUG2
01924 else
01925 {
01926 RioErr << "[PLSessionManager - AllocClient] Cliente "
01927 << clientPID << " transferido para o grupo de inativos."
01928 << endl;
01929 }
01930 #endif
01931 }
01932 else if( ( group = FindGroup( client, block, &new_block ) ) == NULL )
01933 {
01934
01935
01936
01937 if( ( clientMode == LEADER ) || ( clientMode == SUBLEADER ) )
01938 {
01939 if( client->NextMember != NULL )
01940 {
01941
01942
01943
01944
01945
01946
01947
01948
01949
01950 if( client->NextMember->mode == SUBLEADER )
01951 {
01952 ModeMsgBlock = -1;
01953
01954 #ifdef RIO_DEBUG2
01955 RioErr << "[AllocClient13] Enviando IPMsg para submembros "
01956 << "abandonarem fluxo principal." << endl;
01957 #endif
01958
01959 for( InfoClient *aux = client->NextMember;
01960 aux != NULL;
01961 aux = aux->NextMember
01962 )
01963 {
01964 CommMulticastStream->sendIPMsg( aux->PIDClientThread,
01965 clientIP, clientPort,
01966 aux->mode, -1,
01967 LEAVE_THIS );
01968 }
01969 }
01970 else
01971 {
01972 ModeMsgBlock = clientCurrentBlock;
01973 }
01974
01975 #ifdef RIO_DEBUG2
01976 RioErr << "[AllocClient14] Enviando ModeMsg pra novo "
01977 << "(sub)l�der."
01978 << endl;
01979 #endif
01980
01981 CommMulticastStream->sendModeMsg(
01982 client->NextMember->PIDClientThread,
01983 clientMode, ModeMsgBlock, clientTarget );
01984 }
01985 }
01986
01987
01988 if( ( RemoveMember( client ) == PL_ERROR ) ||
01989 ( CreateNewGroup( client, block ) == PL_ERROR )
01990 )
01991 {
01992 #ifdef RIO_DEBUG2
01993 RioErr << "[PLSessionManager - AllocClient] Erro ao tentar "
01994 << "transferir cliente " << clientPID << " para novo grupo."
01995 << endl;
01996 #endif
01997
01998 #ifdef RIO_DEBUG1
01999 RioErr << "### [PLSessionManager - AllocClient] Finish 5" << endl;
02000 #endif
02001
02002 return PL_ERROR;
02003 }
02004
02005 if( ( clientMode != INACTIVE ) &&
02006 ( clientMode != UNAVAILABLE )
02007 )
02008 {
02009
02010
02011 #ifdef RIO_DEBUG2
02012 RioErr << "[AllocClient15] Enviando IPMsg para cliente abandonar"
02013 << " todos os fluxos." << endl;
02014 #endif
02015
02016
02017
02018 CommMulticastStream->sendIPMsg( clientPID, clientIP, clientPort,
02019 LEADER, -1, LEAVE_ALL, INT_MAX );
02020 }
02021
02022 #ifdef RIO_DEBUG2
02023 RioErr << "[AllocClient16] Enviando IPMsg para cliente entrar em "
02024 << "novo fluxo." << endl;
02025 #endif
02026
02027
02028 CommMulticastStream->sendIPMsg( clientPID, client->multicast_ip,
02029 client->multicast_port, LEADER,
02030 -1, JOIN_THIS, INT_MAX );
02031
02032 #if 1 //liberando este trecho faz-se funcionar o PIE
02033 InfoClient *merge_group = FindMergeGroup( client );
02034 if( merge_group != NULL )
02035 {
02036 #ifdef RIO_DEBUG2
02037 RioErr << "AllocClient: Encontrado grupo que ir� se unir a este "
02038 << "grupo! " << "Informa��es: Grupo "
02039 << merge_group->multicast_port << " ip "
02040 << merge_group->multicast_ip << " group block "
02041 << merge_group->CurrentBlock << endl;
02042
02043 RioErr << "[PLSessionManager - AllocClient] L�der "
02044 << merge_group->PIDClientThread << "(grupo "
02045 << merge_group->multicast_port <<") tornando-se sublider do "
02046 << "grupo " << client->multicast_port << "." << endl;
02047 #endif
02048
02049 if( MergeGroups( merge_group, client ) == PL_ERROR )
02050 {
02051 #ifdef RIO_DEBUG2
02052 RioErr << "[PLSessionManager - AllocClient] Erro ao tornar "
02053 << "cliente " << merge_group->PIDClientThread
02054 << " o subl�der do grupo " << clientPort << "(lider "
02055 << clientPID << ")." << endl;
02056 #endif
02057
02058 #ifdef RIO_DEBUG1
02059 RioErr << "### [PLSessionManager - AllocClient] Finish 6" << endl;
02060 #endif
02061
02062 return PL_ERROR;
02063 }
02064
02065 #ifdef RIO_DEBUG2
02066 RioErr << "[AllocClient17] Enviando IPMsg para submembros "
02067 << "entrarem em novo fluxo." << endl;
02068 #endif
02069
02070
02071
02072
02073
02074
02075
02076 for( InfoClient *subMember = merge_group;
02077 subMember != NULL;
02078 subMember = subMember->NextMember )
02079 {
02080 int newTarget = -1;
02081 if( subMember == merge_group )
02082 {
02083 newTarget = client->CurrentBlock;
02084 subMember->isPatching = true;
02085 subMember->target = newTarget;
02086 }
02087
02088 CommMulticastStream->sendIPMsg( subMember->PIDClientThread,
02089 client->multicast_ip,
02090 client->multicast_port,
02091 subMember->mode, -1,
02092 JOIN_THIS,
02093 newTarget );
02094 }
02095 }
02096 #ifdef RIO_DEBUG2
02097 else
02098 {
02099 RioErr << "AllocClient: Nenhum grupo est� apto a se unir a este "
02100 << "novo grupo!" << endl;
02101 }
02102 #endif
02103 #endif // fim do #if 1
02104 }
02105 else if( group != NULL )
02106 {
02107
02108
02109 if( group != GetGroupLeader( client ) )
02110 {
02111
02112
02113 if( ( clientMode == LEADER ) || ( clientMode == SUBLEADER ) )
02114 {
02115 if( client->NextMember != NULL )
02116 {
02117
02118
02119
02120
02121
02122
02123
02124
02125
02126 if( client->NextMember->mode == SUBLEADER )
02127 {
02128 ModeMsgBlock = -1;
02129
02130 #ifdef RIO_DEBUG2
02131 RioErr << "[AllocClient18] Enviando IPMsg para "
02132 << "submembros abandonarem fluxo principal."
02133 << endl;
02134 #endif
02135
02136 for( InfoClient *aux = client->NextMember;
02137 aux != NULL;
02138 aux = aux->NextMember
02139 )
02140 {
02141 CommMulticastStream->sendIPMsg(
02142 aux->PIDClientThread,
02143 clientIP, clientPort,
02144 aux->mode, -1,
02145 LEAVE_THIS );
02146 }
02147 }
02148 else
02149 {
02150 ModeMsgBlock = clientCurrentBlock;
02151 }
02152
02153 #ifdef RIO_DEBUG2
02154 RioErr << "[AllocClient19] Enviando ModeMsg pra novo "
02155 << "(sub)l�der." << endl;
02156 #endif
02157
02158 CommMulticastStream->sendModeMsg(
02159 client->NextMember->PIDClientThread,
02160 clientMode, ModeMsgBlock, clientTarget );
02161 }
02162 }
02163
02164 if( ( clientMode != INACTIVE ) &&
02165 ( clientMode != UNAVAILABLE )
02166 )
02167 {
02168
02169
02170 #ifdef RIO_DEBUG2
02171 RioErr << "[AllocClient20] Enviando IPMsg para cliente "
02172 << "abandonar todos os fluxos." << endl;
02173 #endif
02174
02175
02176
02177 CommMulticastStream->sendIPMsg( clientPID, clientIP, clientPort,
02178 PASSIVE, -1, LEAVE_ALL );
02179 }
02180
02181 #ifdef RIO_DEBUG2
02182 RioErr << "[AllocClient21] Enviando IPMsg para cliente entrar em "
02183 << " fluxo ativo." << endl;
02184 #endif
02185
02186 CommMulticastStream->sendIPMsg( clientPID, group->multicast_ip,
02187 group->multicast_port,
02188 PASSIVE, new_block, JOIN_THIS,
02189 group->CurrentBlock );
02190
02191
02192
02193
02194 if( ( RemoveMember( client ) == PL_ERROR ) ||
02195 ( InsertMemberInActiveGroup( client, group, true ) == PL_ERROR )
02196 )
02197 {
02198 #ifdef RIO_DEBUG2
02199 RioErr << "[PLSessionManager - AllocClient] Erro ao tentar "
02200 << "transferir cliente " << clientPID << " para grupo "
02201 << group->multicast_port << "." << endl;
02202 #endif
02203
02204 #ifdef RIO_DEBUG1
02205 RioErr << "### [PLSessionManager - AllocClient] Finish 7" << endl;
02206 #endif
02207
02208 return PL_ERROR;
02209 }
02210 }
02211 else
02212 {
02213
02214
02215
02216
02217
02218 if( clientMode == SUBLEADER )
02219 {
02220 if( client->NextMember != NULL )
02221 {
02222
02223
02224
02225
02226
02227 #ifdef RIO_DEBUG2
02228 RioErr << "[AllocClient22] Enviando ModeMsg pra novo "
02229 << "subl�der." << endl;
02230 #endif
02231
02232 CommMulticastStream->sendModeMsg(
02233 client->NextMember->PIDClientThread,
02234 SUBLEADER, clientCurrentBlock,
02235 clientTarget );
02236
02237 SetMode( client->NextMember, SUBLEADER );
02238 client->NextMember->isPatching = true;
02239 client->NextMember->target = clientTarget;
02240 ChangePIDPort( clientPID,
02241 client->NextMember->PIDClientThread );
02242 }
02243 else
02244 {
02245 DischargeMulticastPort( clientPID );
02246 }
02247
02248 #ifdef RIO_DEBUG2
02249 RioErr << "[AllocClient23] Enviando IPMsg para subl�der"
02250 << " repassar a sublideran�a e deixar o subgrupo "
02251 << clientPort << "." << endl;
02252 #endif
02253
02254 CommMulticastStream->sendIPMsg( clientPID, clientIP,
02255 clientPort, PASSIVE,
02256 new_block, LEAVE_THIS,
02257 group->CurrentBlock );
02258 SetMode( client, PASSIVE );
02259 }
02260 else
02261 {
02262 #ifdef RIO_DEBUG2
02263 RioErr << "[AllocClient24] Enviando IPMsg para cliente "
02264 << "permanecer no fluxo atual." << endl;
02265 #endif
02266
02267
02268
02269 CommMulticastStream->sendIPMsg( clientPID, clientIP, clientPort,
02270 PASSIVE, new_block, KEEP_THIS,
02271 group->CurrentBlock );
02272 }
02273 }
02274 }
02275
02276 #ifdef RIO_DEBUG2
02277 PrintClientTable();
02278 #endif
02279
02280 if( ( clientMode == LEADER ) || ( clientMode == INACTIVE ) )
02281 PrintLogFile();
02282
02283 MoveMutexUnlock( "alloccli1" );
02284
02285 #ifdef RIO_DEBUG2
02286 RioErr << "[AllocClient] Saindo da AllocClient..." << endl;
02287 #endif
02288
02289 #ifdef RIO_DEBUG1
02290 RioErr << "### [PLSessionManager - AllocClient] Finish 8" << endl;
02291 #endif
02292
02293 return PL_OK;
02294 }
02295
02296 void PLSessionManager::PrintLogFile()
02297 {
02298 #ifdef RIO_DEBUG1
02299 RioErr << "### [PLSessionManager - PrintLogFile] Start" << endl;
02300 #endif
02301
02302 InfoClient *aux = PLSessionManager::m_InfoClient;
02303 InfoClient *groupleader = aux;
02304 int inactive_clients = 0;
02305 int multicast_streams = 0;
02306 int total_clients = 0;
02307 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
02308 unsigned int calculeted_time = 0;
02309
02310
02311
02312
02313
02314
02315
02316 gettimeofday( &time_list, NULL );
02317
02318 final_time.tv_sec = time_list.tv_sec - initial_time.tv_sec;
02319 final_time.tv_usec = time_list.tv_usec - initial_time.tv_usec;
02320
02321 if( final_time.tv_usec < 0 )
02322 {
02323 final_time.tv_sec -= 1;
02324 final_time.tv_usec += 1000000;
02325 }
02326
02327 calculeted_time = ( unsigned int ) (final_time.tv_sec * 1000.0 +
02328 final_time.tv_usec/1000.0);
02329
02330 calculeted_time = calculeted_time/1000;
02331 #endif
02332
02333 while( groupleader )
02334 {
02335 aux = groupleader;
02336 multicast_streams++;
02337 while( aux )
02338 {
02339 total_clients++;
02340 aux = aux->NextMember;
02341 }
02342 groupleader = groupleader->NextGroup;
02343 }
02344
02345 aux = PLSessionManager::m_InactiveClient;
02346
02347 while( aux )
02348 {
02349 inactive_clients++;
02350 aux = aux->NextMember;
02351 }
02352
02353 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
02354 m_log_timer << "\t" << multicast_streams << "\t"
02355 << CommMulticastStream->GetNPatchingStream() << "\t"
02356 << total_clients << "\t"
02357 << inactive_clients << "\t"
02358 << calculeted_time << endl;
02359 #endif
02360
02361 #ifdef RIO_DEBUG1
02362 RioErr << "### [PLSessionManager - PrintLogFile] Finish" << endl;
02363 #endif
02364 }
02365
02366 InfoClient *PLSessionManager::FindGroup( InfoClient *current_client, int block,
02367 int *new_block )
02368 {
02369 #ifdef RIO_DEBUG1
02370 RioErr << "### [PLSessionManager - FindGroup] Start" << endl;
02371 #endif
02372
02373 InfoClient *aux = m_InfoClient;
02374 InfoClient *best_group = NULL;
02375 int ams_patch_size = 0;
02376 int patch_size = -1;
02377 enum
02378 {
02379 NONE ,
02380 BEFORE ,
02381 AFTER
02382 } patch_type;
02383
02384 *new_block = -1;
02385
02386 if( !current_client || ( block < 0 ) )
02387 {
02388 #ifdef RIO_DEBUG2
02389 RioErr << "[FindGroup] Erro: Current_client == NULL ou block == -1!"
02390 << endl;
02391 #endif
02392
02393 #ifdef RIO_DEBUG1
02394 RioErr << "### [PLSessionManager - FindGroup] Finish 1" << endl;
02395 #endif
02396
02397 return NULL;
02398 }
02399
02400 #ifdef RIO_DEBUG2
02401 RioErr << "[PLSessionManager - FindGroup] Finding group for client "
02402 << current_client->PIDClientThread << endl;
02403 #endif
02404
02405 patch_type = NONE;
02406
02407 #ifdef RIO_DEBUG2
02408 RioErr << "[FindGroup] - m_DeltaBefore = " << m_DeltaBefore << " blocks"
02409 << endl;
02410 RioErr << "[FindGroup] - m_DeltaAfter = " << m_DeltaAfter << " blocks"
02411 << endl;
02412 #endif
02413
02414 while( aux )
02415 {
02416 if( ( current_client != aux ) &&
02417 ( strcmp( current_client->video_name, aux->video_name ) == 0 ) &&
02418 ( aux->mode == LEADER )
02419
02420
02421 )
02422 {
02423
02424
02425
02426
02427 ams_patch_size = aux->CurrentBlock - block;
02428
02429 if( ( aux->CurrentBlock <= block ) &&
02430 ( aux->CurrentBlock >= ( block - m_DeltaBefore ) ) &&
02431 ( aux->CurrentBlock > -1 )
02432 )
02433 {
02434 ams_patch_size *= -1;
02435 if( ( patch_type == AFTER ) ||
02436 ( patch_type == NONE ) ||
02437 ( patch_size > ams_patch_size )
02438 )
02439 {
02440 best_group = aux;
02441 patch_size = ams_patch_size;
02442 patch_type = BEFORE;
02443
02444
02445
02446
02447 if( new_block )
02448 *new_block = best_group->CurrentBlock - 2;
02449 }
02450 }
02451 else if( ( aux->CurrentBlock >= block ) &&
02452 ( aux->CurrentBlock <= ( block + m_DeltaAfter ) ) &&
02453 ( aux->CurrentBlock > -1 )
02454 )
02455 {
02456 if( ( patch_type == NONE ) ||
02457 ( patch_type == AFTER && patch_size > ams_patch_size ) )
02458 {
02459 patch_type = AFTER;
02460 best_group = aux;
02461 patch_size = ams_patch_size;
02462 }
02463 }
02464 }
02465 aux = aux->NextGroup;
02466 }
02467
02468 #ifdef RIO_DEBUG2
02469 if( best_group )
02470 RioErr << "[PLSessionManager] Saindo da FindGroup. Retornando grupo "
02471 << best_group->multicast_port << "." << endl;
02472 else
02473 RioErr << "[PLSessionManager] Saindo da FindGroup. "
02474 << "Grupo n�o encontrado." << endl;
02475 #endif
02476
02477 #ifdef RIO_DEBUG1
02478 RioErr << "### [PLSessionManager - FindGroup] Finish 2" << endl;
02479 #endif
02480
02481 return best_group;
02482 }
02483
02484 InfoClient *PLSessionManager::FindMergeGroup( InfoClient *group )
02485 {
02486 #ifdef RIO_DEBUG1
02487 RioErr << "### [PLSessionManager - FindMergeGroup] Start" << endl;
02488 #endif
02489
02490 InfoClient *aux = m_InfoClient;
02491 InfoClient *merge_group = NULL;
02492 int ams_patch_size = 0;
02493 int patch_size = INT_MAX;
02494
02495 #ifdef RIO_DEBUG2
02496 RioErr << "[PLSessionManager - FindMergeGroup] Finding merge group for "
02497 << "block " << group->CurrentBlock << endl;
02498 #endif
02499
02500 if( ( group == NULL ) || ( group->mode != LEADER ) )
02501 {
02502 RioErr << "[FindMergeGroup] Erro: group inv�lido! Membro n�o � l�der"
02503 << endl;
02504
02505 #ifdef RIO_DEBUG1
02506 RioErr << "### [PLSessionManager - FindMergeGroup] Finish 1" << endl;
02507 #endif
02508
02509 return NULL;
02510 }
02511
02512 #ifdef RIO_DEBUG2
02513 RioErr << "[FindMergeGroup] - m_DeltaAfter = " << m_DeltaAfter << " blocks"
02514 << endl;
02515 #endif
02516
02517 while( aux )
02518 {
02519
02520
02521 if( ( strcmp( group->video_name, aux->video_name ) == 0 ) &&
02522 (aux != group ) &&
02523 isMergeableGroup( aux )
02524 )
02525 {
02526
02527 if( ( aux->CurrentBlock <= group->CurrentBlock ) &&
02528 ( aux->CurrentBlock >= ( group->CurrentBlock -
02529 m_DeltaAfter ) ) &&
02530 ( aux->CurrentBlock > -1 )
02531 )
02532 {
02533 ams_patch_size = group->CurrentBlock - aux->CurrentBlock;
02534
02535 if( patch_size > ams_patch_size )
02536 {
02537 merge_group = aux;
02538 patch_size = ams_patch_size;
02539 }
02540 }
02541 #ifdef RIO_DEBUG2
02542 else
02543 {
02544 RioErr << "[FindMergeGroup] Grupo " << aux->multicast_port
02545 << " com CurrentBlock " << aux->CurrentBlock
02546 << " inapto para merging." << endl;
02547 }
02548 #endif
02549 }
02550 aux = aux->NextGroup;
02551 }
02552
02553 #ifdef RIO_DEBUG2
02554 RioErr << "[PLSessionManager] Saindo da FindMergeGroup..." << endl;
02555 #endif
02556
02557 #ifdef RIO_DEBUG1
02558 RioErr << "### [PLSessionManager - FindMergeGroup] Finish 2" << endl;
02559 #endif
02560
02561 return merge_group;
02562 }
02563
02564 int PLSessionManager::SetMode( InfoClient *client, ClientMode mode )
02565 {
02566 #ifdef RIO_DEBUG1
02567 RioErr << "### [PLSessionManager - SetMode] Start" << endl;
02568 #endif
02569
02570 if( !client )
02571 {
02572 #ifdef RIO_DEBUG1
02573 RioErr << "### [PLSessionManager - SetMode] Finish 1" << endl;
02574 #endif
02575
02576 return PL_ERROR;
02577 }
02578
02579 client->mode = mode;
02580
02581 #ifdef RIO_DEBUG1
02582 RioErr << "### [PLSessionManager - SetMode] Finish 2" << endl;
02583 #endif
02584
02585 return PL_OK;
02586 }
02587
02588 int PLSessionManager::CreateNewGroup( InfoClient *member, int block )
02589 {
02590 #ifdef RIO_DEBUG1
02591 RioErr << "### [PLSessionManager - CreateNewGroup] Start" << endl;
02592 #endif
02593
02594 InfoClient *group = m_InfoClient;
02595
02596 if( !member )
02597 {
02598 #ifdef RIO_DEBUG1
02599 RioErr << "### [PLSessionManager - SetMode] Finish 1" << endl;
02600 #endif
02601
02602 return PL_ERROR;
02603 }
02604
02605 member->PrevGroup = NULL;
02606 member->PrevMember = NULL;
02607 member->NextGroup = NULL;
02608 member->NextMember = NULL;
02609 member->multicast_port = ChooseMulticastPort( member->PIDClientThread );
02610
02611 strcpy( member->multicast_ip, m_MulticastIP );
02612
02613 SetMode( member, LEADER );
02614 member->CurrentBlock = block;
02615
02616 if( !m_InfoClient )
02617 {
02618 m_InfoClient = member;
02619 }
02620 else
02621 {
02622 while( group->NextGroup != NULL )
02623 {
02624 group = group->NextGroup;
02625 }
02626 group->NextGroup = member;
02627 member->PrevGroup = group;
02628 }
02629
02630 #ifdef RIO_DEBUG2
02631 RioErr << "[CreateNewGroup] Criado novo grupo para cliente "
02632 << member->PIDClientThread << " com bloco inicial " << block << endl;
02633 #endif
02634
02635 #ifdef RIO_DEBUG1
02636 RioErr << "### [PLSessionManager - SetMode] Finish2" << endl;
02637 #endif
02638
02639 return PL_OK;
02640 }
02641
02642 int PLSessionManager::InsertMemberInActiveGroup( InfoClient *client,
02643 InfoClient *group,
02644 bool isPatching )
02645 {
02646 #ifdef RIO_DEBUG1
02647 RioErr << "### [PLSessionManager - InsertMemberInActiveGroup] Start" << endl;
02648 #endif
02649
02650 InfoClient *last = NULL;
02651 InfoClient *subLeader = NULL;
02652
02653 if( !client ||
02654 !group ||
02655 ( group->mode == INACTIVE ) ||
02656 ( group->mode == UNAVAILABLE )
02657 )
02658 {
02659 #ifdef RIO_DEBUG2
02660 RioErr << "[PLSessionManager] InsertMemberInActiveGroup error: "
02661 << "Par�metros inv�lidos" << endl;
02662 #endif
02663
02664 #ifdef RIO_DEBUG1
02665 RioErr << "### [PLSessionManager - InsertMemberInActiveGroup] Finish 1" << endl;
02666 #endif
02667
02668 return PL_ERROR;
02669 }
02670
02671 subLeader = GetGroupSubLeader( group );
02672
02673 if( subLeader == NULL )
02674 last = GetLastInGroup( group );
02675
02676 SetMode( client, PASSIVE );
02677
02678 if( !last && !subLeader )
02679 {
02680 #ifdef RIO_DEBUG2
02681 RioErr << "[PLSessionManager] InsertMemberInActiveGroup error: "
02682 << "�ltimo membro nem subl�der encontrados!" << endl;
02683 #endif
02684
02685 #ifdef RIO_DEBUG1
02686 RioErr << "### [PLSessionManager - InsertMemberInActiveGroup] Finish 2" << endl;
02687 #endif
02688
02689 return PL_ERROR;
02690 }
02691
02692
02693
02694
02695 if( subLeader != NULL )
02696 InsertMemberBefore( client, subLeader );
02697 else
02698 InsertMemberAfter( client, last );
02699
02700 strcpy( client->video_name, group->video_name );
02701 strcpy( client->multicast_ip, group->multicast_ip );
02702 client->multicast_port = group->multicast_port;
02703 client->CurrentBlock = -1;
02704 client->isPatching = isPatching;
02705
02706 #ifdef RIO_DEBUG1
02707 RioErr << "### [PLSessionManager - InsertMemberInActiveGroup] Finish 3" << endl;
02708 #endif
02709
02710 return PL_OK;
02711 }
02712
02713 int PLSessionManager::InsertMemberInInactiveGroup( InfoClient *client )
02714 {
02715 #ifdef RIO_DEBUG1
02716 RioErr << "### [PLSessionManager - InsertMemberInInactiveGroup] Start" << endl;
02717 #endif
02718
02719 if( !client )
02720 {
02721 #ifdef RIO_DEBUG1
02722 RioErr << "### [PLSessionManager - InsertMemberInInactiveGroup] Finish 1" << endl;
02723 #endif
02724
02725 return PL_ERROR;
02726 }
02727
02728 if( !m_InactiveClient )
02729 m_InactiveClient = client;
02730 else
02731 InsertMemberAfter( client, m_InactiveClient );
02732
02733 SetMode( client, INACTIVE );
02734 client->CurrentBlock = -1;
02735 strcpy( client->multicast_ip, "0.0.0.0" );
02736 client->multicast_port = 0;
02737
02738 #ifdef RIO_DEBUG1
02739 RioErr << "### [PLSessionManager - InsertMemberInInactiveGroup] Finish 2" << endl;
02740 #endif
02741
02742 return PL_OK;
02743 }
02744
02745 int PLSessionManager::SwitchToLeader( InfoClient *new_leader )
02746 {
02747 #ifdef RIO_DEBUG1
02748 RioErr << "### [PLSessionManager - SwitchToLeader] Start" << endl;
02749 #endif
02750
02751 InfoClient *first_member;
02752
02753 if( !new_leader )
02754 {
02755 #ifdef RIO_DEBUG2
02756 RioErr << "[SwitchToLeader] new_leader inv�lido!" << endl;
02757 #endif
02758
02759 #ifdef RIO_DEBUG1
02760 RioErr << "### [PLSessionManager - SwitchToLeader] Finish 1" << endl;
02761 #endif
02762
02763 return PL_ERROR;
02764 }
02765
02766 first_member = GetGroupLeader( new_leader );
02767
02768 if( first_member == NULL || first_member == m_InactiveClient )
02769 {
02770 #ifdef RIO_DEBUG2
02771 RioErr << "[SwitchToLeader] first_member inv�lido!" << endl;
02772 #endif
02773
02774 #ifdef RIO_DEBUG1
02775 RioErr << "### [PLSessionManager - SwitchToLeader] Finish 2" << endl;
02776 #endif
02777
02778 return PL_ERROR;
02779 }
02780
02781 if( first_member == new_leader )
02782 {
02783 #ifdef RIO_DEBUG1
02784 RioErr << "### [PLSessionManager - SwitchToLeader] Finish 3" << endl;
02785 #endif
02786
02787 return PL_OK;
02788 }
02789
02790
02791
02792 if( RemoveMember( new_leader ) == PL_OK )
02793 {
02794
02795
02796
02797 if( InsertMemberBefore( new_leader, first_member ) == PL_OK )
02798 {
02799 #ifdef RIO_DEBUG1
02800 RioErr << "### [PLSessionManager - SwitchToLeader] Finish 4" << endl;
02801 #endif
02802
02803 return PL_OK;
02804 }
02805 #ifdef RIO_DEBUG2
02806 else
02807 RioErr << "[SwitchToLeader] Falha na Inser��o de new_leader no "
02808 << "grupo!" << endl;
02809 #endif
02810 }
02811 #ifdef RIO_DEBUG2
02812 else
02813 RioErr << "[SwitchToLeader] Falha na remo��o de new_leader!" << endl;
02814 #endif
02815
02816 #ifdef RIO_DEBUG1
02817 RioErr << "### [PLSessionManager - SwitchToLeader] Finish 5" << endl;
02818 #endif
02819
02820 return PL_ERROR;
02821 }
02822
02823 int PLSessionManager::SwitchToSubLeader( InfoClient *new_subleader )
02824 {
02825 #ifdef RIO_DEBUG1
02826 RioErr << "### [PLSessionManager - SwitchToSubLeader] Start" << endl;
02827 #endif
02828
02829 InfoClient *subleader_member;
02830
02831 if( !new_subleader )
02832 {
02833 #ifdef RIO_DEBUG1
02834 RioErr << "### [PLSessionManager - SwitchToSubLeader] Finish 1" << endl;
02835 #endif
02836
02837 return PL_ERROR;
02838 }
02839
02840 subleader_member = GetGroupSubLeader( new_subleader );
02841
02842 if( subleader_member == NULL )
02843 {
02844 #ifdef RIO_DEBUG1
02845 RioErr << "### [PLSessionManager - SwitchToSubLeader] Finish 2" << endl;
02846 #endif
02847
02848 return PL_ERROR;
02849 }
02850
02851 if( subleader_member == new_subleader )
02852 {
02853 #ifdef RIO_DEBUG1
02854 RioErr << "### [PLSessionManager - SwitchToSubLeader] Finish 3" << endl;
02855 #endif
02856
02857 return PL_OK;
02858 }
02859
02860
02861
02862 if( RemoveMember( new_subleader ) == PL_OK )
02863 {
02864 if( InsertMemberBefore( new_subleader, subleader_member ) == PL_OK )
02865 {
02866
02867
02868
02869
02870
02871 SetMode( new_subleader, SUBLEADER );
02872 SetMode( subleader_member, PASSIVE );
02873
02874 #ifdef RIO_DEBUG1
02875 RioErr << "### [PLSessionManager - SwitchToSubLeader] Finish 4" << endl;
02876 #endif
02877
02878 return PL_OK;
02879 }
02880 }
02881
02882 #ifdef RIO_DEBUG1
02883 RioErr << "### [PLSessionManager - SwitchToSubLeader] Finish 5" << endl;
02884 #endif
02885
02886 return PL_ERROR;
02887 }
02888
02889 int PLSessionManager::MergeGroups( InfoClient *slaveGroup,
02890 InfoClient *masterGroup )
02891 {
02892 #ifdef RIO_DEBUG1
02893 RioErr << "### [PLSessionManager - MergeGroups] Start" << endl;
02894 #endif
02895
02896 InfoClient *lastMasterMember;
02897
02898 if( !slaveGroup || !masterGroup || ( slaveGroup->mode != LEADER ) ||
02899 ( masterGroup->mode!= LEADER ) || ( slaveGroup == m_InactiveClient )
02900 )
02901 {
02902 #ifdef RIO_DEBUG1
02903 RioErr << "### [PLSessionManager - MergeGroups] Finish 1" << endl;
02904 #endif
02905
02906 return PL_ERROR;
02907 }
02908
02909 lastMasterMember = GetLastInGroup( masterGroup );
02910
02911 if( m_InfoClient == slaveGroup )
02912 m_InfoClient = slaveGroup->NextGroup;
02913
02914 lastMasterMember->NextMember = slaveGroup;
02915 slaveGroup->PrevMember = lastMasterMember;
02916
02917
02918 if( slaveGroup->PrevGroup )
02919 slaveGroup->PrevGroup->NextGroup = slaveGroup->NextGroup;
02920
02921 if( slaveGroup->NextGroup )
02922 slaveGroup->NextGroup->PrevGroup = slaveGroup->PrevGroup;
02923
02924 slaveGroup->PrevGroup = NULL;
02925 slaveGroup->NextGroup = NULL;
02926
02927 SetMode( slaveGroup, SUBLEADER );
02928
02929 #ifdef RIO_DEBUG1
02930 RioErr << "### [PLSessionManager - MergeGroups] Finish 2" << endl;
02931 #endif
02932
02933 return PL_OK;
02934 }
02935
02936
02937
02938
02939
02940
02941
02942
02943
02944
02945
02946
02947
02948
02949
02950
02951
02952
02953
02954
02955
02956
02957
02958
02959
02960
02961
02962
02963
02964
02965
02966
02967
02968
02969
02970 int PLSessionManager::SwitchToPassiveUser( InfoClient *member )
02971 {
02972 #ifdef RIO_DEBUG1
02973 RioErr << "### [PLSessionManager - SwitchToPassiveUser] Start" << endl;
02974 #endif
02975
02976 #ifdef RIO_DEBUG2
02977 RioErr << "[PLSessionManager] Entrei na SwitchToPassiveUser..." << endl;
02978 #endif
02979
02980
02981
02982
02983 if( !member ||
02984 ( ( member->mode == LEADER ) && ( member->NextMember == NULL ) )
02985 )
02986 {
02987 #ifdef RIO_DEBUG2
02988 RioErr << "[SwitchToPassiveUser] Membro nulo ou lider de grupo unitario"
02989 << endl;
02990 #endif
02991
02992 #ifdef RIO_DEBUG1
02993 RioErr << "### [PLSessionManager - SwitchToPassiveUser] Finish 1" << endl;
02994 #endif
02995
02996 return PL_ERROR;
02997 }
02998
02999 bool isPatching = member->isPatching;
03000
03001 InfoClient *group = GetGroupLeader( member );
03002
03003 if( !group )
03004 {
03005 #ifdef RIO_DEBUG2
03006 RioErr << "[SwitchToPassiveUser] Membro de grupo em dissolu��o"
03007 << endl;
03008 #endif
03009
03010 #ifdef RIO_DEBUG1
03011 RioErr << "### [PLSessionManager - SwitchToPassiveUser] Finish 2" << endl;
03012 #endif
03013
03014 return PL_ERROR;
03015 }
03016
03017 if( ( RemoveMember( member ) != PL_OK ) ||
03018 ( InsertMemberInActiveGroup( member, group, isPatching ) != PL_OK )
03019 )
03020 {
03021 #ifdef RIO_DEBUG2
03022 RioErr << "[SwitchToPassiveUser] Falha na realoca��o do mebro no grupo"
03023 << endl;
03024 #endif
03025
03026 #ifdef RIO_DEBUG1
03027 RioErr << "### [PLSessionManager - SwitchToPassiveUser] Finish 3" << endl;
03028 #endif
03029
03030 return PL_ERROR;
03031 }
03032
03033 #ifdef RIO_DEBUG1
03034 RioErr << "### [PLSessionManager - SwitchToPassiveUser] Finish 4" << endl;
03035 #endif
03036
03037 return PL_OK;
03038 }
03039
03040 int PLSessionManager::RemoveMember( InfoClient *member )
03041 {
03042 #ifdef RIO_DEBUG1
03043 RioErr << "### [PLSessionManager - RemoveMember] Start" << endl;
03044 #endif
03045
03046 #ifdef RIO_DEBUG2
03047 RioErr << "[PLSession] Entrei na RemoveMember..." << endl;
03048 #endif
03049
03050 if( !member )
03051 {
03052 #ifdef RIO_DEBUG1
03053 RioErr << "### [PLSessionManager - RemoveMember] Finish 1" << endl;
03054 #endif
03055
03056 return PL_ERROR;
03057 }
03058
03059
03060
03061 if( ( member->mode == LEADER ) || ( member->mode == SUBLEADER ) )
03062 {
03063
03064
03065 if( member == m_InfoClient )
03066 {
03067 if( member->NextMember )
03068 m_InfoClient = member->NextMember;
03069 else
03070 m_InfoClient = member->NextGroup;
03071 }
03072
03073
03074
03075
03076 if( member->NextMember == NULL )
03077 {
03078
03079
03080 if( m_InactiveClient != member )
03081 DischargeMulticastPort( member->PIDClientThread );
03082 }
03083 else
03084 {
03085
03086
03087
03088
03089
03090
03091
03092
03093 if( member->NextMember->mode != SUBLEADER )
03094 {
03095 member->NextMember->CurrentBlock = member->CurrentBlock;
03096 ChangePIDPort( member->PIDClientThread,
03097 member->NextMember->PIDClientThread );
03098 }
03099 else
03100 DischargeMulticastPort( member->PIDClientThread );
03101
03102 SetMode( member->NextMember, member->mode );
03103 member->NextMember->target = member->target;
03104
03105 if( member->mode == SUBLEADER )
03106 member->NextMember->isPatching = true;
03107 }
03108
03109 }
03110 else if( ( GetGroupLeader( member ) == NULL ) &&
03111 ( member->mode == PASSIVE )
03112 )
03113 {
03114
03115
03116 if( member == GetFirstInGroup( member) )
03117 {
03118
03119
03120 if( member == m_InfoClient )
03121 {
03122 if( member->NextMember )
03123 m_InfoClient = member->NextMember;
03124 else
03125 m_InfoClient = member->NextGroup;
03126 }
03127
03128
03129
03130
03131 if( ( member->NextMember != NULL ) &&
03132 ( member->NextMember->mode != SUBLEADER )
03133 )
03134 ChangePIDPort( member->PIDClientThread,
03135 member->NextMember->PIDClientThread );
03136 else
03137 DischargeMulticastPort( member->PIDClientThread );
03138 }
03139 }
03140
03141
03142 if( member->PrevGroup )
03143 {
03144 if( member->NextMember )
03145 {
03146 member->NextMember->PrevGroup = member->PrevGroup;
03147 member->PrevGroup->NextGroup = member->NextMember;
03148 }
03149 else
03150 {
03151 member->PrevGroup->NextGroup = member->NextGroup;
03152 }
03153 }
03154
03155
03156 if( member->NextGroup )
03157 {
03158 if( member->NextMember )
03159 {
03160 member->NextMember->NextGroup = member->NextGroup;
03161 member->NextGroup->PrevGroup = member->NextMember;
03162 }
03163 else
03164 {
03165 member->NextGroup->PrevGroup = member->PrevGroup;
03166 }
03167 }
03168
03169
03170 if( member->NextMember )
03171 member->NextMember->PrevMember = member->PrevMember;
03172
03173
03174 if( member->PrevMember )
03175 member->PrevMember->NextMember = member->NextMember;
03176
03177
03178
03179 if( m_InactiveClient == member )
03180 m_InactiveClient = member->NextMember;
03181
03182 SetMode( member, UNAVAILABLE );
03183
03184
03185 member->NextGroup = NULL;
03186 member->PrevGroup = NULL;
03187 member->NextMember = NULL;
03188 member->PrevMember = NULL;
03189 member->isPatching = false;
03190 member->CurrentBlock = -1;
03191 member->target = INT_MAX;
03192
03193 #ifdef RIO_DEBUG2
03194 RioErr << "[PLSession] Saindo da RemoveMember..." << endl;
03195 #endif
03196
03197 #ifdef RIO_DEBUG1
03198 RioErr << "### [PLSessionManager - RemoveMember] Finish 2" << endl;
03199 #endif
03200
03201 return PL_OK;
03202 }
03203
03204 int PLSessionManager::InsertMemberBefore( InfoClient *member,
03205 InfoClient *o_member )
03206 {
03207 #ifdef RIO_DEBUG1
03208 RioErr << "### [PLSessionManager - InsertMemberBefore] Start" << endl;
03209 #endif
03210
03211 #ifdef RIO_DEBUG2
03212 RioErr << "[PLSessionManager] Entrei na InsertMemberBefore... Inserindo "
03213 << member->PIDClientThread << " antes de cliente "
03214 << o_member->PIDClientThread << " no grupo "
03215 << o_member->multicast_port << endl;
03216 #endif
03217
03218 if( ( member == NULL ) ||
03219 ( o_member == NULL ) ||
03220 ( member == o_member ) ||
03221 ( o_member->mode == UNAVAILABLE )
03222 )
03223 {
03224 #ifdef RIO_DEBUG2
03225 RioErr << "[InsertMemberBefore] Erro cr�tico! Par�metros inv�lidos!"
03226 << endl;
03227 #endif
03228
03229 #ifdef RIO_DEBUG1
03230 RioErr << "### [PLSessionManager - InsertMemberBefore] Finish 1" << endl;
03231 #endif
03232
03233 return PL_ERROR;
03234 }
03235
03236 member->NextGroup = NULL;
03237 member->PrevGroup = NULL;
03238 member->NextMember = NULL;
03239 member->PrevMember = NULL;
03240 member->CurrentBlock = -1;
03241
03242
03243
03244 if( o_member->mode == LEADER )
03245 {
03246 SetMode( o_member, PASSIVE );
03247 SetMode( member, LEADER );
03248
03249 if( o_member == m_InfoClient )
03250 m_InfoClient = member;
03251
03252 if( o_member->PrevGroup )
03253 {
03254 member->PrevGroup = o_member->PrevGroup;
03255 o_member->PrevGroup = NULL;
03256 member->PrevGroup->NextGroup = member;
03257 }
03258
03259 if( o_member->NextGroup )
03260 {
03261 member->NextGroup = o_member->NextGroup;
03262 o_member->NextGroup->PrevGroup = member;
03263 o_member->NextGroup = NULL;
03264 }
03265
03266 member->CurrentBlock = o_member->CurrentBlock;
03267 o_member->CurrentBlock = -1;
03268 }
03269
03270 if( o_member->PrevMember )
03271 o_member->PrevMember->NextMember = member;
03272
03273 member->PrevMember = o_member->PrevMember;
03274 member->NextMember = o_member;
03275 o_member->PrevMember = member;
03276
03277 member->multicast_port = o_member->multicast_port;
03278
03279 strcpy( member->multicast_ip, o_member->multicast_ip );
03280
03281 #ifdef RIO_DEBUG2
03282 RioErr << "[PLSessionManager] Saindo da InsertMemberBefore..." << endl;
03283 #endif
03284
03285 #ifdef RIO_DEBUG1
03286 RioErr << "### [PLSessionManager - InsertMemberBefore] Finish 2" << endl;
03287 #endif
03288
03289 return PL_OK;
03290 }
03291
03292 int PLSessionManager::InsertMemberAfter( InfoClient *member,
03293 InfoClient *o_member )
03294 {
03295 #ifdef RIO_DEBUG1
03296 RioErr << "### [PLSessionManager - InsertMemberAfter] Start" << endl;
03297 #endif
03298
03299 #ifdef RIO_DEBUG2
03300 RioErr << "[PLSessionManager] Entrei na InsertMemberAfter... Inserindo "
03301 << member->PIDClientThread << " ap�s cliente "
03302 << o_member->PIDClientThread << " no grupo "
03303 << o_member->multicast_port << endl;
03304 #endif
03305
03306 if( ( member == NULL ) ||
03307 ( o_member == NULL ) ||
03308 ( member == o_member ) ||
03309 ( o_member->mode == UNAVAILABLE )
03310 )
03311 {
03312 #ifdef RIO_DEBUG2
03313 RioErr << "[InsertMemberAfter] Erro cr�tico! Par�metros inv�lidos!"
03314 << endl;
03315 #endif
03316
03317 #ifdef RIO_DEBUG1
03318 RioErr << "### [PLSessionManager - InsertMemberAfter] Finish 1" << endl;
03319 #endif
03320
03321 return PL_ERROR;
03322 }
03323
03324 member->NextGroup = NULL;
03325 member->PrevGroup = NULL;
03326 member->NextMember = NULL;
03327 member->PrevMember = NULL;
03328 member->CurrentBlock = -1;
03329
03330 member->PrevMember = o_member;
03331
03332 if( o_member->NextMember )
03333 {
03334 member->NextMember = o_member->NextMember;
03335 o_member->NextMember->PrevMember = member;
03336 }
03337
03338 o_member->NextMember = member;
03339 member->multicast_port = o_member->multicast_port;
03340
03341 strcpy( member->multicast_ip, o_member->multicast_ip );
03342 member->CurrentBlock = o_member->CurrentBlock;
03343
03344 #ifdef RIO_DEBUG2
03345 RioErr << "[PLSessionManager] Saindo da InsertMemberAfter..." << endl;
03346 #endif
03347
03348 #ifdef RIO_DEBUG1
03349 RioErr << "### [PLSessionManager - InsertMemberAfter] Finish 2" << endl;
03350 #endif
03351
03352 return PL_OK;
03353 }
03354
03355 InfoClient *PLSessionManager::GetGroupLeader( InfoClient *member )
03356 {
03357 #ifdef RIO_DEBUG1
03358 RioErr << "### [PLSessionManager - GetGroupLeader] Start" << endl;
03359 #endif
03360
03361 InfoClient *member_result = member;
03362
03363 if ( member_result == NULL )
03364 {
03365 #ifdef RIO_DEBUG2
03366 RioErr << "[GetGroupLeader] Erro: member == NULL !!!!" << endl;
03367 #endif
03368
03369 #ifdef RIO_DEBUG1
03370 RioErr << "### [PLSessionManager - GetGroupLeader] Finish 1" << endl;
03371 #endif
03372
03373 return NULL;
03374 }
03375
03376 if ( member_result->mode == INACTIVE )
03377 {
03378 #ifdef RIO_DEBUG2
03379 RioErr << "[GetGroupLeader] Member INACTIVE !!!!" << endl;
03380 #endif
03381
03382 #ifdef RIO_DEBUG1
03383 RioErr << "### [PLSessionManager - GetGroupLeader] Finish 2" << endl;
03384 #endif
03385
03386 return NULL;
03387 }
03388
03389 while( member_result->PrevMember != NULL )
03390 member_result = member_result->PrevMember;
03391
03392 if ( member_result->mode != LEADER )
03393 {
03394 #ifdef RIO_DEBUG2
03395 RioErr << "[GetGroupLeader] Erro: first member n�o � LEADER !!!!" << endl;
03396 #endif
03397
03398 #ifdef RIO_DEBUG1
03399 RioErr << "### [PLSessionManager - GetGroupLeader] Finish 3" << endl;
03400 #endif
03401
03402 return NULL;
03403 }
03404
03405 #ifdef RIO_DEBUG2
03406 if ( member_result == NULL )
03407 RioErr << "[GetGroupLeader] Erro: Grupo de " << member->PIDClientThread
03408 << " n�o tem l�der!!!!" << endl;
03409 #endif
03410
03411 #ifdef RIO_DEBUG1
03412 RioErr << "### [PLSessionManager - GetGroupLeader] Finish 4" << endl;
03413 #endif
03414
03415 return member_result;
03416 }
03417
03418 InfoClient *PLSessionManager::GetGroupSubLeader( InfoClient *member )
03419 {
03420 #ifdef RIO_DEBUG1
03421 RioErr << "### [PLSessionManager - GetGroupSubLeader] Start" << endl;
03422 #endif
03423
03424 InfoClient *member_result = member;
03425
03426 if ( member_result == NULL )
03427 {
03428 #ifdef RIO_DEBUG2
03429 RioErr << "[GetGroupSubLeader] Erro: member == NULL !!!!" << endl;
03430 #endif
03431
03432 #ifdef RIO_DEBUG1
03433 RioErr << "### [PLSessionManager - GetGroupLeader] Finish 1" << endl;
03434 #endif
03435
03436 return NULL;
03437 }
03438
03439 if ( member_result->mode == INACTIVE )
03440 {
03441 #ifdef RIO_DEBUG2
03442 RioErr << "[GetGroupSubLeader] Erro: member == INACTIVE !!!!" << endl;
03443 #endif
03444
03445 #ifdef RIO_DEBUG1
03446 RioErr << "### [PLSessionManager - GetGroupLeader] Finish 2" << endl;
03447 #endif
03448
03449 return NULL;
03450 }
03451
03452
03453 while( ( member_result != NULL ) &&
03454 ( member_result->mode != SUBLEADER )
03455 )
03456 member_result = member_result->PrevMember;
03457
03458 if( ( member_result == NULL ) )
03459 {
03460 member_result = member;
03461
03462 while( ( member_result != NULL ) &&
03463 ( member_result->mode != SUBLEADER )
03464 )
03465 member_result = member_result->NextMember;
03466 }
03467
03468 #ifdef RIO_DEBUG1
03469 RioErr << "### [PLSessionManager - GetGroupLeader] Finish 3" << endl;
03470 #endif
03471
03472 return member_result;
03473 }
03474
03475 InfoClient *PLSessionManager::GetFirstInGroup( InfoClient *member )
03476 {
03477 #ifdef RIO_DEBUG1
03478 RioErr << "### [PLSessionManager - GetFirstInGroup] Start" << endl;
03479 #endif
03480
03481 InfoClient *member_result = member;
03482
03483 if ( member_result == NULL )
03484 {
03485 #ifdef RIO_DEBUG2
03486 RioErr << "[GetFirstInGroup] Erro: member == NULL !!!!" << endl;
03487 #endif
03488
03489 #ifdef RIO_DEBUG1
03490 RioErr << "### [PLSessionManager - GetFirstInGroup] Finish 1" << endl;
03491 #endif
03492
03493 return NULL;
03494 }
03495
03496 while( member_result->PrevMember != NULL )
03497 member_result = member_result->PrevMember;
03498
03499 #ifdef RIO_DEBUG1
03500 RioErr << "### [PLSessionManager - GetFirstInGroup] Finish 2" << endl;
03501 #endif
03502
03503 return member_result;
03504 }
03505
03506 InfoClient *PLSessionManager::GetLastInGroup( InfoClient *member )
03507 {
03508 #ifdef RIO_DEBUG1
03509 RioErr << "### [PLSessionManager - GetLastInGroup] Start" << endl;
03510 #endif
03511
03512 InfoClient *member_result = member;
03513
03514 if( member_result == NULL )
03515 {
03516 #ifdef RIO_DEBUG1
03517 RioErr << "### [PLSessionManager - GetLastInGroup] Finish 1" << endl;
03518 #endif
03519
03520 return NULL;
03521 }
03522
03523 while( member_result->NextMember != NULL )
03524 member_result = member_result->NextMember;
03525
03526 #ifdef RIO_DEBUG1
03527 RioErr << "### [PLSessionManager - GetLastInGroup] Finish 2" << endl;
03528 #endif
03529
03530 return member_result;
03531 }
03532
03533 int PLSessionManager::DeleteClient( pid_t PIDClientThread )
03534 {
03535 #ifdef RIO_DEBUG1
03536 RioErr << "### [PLSessionManager - DeleteClient] Start" << endl;
03537 #endif
03538
03539 MoveMutexLock( "delcli1" );
03540 InfoClientMutexLock( "delcli1" );
03541
03542 int result = PL_ERROR;
03543 InfoClient *member = FindClient( PIDClientThread );
03544
03545 if( member != NULL )
03546 {
03547 result = RemoveMember( member );
03548 delete( member );
03549 PrintLogFile();
03550 }
03551
03552 InfoClientMutexUnlock( "delcli1" );
03553 MoveMutexUnlock( "delcli1" );
03554
03555 #ifdef RIO_DEBUG1
03556 RioErr << "### [PLSessionManager - DeleteClient] Finish" << endl;
03557 #endif
03558
03559 return result;
03560 }
03561
03562
03563 int PLSessionManager::ExecuteStreamManager( unsigned int Method,
03564 char* Parameter,
03565 unsigned int ParameterSize,
03566 char* Result,
03567 unsigned int* ResultSize,
03568 CRioStream *StreamRioServer,
03569 CRioObject *ObjectRioServer,
03570 RioRequest *RequestRioServer,
03571 SOCKADDR_IN remoteaddress )
03572 {
03573 #ifdef RIO_DEBUG1
03574 RioErr << "### [PLSessionManager - ExecuteStreamManager] Start" << endl;
03575 #endif
03576
03577 int offset = 0;
03578 int open = 0;
03579
03580 switch( Method )
03581 {
03582
03583 case RioMethodStreamManagerClose:
03584 {
03585 RioStreamId Stream;
03586 offset = 0;
03587 HRESULT hresult;
03588
03589
03590
03591
03592 if( !GetLong( Parameter,ParameterSize,&( Stream.Version ),
03593 &offset )
03594 )
03595 {
03596 #ifdef RIO_DEBUG1
03597 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 1" << endl;
03598 #endif
03599
03600 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
03601 }
03602 if( !GetLong( Parameter,ParameterSize,&( Stream.Index ),&offset ) )
03603 {
03604 #ifdef RIO_DEBUG1
03605 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 2" << endl;
03606 #endif
03607
03608 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
03609 }
03610
03611 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
03612 m_log << "Closing Stream... " << endl;
03613 #endif
03614
03615 hresult = StreamManager->CloseStreamRioServer( ResultSize,
03616 StreamRioServer );
03617
03618 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
03619 m_log << "[ExecuteStreamManager] Failed1: "
03620 << GetErrorDescription( hresult ) << endl;
03621 #endif
03622
03623 offset = 0;
03624
03625
03626 SetLong( Result, hresult, &offset );
03627
03628
03629 *ResultSize = offset;
03630
03631 #ifdef RIO_DEBUG1
03632 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 3" << endl;
03633 #endif
03634
03635 return S_OK;
03636 }
03637
03638
03639 case RioMethodStreamManagerOpenObject:
03640 {
03641 open = 1;
03642 }
03643
03644
03645 case RioMethodStreamManagerOpenObjectSubmitToCAC:
03646 {
03647 RioStreamId Stream;
03648 char *ObjectName;
03649 RioAccess Access;
03650 ObjectHandle Handle;
03651 struct timeval average_RTT;
03652 int BufferSize;
03653 unsigned int highpart = 0;
03654 unsigned int lowpart = 0;
03655 HRESULT hresult;
03656 unsigned int Size;
03657 int AuxInt;
03658
03659 Handle.Version = 0;
03660 Handle.Index = 0;
03661
03662 if( !open )
03663 {
03664 offset = 0;
03665
03666
03667
03668
03669 if( !GetLong( Parameter,ParameterSize,&( Stream.Version ),
03670 &offset )
03671 )
03672 {
03673 #ifdef RIO_DEBUG1
03674 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 4" << endl;
03675 #endif
03676
03677 return ( ERROR_PLCONNECTIONMANAGER +
03678 ERROR_INVALID_METHOD_PARAM );
03679 }
03680 if( !GetLong( Parameter,ParameterSize,&( Stream.Index ),
03681 &offset )
03682 )
03683 {
03684 #ifdef RIO_DEBUG1
03685 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 5" << endl;
03686 #endif
03687
03688 return ( ERROR_PLCONNECTIONMANAGER +
03689 ERROR_INVALID_METHOD_PARAM );
03690 }
03691
03692
03693 if( !GetString( Parameter,ParameterSize,&ObjectName,&offset ) )
03694 {
03695 #ifdef RIO_DEBUG1
03696 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 6" << endl;
03697 #endif
03698
03699 return ( ERROR_PLCONNECTIONMANAGER +
03700 ERROR_INVALID_METHOD_PARAM );
03701 }
03702
03703
03704 if( !GetULong( Parameter,ParameterSize,&Access,&offset ) )
03705 {
03706 #ifdef RIO_DEBUG1
03707 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 7" << endl;
03708 #endif
03709
03710 return ( ERROR_PLCONNECTIONMANAGER +
03711 ERROR_INVALID_METHOD_PARAM );
03712 }
03713
03714
03715
03716 if( !GetLong( Parameter,ParameterSize, &AuxInt, &offset )
03717 )
03718 {
03719 #ifdef RIO_DEBUG1
03720 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 8" << endl;
03721 #endif
03722
03723 return ( ERROR_PLCONNECTIONMANAGER +
03724 ERROR_INVALID_METHOD_PARAM );
03725 }
03726 average_RTT.tv_sec = AuxInt;
03727 if( !GetLong( Parameter,ParameterSize, &AuxInt, &offset )
03728 )
03729 {
03730 #ifdef RIO_DEBUG1
03731 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 9" << endl;
03732 #endif
03733
03734 return ( ERROR_PLCONNECTIONMANAGER +
03735 ERROR_INVALID_METHOD_PARAM );
03736 }
03737 average_RTT.tv_usec = AuxInt;
03738
03739 if( !GetLong( Parameter,ParameterSize,&( BufferSize ),
03740 &offset )
03741 )
03742 {
03743 #ifdef RIO_DEBUG1
03744 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 10" << endl;
03745 #endif
03746
03747 return ( ERROR_PLCONNECTIONMANAGER +
03748 ERROR_INVALID_METHOD_PARAM );
03749 }
03750
03751 hresult = StreamManager->OpenCAC( ObjectName,
03752 Access,
03753 average_RTT,
03754 BufferSize,
03755 StreamRioServer,
03756 ResultSize,
03757 ObjectRioServer );
03758 if( FAILED(hresult) )
03759 {
03760 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
03761 m_log << "[ExecuteStreamManager] Failed2: "
03762 << GetErrorDescription( hresult ) << endl;
03763 #endif
03764
03765 #ifdef RIO_DEBUG1
03766 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 11" << endl;
03767 #endif
03768
03769 return hresult;
03770 }
03771 }
03772 else
03773 {
03774
03775
03776
03777 if( !GetLong( Parameter,ParameterSize,&( Stream.Version ),
03778 &offset )
03779 )
03780 {
03781 #ifdef RIO_DEBUG1
03782 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 12" << endl;
03783 #endif
03784
03785 return ( ERROR_PLCONNECTIONMANAGER +
03786 ERROR_INVALID_METHOD_PARAM );
03787 }
03788 if( !GetLong( Parameter,ParameterSize,&( Stream.Index ),
03789 &offset )
03790 )
03791 {
03792 #ifdef RIO_DEBUG1
03793 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 13" << endl;
03794 #endif
03795
03796 return ( ERROR_PLCONNECTIONMANAGER +
03797 ERROR_INVALID_METHOD_PARAM );
03798 }
03799
03800
03801 if( !GetString( Parameter,ParameterSize,&ObjectName,&offset ) )
03802 {
03803 #ifdef RIO_DEBUG1
03804 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 14" << endl;
03805 #endif
03806
03807 return ( ERROR_PLCONNECTIONMANAGER +
03808 ERROR_INVALID_METHOD_PARAM );
03809 }
03810
03811
03812 if( !GetULong( Parameter,ParameterSize,&Access,&offset ) )
03813 {
03814 #ifdef RIO_DEBUG1
03815 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 15" << endl;
03816 #endif
03817
03818 return ( ERROR_PLCONNECTIONMANAGER +
03819 ERROR_INVALID_METHOD_PARAM );
03820 }
03821
03822 hresult = StreamManager->Open( ObjectName,
03823 Access,
03824 StreamRioServer,
03825 ObjectRioServer );
03826 if( FAILED(hresult) )
03827 {
03828 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
03829 m_log << "[ExecuteStreamManager] Failed3: "
03830 << GetErrorDescription( hresult ) << endl;
03831 #endif
03832
03833 #ifdef RIO_DEBUG1
03834 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 16" << endl;
03835 #endif
03836
03837 return hresult;
03838 }
03839 }
03840
03841 RioObjectSize ObjSize;
03842
03843 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
03844 m_log << "Object SIzebjeto \"" << ObjectName << "\" ... " << endl;
03845 #endif
03846
03847 hresult = ObjectRioServer->GetSize( &ObjSize );
03848
03849 if( FAILED(hresult) )
03850 {
03851 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
03852 m_log << "[ExecuteStreamManager] Failed4: "
03853 << GetErrorDescription( hresult ) << endl;
03854 #endif
03855
03856 #ifdef RIO_DEBUG1
03857 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 17" << endl;
03858 #endif
03859
03860 return ERROR_PLCONNECTIONMANAGER + ( hresult & 0x000000FF );
03861 }
03862 else
03863 {
03864
03865 highpart = ( unsigned int )(( ObjSize >> 32 ) & 0xffffffff);
03866 lowpart = ( unsigned int )( ObjSize & 0xffffffff);
03867 }
03868
03869 if( ObjSize == 0 )
03870 {
03871 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
03872 m_log << " Empty" << endl;
03873 #endif
03874
03875 #ifdef RIO_DEBUG1
03876 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 18" << endl;
03877 #endif
03878
03879 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_BLOCKSIZE;
03880 }
03881
03882 unsigned int TotalBlocks = (int)(((ObjSize-1)/BlockSize)+1);
03883
03884 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
03885 m_log << " OK" << endl;
03886 #endif
03887
03888
03889
03890
03891 MoveMutexLock( "exstmg1" );
03892
03893
03894 InfoClient *client = CreateNewMember( ObjectName, TotalBlocks );
03895
03896 MoveMutexUnlock( "exstmg1" );
03897
03898
03899 Size = 5 * MAX_LONG_STRING_SIZE + MAX_LONG_STRING_SIZE +
03900 2 * MAX_ULONG_STRING_SIZE + 1 ;
03901 if( Size > *ResultSize )
03902 {
03903 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
03904 m_log << "Error: Message too int." << endl;
03905 #endif
03906
03907 #ifdef RIO_DEBUG1
03908 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 19" << endl;
03909 #endif
03910
03911 return ( ERROR_PLCONNECTIONMANAGER +
03912 ERROR_RESULT_MESSAGE_OVERFLOW );
03913 }
03914
03915 offset = 0;
03916
03917
03918 SetLong( Result, hresult, &offset );
03919
03920
03921 SetLong( Result, Handle.Version, &offset );
03922 SetLong( Result, Handle.Index, &offset );
03923
03924 SetULong( Result, highpart, &offset );
03925 SetULong( Result, lowpart, &offset );
03926
03927 SetLong( Result, client->mode, &offset );
03928 SetLong( Result, client->PIDClientThread, &offset );
03929
03930
03931 *ResultSize = offset;
03932
03933 #ifdef RIO_DEBUG1
03934 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 20" << endl;
03935 #endif
03936
03937 return S_OK;
03938 }
03939
03940
03941 case RioMethodStreamManagerMaxRequests:
03942 {
03943 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
03944 m_log << "[PLSessionManager]: Invalid Method "
03945 << "RioMethodStreamManagerMaxRequests" << endl;
03946 #endif
03947
03948 #ifdef RIO_DEBUG1
03949 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 21" << endl;
03950 #endif
03951
03952 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD;
03953 }
03954
03955
03956 case RioMethodStreamManagerClientCanStart:
03957 {
03958 RioStreamId Stream;
03959 HRESULT hresult;
03960
03961
03962
03963
03964 if( !GetLong( Parameter,ParameterSize,
03965 &( Stream.Version ),&offset )
03966 )
03967 {
03968 #ifdef RIO_DEBUG1
03969 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 22" << endl;
03970 #endif
03971
03972 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
03973 }
03974 if( !GetLong( Parameter,ParameterSize,&( Stream.Index ),&offset ) )
03975 {
03976 #ifdef RIO_DEBUG1
03977 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 23" << endl;
03978 #endif
03979
03980 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
03981 }
03982
03983 hresult = StreamManager->CanStart( StreamRioServer, ResultSize );
03984
03985 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
03986 m_log << "[ExecuteStreamManager] Failed5: "
03987 << GetErrorDescription( hresult ) << endl;
03988 #endif
03989
03990 offset = 0;
03991
03992
03993 SetLong( Result, hresult, &offset );
03994
03995
03996 *ResultSize = offset;
03997
03998 #ifdef RIO_DEBUG1
03999 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 24" << endl;
04000 #endif
04001
04002 return S_OK;
04003 }
04004
04005
04006
04007 default:
04008 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
04009 m_log << "[ExecuteStreamManager] Failed5: " << endl;
04010 #endif
04011 {
04012 #ifdef RIO_DEBUG1
04013 RioErr << "### [PLSessionManager - ExecuteStreamManager] Finish 25" << endl;
04014 #endif
04015
04016 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD;
04017 }
04018 }
04019 }
04020
04021 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
04022 void PLSessionManager::PrintClientInfo( InfoClient *Client )
04023 {
04024 #ifdef RIO_DEBUG1
04025 RioErr << "### [PLSessionManager - PrintClientInfo] Start" << endl;
04026 #endif
04027
04028 m_log << endl;
04029 m_log << "********************************************************" << endl;
04030 m_log << "Video Name(total blocks): " << Client->video_name << "("
04031 << Client->videoTotalBlocks << ")" << endl;
04032 m_log << "PID Client Thread: " << Client->PIDClientThread << endl;
04033 m_log << "Mode: "
04034 << CommMulticastStream->ClientMode2String( Client->mode ) << endl;
04035 m_log << "MulticastAddr/MulticastPort: " << Client->multicast_ip << "/"
04036 << Client->multicast_port << endl;
04037 m_log << "CurrentBlock: " << Client->CurrentBlock << endl;
04038 m_log << "isPatching: " << Client->isPatching << endl;
04039 m_log << "********************************************************" << endl;
04040 m_log << endl;
04041
04042 #ifdef RIO_DEBUG1
04043 RioErr << "### [PLSessionManager - PrintClientInfo] Finish" << endl;
04044 #endif
04045
04046 }
04047
04048 void PLSessionManager::PrintClientTable()
04049 {
04050 #ifdef RIO_DEBUG1
04051 RioErr << "### [PLSessionManager - PrintClientTable] Start" << endl;
04052 #endif
04053
04054 InfoClient *aux = PLSessionManager::m_InfoClient;
04055 InfoClient *groupleader = aux;
04056
04057 m_log << "-------MulticastPortInfo-------" << endl;
04058 MulticastPortInfoMutexLock( "ptcltb2" );
04059 for( int mpinfo = 0; mpinfo < m_num_ports; mpinfo++ )
04060 {
04061 if( MulticastPortInfo[mpinfo][1] != 0 )
04062 m_log << "Port: " << MulticastPortInfo[mpinfo][0]
04063 << "\tPID:" << MulticastPortInfo[mpinfo][1]
04064 << endl;
04065 }
04066 MulticastPortInfoMutexUnlock( "ptcltb2" );
04067
04068 m_log << "--------- ACTIVE LIST ---------" << endl;
04069 m_log << "|ClientPID (Group) - VideoName - (ClientMode) [CurrentBlock, "
04070 << "Target] - IsPatching|" << endl;
04071
04072 while( groupleader )
04073 {
04074 aux = groupleader;
04075 while( aux )
04076 {
04077 m_log << "|" << aux->PIDClientThread << "("
04078 << aux->multicast_port << ") - "
04079 << aux->video_name << " - ("
04080 << CommMulticastStream->ClientMode2String( aux->mode )
04081 << ") [" << aux->CurrentBlock << "," << aux->target << "] - "
04082 << aux->isPatching << "|" << endl;
04083
04084 aux = aux->NextMember;
04085 }
04086 groupleader = groupleader->NextGroup;
04087 m_log << " " << endl;
04088 }
04089
04090 aux = PLSessionManager::m_InactiveClient;
04091 m_log << "--------- INACTIVE LIST ---------" << endl;
04092 while( aux )
04093 {
04094 m_log << "|" << aux->PIDClientThread << "(" << aux->multicast_port
04095 << ") - " << aux->video_name << " - ("
04096 << CommMulticastStream->ClientMode2String( aux->mode )
04097 << ") [" << aux->CurrentBlock << ", " << aux->target << "] - "
04098 << aux->isPatching << "|" << endl;
04099
04100 aux = aux->NextMember;
04101 }
04102 m_log << endl;
04103 m_log << "---------//-----------//---------" << endl;
04104
04105 #ifdef RIO_DEBUG1
04106 RioErr << "### [PLSessionManager - PrintClientTable] Finish" << endl;
04107 #endif
04108
04109 }
04110 #endif
04111
04112
04113 int PLSessionManager::ExecuteRouter( unsigned int Method,
04114 char *Parameter,
04115 unsigned int ParameterSize,
04116 char *Result,
04117 unsigned int *ResultSize,
04118 CRioObject *ObjectRioServer,
04119 RioRequest *RequestRioServer,
04120 CRioSession *SessionRioServer,
04121 SOCKADDR_IN remoteAddress )
04122 {
04123 #ifdef RIO_DEBUG1
04124 RioErr << "### [PLSessionManager - ExecuteRouter] Start" << endl;
04125 #endif
04126
04127 int offset = 0;
04128 unsigned int Size;
04129
04130 switch( Method )
04131 {
04132
04133 case RioMethodRouterDataRequest:
04134 {
04135 u32 reqid, ipaddr, objid, block, repbits;
04136 u16 port, operation, u16traffic;
04137 RioStreamType traffic;
04138
04139 if( !GetULong( Parameter, ParameterSize, &reqid, &offset ) )
04140 {
04141 #ifdef RIO_DEBUG1
04142 RioErr << "### [PLSessionManager - ExecuteRouter] Finish 1" << endl;
04143 #endif
04144
04145 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
04146 }
04147 if( !GetULong( Parameter, ParameterSize, &ipaddr, &offset ) )
04148 {
04149 #ifdef RIO_DEBUG1
04150 RioErr << "### [PLSessionManager - ExecuteRouter] Finish 2" << endl;
04151 #endif
04152
04153 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
04154 }
04155 if( !GetULong( Parameter, ParameterSize, &objid ,&offset ) )
04156 {
04157 #ifdef RIO_DEBUG1
04158 RioErr << "### [PLSessionManager - ExecuteRouter] Finish 3" << endl;
04159 #endif
04160
04161 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
04162 }
04163 if( !GetULong( Parameter, ParameterSize, &block, &offset ) )
04164 {
04165 #ifdef RIO_DEBUG1
04166 RioErr << "### [PLSessionManager - ExecuteRouter] Finish 4" << endl;
04167 #endif
04168
04169 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
04170 }
04171 if( !GetULong( Parameter, ParameterSize, &repbits, &offset ) )
04172 {
04173 #ifdef RIO_DEBUG1
04174 RioErr << "### [PLSessionManager - ExecuteRouter] Finish 5" << endl;
04175 #endif
04176
04177 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
04178 }
04179 if( !GetUShort( Parameter, ParameterSize, &port, &offset ) )
04180 {
04181 #ifdef RIO_DEBUG1
04182 RioErr << "### [PLSessionManager - ExecuteRouter] Finish 6" << endl;
04183 #endif
04184
04185 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
04186 }
04187 if( !GetUShort( Parameter, ParameterSize, &operation, &offset ) )
04188 {
04189 #ifdef RIO_DEBUG1
04190 RioErr << "### [PLSessionManager - ExecuteRouter] Finish 7" << endl;
04191 #endif
04192
04193 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
04194 }
04195 if( !GetUShort( Parameter, ParameterSize, &u16traffic, &offset ) )
04196 {
04197 #ifdef RIO_DEBUG1
04198 RioErr << "### [PLSessionManager - ExecuteRouter] Finish 8" << endl;
04199 #endif
04200
04201 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
04202 }
04203
04204 traffic = (RioStreamType) u16traffic;
04205
04206 InfoClient *client;
04207
04208
04209
04210 MoveMutexLock( "exrtr1" );
04211
04212 client = FindClient( pthread_self() );
04213
04214 if( client != NULL )
04215 {
04216 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
04217 m_log << "Requested Block: " << block << endl;
04218 PrintClientInfo( client );
04219 #endif
04220 }
04221 else
04222 {
04223 MoveMutexUnlock( "exrtr1error1" );
04224
04225 #ifdef RIO_DEBUG1
04226 RioErr << "### [PLSessionManager - ExecuteRouter] Finish 9" << endl;
04227 #endif
04228
04229 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
04230 }
04231
04232 u32 ip, client_port;
04233
04234 InfoClientMutexLock( "exrtr2" );
04235
04236 if( ( ( client->mode == LEADER ) ||
04237 ( client->mode == SUBLEADER )
04238 ) && ( traffic == MULTICASTTRAFFIC )
04239 )
04240 {
04241 client->CurrentBlock = block;
04242 }
04243
04244 InfoClientMutexUnlock( "exrtr2" );
04245
04246
04247 if( ( ( client->mode == LEADER ) ||
04248 ( client->mode == SUBLEADER )
04249 ) &&
04250 ( traffic == MULTICASTTRAFFIC ) &&
04251 ( client != m_InactiveClient )
04252 )
04253 {
04254 #ifdef RIO_DEBUG2
04255 RioErr << "[PLSessionManager - ExecRouter] Cliente ("
04256 << CommMulticastStream->ClientMode2String( client->mode )
04257 << ") " << client->PIDClientThread << " pedindo bloco "
04258 << block << " para o grupo " << client->multicast_port
04259 << endl;
04260 #endif
04261
04262 ip = inet_addr( client->multicast_ip );
04263 client_port = client->multicast_port;
04264 RequestRioServer->reqid = block;
04265 }
04266 else
04267 {
04268 InfoClient *leader = GetGroupLeader( client );
04269
04270
04271
04272
04273
04274
04275
04276 if( ( leader != NULL ) &&
04277 ( traffic == UNICASTTRAFFIC ) &&
04278 ( ( int ) block > leader->CurrentBlock + 2 ) &&
04279 ( client != leader )
04280 )
04281 {
04282 #ifdef RIO_DEBUG2
04283 RioErr << "[PLSessionManager - ExecRouter] Cliente "
04284 << client->PIDClientThread << ", com bloco " << block
04285 << ", ultrapassou seu l�der ("
04286 << leader->PIDClientThread << ", com bloco "
04287 << leader->CurrentBlock << ") e assumir� a "
04288 << "lideran�a do grupo " << leader->multicast_port
04289 << "." << endl;
04290 #endif
04291
04292 if( client->mode == SUBLEADER )
04293 {
04294
04295
04296
04297
04298
04299
04300
04301
04302 ClientMode sendMode;
04303 int sendCurrentBlock;
04304
04305 for( InfoClient *aux = client;
04306 aux != NULL;
04307 aux = aux->NextMember
04308 )
04309 {
04310 if( aux->mode == SUBLEADER )
04311 {
04312
04313
04314
04315
04316 sendMode = LEADER;
04317 sendCurrentBlock = leader->CurrentBlock;
04318 }
04319 else
04320 {
04321 sendMode = PASSIVE;
04322 sendCurrentBlock = -1;
04323 }
04324
04325
04326
04327
04328
04329
04330 #ifdef RIO_DEBUG2
04331 RioErr << "[PLSessionManager - ExecRouter] O "
04332 << "subl�der " << client->PIDClientThread
04333 << " ultrapassou o l�der. Enviando mensagem "
04334 << "pro submembro " << aux->PIDClientThread
04335 << " abandonar o subfluxo." << endl;
04336 #endif
04337
04338 CommMulticastStream->sendIPMsg(
04339 aux->PIDClientThread,
04340 client->multicast_ip,
04341 client->multicast_port,
04342 sendMode,
04343 sendCurrentBlock,
04344 LEAVE_THIS );
04345 }
04346 DischargeMulticastPort( client->PIDClientThread );
04347 }
04348 else
04349 {
04350 #ifdef RIO_DEBUG2
04351 RioErr << "[PLSessionManager - ExecRouter] Cliente "
04352 << client->PIDClientThread
04353 << " tornando-se lider do grupo "
04354 << client->multicast_port << ". CurrentBlock = "
04355 << leader->CurrentBlock << endl;
04356 #endif
04357
04358
04359
04360
04361
04362 CommMulticastStream->sendModeMsg(
04363 client->PIDClientThread,
04364 LEADER, leader->CurrentBlock );
04365 }
04366
04367 #ifdef RIO_DEBUG2
04368 RioErr << "[PLSessionManager - ExecRouter] cliente "
04369 << leader->PIDClientThread << " deixando lideran�a "
04370 << "do grupo " << leader->multicast_port << "."
04371 << endl;
04372 #endif
04373
04374
04375 CommMulticastStream->sendModeMsg( leader->PIDClientThread,
04376 PASSIVE );
04377
04378 if( SwitchToLeader( client ) == PL_ERROR )
04379 {
04380 #ifdef RIO_DEBUG2
04381 RioErr << "[PLSessionManager - ExecRouter] Erro ao "
04382 << "tornar cliente " << client->PIDClientThread
04383 << " o l�der do grupo "
04384 << leader->PIDClientThread << "." << endl;
04385 #endif
04386
04387 MoveMutexUnlock( "exrtr1error2" );
04388
04389 #ifdef RIO_DEBUG1
04390 RioErr << "### [PLSessionManager - ExecuteRouter] Finish 10" << endl;
04391 #endif
04392
04393 return PL_ERROR;
04394 }
04395
04396 }
04397 else
04398 {
04399
04400
04401
04402 int subLeaderBlock = INT_MAX - 2;
04403
04404 if( isSubMember( client, &subLeaderBlock ) &&
04405 ( traffic == UNICASTTRAFFIC ) &&
04406 ( ( int ) block > subLeaderBlock + 2 )
04407 )
04408 {
04409 InfoClient *subLeader = GetGroupSubLeader( client );
04410
04411 if( SwitchToPassiveUser( client ) == PL_ERROR )
04412 {
04413 #ifdef RIO_DEBUG2
04414 RioErr << "[PLSessionManager - ExecRouter] Erro ao "
04415 << "tornar cliente "
04416 << client->PIDClientThread
04417 << " passivo do grupo "
04418 << leader->PIDClientThread << "." << endl;
04419 #endif
04420
04421 MoveMutexUnlock( "exrtr1error3" );
04422
04423 #ifdef RIO_DEBUG1
04424 RioErr << "### [PLSessionManager - ExecuteRouter] Finish 11" << endl;
04425 #endif
04426
04427 return PL_ERROR;
04428 }
04429
04430 #ifdef RIO_DEBUG2
04431 RioErr << "[PLSessionManager] SubMembro "
04432 << client->PIDClientThread << " ultrapassou seu "
04433 << " subl�der e deixar� o subfluxo "
04434 << subLeader->multicast_port << endl;
04435 #endif
04436
04437 CommMulticastStream->sendIPMsg(
04438 client->PIDClientThread,
04439 subLeader->multicast_ip,
04440 subLeader->multicast_port,
04441 PASSIVE, -1, LEAVE_THIS );
04442 }
04443 }
04444
04445 ip = ( unsigned ) remoteAddress.sin_addr.s_addr;
04446 client_port = port;
04447
04448
04449
04450
04451
04452
04453 traffic = MULTICASTTRAFFIC;
04454 RequestRioServer->reqid = htonl( reqid );
04455
04456 #ifdef RIO_DEBUG2
04457 RioErr << "[PLSessionManager - ExecRouter] Cliente "
04458 << client->PIDClientThread << ", membro do grupo "
04459 << client->multicast_port << ", fazendo patching do "
04460 << "bloco " << block << endl;
04461 #endif
04462
04463 }
04464
04465 MoveMutexUnlock( "exrtr1" );
04466
04467 HRESULT hresult;
04468
04469 hresult = ObjectManager->PrefetchBlock( block,
04470 BlockSize,
04471 RequestRioServer,
04472 ObjectRioServer,
04473 ip,
04474 traffic,
04475 client_port );
04476 if( FAILED( hresult ) )
04477 {
04478 RioErr << "Failed to call block from server " << endl;
04479 RioErr << "ClienteIP: " << ip << " port: " << client_port
04480 << endl;
04481 }
04482 else
04483 hresult = S_OK;
04484
04485
04486 Size = MAX_LONG_STRING_SIZE + 2 * MAX_SHORT_STRING_SIZE;
04487 if( Size > *ResultSize )
04488 {
04489 #ifdef RIO_DEBUG1
04490 RioErr << "### [PLSessionManager - ExecuteRouter] Finish 12" << endl;
04491 #endif
04492
04493 return ERROR_PLCONNECTIONMANAGER + ERROR_RESULT_MESSAGE_OVERFLOW;
04494 }
04495
04496 offset = 0;
04497
04498
04499 SetLong( Result, hresult, &offset );
04500
04501
04502 *ResultSize = offset;
04503
04504 #ifdef RIO_DEBUG1
04505 RioErr << "### [PLSessionManager - ExecuteRouter] Finish 13" << endl;
04506 #endif
04507
04508 return hresult;
04509 }
04510
04511
04512 default:
04513 {
04514 #ifdef RIO_DEBUG1
04515 RioErr << "### [PLSessionManager - ExecuteRouter] Finish 14" << endl;
04516 #endif
04517
04518 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD;
04519 }
04520 }
04521 }
04522
04523
04524 int PLSessionManager::ExecuteObjectManager( unsigned int Method,
04525 char* Parameter,
04526 unsigned int ParameterSize,
04527 char* Result,
04528 unsigned int* ResultSize,
04529 CRioObject *ObjectRioServer )
04530 {
04531 #ifdef RIO_DEBUG1
04532 RioErr << "### [PLSessionManager - ExecuteObjectManager] Start" << endl;
04533 #endif
04534
04535 int offset = 0;
04536
04537 switch( Method )
04538 {
04539 case RioMethodObjectManagerClose:
04540 {
04541 ObjectHandle Handle;
04542 offset = 0;
04543 HRESULT hresult;
04544
04545
04546
04547
04548 if( !GetLong( Parameter,ParameterSize,&( Handle.Version ),
04549 &offset )
04550 )
04551 {
04552 #ifdef RIO_DEBUG1
04553 RioErr << "### [PLSessionManager - ExecuteObjectManager] Finish 1" << endl;
04554 #endif
04555
04556 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
04557 }
04558 if( !GetLong( Parameter,ParameterSize,&( Handle.Index ),&offset ) )
04559 {
04560 #ifdef RIO_DEBUG1
04561 RioErr << "### [PLSessionManager - ExecuteObjectManager] Finish 2" << endl;
04562 #endif
04563
04564 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD_PARAM;
04565 }
04566
04567 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
04568 m_log << "Closing Object... " << endl;
04569 #endif
04570
04571 hresult = ObjectManager->Close( ResultSize,
04572 ObjectRioServer );
04573
04574 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
04575 if( FAILED( hresult ) )
04576 m_log << " Failed: " << GetErrorDescription( hresult ) << endl;
04577 #endif
04578
04579 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
04580 m_log << "OK" << endl;
04581 #endif
04582
04583 offset = 0;
04584
04585
04586 SetLong( Result, hresult, &offset );
04587
04588
04589 *ResultSize = offset;
04590
04591 #ifdef RIO_DEBUG1
04592 RioErr << "### [PLSessionManager - ExecuteObjectManager] Finish 3" << endl;
04593 #endif
04594
04595 return S_OK;
04596 }
04597
04598
04599
04600 default:
04601 {
04602 #ifdef RIO_DEBUG1
04603 RioErr << "### [PLSessionManager - ExecuteObjectManager] Finish 4" << endl;
04604 #endif
04605
04606 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_METHOD;
04607 }
04608 }
04609 }
04610
04611
04612 int PLSessionManager::ExecuteCall( unsigned int Class,
04613 unsigned int Method,
04614 char *Parameter,
04615 unsigned int ParameterSize,
04616 char *Result,
04617 unsigned int *ResultSize,
04618 CRioSession *SessionRioServer,
04619 CRioStream *StreamRioServer,
04620 CRioObject *ObjectRioServer,
04621 RioRequest *RequestRioServer,
04622 SOCKADDR_IN remoteAddress )
04623 {
04624 #ifdef RIO_DEBUG1
04625 RioErr << "### [PLSessionManager - ExecuteCall] Start" << endl;
04626 #endif
04627
04628
04629
04630
04631
04632 this->ObjectRioServer = ObjectRioServer;
04633 this->RequestRioServer = RequestRioServer;
04634
04635 switch( Class )
04636 {
04637 case RioClassSessionManager:
04638 {
04639 #ifdef RIO_DEBUG1
04640 RioErr << "### [PLSessionManager - ExecuteCall] Finish 1" << endl;
04641 #endif
04642
04643 return ExecuteSessionManager( Method,
04644 Parameter,
04645 ParameterSize,
04646 Result,
04647 ResultSize,
04648 SessionRioServer,
04649 StreamRioServer,
04650 RequestRioServer );
04651 }
04652 case RioClassStreamManager:
04653 {
04654 #ifdef RIO_DEBUG1
04655 RioErr << "### [PLSessionManager - ExecuteCall] Finish 2" << endl;
04656 #endif
04657
04658 return ExecuteStreamManager( Method,
04659 Parameter,
04660 ParameterSize,
04661 Result,
04662 ResultSize,
04663 StreamRioServer,
04664 ObjectRioServer,
04665 RequestRioServer,
04666 remoteAddress );
04667 }
04668 case RioClassObjectManager:
04669 {
04670 #ifdef RIO_DEBUG1
04671 RioErr << "### [PLSessionManager - ExecuteCall] Finish 3" << endl;
04672 #endif
04673
04674 return ExecuteObjectManager( Method,
04675 Parameter,
04676 ParameterSize,
04677 Result,
04678 ResultSize,
04679 ObjectRioServer );
04680 }
04681 case RioClassRouter:
04682 {
04683 #ifdef RIO_DEBUG1
04684 RioErr << "### [PLSessionManager - ExecuteCall] Finish 4" << endl;
04685 #endif
04686
04687 return ExecuteRouter( Method,
04688 Parameter,
04689 ParameterSize,
04690 Result,
04691 ResultSize,
04692 ObjectRioServer,
04693 RequestRioServer,
04694 SessionRioServer,
04695 remoteAddress );
04696 }
04697
04698 default:
04699 {
04700 #if defined(RIO_DEBUG2) || defined(RIO_EMUL)
04701 m_log << "[PLSessionManager]: Executecall: invalid class " << endl;
04702 #endif
04703
04704 #ifdef RIO_DEBUG1
04705 RioErr << "### [PLSessionManager - ExecuteCall] Finish 5" << endl;
04706 #endif
04707
04708 return ERROR_PLCONNECTIONMANAGER + ERROR_INVALID_CLASS;
04709 }
04710 }
04711
04712 return S_OK;
04713 }
04714
04715 InfoClient *PLSessionManager::CreateNewMember( char *ObjectName,
04716 unsigned int ObjectSize )
04717 {
04718 #ifdef RIO_DEBUG1
04719 RioErr << "### [PLSessionManager - CreateNewMember] Start" << endl;
04720 #endif
04721
04722 InfoClient *client;
04723
04724 client = new InfoClient;
04725
04726 client->PIDClientThread = pthread_self();
04727 strcpy( client->multicast_ip, "0.0.0.0" );
04728 client->multicast_port = 0;
04729 strcpy( client->video_name, ObjectName );
04730 client->videoTotalBlocks = ObjectSize;
04731 client->isPatching = false;
04732
04733 SetMode( client, UNAVAILABLE );
04734
04735
04736
04737 client->PrevGroup = NULL;
04738 client->PrevMember = NULL;
04739 client->NextGroup = NULL;
04740 client->NextMember = NULL;
04741
04742
04743
04744 InsertMemberInInactiveGroup( client );
04745
04746 #ifdef RIO_DEBUG1
04747 RioErr << "### [PLSessionManager - CreateNewMember] Finish" << endl;
04748 #endif
04749
04750 return client;
04751 }
04752
04753 bool PLSessionManager::isMergeableGroup( InfoClient *group )
04754 {
04755 #ifdef RIO_DEBUG1
04756 RioErr << "### [PLSessionManager - isMergeableGroup] Start" << endl;
04757 #endif
04758
04759 bool status = true;
04760
04761 if( group->mode != LEADER )
04762 {
04763 #ifdef RIO_DEBUG2
04764 RioErr << "[isMergeableGroup] Grupo " << group->multicast_port
04765 << " em dissolu��o e n�o apto para merging." << endl;
04766 #endif
04767
04768 status = false;
04769 }
04770 else
04771 {
04772 for( InfoClient *aux = group;
04773 status && ( aux != NULL );
04774 aux = aux->NextMember
04775 )
04776 {
04777 if( aux->isPatching || ( aux->mode == SUBLEADER ) )
04778 {
04779 #ifdef RIO_DEBUG2
04780 RioErr << "[isMergeableGroup] Grupo " << group->multicast_port
04781 << " inst�vel e n�o apto para merging (cliente "
04782 << aux->PIDClientThread << " em modo "
04783 << CommMulticastStream->ClientMode2String( aux->mode)
04784 << " com isPatching = " << aux->isPatching << ")."
04785 << endl;
04786 #endif
04787
04788 status = false;
04789 }
04790 #ifdef RIO_DEBUG2
04791 else
04792 {
04793 RioErr << "[isMergeableGroup] Cliente " << aux->PIDClientThread
04794 << " n�o impede o grupo " << group->multicast_port
04795 << " de ser mergeable (mode = "
04796 << CommMulticastStream->ClientMode2String( aux->mode)
04797 << ", isPatching = " << aux->isPatching << ")." << endl;
04798 }
04799 #endif
04800 }
04801 }
04802
04803 #ifdef RIO_DEBUG1
04804 RioErr << "### [PLSessionManager - isMergeableGroup] Finish" << endl;
04805 #endif
04806
04807 return status;
04808 }
04809
04810 bool PLSessionManager::isSubMember( InfoClient *member, int *subLeaderBlock )
04811 {
04812 #ifdef RIO_DEBUG1
04813 RioErr << "### [PLSessionManager - isSubMember] Start" << endl;
04814 #endif
04815
04816 bool status = false;
04817
04818 *subLeaderBlock = -1;
04819
04820 if( member == NULL )
04821 {
04822 #ifdef RIO_DEBUG2
04823 RioErr << "[isSubMember] Par�metro member inv�lido!" << endl;
04824 #endif
04825
04826 #ifdef RIO_DEBUG1
04827 RioErr << "### [PLSessionManager - isSubMember] Finish 1" << endl;
04828 #endif
04829
04830 return false;
04831 }
04832
04833 InfoClient *subLeader = GetGroupSubLeader( member );
04834
04835 if( subLeader != NULL )
04836 {
04837 *subLeaderBlock = subLeader->CurrentBlock;
04838
04839 for( InfoClient *aux = subLeader;
04840 !status && ( aux != NULL );
04841 aux = aux->NextMember
04842 )
04843 {
04844 if( aux->PIDClientThread == member->PIDClientThread )
04845 status = true;
04846 }
04847 }
04848
04849 #ifdef RIO_DEBUG1
04850 RioErr << "### [PLSessionManager - isSubMember] Finish 2" << endl;
04851 #endif
04852
04853 return status;
04854 }
04855
04856 void PLSessionManager::InfoClientMutexLock( string msg )
04857 {
04858 #ifdef RIO_DEBUG1
04859 RioErr << "### [PLSessionManager - InfoClientMutexLock] Start" << endl;
04860 #endif
04861
04862 #ifdef RIO_DEBUG2
04863 if( msg.length() )
04864 RioErr << "[InfoClientMutexLock] " << msg << "... " << endl;
04865 #endif
04866
04867 pthread_mutex_lock( &InfoClientMutex );
04868
04869 #ifdef RIO_DEBUG2
04870 if( msg.length() )
04871 RioErr << "..." << msg << " OK!" << endl;
04872 #endif
04873
04874 #ifdef RIO_DEBUG1
04875 RioErr << "### [PLSessionManager - InfoClientMutexLock] Finish" << endl;
04876 #endif
04877
04878 }
04879
04880 void PLSessionManager::InfoClientMutexUnlock( string msg )
04881 {
04882 #ifdef RIO_DEBUG1
04883 RioErr << "### [PLSessionManager - InfoClientMutexUnlock] Start" << endl;
04884 #endif
04885
04886 #ifdef RIO_DEBUG2
04887 if( msg.length() )
04888 RioErr << "[InfoClientMutexUnlock] " << msg << endl;
04889 #endif
04890
04891 pthread_mutex_unlock( &InfoClientMutex );
04892
04893 #ifdef RIO_DEBUG1
04894 RioErr << "### [PLSessionManager - InfoClientMutexUnlock] Finish" << endl;
04895 #endif
04896
04897 }
04898
04899 void PLSessionManager::MoveMutexLock( string msg )
04900 {
04901 #ifdef RIO_DEBUG1
04902 RioErr << "### [PLSessionManager - MoveMutexLock] Start" << endl;
04903 #endif
04904
04905 #ifdef RIO_DEBUG2
04906 if( msg.length() )
04907 RioErr << "[MoveMutexLock] " << msg << "... " << endl;
04908 #endif
04909
04910 pthread_mutex_lock( &MoveMutex );
04911
04912 #ifdef RIO_DEBUG2
04913 if( msg.length() )
04914 RioErr << "..." << msg << " OK!" << endl;
04915 #endif
04916
04917 #ifdef RIO_DEBUG1
04918 RioErr << "### [PLSessionManager - MoveMutexLock] Finish" << endl;
04919 #endif
04920
04921 }
04922
04923 void PLSessionManager::MoveMutexUnlock( string msg )
04924 {
04925 #ifdef RIO_DEBUG1
04926 RioErr << "### [PLSessionManager - MoveMutexUnlock] Start" << endl;
04927 #endif
04928
04929 #ifdef RIO_DEBUG2
04930 if( msg.length() )
04931 RioErr << "[MoveMutexUnlock] " << msg << endl;
04932 #endif
04933
04934 pthread_mutex_unlock( &MoveMutex );
04935
04936 #ifdef RIO_DEBUG1
04937 RioErr << "### [PLSessionManager - MoveMutexUnlock] Finish" << endl;
04938 #endif
04939
04940 }
04941
04942 void PLSessionManager::MulticastPortInfoMutexLock( string msg )
04943 {
04944 #ifdef RIO_DEBUG1
04945 RioErr << "### [PLSessionManager - MulticastPortInfoMutexLock] Start" << endl;
04946 #endif
04947
04948 #ifdef RIO_DEBUG2
04949 if( msg.length() )
04950 RioErr << "[MulticastPortInfoMutexLock] " << msg << "... " << endl;
04951 #endif
04952
04953 pthread_mutex_lock( &MulticastPortInfoMutex );
04954
04955 #ifdef RIO_DEBUG2
04956 if( msg.length() )
04957 RioErr << "..." << msg << " OK!" << endl;
04958 #endif
04959
04960 #ifdef RIO_DEBUG1
04961 RioErr << "### [PLSessionManager - MulticastPortInfoMutexLock] Finish" << endl;
04962 #endif
04963
04964 }
04965
04966 void PLSessionManager::MulticastPortInfoMutexUnlock( string msg )
04967 {
04968 #ifdef RIO_DEBUG1
04969 RioErr << "### [PLSessionManager - MulticastPortInfoMutexUnlock] Start" << endl;
04970 #endif
04971
04972 #ifdef RIO_DEBUG2
04973 if( msg.length() )
04974 RioErr << "[MulticastPortInfoMutexUnlock] " << msg << endl;
04975 #endif
04976
04977 pthread_mutex_unlock( &MulticastPortInfoMutex );
04978
04979 #ifdef RIO_DEBUG1
04980 RioErr << "### [PLSessionManager - MulticastPortInfoMutexUnlock] Finish" << endl;
04981 #endif
04982
04983 }