Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
/*
 * handleMessage
 * 1. Create new TcpConnection or UdpConnection (depends on the request)
 * 2. Register the new connection in the DistributionManager
 */
void handleMessage(int client_sockfd,ConnectionListenerInternal* connectionListenerInternal,Command command,char* ip) {
    DistributionManager distributionManager=connectionListenerInternal->distributionManager;
    distributionManagerConsumeTcp(distributionManager,command);
    int status=commandGetInt(command,COMMAND_STATUS);
    if(status==VALID) {
        int type=commandGetInt(command,COMMAND_TYPE);
        if(IS_TCP_CONNECTION(type)) {
            // Create Tcp connection
            int coreManagerId=commandGetInt(command,CORE_MANAGER_ID);
            TcpConnection connection=tcpConnectionCreate(distributionManager,client_sockfd,coreManagerId,ip,commandGetInt(command,PORT));
            distributionManagerRegisterTcp(distributionManager,connection,coreManagerId);
            tcpConnectionStartListener(connection);
        }
        if(IS_BROADCAST_TCP_CONNECTION(type)) {
            // Create Tcp connection
            int coreManagerId=commandGetInt(command,CORE_MANAGER_ID);
            TcpConnection connection=tcpConnectionCreate(distributionManager,client_sockfd,coreManagerId,ip,commandGetInt(command,PORT));
            distributionManagerRegisterBroadcastTcp(distributionManager,connection,coreManagerId);
            tcpConnectionStartWriter(connection);
        }
        if(IS_UDP_CONNECTION(type)) {
            // Create Udp connection
            char* interface=propertiesGet(connectionListenerInternal->properties,"interface");
            int coreManagerId=commandGetInt(command,CORE_MANAGER_ID);
            int isRegistred=distributionManagerIsRegistred(distributionManager,coreManagerId);
            if(isRegistred==FALSE) {
                UdpConnection connection=udpConnectionCreate(distributionManager,coreManagerId,ip,commandGetInt(command,PORT),interface);
                distributionManagerRegisterUdp(distributionManager,connection,coreManagerId);
                udpConnectionSatrt(connection);
            }
        }
        if(IS_MULTICAST_CONNECTION(type)) {
            int coreManagerId=commandGetInt(command,CORE_MANAGER_ID);
            distributionManagerRegisterMulticast(distributionManager,NULL,coreManagerId);
        }
        int coreManagerId=distributionManagerGetCoreManager(distributionManager);
        int status=VALID;
        commandPush(command,CORE_MANAGER_ID,INTEGER,&coreManagerId);
        commandPush(command,COMMAND_STATUS,INTEGER,&status);
    }
}