00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <clientinterface.h>
00024
00025 #include <extern.h>
00026 #include <math.h>
00027
00028 #include <iostream>
00029 #include <stdio.h>
00030
00031 #include "../interface/RioError.h"
00032
00033
00034 #include <sys/socket.h>
00035 #include <netinet/in.h>
00036 #include <arpa/inet.h>
00037
00038
00039
00040
00041
00042
00043
00044 CClientInterface::CClientInterface()
00045 {
00046 m_SendFlag = 0;
00047 m_NumberOfBlocksToSend = 0;
00048 }
00049
00050
00051 CClientInterface::~CClientInterface()
00052 {
00053 }
00054
00055
00056
00057 int CClientInterface::Initialize()
00058 {
00059 return 0;
00060 }
00061
00062
00063 int CClientInterface::Initialize(int SendFlag, double EstimatedParameter)
00064 {
00065 m_SendFlag = SendFlag;
00066 #ifdef RIO_DEBUG2
00067 RioErr << "Init SendFlag " << SendFlag << endl;
00068 #endif
00069 return 0;
00070 }
00071
00072
00073
00074 void CClientInterface::Stop()
00075 {
00076 return;
00077 }
00078
00079
00080
00081
00082 void SendComplete ( void* Param, int Result)
00083 {
00084 StrRequest* Request = (StrRequest*) Param;
00085
00086 int FlagStatus;
00087 u16 Command = Request->Command;
00088
00089 if( Result )
00090 {
00091 struct in_addr clientip;
00092 clientip.s_addr = Request->IPaddress;
00093 RioErr << "clientinterface.SendComplete - bad result : "
00094 << (int *) Result
00095 << " " << (unsigned int) Result
00096 << " : Reqid " << Request->ClientId
00097 << " IP " << inet_ntoa(clientip)
00098 << " Port " << ntohs( Request->Port )
00099 << endl;
00100 }
00101
00102
00103
00104
00105 if( ( Command & RSS_NOCOMPLETE ) && ( Result == 0 ))
00106 FlagStatus = 0;
00107 else
00108 FlagStatus = 1;
00109
00110 StrMsg* Slot = NULL;
00111
00112 u32 temp;
00113
00114
00115 if( FlagStatus )
00116 {
00117
00118 Slot = MsgManager.New();
00119 Slot->Msg.Storage.Status.Type = MSG_RSS_SENDCOMPLETE;
00120 Slot->Msg.Storage.Status.Size = SizeMsgRSSstatus;
00121 Slot->Msg.Storage.Status.RouterId = Request->RouterId;
00122 Slot->Msg.Storage.Status.StorageId = Request->Id;
00123 Slot->Msg.Storage.Status.ActiveThreads = 0;
00124
00125 if( Result == 0 )
00126 Slot->Msg.Storage.Status.Error = 0;
00127 else
00128 Slot->Msg.Storage.Status.Error = ERROR_RSS_SEND_FAILED;
00129 temp = Request->Id;
00130 }
00131
00132
00133 RequestManager.Free( Request );
00134
00135
00136 if( FlagStatus )
00137 {
00138 #ifdef RIO_DEBUG2
00139 RioErr <<"Sending status (SEND COMPLETE) to router " << endl;
00140 #endif
00141
00142 Router.Send(Slot);
00143 }
00144 }
00145
00146
00147
00148 void ReceiveComplete ( void* Param, int Result)
00149 {
00150 #ifdef RIO_DEBUG1
00151 RioErr << "[ClientInterface - ReceiveComplete] Start" << endl;
00152 #endif
00153 StrRequest* Request = (StrRequest*) Param;
00154 int FlagStatus;
00155 u16 Command = Request->Command;
00156
00157
00158
00159 if( ( Command & 0xf ) == MSG_RSS_WRITE )
00160 {
00161 #ifdef RIO_DEBUG2
00162 RioErr << "[ClientInterface - ReceiveComplete] Command: Write" << endl;
00163 #endif
00164
00165
00166 if( Command & RSS_INTERMEDIATE )
00167 FlagStatus = 1;
00168 else
00169 FlagStatus = 0;
00170 }
00171 else
00172 {
00173 #ifdef RIO_DEBUG2
00174 RioErr << "[ClientInterface - ReceiveComplete] Command: Receive" << endl;
00175 #endif
00176
00177
00178 if( ( Command & RSS_NOCOMPLETE ) && ( Result == 0 ))
00179 FlagStatus = 0;
00180 else
00181 FlagStatus = 1;
00182 }
00183
00184
00185 Command = Command & 0xf;
00186
00187
00188 if( Command == MSG_RSS_RECEIVE )
00189 {
00190 Request->Status = RequestStatusWaitFlushCommand;
00191 }
00192 else
00193 {
00194 Request->Status = RequestStatusReceiveDone;
00195 }
00196
00197 StrMsg* Slot = NULL;
00198
00199
00200 if( FlagStatus )
00201 {
00202 #ifdef RIO_DEBUG2
00203 RioErr << "[ClientInterface - ReceiveComplete] Preparing status message to router" << endl;
00204 #endif
00205
00206 Slot = MsgManager.New();
00207 Slot->Msg.Storage.Status.Type = MSG_RSS_RECEIVECOMPLETE;
00208 Slot->Msg.Storage.Status.Size = SizeMsgRSSstatus;
00209 Slot->Msg.Storage.Status.RouterId = Request->RouterId;
00210 Slot->Msg.Storage.Status.StorageId = Request->Id;
00211 Slot->Msg.Storage.Status.ActiveThreads = 0;
00212
00213 if( Result == 0 )
00214 Slot->Msg.Storage.Status.Error = 0;
00215 else
00216 Slot->Msg.Storage.Status.Error = ERROR_RSS_RECEIVE_FAILED;
00217 }
00218
00219 if( Result != 0 )
00220 {
00221 #ifdef RIO_DEBUG2
00222 struct in_addr clientip;
00223 clientip.s_addr = Request->IPaddress;
00224 RioErr << "clientinterface.ReceiveComplete - bad result : "
00225 << (int *) Result
00226 << " " << (unsigned int) Result
00227 << " : Reqid " << Request->ClientId
00228 << " IP " << inet_ntoa(clientip)
00229 << " Port " << ntohs( Request->Port )
00230 << endl;
00231 #endif
00232
00233
00234 RequestManager.Free(Request);
00235 }
00236
00237
00238 if( FlagStatus )
00239 {
00240 #ifdef RIO_DEBUG2
00241 RioErr << "[ClientInterface - ReceiveComplete] Sending status message "
00242 << "to router" << endl;
00243 #endif
00244
00245 Router.Send(Slot);
00246 }
00247
00248 if( Result == 0 )
00249 {
00250
00251 if( Command == MSG_RSS_WRITE )
00252 {
00253 #ifdef RIO_DEBUG2
00254 RioErr << "[ClientInterface - ReceiveComplete] Sending data to device" << endl;
00255 #endif
00256 Device[Request->Disk].ProcessRequest(Request);
00257 }
00258 }
00259 #ifdef RIO_DEBUG1
00260 RioErr << "[ClientInterface - ReceiveComplete] Finish" << endl;
00261 #endif
00262 }
00263
00264
00265
00266 void CClientInterface::Send(StrRequest* Request)
00267 {
00268
00269
00270 bool UseTcp;
00271
00272 #ifdef RIO_DEBUG2
00273 struct in_addr clientip;
00274 clientip.s_addr = Request->IPaddress;
00275
00276 RioErr << "clientinterface: Thread " << pthread_self()
00277 << " : SendFlag " << m_SendFlag
00278 << " Send data to ip " << inet_ntoa(clientip)
00279 << " port " << ntohs( Request->Port )
00280 << " ClientId " << Request->ClientId << endl;
00281 #endif
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297 UseTcp = ( Request->StreamTraffic != 3 );
00298
00299 if( m_SendFlag == 1 )
00300 {
00301
00302
00303
00304
00305
00306
00307
00308 if( NetInterface.FindIPAndPort( Request->IPaddress, Request->Port,
00309 UseTcp ) )
00310 {
00311 #ifdef RIO_DEBUG2
00312 RioErr << "CClientInterface::Send enviando um bloco de "
00313 << Request->Size << " bytes para o cliente no IP "
00314 << inet_ntoa( clientip ) << " e na porta "
00315 << ntohs( Request->Port ) << ", com o ID "
00316 << Request->ClientId << " usando a nova implementacao de "
00317 << "rede." << endl;
00318 #endif
00319
00320 NetInterface.SendBlock ( Request->IPaddress,
00321 Request->Port,
00322 Request->ClientId,
00323 Request->Data,
00324 Request->Size,
00325 SendComplete,
00326 (void*) Request,
00327 UseTcp,
00328 Request->VideoRate );
00329 }
00330 else
00331 {
00332 #ifdef RIO_DEBUG2
00333 RioErr << "CClientInterface::Send enviando um bloco de "
00334 << Request->Size << " bytes para o cliente no IP "
00335 << inet_ntoa( clientip ) << " e na porta "
00336 << ntohs( Request->Port ) << ", com o ID "
00337 << Request->ClientId << " usando a implementacao antiga de "
00338 << "rede." << endl;
00339 #endif
00340
00341 NetManager.SendBlock ( Request->IPaddress,
00342 Request->Port,
00343 Request->ClientId,
00344 Request->Data,
00345 Request->Size,
00346 SendComplete,
00347 (void*) Request,
00348 Request->StreamTraffic,
00349 Request->VideoRate );
00350 }
00351 }
00352 else
00353 {
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 if( NetInterface.FindIPAndPort( Request->IPaddress, Request->Port,
00364 UseTcp ) )
00365 {
00366 #ifdef RIO_DEBUG2
00367 RioErr << "CClientInterface::Send enviando um bloco de "
00368 << Request->Size << " bytes para o cliente no IP "
00369 << inet_ntoa( clientip ) << " e na porta "
00370 << ntohs( Request->Port ) << ", com o ID "
00371 << Request->ClientId << " usando a nova implementacao de "
00372 << "rede." << endl;
00373 #endif
00374
00375 NetInterface.SendBlock ( Request->IPaddress,
00376 Request->Port,
00377 Request->ClientId,
00378 Request->Data,
00379 1452,
00380 SendComplete,
00381 (void*) Request,
00382 UseTcp,
00383 Request->VideoRate );
00384 }
00385 else
00386 {
00387 #ifdef RIO_DEBUG2
00388 RioErr << "CClientInterface::Send enviando um bloco de "
00389 << Request->Size << " bytes para o cliente no IP "
00390 << inet_ntoa( clientip ) << " e na porta "
00391 << ntohs( Request->Port ) << ", com o ID "
00392 << Request->ClientId << " usando a implementacao antiga de "
00393 << "rede." << endl;
00394 #endif
00395
00396 NetManager.SendBlock ( Request->IPaddress,
00397 Request->Port,
00398 Request->ClientId,
00399 Request->Data,
00400 1452,
00401 SendComplete,
00402 (void*) Request,
00403 Request->StreamTraffic,
00404 Request->VideoRate );
00405 }
00406 }
00407 }
00408
00409
00410
00411 void CClientInterface::Receive( StrRequest* Request )
00412 {
00413
00414
00415 struct in_addr clientip;
00416
00417 clientip.s_addr = Request->IPaddress;
00418
00419
00420
00421
00422
00423
00424
00425 Request->Status = RequestStatusWaitReceive;
00426
00427
00428
00429
00430
00431
00432
00433 if( NetInterface.FindIPAndPort( Request->IPaddress, Request->Port ) )
00434 {
00435 #ifdef RIO_DEBUG2
00436 RioErr << "CClientInterface::Receive recebendo um bloco de "
00437 << Request->Size << " bytes para o cliente no IP "
00438 << inet_ntoa( clientip ) << " e na porta "
00439 << ntohs( Request->Port ) << ", com o ID "
00440 << Request->ClientId << " usando a nova implementacao de "
00441 << "rede." << endl;
00442 #endif
00443
00444 NetInterface.GetBlock ( Request->IPaddress,
00445 Request->Port,
00446 Request->ClientId,
00447 Request->Data,
00448 Request->Size,
00449 ReceiveComplete,
00450 (void*) Request);
00451 }
00452 else
00453 {
00454 #ifdef RIO_DEBUG2
00455 RioErr << "CClientInterface::Receive recebendo um bloco de "
00456 << Request->Size << " bytes para o cliente no IP "
00457 << inet_ntoa( clientip ) << " e na porta "
00458 << ntohs( Request->Port ) << ", com o ID "
00459 << Request->ClientId << " usando a implementacao antiga de "
00460 << "rede." << endl;
00461 #endif
00462
00463 NetManager.GetBlock ( Request->IPaddress,
00464 Request->Port,
00465 Request->ClientId,
00466 Request->Data,
00467 Request->Size,
00468 ReceiveComplete,
00469 (void*) Request);
00470 }
00471 }
00472