00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifdef WINDOWS
00032
00033 #include "stdafx.h"
00034 #else
00035 #include <netinet/in.h>
00036 #include <netdb.h>
00037 #endif
00038
00039
00040 #include "RioInterface.h"
00041
00042 #include <stdio.h>
00043
00044 #include "RioError.h"
00045 #include "RioTCP.h"
00046 #include "SessionManager.h"
00047
00048 #include "RioTCPTypes.h"
00049
00050
00051 #include "CommonLibraries.h"
00052
00053 #ifdef RIO_DEBUG2
00054 #include <errno.h>
00055 #endif
00056
00057
00058
00059 CRioSession::CRioSession()
00060 {
00061 #ifdef RIO_DEBUG1
00062 RioErr << "### [CRioSession - Constructor] Start" << endl;
00063 #endif
00064
00065 m_SessionId.Version = -1;
00066 m_SessionId.Index = -1;
00067 m_NetMgr = NULL;
00068 m_SessionManager = NULL;
00069 m_TCPconnection = NULL;
00070 m_BlockSize = 0;
00071 m_MaxPktLen = 0;
00072
00073 #ifdef RIO_DEBUG1
00074 RioErr << "### [CRioSession - Constructor] Finish" << endl;
00075 #endif
00076
00077
00078
00079 m_ServerAddress = NULL;
00080 m_ServerAddressSize = 0;
00081
00082
00083 m_NetInterface = NULL;
00084 }
00085
00086 CRioSession::~CRioSession()
00087 {
00088
00089 RioResult RioStatus;
00090
00091 #ifdef RIO_DEBUG1
00092 RioErr << "### [CRioSession - Destructor] Start" << endl;
00093 #endif
00094
00095 if( isConnected() )
00096 {
00097 Disconnect();
00098 }
00099
00100 if( m_NetMgr )
00101 {
00102 m_NetMgr->Stop();
00103 delete m_NetMgr;
00104 m_NetMgr = 0;
00105 }
00106
00107
00108 if( m_NetInterface != NULL )
00109 {
00110 RioStatus = m_NetInterface->Stop();
00111 #ifdef RIO_DEBUG2
00112 if( FAILED( RioStatus ) )
00113 {
00114 RioErr << "CRioSession::~CRioSession erro 0x" << hex << RioStatus
00115 << dec << " (" << GetErrorDescription( RioStatus )
00116 << ") ao finalizar o objeto da classe CNetInterface."
00117 << endl;
00118 }
00119 #endif
00120
00121 delete m_NetInterface;
00122
00123 m_NetInterface = NULL;
00124 }
00125
00126
00127 #ifdef RIO_DEBUG1
00128 RioErr << "### [CRioSession - Destructor] Finish" << endl;
00129 #endif
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 #ifdef RIO_DEBUG_FILE
00142 RioResult CRioSession::Connect( const char *ServerMachine,
00143 const char *UserName,
00144 const char *Password,
00145 int RioServer,
00146 const char *RioNetiLogPath )
00147 #else
00148 RioResult CRioSession::Connect( const char *ServerMachine,
00149 const char *UserName,
00150 const char *Password,
00151 int RioServer )
00152 #endif
00153 {
00154 #ifdef RIO_DEBUG1
00155 RioErr << "### [CRioSession - Connect] Start" << endl;
00156 #endif
00157
00158 int rc;
00159 unsigned short TCPport;
00160
00161
00162 if( isConnected() )
00163 {
00164 #ifdef RIO_DEBUG1
00165 RioErr << "### [CRioSession - Connect] Finish1" << endl;
00166 #endif
00167
00168 return ERROR_RIOSESSION + ERROR_SESSION_ALREADY_OPENED;
00169 }
00170 else
00171 {
00172 this->ServerName = strdup( ServerMachine );
00173
00174
00175
00176 if( RioServer == 1 )
00177 TCPport = RioTCPport;
00178 else
00179 TCPport = RioTCPportPL;
00180
00181
00182 m_TCPconnection = new CRioTCP;
00183
00184 RioResult hResult= m_TCPconnection->Connect( ServerMachine, TCPport );
00185
00186 if( FAILED( hResult ) )
00187 {
00188 #ifdef RIO_DEBUG1
00189 RioErr << "### [CRioSession - Connect] Finish2" << endl;
00190 #endif
00191
00192 return( hResult );
00193 }
00194
00195
00196 m_SessionManager = new CSessionManager( m_TCPconnection );
00197
00198 hResult = m_SessionManager->Open( UserName,
00199 Password,
00200 &m_SessionId );
00201
00202 if( FAILED( hResult ) )
00203 {
00204 delete m_SessionManager;
00205 m_SessionManager = NULL;
00206
00207
00208 m_TCPconnection->Disconnect();
00209 delete m_TCPconnection;
00210 m_TCPconnection = NULL;
00211
00212 #ifdef RIO_DEBUG1
00213 RioErr << "### [CRioSession - Connect] Finish3" << endl;
00214 #endif
00215
00216 return hResult;
00217 }
00218
00219 else
00220 {
00221 m_BlockSize = hResult;
00222 }
00223
00224 m_MaxPktLen = FRAGMENTSIZE;
00225
00226
00227 m_NetMgr = new NetMgr();
00228
00229
00230
00231
00232 #ifdef RIO_DEBUG_FILE
00233
00234
00235
00236
00237
00238 rc = m_NetMgr->Start( 0, m_BlockSize, m_MaxPktLen, RioNetiLogPath );
00239 #else
00240 rc = m_NetMgr->Start( 0, m_BlockSize, m_MaxPktLen );
00241 #endif
00242 if( rc != 0 )
00243 {
00244 #ifdef RIO_DEBUG1
00245 RioErr << "### [CRioSession - Connect] Finish4" << endl;
00246 #endif
00247
00248 return ERROR_RIOSESSION + ERROR_SOCKET_BIND;
00249 }
00250
00251
00252 if( RioServer )
00253 {
00254
00255
00256
00257 hResult = m_SessionManager->GetServerAddress( &m_ServerAddress,
00258 &m_ServerAddressSize );
00259
00260 if( FAILED( hResult ) )
00261 {
00262 if( (unsigned int) hResult == ERROR_CONNECTIONMANAGER +
00263 ERROR_INVALID_METHOD )
00264 {
00265
00266
00267
00268 #ifdef RIO_DEBUG1
00269 RioErr << "### [CRioSession - Connect] Finish5" << endl;
00270 #endif
00271 return( ERROR_RIOSESSION + ERROR_NO_MAPPING );
00272 }
00273 else
00274 {
00275 delete m_SessionManager;
00276 m_SessionManager =0;
00277 #ifdef RIO_DEBUG1
00278 RioErr << "### [CRioSession - Connect] Finish6" << endl;
00279 #endif
00280 }
00281 return hResult;
00282 }
00283
00284
00285 try
00286 {
00287
00288
00289 m_NetInterface = new CNetInterface;
00290
00291
00292
00293
00294
00295
00296
00297
00298 #ifdef RIO_DEBUG_FILE
00299 rc = m_NetInterface->Start( m_ServerAddress,
00300 m_ServerAddressSize, 0,
00301 NETTCPTIMEOUTSECONDS,
00302 NETTCPTIMEOUTSECONDS,
00303 RioNetiLogPath );
00304 #else
00305 rc = m_NetInterface->Start( m_ServerAddress,
00306 m_ServerAddressSize, 0,
00307 NETTCPTIMEOUTSECONDS,
00308 NETTCPTIMEOUTSECONDS );
00309 #endif
00310 if( rc != 0 )
00311 {
00312 #ifdef RIO_DEBUG2
00313 RioErr << "CRioSession::Connect Erro 0x" << hex << rc
00314 << dec << " (" << GetErrorDescription( rc )
00315 << ") ao executar a funcao Start do objeto da "
00316 << "classe CNetInterface. Usando a implementacao "
00317 << "antiga!" << endl;
00318 #endif
00319
00320
00321
00322
00323 delete m_NetInterface;
00324
00325 m_NetInterface = NULL;
00326
00327 }
00328 }
00329 catch( bad_alloc &ba )
00330 {
00331 #ifdef RIO_DEBUG2
00332 RioErr << "CRioSession::Connect erro de alocacao de "
00333 << "memoria (bad_alloc, error=" << ba.what()
00334 << ") ao criar um objeto da classe CNetInterface. "
00335 << "Usando a implementacao antiga!" << endl;
00336 #endif
00337
00338 m_NetInterface = NULL;
00339 }
00340
00341
00342
00343 #ifdef RIO_DEBUG1
00344 RioErr << "CRioSession::Connect executando a funcao RequestMapping "
00345 << " com os parametros IP = "
00346 << inet_ntoa( m_ServerAddress[ 0 ].sin_addr )
00347 << " e porta = " << ntohs( m_ServerAddress[ 0 ].sin_port )
00348 << endl;
00349 #endif
00350 m_NetMgr->RequestMultipleMappings( m_ServerAddress,
00351 m_ServerAddressSize );
00352 #ifdef RIO_DEBUG2
00353
00354 int IP, port;
00355 struct in_addr mapip;
00356 m_NetMgr->getmyaddrport( &IP, &port );
00357 mapip.s_addr = IP;
00358 RioErr << "CRioSession::Connect mapeamento IP = "
00359 << inet_ntoa( mapip ) << ", porta = " << ntohs( port )
00360 << endl;
00361 #endif
00362
00363
00364
00365
00366 if( m_NetMgr->IsBehindNAT() )
00367 {
00368 #ifdef RIO_DEBUG1
00369 RioErr << "CRioSession::Connect o cliente esta atras de um NAT"
00370 << endl;
00371 #endif
00372
00373
00374
00375
00376
00377 rc = m_NetMgr->CreateKeepaliveThread( );
00378 if( rc )
00379 {
00380 #ifdef RIO_DEBUG1
00381 RioErr << "### [CRioSession - Connect] Finish7" << endl;
00382 #endif
00383 return ERROR_RIOSESSION + ERROR_UNEXPECTED;
00384 }
00385
00386 }
00387
00388
00389
00390
00391
00392 int *IPs, *IPsDest;
00393 int *ports, *PortsDest;
00394 unsigned int TotalPairs;
00395 HRESULT status;
00396
00397 m_NetMgr->getalladdrport( &IPs, &ports );
00398
00399
00400
00401
00402
00403
00404
00405
00406 IPsDest = NULL;
00407 PortsDest = NULL;
00408 TotalPairs = m_ServerAddressSize;
00409
00410
00411
00412 if( m_NetInterface != NULL )
00413 {
00414 try
00415 {
00416
00417 IPsDest = new int[ 2 * m_ServerAddressSize ];
00418 PortsDest = new int[ 2 * m_ServerAddressSize ];
00419
00420
00421 for( unsigned int i = 0; i < m_ServerAddressSize; i++ )
00422 {
00423 IPsDest[ i ] = IPs[ i ];
00424 PortsDest[ i ] = ports[ i ];
00425 }
00426
00427
00428
00429
00430 m_NetInterface->getalladdrport( &IPs, &ports );
00431
00432
00433
00434 for( unsigned int i = m_ServerAddressSize;
00435 i < 2 * m_ServerAddressSize; i++ )
00436 {
00437 IPsDest[ i ] = IPs[ i - m_ServerAddressSize ];
00438 PortsDest[ i ] = ports[ i - m_ServerAddressSize ];
00439 }
00440
00441 TotalPairs = 2 * m_ServerAddressSize;
00442 }
00443 catch( bad_alloc &ba )
00444 {
00445 #ifdef RIO_DEBUG2
00446 RioErr << "CRioSession::Connect erro de alocacao de "
00447 << "memoria (bad_alloc, error=" << ba.what()
00448 << ") ao criar os vetores com os ips e as portas. "
00449 << "Usando a implementacao antiga!" << endl;
00450 #endif
00451
00452
00453
00454 if( IPsDest != NULL )
00455 delete[] IPsDest;
00456
00457 IPsDest = NULL;
00458
00459
00460
00461
00462 delete m_NetInterface;
00463 m_NetInterface = NULL;
00464 }
00465 }
00466
00467
00468 if( ( IPsDest != NULL ) && ( PortsDest != NULL ) )
00469 {
00470 #ifdef RIO_DEBUG2
00471 in_addr ipdest;
00472
00473 RioErr << "CRioSession::Connect enviando os seguintes pares "
00474 << "IP, porta ao servidor (total de " << TotalPairs
00475 << " pares: ";
00476 for( unsigned int i = 0; i < TotalPairs; i++ )
00477 {
00478 ipdest.s_addr = IPsDest[ i ];
00479 RioErr << "(" << inet_ntoa( ipdest ) << ", "
00480 << ntohs( PortsDest[ i ] ) << ")";
00481 if( i != ( TotalPairs - 1 ) )
00482 RioErr << ", ";
00483 else
00484 RioErr << "." << endl;
00485 }
00486 #endif
00487
00488 status = m_SessionManager->SendNATMappings( IPsDest, PortsDest,
00489 TotalPairs );
00490
00491
00492 delete[] IPsDest;
00493 delete[] PortsDest;
00494 }
00495 else
00496 {
00497 #ifdef RIO_DEBUG2
00498 in_addr ipdest;
00499
00500 RioErr << "CRioSession::Connect enviando os seguintes pares "
00501 << "IP, porta ao servidor (total de "
00502 << m_ServerAddressSize << " pares: ";
00503 for( unsigned int i = 0; i < m_ServerAddressSize; i++ )
00504 {
00505 ipdest.s_addr = IPs[ i ];
00506 RioErr << "(" << inet_ntoa( ipdest ) << ", "
00507 << ntohs( ports[ i ] ) << ")";
00508 if( i != ( m_ServerAddressSize - 1 ) )
00509 RioErr << ", ";
00510 else
00511 RioErr << "." << endl;
00512 }
00513 #endif
00514
00515 status = m_SessionManager->SendNATMappings( IPs, ports,
00516 m_ServerAddressSize );
00517 }
00518
00519 if( ( (unsigned int) status != ERROR_CONNECTIONMANAGER +
00520 ERROR_INVALID_METHOD ) &&
00521 ( status != S_OK ) )
00522 {
00523 #ifdef RIO_DEBUG1
00524 RioErr << "### [CRioSession - Connect] Finish8" << endl;
00525 #endif
00526 return status;
00527 }
00528 }
00529 else
00530 m_NetInterface = NULL;
00531
00532 #ifdef RIO_DEBUG1
00533 RioErr << "### [CRioSession - Connect] Finish10" << endl;
00534 #endif
00535
00536 return S_OK;
00537 }
00538 }
00539
00540
00541 RioResult CRioSession::Disconnect()
00542 {
00543 #ifdef RIO_DEBUG1
00544 RioErr << "### [CRioSession - Disconnect] Start" << endl;
00545 #endif
00546
00547
00548 if( !isConnected() )
00549 {
00550 #ifdef RIO_DEBUG1
00551 RioErr << "### [CRioSession - Disconnect] Finish1" << endl;
00552 #endif
00553
00554 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
00555 }
00556
00557 HRESULT hResult;
00558 HRESULT lastresult = S_OK;
00559
00560
00561 hResult = m_SessionManager->Close( m_SessionId );
00562 if( FAILED( hResult ) )
00563 {
00564 lastresult = hResult;
00565 }
00566
00567
00568 delete m_SessionManager;
00569 m_SessionManager = 0;
00570
00571
00572 m_TCPconnection->Disconnect();
00573
00574 delete m_TCPconnection;
00575 m_TCPconnection = 0;
00576
00577 #ifdef RIO_DEBUG1
00578 RioErr << "### [CRioSession - Disconnect] Finish2" << endl;
00579 #endif
00580
00581
00582
00583 if( m_ServerAddress != NULL )
00584 delete[] m_ServerAddress;
00585
00586 m_ServerAddress = NULL;
00587 m_ServerAddressSize = 0;
00588
00589 return lastresult;
00590 }
00591
00592
00593 RioResult CRioSession::CreateObject( const char* ObjectName,
00594 const short ObjectType,
00595 const RioSecurity* Security )
00596 {
00597 #ifdef RIO_DEBUG1
00598 RioErr << "### [CRioSession - CreateObject] Start" << endl;
00599 #endif
00600
00601
00602 if( !isConnected() )
00603 {
00604 #ifdef RIO_DEBUG1
00605 RioErr << "### [CRioSession - CreateObject] Finish1" << endl;
00606 #endif
00607
00608 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
00609 }
00610
00611
00612 HRESULT hResult;
00613
00614 hResult = m_SessionManager->CreateObject( m_SessionId,
00615 ObjectName,
00616 ObjectType );
00617
00618 if( FAILED( hResult ) )
00619 {
00620 #ifdef RIO_DEBUG1
00621 RioErr << "### [CRioSession - CreateObject] Finish2" << endl;
00622 #endif
00623
00624 return hResult;
00625 }
00626
00627 #ifdef RIO_DEBUG1
00628 RioErr << "### [CRioSession - CreateObject] Finish3" << endl;
00629 #endif
00630
00631 return S_OK;
00632 }
00633
00634 RioResult CRioSession::DeleteObject( const char* ObjectName )
00635 {
00636 #ifdef RIO_DEBUG1
00637 RioErr << "### [CRioSession - DeleteObject] Start" << endl;
00638 #endif
00639
00640
00641 if( !isConnected() )
00642 {
00643 #ifdef RIO_DEBUG1
00644 RioErr << "### [CRioSession - DeleteObject] Finish1" << endl;
00645 #endif
00646
00647 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
00648 }
00649
00650
00651 HRESULT hResult;
00652 hResult = m_SessionManager->DeleteObject( m_SessionId,
00653 ObjectName );
00654 if( FAILED( hResult ) )
00655 {
00656 #ifdef RIO_DEBUG1
00657 RioErr << "### [CRioSession - DeleteObject] Finish2" << endl;
00658 #endif
00659
00660 return hResult;
00661 }
00662
00663 #ifdef RIO_DEBUG1
00664 RioErr << "### [CRioSession - DeleteObject] Finish3" << endl;
00665 #endif
00666
00667 return S_OK;
00668 }
00669
00670 RioResult CRioSession::RenameObject( const char* CurrentName,
00671 const char* NewName )
00672 {
00673 #ifdef RIO_DEBUG1
00674 RioErr << "### [CRioSession - RenameObject] Start" << endl;
00675 #endif
00676
00677
00678 if( !isConnected() )
00679 {
00680 #ifdef RIO_DEBUG1
00681 RioErr << "### [CRioSession - RenameObject] Finish1" << endl;
00682 #endif
00683
00684 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
00685 }
00686
00687
00688 HRESULT hResult;
00689 hResult = m_SessionManager->RenameObject( m_SessionId,
00690 CurrentName,
00691 NewName );
00692 if( FAILED( hResult ) )
00693 {
00694 #ifdef RIO_DEBUG1
00695 RioErr << "### [CRioSession - RenameObject] Finish2" << endl;
00696 #endif
00697
00698 return hResult;
00699 }
00700
00701 #ifdef RIO_DEBUG1
00702 RioErr << "### [CRioSession - RenameObject] Finish3" << endl;
00703 #endif
00704
00705 return S_OK;
00706 }
00707
00708 RioResult CRioSession::GetBlockSize( unsigned int* BlockSize )
00709 {
00710 #ifdef RIO_DEBUG1
00711 RioErr << "### [CRioSession - GetBlockSize] Start" << endl;
00712 #endif
00713
00714
00715
00716
00717 if( m_BlockSize != 0 )
00718 {
00719 *BlockSize = m_BlockSize;
00720
00721 #ifdef RIO_DEBUG1
00722 RioErr << "### [CRioSession - GetBlockSize] Finish1" << endl;
00723 #endif
00724
00725 return S_OK;
00726 }
00727
00728
00729 HRESULT hResult;
00730
00731 hResult = m_SessionManager->GetBlockSize( m_SessionId,BlockSize );
00732
00733 if( FAILED( hResult ) )
00734 {
00735 #ifdef RIO_DEBUG1
00736 RioErr << "### [CRioSession - GetBlockSize] Finish2" << endl;
00737 #endif
00738
00739 return hResult;
00740 }
00741
00742 #ifdef RIO_DEBUG1
00743 RioErr << "### [CRioSession - GetBlockSize] Finish3" << endl;
00744 #endif
00745
00746 return S_OK;
00747 }
00748
00749 char *CRioSession::GetServerName()
00750 {
00751 #ifdef RIO_DEBUG1
00752 RioErr << "### [CRioSession - GetServerName] Single" << endl;
00753 #endif
00754
00755 return ServerName;
00756 }
00757
00758
00759 #ifdef WINDOWS
00760
00761 RioResult CRioSession::GetRTT( struct _SYSTEMTIME * RTT_openConnection,
00762 struct _SYSTEMTIME * RTT_openStream )
00763 {
00764 #ifdef RIO_DEBUG1
00765 RioErr << "### [CRioSession - GetRTT1] Start" << endl;
00766 #endif
00767
00768 HRESULT hResult;
00769 hResult = m_SessionManager->GetRTT( RTT_openConnection, RTT_openStream );
00770 if( FAILED( hResult ) )
00771 {
00772 #ifdef RIO_DEBUG1
00773 RioErr << "### [CRioSession - GetRTT1] Finish1" << endl;
00774 #endif
00775
00776 return hResult;
00777 }
00778
00779 #ifdef RIO_DEBUG1
00780 RioErr << "### [CRioSession - GetRTT1] Finish2" << endl;
00781 #endif
00782
00783 return S_OK;
00784 }
00785 #else
00786
00787 RioResult CRioSession::GetRTT( struct timeval* RTT_openConnection,
00788 struct timeval* RTT_openStream )
00789 {
00790 #ifdef RIO_DEBUG1
00791 RioErr << "### [CRioSession - GetRTT2] Start" << endl;
00792 #endif
00793
00794 HRESULT hResult;
00795 hResult = m_SessionManager->GetRTT( RTT_openConnection, RTT_openStream );
00796 if( FAILED( hResult ) )
00797 {
00798 #ifdef RIO_DEBUG1
00799 RioErr << "### [CRioSession - GetRTT2] Finish1" << endl;
00800 #endif
00801
00802 return hResult;
00803 }
00804
00805 #ifdef RIO_DEBUG1
00806 RioErr << "### [CRioSession - GetRTT2] Finish2" << endl;
00807 #endif
00808
00809 return S_OK;
00810 }
00811 #endif
00812
00813
00814
00815
00816 #ifdef WINDOWS
00817
00818 RioResult CRioSession::GetAverageRTT( struct _SYSTEMTIME * RTT_average )
00819 {
00820 #ifdef RIO_DEBUG1
00821 RioErr << "### [CRioSession - GetAverageRTT1] Start" << endl;
00822 #endif
00823
00824 HRESULT hResult;
00825 hResult = m_SessionManager->GetAverageRTT( RTT_average );
00826 if( FAILED( hResult ) )
00827 {
00828 #ifdef RIO_DEBUG1
00829 RioErr << "### [CRioSession - GetAverageRTT1] Finish1" << endl;
00830 #endif
00831
00832 return hResult;
00833 }
00834
00835 #ifdef RIO_DEBUG1
00836 RioErr << "### [CRioSession - GetAverageRTT1] Finish2" << endl;
00837 #endif
00838
00839 return S_OK;
00840 }
00841 #else
00842
00843 RioResult CRioSession::GetAverageRTT( struct timeval* RTT_average )
00844 {
00845 #ifdef RIO_DEBUG1
00846 RioErr << "### [CRioSession - GetAverageRTT2] Start" << endl;
00847 #endif
00848
00849 HRESULT hResult;
00850 hResult = m_SessionManager->GetAverageRTT( RTT_average );
00851 if( FAILED( hResult ) )
00852 {
00853 #ifdef RIO_DEBUG1
00854 RioErr << "### [CRioSession - GetAverageRTT2] Finish1" << endl;
00855 #endif
00856
00857 return hResult;
00858 }
00859
00860 #ifdef RIO_DEBUG1
00861 RioErr << "### [CRioSession - GetAverageRTT2] Finish2" << endl;
00862 #endif
00863
00864 return S_OK;
00865 }
00866 #endif
00867
00868
00869 RioResult CRioSession::GetId( RioSessionId* Id )
00870 {
00871 #ifdef RIO_DEBUG1
00872 RioErr << "### [CRioSession - GetId] Start" << endl;
00873 #endif
00874
00875
00876 if( !isConnected() )
00877 {
00878 Id = NULL;
00879
00880 #ifdef RIO_DEBUG1
00881 RioErr << "### [CRioSession - GetId] Finish1" << endl;
00882 #endif
00883
00884 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
00885 }
00886
00887 *Id = * ( ( RioSessionId* ) &m_SessionId );
00888
00889 #ifdef RIO_DEBUG1
00890 RioErr << "### [CRioSession - GetId] Finish2" << endl;
00891 #endif
00892
00893 return S_OK;
00894 }
00895
00896 RioResult CRioSession::ChangeDir ( char* NewDir )
00897 {
00898 #ifdef RIO_DEBUG1
00899 RioErr << "### [CRioSession - ChangeDir] Start" << endl;
00900 #endif
00901
00902
00903 if( !isConnected() )
00904 {
00905 #ifdef RIO_DEBUG1
00906 RioErr << "### [CRioSession - ChangeDir] Finish1" << endl;
00907 #endif
00908
00909 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
00910 }
00911
00912
00913 HRESULT hResult;
00914 hResult = m_SessionManager->ChangeDir( m_SessionId,
00915 ( signed char* ) NewDir );
00916 if( FAILED( hResult ) )
00917 {
00918 #ifdef RIO_DEBUG1
00919 RioErr << "### [CRioSession - ChangeDir] Finish2" << endl;
00920 #endif
00921
00922 return hResult;
00923 }
00924
00925 #ifdef RIO_DEBUG1
00926 RioErr << "### [CRioSession - ChangeDir] Finish3" << endl;
00927 #endif
00928
00929 return S_OK;
00930 }
00931
00932
00933 RioResult CRioSession::GetCurrentDir ( char* Dir, const int SizeDir )
00934 {
00935 #ifdef RIO_DEBUG1
00936 RioErr << "### [CRioSession - GetCurrentDir] Start" << endl;
00937 #endif
00938
00939
00940 if( !isConnected() )
00941 {
00942 #ifdef RIO_DEBUG1
00943 RioErr << "### [CRioSession - GetCurrentDir] Finish1" << endl;
00944 #endif
00945
00946 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
00947 }
00948
00949
00950 HRESULT hResult;
00951
00952 hResult = m_SessionManager->GetCurrentDir( m_SessionId,
00953 SizeDir,
00954 ( signed char* ) Dir );
00955 if( FAILED( hResult ) )
00956 {
00957 #ifdef RIO_DEBUG1
00958 RioErr << "### [CRioSession - GetCurrentDir] Finish2" << endl;
00959 #endif
00960
00961 return hResult;
00962 }
00963
00964 #ifdef RIO_DEBUG1
00965 RioErr << "### [CRioSession - GetCurrentDir] Finish3" << endl;
00966 #endif
00967
00968 return S_OK;
00969
00970 }
00971
00972
00973 RioResult CRioSession::GetObjectInfo ( const char* ObjectName,
00974 ObjectInfo* ObjectInfo )
00975 {
00976 #ifdef RIO_DEBUG1
00977 RioErr << "### [CRioSession - GetObjectInfo] Start" << endl;
00978 #endif
00979
00980
00981 if( !isConnected() )
00982 {
00983 #ifdef RIO_DEBUG1
00984 RioErr << "### [CRioSession - GetObjectInfo] Finish1" << endl;
00985 #endif
00986
00987 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
00988 }
00989
00990
00991 HRESULT hResult;
00992 hResult = m_SessionManager->GetObjectInfo( m_SessionId, ObjectName,
00993 ObjectInfo );
00994
00995 if( FAILED( hResult ) )
00996 {
00997 #ifdef RIO_DEBUG1
00998 RioErr << "### [CRioSession - GetObjectInfo] Finish2" << endl;
00999 #endif
01000
01001 return hResult;
01002 }
01003
01004 #ifdef RIO_DEBUG1
01005 RioErr << "### [CRioSession - GetObjectInfo] Finish3" << endl;
01006 #endif
01007
01008 return S_OK;
01009 }
01010
01011
01012
01013 RioResult CRioSession::GetNumberOfDisks( unsigned int *NumberOfDisks )
01014 {
01015 #ifdef RIO_DEBUG1
01016 RioErr << "### [CRioSession - GetNumberOfDisks] Start" << endl;
01017 #endif
01018
01019
01020 if( !isConnected() )
01021 {
01022 #ifdef RIO_DEBUG1
01023 RioErr << "### [CRioSession - GetNumberOfDisks] Finish1" << endl;
01024 #endif
01025
01026 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
01027 }
01028
01029
01030 HRESULT hResult;
01031 hResult = m_SessionManager->GetNumberOfDisks( m_SessionId, NumberOfDisks );
01032
01033 if( FAILED( hResult ) )
01034 {
01035 #ifdef RIO_DEBUG1
01036 RioErr << "### [CRioSession - GetNumberOfDisks] Finish2" << endl;
01037 #endif
01038
01039 return hResult;
01040 }
01041
01042 #ifdef RIO_DEBUG1
01043 RioErr << "### [CRioSession - GetNumberOfDisks] Finish3" << endl;
01044 #endif
01045
01046 return S_OK;
01047 }
01048
01049
01050
01051 RioResult CRioSession::GetNumberOfStorageNodes(
01052 unsigned int *NumberOfStorageNodes )
01053 {
01054 #ifdef RIO_DEBUG1
01055 RioErr << "### [CRioSession - GetNumberOfStorageNodes] Start" << endl;
01056 #endif
01057
01058
01059 if( !isConnected() )
01060 {
01061 #ifdef RIO_DEBUG1
01062 RioErr << "### [CRioSession - GetNumberOfStorageNodes] Finish1" << endl;
01063 #endif
01064
01065 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
01066 }
01067
01068
01069 HRESULT hResult;
01070 hResult = m_SessionManager->GetNumberOfStorageNodes( m_SessionId,
01071 NumberOfStorageNodes );
01072 if( FAILED( hResult ) )
01073 {
01074 #ifdef RIO_DEBUG1
01075 RioErr << "### [CRioSession - GetNumberOfStorageNodes] Finish2" << endl;
01076 #endif
01077
01078 return hResult;
01079 }
01080
01081 #ifdef RIO_DEBUG1
01082 RioErr << "### [CRioSession - GetNumberOfStorageNodes] Finish3" << endl;
01083 #endif
01084
01085 return S_OK;
01086 }
01087
01088
01089 RioResult CRioSession::GetStorageNodeInfo( RioStorageNodeInfo* StorageNodeInfo,
01090 unsigned int StorageNodeIndex )
01091 {
01092 #ifdef RIO_DEBUG1
01093 RioErr << "### [CRioSession - GetStorageNodeInfo] Start" << endl;
01094 #endif
01095
01096
01097 if( !isConnected() )
01098 {
01099 #ifdef RIO_DEBUG1
01100 RioErr << "### [CRioSession - GetStorageNodeInfo] Finish1" << endl;
01101 #endif
01102
01103 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
01104 }
01105
01106
01107 HRESULT hResult;
01108 hResult = m_SessionManager->GetStorageNodeInfo( m_SessionId,
01109 StorageNodeInfo,
01110 StorageNodeIndex );
01111 if( FAILED( hResult ) )
01112 {
01113 #ifdef RIO_DEBUG1
01114 RioErr << "### [CRioSession - GetStorageNodeInfo] Finish2" << endl;
01115 #endif
01116
01117 return hResult;
01118 }
01119
01120 #ifdef RIO_DEBUG1
01121 RioErr << "### [CRioSession - GetStorageNodeInfo] Finish3" << endl;
01122 #endif
01123
01124 return S_OK;
01125 }
01126
01127
01128
01129 RioResult CRioSession::GetMonitorTable( char *UserName,
01130 vector<ClientData> *client_data,
01131 vector<StorageData> *storage_data )
01132 {
01133 #ifdef RIO_DEBUG1
01134 RioErr << "### [CRioSession - GetMonitorTable] Start" << endl;
01135 #endif
01136
01137
01138 if( !isConnected() )
01139 {
01140 #ifdef RIO_DEBUG1
01141 RioErr << "### [CRioSession - GetMonitorTable] Finish1" << endl;
01142 #endif
01143
01144 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
01145 }
01146
01147
01148 HRESULT hResult;
01149 hResult = m_SessionManager->GetMonitorTable( UserName,
01150 client_data,
01151 storage_data );
01152 if( FAILED( hResult ) )
01153 {
01154 #ifdef RIO_DEBUG1
01155 RioErr << "### [CRioSession - GetMonitorTable] Finish2" << endl;
01156 #endif
01157
01158 return hResult;
01159 }
01160
01161 #ifdef RIO_DEBUG1
01162 RioErr << "### [CRioSession - GetMonitorTable] Finish3" << endl;
01163 #endif
01164
01165 return S_OK;
01166 }
01167
01168
01169 RioResult CRioSession::GetNumberOfActiveAndMaxSessions(
01170 unsigned int *NumberOfActiveSessions,
01171 unsigned int *NumberOfMaxSessions )
01172 {
01173 #ifdef RIO_DEBUG1
01174 RioErr << "### [CRioSession - GetNumberOfActiveAndMaxSessions] Start" << endl;
01175 #endif
01176
01177
01178 if( !isConnected() )
01179 {
01180 #ifdef RIO_DEBUG1
01181 RioErr << "### [CRioSession - GetNumberOfActiveAndMaxSessions] Finish1" << endl;
01182 #endif
01183
01184 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
01185 }
01186
01187
01188 HRESULT hResult;
01189 hResult = m_SessionManager->GetNumberOfActiveAndMaxSessions( m_SessionId,
01190 NumberOfActiveSessions, NumberOfMaxSessions );
01191 if( FAILED( hResult ) )
01192 {
01193 #ifdef RIO_DEBUG1
01194 RioErr << "### [CRioSession - GetNumberOfActiveAndMaxSessions] Finish2" << endl;
01195 #endif
01196
01197 return hResult;
01198 }
01199
01200 #ifdef RIO_DEBUG1
01201 RioErr << "### [CRioSession - GetNumberOfActiveAndMaxSessions] Finish3" << endl;
01202 #endif
01203
01204 return S_OK;
01205 }
01206
01207
01208 RioResult CRioSession::SaveMeasures()
01209 {
01210 #ifdef RIO_DEBUG1
01211 RioErr << "### [CRioSession - SaveMeasures] Start" << endl;
01212 #endif
01213
01214
01215 if( !isConnected() )
01216 {
01217 #ifdef RIO_DEBUG1
01218 RioErr << "### [CRioSession - SaveMeasures] Finish1" << endl;
01219 #endif
01220
01221
01222 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
01223 }
01224
01225
01226 HRESULT hResult;
01227 hResult = m_SessionManager->SaveMeasures( m_SessionId );
01228 if( FAILED( hResult ) )
01229 {
01230 #ifdef RIO_DEBUG1
01231 RioErr << "### [CRioSession - SaveMeasures] Finish2" << endl;
01232 #endif
01233
01234 return hResult;
01235 }
01236
01237 #ifdef RIO_DEBUG1
01238 RioErr << "### [CRioSession - SaveMeasures] Finish3" << endl;
01239 #endif
01240
01241 return S_OK;
01242 }
01243
01244 int CRioSession::getipaddr()
01245 {
01246 #ifdef RIO_DEBUG1
01247 RioErr << "### [CRioSession - getipaddr] Single" << endl;
01248 #endif
01249
01250
01251
01252
01253
01254 return m_NetMgr->getipaddr();
01255 }
01256
01257
01258 bool CRioSession::isConnected( void )
01259 {
01260 #ifdef RIO_DEBUG1
01261 RioErr << "### [CRioSession - isConnected] Start" << endl;
01262 #endif
01263
01264 bool isConnected = true;
01265
01266 if( m_TCPconnection == NULL )
01267 isConnected = false;
01268 else
01269 {
01270 isConnected = m_TCPconnection->isConnected();
01271 }
01272
01273 #ifdef RIO_DEBUG1
01274 RioErr << "### [CRioSession - isConnected] Finish1" << endl;
01275 #endif
01276
01277 return isConnected;
01278 }
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292 RioResult CRioSession::CheckStreamControl( bool *IsImplemented )
01293 {
01294 #ifdef RIO_DEBUG1
01295 RioErr << "### [CRioSession - CheckStreamControl] Start" << endl;
01296 #endif
01297
01298
01299 if( !isConnected() )
01300 {
01301 #ifdef RIO_DEBUG1
01302 RioErr << "### [CRioSession - CheckStreamControl] Finish1" << endl;
01303 #endif
01304
01305
01306 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
01307 }
01308
01309
01310 HRESULT hResult;
01311 hResult = m_SessionManager->CheckStreamControl( IsImplemented );
01312 if( FAILED( hResult ) )
01313 {
01314 #ifdef RIO_DEBUG1
01315 RioErr << "### [CRioSession - CheckStreamControl] Finish2" << endl;
01316 #endif
01317
01318 return hResult;
01319 }
01320
01321 #ifdef RIO_DEBUG1
01322 RioErr << "### [CRioSession - CheckStreamControl] Finish3" << endl;
01323 #endif
01324
01325 return S_OK;
01326 }
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336 RioResult CRioSession::SearchLogs( unsigned int SearchType, time_t StartTime,
01337 time_t EndTime, int ServerIP, int ServerPort,
01338 char *ResultFileName, callback_t callback,
01339 void *callbackparam )
01340 {
01341
01342
01343 int ReqId;
01344
01345 int ClientIP, ClientPort;
01346
01347 SearchLogsData *SearchData;
01348
01349 int PosServer;
01350
01351 #ifdef RIO_DEBUG1
01352 RioErr << "### [CRioSession - SearchLogs] Start" << endl;
01353 #endif
01354
01355
01356 if( !isConnected() )
01357 {
01358 #ifdef RIO_DEBUG1
01359 RioErr << "### [CRioSession - SearchLogs] Finish1" << endl;
01360 #endif
01361
01362
01363 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
01364 }
01365
01366
01367
01368 SearchData = new SearchLogsData;
01369
01370 if( SearchData == NULL )
01371 {
01372 #ifdef RIO_DEBUG1
01373 RioErr << "### [CRioSession - SearchLogs] Finish2" << endl;
01374 #endif
01375
01376 return ERROR_RIOSESSION + ERROR_MEMORY;
01377 }
01378
01379
01380
01381
01382 PosServer = -1;
01383 for( unsigned int Pos = 0; Pos < m_ServerAddressSize; Pos++ )
01384 if( ( m_ServerAddress[ Pos ].sin_addr.s_addr ==
01385 ( unsigned int ) ServerIP ) &&
01386 ( m_ServerAddress[ Pos ].sin_port == ( unsigned int ) ServerPort ) )
01387 {
01388 PosServer = Pos;
01389 break;
01390 }
01391
01392
01393
01394 if( PosServer == -1 )
01395 {
01396 #ifdef RIO_DEBUG1
01397 RioErr << "### [CRioSession - SearchLogs] Finish3" << endl;
01398 #endif
01399
01400 return ERROR_RIOSESSION + ERROR_INVALID_HOST;
01401 }
01402
01403
01404
01405
01406
01407
01408 if( m_NetInterface != NULL )
01409 m_NetInterface->getmyaddrport( &ClientIP, &ClientPort, PosServer );
01410 else
01411 m_NetMgr->getmyaddrport( &ClientIP, &ClientPort, PosServer );
01412
01413
01414
01415 if( ( ClientIP == -1 ) && ( ClientPort == 0 ) )
01416 {
01417 #ifdef RIO_DEBUG1
01418 RioErr << "### [CRioSession - SearchLogs] Finish4" << endl;
01419 #endif
01420
01421 return ERROR_RIOSESSION + ERROR_SERVICE_TEMPORARY_UNAVAILABLE;
01422 }
01423
01424
01425 SearchData->session = this;
01426 SearchData->ServerIP = ServerIP;
01427 SearchData->ServerPort = ServerPort;
01428 SearchData->ClientIP = ClientIP;
01429 SearchData->ClientPort = ClientPort;
01430 SearchData->ResultFileSize = 0;
01431 SearchData->ResultFileId = 0;
01432 SearchData->ResultFileName = new char[ strlen( ResultFileName ) + 1 ];
01433 if( SearchData->ResultFileName == NULL )
01434 {
01435 delete SearchData;
01436
01437 #ifdef RIO_DEBUG1
01438 RioErr << "### [CRioSession - SearchLogs] Finish5" << endl;
01439 #endif
01440
01441 return ERROR_RIOSESSION + ERROR_MEMORY;
01442 }
01443 strcpy( SearchData->ResultFileName, ResultFileName );
01444 SearchData->CurrentBlock = 0;
01445 SearchData->ResultFile = NULL;
01446 SearchData->callback = callback;
01447 SearchData->callbackparam = callbackparam;
01448
01449 SearchData->BlockBuffer = new char[ m_BlockSize ];
01450 if( SearchData->BlockBuffer == NULL )
01451 {
01452 delete[] SearchData->ResultFileName;
01453 delete SearchData;
01454
01455 #ifdef RIO_DEBUG1
01456 RioErr << "### [CRioSession - SearchLogs] Finish6" << endl;
01457 #endif
01458
01459 return ERROR_RIOSESSION + ERROR_MEMORY;
01460 }
01461
01462 #ifdef RIO_DEBUG2
01463 in_addr ip;
01464 ip.s_addr = SearchData->ClientIP;
01465 RioErr << "CRioSession::SearchLog iniciando a busca do tipo "
01466 << SearchType << " do tempo " << StartTime << " ate o tempo "
01467 << EndTime << " do cliente com o IP " << inet_ntoa( ip )
01468 << " e a porta " << ntohs( SearchData->ClientPort )
01469 << " para o servidor com o IP ";
01470 ip.s_addr = SearchData->ServerIP;
01471 RioErr << inet_ntoa( ip ) << " e a porta "
01472 << ntohs( SearchData->ServerPort ) << endl;
01473 #endif
01474
01475
01476
01477
01478 if( m_NetInterface != NULL )
01479 ReqId = m_NetInterface->ExpectCmd( &FinalizeSearchCmd,
01480 ( void * ) SearchData,
01481 &ProcessSearchCmd );
01482 else
01483 ReqId = m_NetMgr->ExpectCmd( &FinalizeSearchCmd, ( void * ) SearchData,
01484 &ProcessSearchCmd );
01485
01486
01487 HRESULT hResult;
01488 hResult = m_SessionManager->SearchLogsRequest( SearchType, StartTime,
01489 EndTime, ServerIP,
01490 ServerPort, ClientIP,
01491 ClientPort, ReqId );
01492 if( FAILED( hResult ) )
01493 {
01494 #ifdef RIO_DEBUG1
01495 RioErr << "### [CRioSession - SearchLogsRequest] Finish7" << endl;
01496 #endif
01497
01498 if( m_NetInterface != NULL )
01499 m_NetInterface->CancelExpect( ReqId, hResult );
01500 else
01501 m_NetMgr->CancelExpect( ReqId, hResult );
01502
01503 return hResult;
01504 }
01505
01506 #ifdef RIO_DEBUG1
01507 RioErr << "### [CRioSession - SearchLogsRequest] Finish8" << endl;
01508 #endif
01509
01510 return S_OK;
01511 }
01512
01513
01514
01515
01516 void CRioSession::GetServerAddress( struct sockaddr_in **ServerAddress,
01517 unsigned int *ServerAddressSize )
01518 {
01519 *ServerAddress = m_ServerAddress;
01520 *ServerAddressSize = m_ServerAddressSize;
01521 }
01522
01523
01524
01525
01526 void CRioSession::ProcessSearchCmd( void *parm, char *pktp, int pktl )
01527 {
01528 SearchLogsData *SearchData;
01529
01530 SearchData = ( SearchLogsData * ) parm;
01531
01532
01533
01534 if( pktl == sizeof( int ) + sizeof( unsigned long long int ) )
01535 {
01536 #ifdef RIO_DEBUG2
01537
01538 RioErr << "CRioSession::ProcessSearchCmd pktp = ";
01539 char Hex[ 3 ];
01540 for( unsigned int i = 0; i < sizeof( int ) +
01541 sizeof( unsigned long long int ); i++ )
01542 {
01543 sprintf( Hex, "%02X", ( unsigned char ) pktp[ i ] );
01544 RioErr << Hex << " ";
01545 }
01546 RioErr << ", sizeof( pktp ) = " << pktl << endl;
01547 #endif
01548
01549 SearchData->ResultFileSize = *( ( int * ) pktp );
01550
01551 SearchData->ResultFileId = *( ( unsigned long long int * )
01552 &pktp[ sizeof( int ) ] );
01553 #ifdef RIO_DEBUG2
01554 RioErr << "CRioSession::ProcessSearchCmd os dados da busca foram "
01555 << "corretamente recebidos. Tamanho do arquivo de busca e de "
01556 << "(0 indica que a busca nao teve sucesso) "
01557 << SearchData->ResultFileSize << ", e identificador do arquivo "
01558 << "de busca e igual a (0 indica que ocorreu um erro ao "
01559 << "executar a busca) " << SearchData->ResultFileId << endl;
01560 #endif
01561 }
01562 else
01563 {
01564
01565
01566
01567
01568 #ifdef RIO_DEBUG2
01569 RioErr << "CRioSession::ProcessSearchCmd os dados da busca foram "
01570 << "incorretamente recebidos. A busca sera ignorada!"
01571 << endl;
01572 #endif
01573 SearchData->ResultFileSize = 0;
01574 SearchData->ResultFileId = 0;
01575 }
01576 }
01577
01578
01579
01580
01581 void CRioSession::FinalizeSearchCmd( void *parm, int result )
01582 {
01583 int ReqId;
01584 HRESULT hResult;
01585 SearchLogsData *SearchData;
01586 CRioSession *session;
01587
01588 SearchData = ( SearchLogsData * ) parm;
01589 session = SearchData->session;
01590
01591
01592
01593
01594
01595 if( result != S_OK )
01596 {
01597 #ifdef RIO_DEBUG2
01598 RioErr << "CRioSession::FinalizeSearchCmd execucao da busca terminou "
01599 << "com erro: result = " << result << endl;
01600 #endif
01601
01602 SearchData->callback( SearchData->callbackparam, SEARCHLOGSERROR );
01603 delete[] SearchData->ResultFileName;
01604 delete[] SearchData->BlockBuffer;
01605 delete SearchData;
01606 }
01607 else if( SearchData->ResultFileSize == 0 )
01608 {
01609
01610
01611 #ifdef RIO_DEBUG2
01612 RioErr << "CRioSession::FinalizeSearchCmd execucao da busca terminou "
01613 << "com sucesso, mas nenhum resultado foi encontrado" << endl;
01614 #endif
01615 SearchData->callback( SearchData->callbackparam, SEARCHLOGSFAILED );
01616 delete[] SearchData->ResultFileName;
01617 delete[] SearchData->BlockBuffer;
01618 delete SearchData;
01619 }
01620 else
01621 {
01622
01623
01624
01625 #ifdef RIO_DEBUG2
01626 RioErr << "CRioSession::FinalizeSearchCmd execucao da busca terminou "
01627 << "com sucesso, e o arquivo " << SearchData->ResultFileName
01628 << " com o resultado, com tamanho de "
01629 << SearchData->ResultFileSize << " bytes e identificador igual "
01630 << "a " << SearchData->ResultFileId << " comecara a ser "
01631 << "copiado do servidor" << endl;
01632 #endif
01633 SearchData->ResultFile = fopen( SearchData->ResultFileName, "w" );
01634 if( SearchData->ResultFile == NULL )
01635 {
01636 #ifdef RIO_DEBUG2
01637 RioErr << "CRioSession::FinalizeSearchCmd erro " << errno
01638 << "(" << strerror( errno ) << ") ao abrir o arquivo "
01639 << SearchData->ResultFileName << " para escrita" << endl;
01640 #endif
01641
01642 SearchData->callback( SearchData->callbackparam, SEARCHLOGSERROR );
01643 delete[] SearchData->ResultFileName;
01644 delete[] SearchData->BlockBuffer;
01645 delete SearchData;
01646 }
01647
01648
01649
01650
01651 if( session->m_NetInterface != NULL )
01652 ReqId = session->m_NetInterface->ExpectBlock(
01653 SearchData->BlockBuffer,
01654 session->m_BlockSize,
01655 &FinalizeReadBlock,
01656 ( void * ) SearchData,
01657 1 );
01658 else
01659 ReqId = session->m_NetMgr->ExpectBlock( SearchData->BlockBuffer,
01660 session->m_BlockSize,
01661 &FinalizeReadBlock,
01662 ( void * ) SearchData, 1 );
01663
01664 #ifdef RIO_DEBUG2
01665 RioErr << "CRioSession::FinalizeSearchCmd Pedido de bloco 0 do arquivo "
01666 << "iniciado com a id " << ReqId << endl;
01667 #endif
01668
01669 hResult = session->m_SessionManager->ResultFileBlockRequest(
01670 SearchData->ServerIP,
01671 SearchData->ServerPort,
01672 SearchData->ClientIP,
01673 SearchData->ClientPort,
01674 SearchData->ResultFileId,
01675 0, ReqId );
01676 if( FAILED( hResult ) )
01677 {
01678
01679 if( session->m_NetInterface != NULL )
01680 session->m_NetMgr->CancelExpect( ReqId, hResult );
01681 else
01682 session->m_NetInterface->CancelExpect( ReqId, hResult );
01683 #ifdef RIO_DEBUG2
01684 RioErr << "CRioSession::FinalizeSearchCmd Erro ao executar o "
01685 << " pedido do bloco 0 com a id " << ReqId << ". O pedido "
01686 << "foi cancelado!" << endl;
01687 #endif
01688
01689
01690 SearchData->callback( SearchData->callbackparam, SEARCHLOGSERROR );
01691 delete[] SearchData->ResultFileName;
01692 delete[] SearchData->BlockBuffer;
01693 delete SearchData;
01694 }
01695 #ifdef RIO_DEBUG2
01696 else
01697 RioErr << "CRioSession::FinalizeSearchCmd Pedido do bloco 0 com a "
01698 << "id " << ReqId << " foi executado com suceso" << endl;
01699 #endif
01700 }
01701 return;
01702 }
01703
01704
01705
01706 void CRioSession::FinalizeReadBlock( void *parm, int result )
01707 {
01708 int ReqId;
01709 SearchLogsData *SearchData;
01710 unsigned int TotalBlocks, WriteSize;
01711 bool LastBlock;
01712 CRioSession *session;
01713 HRESULT hResult;
01714
01715 SearchData = ( SearchLogsData * ) parm;
01716 session = SearchData->session;
01717
01718 if( result != S_OK )
01719 {
01720 #ifdef RIO_DEBUG2
01721 RioErr << "CRioSession::FinalizeReadBlock a leitura do bloco "
01722 << SearchData->CurrentBlock << " do arquivo de busca com o "
01723 << "identificador " << SearchData->ResultFileId
01724 << " falhou. Removendo o arquivo " << SearchData->ResultFileName
01725 << " associado ao identificador e chamando a callback com "
01726 << " um erro" << endl;
01727 #endif
01728
01729
01730
01731 if( SearchData->ResultFile != NULL )
01732 {
01733 fclose( SearchData->ResultFile );
01734 remove( SearchData->ResultFileName );
01735 SearchData->ResultFile = NULL;
01736 }
01737 SearchData->callback( SearchData->callbackparam, SEARCHLOGSERROR );
01738 delete[] SearchData->ResultFileName;
01739 delete[] SearchData->BlockBuffer;
01740 delete SearchData;
01741 }
01742 else
01743 {
01744
01745
01746
01747 TotalBlocks = ( SearchData->ResultFileSize + session->m_BlockSize - 1 )
01748 / session->m_BlockSize;
01749 #ifdef RIO_DEBUG2
01750 RioErr << "CRioSession::FinalizeReadBlock O bloco "
01751 << SearchData->CurrentBlock << " foi recebido com sucesso (de "
01752 << "um total de " << TotalBlocks << " blocos). Ainda faltam "
01753 << TotalBlocks - SearchData->CurrentBlock - 1 << " blocos para "
01754 << "serem recebidos" << endl;
01755 #endif
01756 if( SearchData->CurrentBlock < TotalBlocks - 1 )
01757 {
01758 WriteSize = session->m_BlockSize;
01759 LastBlock = false;
01760 }
01761 else
01762 {
01763 WriteSize = SearchData->ResultFileSize - SearchData->CurrentBlock *
01764 session->m_BlockSize;
01765 LastBlock = true;
01766 }
01767 #ifdef RIO_DEBUG2
01768 RioErr << "CRioSession::FinalizeReadBlock tentando salvar o bloco "
01769 << SearchData->CurrentBlock << " no arquivo "
01770 << SearchData->ResultFileName << endl;
01771 #endif
01772
01773
01774 if( fwrite( SearchData->BlockBuffer, sizeof( char ), WriteSize,
01775 SearchData->ResultFile ) != WriteSize )
01776 {
01777 #ifdef RIO_DEBUG2
01778 RioErr << "CRioSession::FinalizeReadBlock Erro ao salvar o bloco "
01779 << SearchData->CurrentBlock << " no arquivo "
01780 << SearchData->ResultFileName << ": " << errno << "("
01781 << strerror( errno ) << ")" << endl;
01782
01783 #endif
01784 fclose( SearchData->ResultFile );
01785 remove( SearchData->ResultFileName );
01786 SearchData->ResultFile = NULL;
01787 SearchData->callback( SearchData->callbackparam, SEARCHLOGSERROR );
01788 delete[] SearchData->ResultFileName;
01789 delete[] SearchData->BlockBuffer;
01790 delete SearchData;
01791 }
01792
01793
01794
01795
01796 if( LastBlock )
01797 {
01798 #ifdef RIO_DEBUG2
01799 RioErr << "CRioSession::FinalizeReadBlock O bloco "
01800 << SearchData->CurrentBlock << " e o ultimo do arquivo "
01801 << SearchData->ResultFileName << ". Vamos entao fechar o "
01802 << "arquivo, tentar remover o log no servidor e chamar "
01803 << "a callback com sucesso" << endl;
01804
01805 #endif
01806 fclose( SearchData->ResultFile );
01807
01808 hResult = session->m_SessionManager->RemoveResultFile(
01809 SearchData->ServerIP,
01810 SearchData->ServerPort,
01811 SearchData->ResultFileId );
01812 if( FAILED( hResult ) )
01813 {
01814 RioErr << "CRioSession::FinalizeReadBlock aviso erro ao tentar "
01815 << "remover o arquivo com o identificador "
01816 << SearchData->ResultFileId << endl;
01817 }
01818 SearchData->ResultFile = NULL;
01819 SearchData->callback( SearchData->callbackparam, SEARCHLOGSSUCESS );
01820 delete[] SearchData->ResultFileName;
01821 delete[] SearchData->BlockBuffer;
01822 delete SearchData;
01823 }
01824 else
01825 {
01826
01827 SearchData->CurrentBlock++;
01828 #ifdef RIO_DEBUG2
01829 RioErr << "CRioSession::FinalizeReadBlock O bloco "
01830 << SearchData->CurrentBlock << " nao e o ultimo do arquivo "
01831 << SearchData->ResultFileName << ". Vamos entao tentar "
01832 << "pedir o proximo bloco " << SearchData->CurrentBlock
01833 << " deste arquivo" << endl;
01834
01835 #endif
01836
01837
01838
01839
01840 if( session->m_NetInterface != NULL )
01841 ReqId = session->m_NetInterface->ExpectBlock(
01842 SearchData->BlockBuffer,
01843 session->m_BlockSize,
01844 &FinalizeReadBlock,
01845 ( void * ) SearchData,
01846 1 );
01847 else
01848 ReqId = session->m_NetMgr->ExpectBlock( SearchData->BlockBuffer,
01849 session->m_BlockSize,
01850 &FinalizeReadBlock,
01851 ( void * ) SearchData,
01852 1 );
01853
01854
01855 hResult = session->m_SessionManager->ResultFileBlockRequest(
01856 SearchData->ServerIP,
01857 SearchData->ServerPort,
01858 SearchData->ClientIP,
01859 SearchData->ClientPort,
01860 SearchData->ResultFileId,
01861 SearchData->CurrentBlock,
01862 ReqId );
01863 if( FAILED( hResult ) )
01864 {
01865 #ifdef RIO_DEBUG2
01866 RioErr << "CRioSession::FinalizeReadBlock Erro " << hResult
01867 << " ao pedir o bloco " << SearchData->CurrentBlock
01868 << ". O arquivo parcial " << SearchData->ResultFileName
01869 << " sera removido e a callback sera chamada com um "
01870 << "erro." << endl;
01871 #endif
01872
01873 if( session->m_NetInterface != NULL )
01874 session->m_NetInterface->CancelExpect( ReqId, hResult );
01875 else
01876 session->m_NetMgr->CancelExpect( ReqId, hResult );
01877
01878
01879
01880 if( SearchData->ResultFile != NULL )
01881 {
01882 fclose( SearchData->ResultFile );
01883 remove( SearchData->ResultFileName );
01884 SearchData->ResultFile = NULL;
01885 }
01886
01887
01888 SearchData->callback( SearchData->callbackparam,
01889 SEARCHLOGSERROR );
01890 delete[] SearchData->ResultFileName;
01891 delete[] SearchData->BlockBuffer;
01892 delete SearchData;
01893 }
01894 }
01895 }
01896 return;
01897 }
01898
01899
01900
01901
01902
01903 int CRioSession::CheckServerAddress( char *ServerName, bool IsDispatcher,
01904 int *ServerIP, int *ServerPort )
01905 {
01906
01907 struct hostent* hostaddress;
01908
01909 int PosIP;
01910
01911 in_addr HostIP;
01912
01913 int PosServer;
01914
01915 *ServerIP = 0;
01916 *ServerPort = 0;
01917 PosServer = -1;
01918
01919
01920 hostaddress = gethostbyname( ServerName );
01921 if( hostaddress == NULL )
01922 {
01923
01924 return -2;
01925 }
01926
01927 for( unsigned int i = 0; i < m_ServerAddressSize; i++ )
01928 {
01929 PosIP = 0;
01930 while( hostaddress->h_addr_list[ PosIP ] != NULL )
01931 {
01932
01933
01934 memset( &HostIP, 0, sizeof( HostIP ) );
01935 memcpy( &HostIP, hostaddress->h_addr_list[ PosIP ],
01936 hostaddress->h_length );
01937
01938 #ifdef RIO_DEBUG2
01939 RioErr << "CRioSession::CheckServerAddress "
01940 << "m_ServerAddress[ " << i << " ].sin_addr.s_addr = "
01941 << inet_ntoa( m_ServerAddress[ i ].sin_addr ) << endl;
01942 RioErr << "CRioSession::CheckServerAddress "
01943 << "hostaddress->h_addr_list[ " << PosIP << " ] = "
01944 << inet_ntoa( HostIP ) << endl;
01945 #endif
01946
01947 if( m_ServerAddress[ i ].sin_addr.s_addr == HostIP.s_addr )
01948 {
01949 *ServerIP = m_ServerAddress[ i ].sin_addr.s_addr;
01950 *ServerPort = m_ServerAddress[ i ].sin_port;
01951 PosServer = i;
01952 if( ( i > 0 ) || ( ( i == 0 ) && ( IsDispatcher ) ) )
01953 break;
01954 }
01955 PosIP++;
01956 }
01957 }
01958 return PosServer;
01959 }
01960
01961
01962 RioResult CRioSession::CreateUser( char *UserName, char *Password )
01963 {
01964 #ifdef RIO_DEBUG1
01965 RioErr << "### [CRioSession - CreateUser] Start" << endl;
01966 #endif
01967
01968
01969 if( !isConnected() )
01970 {
01971 #ifdef RIO_DEBUG1
01972 RioErr << "### [CRioSession - CreateUser] Finish1" << endl;
01973 #endif
01974
01975
01976 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
01977 }
01978
01979
01980 HRESULT hResult;
01981 hResult = m_SessionManager->CreateUser( UserName, Password );
01982 if( FAILED( hResult ) )
01983 {
01984 #ifdef RIO_DEBUG1
01985 RioErr << "### [CRioSession - CreateUser] Finish2" << endl;
01986 #endif
01987
01988 return hResult;
01989 }
01990
01991 #ifdef RIO_DEBUG1
01992 RioErr << "### [CRioSession - CreateUser] Finish3" << endl;
01993 #endif
01994
01995 return S_OK;
01996 }
01997
01998
01999 RioResult CRioSession::RemoveUser( char *UserName )
02000 {
02001 #ifdef RIO_DEBUG1
02002 RioErr << "### [CRioSession - RemoveUser] Start" << endl;
02003 #endif
02004
02005
02006 if( !isConnected() )
02007 {
02008 #ifdef RIO_DEBUG1
02009 RioErr << "### [CRioSession - RemoveUser] Finish1" << endl;
02010 #endif
02011
02012
02013 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
02014 }
02015
02016
02017 HRESULT hResult;
02018 hResult = m_SessionManager->RemoveUser( UserName );
02019 if( FAILED( hResult ) )
02020 {
02021 #ifdef RIO_DEBUG1
02022 RioErr << "### [CRioSession - RemoveUser] Finish2" << endl;
02023 #endif
02024
02025 return hResult;
02026 }
02027
02028 #ifdef RIO_DEBUG1
02029 RioErr << "### [CRioSession - RemoveUser] Finish3" << endl;
02030 #endif
02031
02032 return S_OK;
02033 }
02034
02035
02036 RioResult CRioSession::ChangePassword( char *UserName, char *Password )
02037 {
02038 #ifdef RIO_DEBUG1
02039 RioErr << "### [CRioSession - ChangePassword] Start" << endl;
02040 #endif
02041
02042
02043 if( !isConnected() )
02044 {
02045 #ifdef RIO_DEBUG1
02046 RioErr << "### [CRioSession - ChangePassword] Finish1" << endl;
02047 #endif
02048
02049
02050 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
02051 }
02052
02053
02054 HRESULT hResult;
02055 hResult = m_SessionManager->ChangePassword( UserName, Password );
02056 if( FAILED( hResult ) )
02057 {
02058 #ifdef RIO_DEBUG1
02059 RioErr << "### [CRioSession - ChangePassword] Finish2" << endl;
02060 #endif
02061
02062 return hResult;
02063 }
02064
02065 #ifdef RIO_DEBUG1
02066 RioErr << "### [CRioSession - ChangePassword] Finish3" << endl;
02067 #endif
02068
02069 return S_OK;
02070 }
02071
02072
02073 RioResult CRioSession::GetUserList( vector< string > &UserList )
02074 {
02075 #ifdef RIO_DEBUG1
02076 RioErr << "### [CRioSession - GetUserList] Start" << endl;
02077 #endif
02078
02079
02080 if( !isConnected() )
02081 {
02082 #ifdef RIO_DEBUG1
02083 RioErr << "### [CRioSession - GetUserList] Finish1" << endl;
02084 #endif
02085
02086
02087 return ERROR_RIOSESSION + ERROR_SESSION_NOT_OPENED;
02088 }
02089
02090
02091 HRESULT hResult;
02092 hResult = m_SessionManager->GetUserList( UserList );
02093 if( FAILED( hResult ) )
02094 {
02095 #ifdef RIO_DEBUG1
02096 RioErr << "### [CRioSession - GetUserList] Finish2" << endl;
02097 #endif
02098
02099 return hResult;
02100 }
02101
02102 #ifdef RIO_DEBUG1
02103 RioErr << "### [CRioSession - ChangePassword] Finish3" << endl;
02104 #endif
02105
02106 return S_OK;
02107 }