void tcpConnectionListenerRunThread(TcpConnection tcpConnection){ TcpConnectionInternal* internal=(TcpConnectionInternal*)tcpConnection; //Declaring proc ess variables. int size ; void* buffer; void *commandBuffer; int commandSize; int currentSize; int tempSize=0; int result; while(internal->active==TRUE){ size=0; while(size<SIZE_OF_INT){ tempSize=read(internal->sockfd,&commandSize+size, SIZE_OF_INT-size); if(tempSize<=0){ internal->active=FALSE; distributionManagerHandleTcpConnectionListenerFailure(internal->distributionManager,internal,internal->coreManagerId); return; } size +=tempSize; } commandSize=htonl(commandSize); buffer=memoryAlloc(commandSize-SIZE_OF_INT); currentSize=0; while(currentSize<commandSize-SIZE_OF_INT){ currentSize+=read(internal->sockfd,buffer+currentSize, MIN(MAX_TCP_MESSAGE_SIZE,commandSize-currentSize-SIZE_OF_INT)); if(currentSize<=0){ internal->active=FALSE; distributionManagerHandleTcpConnectionListenerFailure(internal->distributionManager,internal,internal->coreManagerId); memoryFree(buffer); return; } } Command command=commandDeSerialize(buffer,commandSize-SIZE_OF_INT); commandPush(command,IP,STRING,internal->remoteIp,internal->remoteIpLenght); commandPush(command,PORT,INTEGER,&internal->remotePort); memoryFree(buffer); // handle message distributionManagerConsumeTcp(internal->distributionManager,command); // Create the result message and send it to client commandSize=commandSerialize(command,&commandBuffer); result=writeToSocket(internal->sockfd,commandBuffer,commandSize); memoryFree(commandBuffer); if(result<0){ internal->active=FALSE; distributionManagerHandleTcpConnectionListenerFailure(internal->distributionManager,internal,internal->coreManagerId); return; } // Delete all none model or none atomic commands if(commandIsDeleteOnExit(command)==TRUE){ commandFree(command); } } internal->readyForCleanup+=LISTENER_READY_FOR_CLEANUP; }
void tcpConnectionWriterRunThread(TcpConnection tcpConnection){ TcpConnectionInternal* internal=(TcpConnectionInternal*)tcpConnection; ArrayList tempList; while(internal->active==TRUE){ environmentThreadLock(internal->thread); tempList=internal->commands; internal->commands=arrayListCreate(); environmentThreadUnlock(internal->thread); int i; int length=arrayListSize(tempList); Command command; for(i=0; i < length;i++){ command=arrayListGetValueAt(tempList,i); int result=tcpConnectionSend(tcpConnection,command); if(result<0){ internal->active=FALSE; distributionManagerHandleTcpConnectionListenerFailure(internal->distributionManager,internal,internal->coreManagerId); //TODO Free the commands return; } commandFree(command); } arrayListFree(tempList); // Lock to synchronize environmentThreadLock(internal->thread); if(arrayListSize(internal->commands)==0){ environmentThreadWait(internal->thread); } environmentThreadUnlock(internal->thread); } internal->readyForCleanup+=WRITER_READY_FOR_CLEANUP; }
int tcpConnectionSend(TcpConnection tcpConnection,Command command){ TcpConnectionInternal* internal=(TcpConnectionInternal*)tcpConnection; int size ; void* buffer; void *commandBuffer; int commandSize; int currentSize; int tempSize=0; int result; if(internal->active==FALSE){ return; } commandSize=commandSerialize(command,&commandBuffer); result = tcpConnectionWriteToSocket(internal->sockfd,commandBuffer,commandSize); memoryFree(commandBuffer); if(result!=0){ } size=0; while(size<SIZE_OF_INT){ tempSize=read(internal->sockfd,&commandSize+size, SIZE_OF_INT-size); if(tempSize<0){ internal->active=FALSE; distributionManagerHandleTcpConnectionListenerFailure(internal->distributionManager,internal,internal->coreManagerId); return; } size +=tempSize; } commandSize=htonl(commandSize); buffer=memoryAlloc(commandSize-SIZE_OF_INT); currentSize=0; while(currentSize<commandSize-SIZE_OF_INT){ currentSize+=read(internal->sockfd,buffer+currentSize, MIN(MAX_TCP_MESSAGE_SIZE,commandSize-currentSize-SIZE_OF_INT)); if(currentSize<0){ internal->active=FALSE; distributionManagerHandleTcpConnectionListenerFailure(internal->distributionManager,internal,internal->coreManagerId); memoryFree(buffer); return; } } Command resultCommand=commandDeSerialize(buffer,commandSize-SIZE_OF_INT); commandFill(resultCommand,command); return VALID; }