Пример #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;
}
Пример #2
0
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;
}
Пример #3
0
/*
 * connectionListenerRunThread
 * 1. Listen and wait for connections request
 * 2. convert the request into command
 * 3. pass the request command to the DistributionMAnager
 */
void connectionListenerRunThread(ConnectionListener connectionListener) {

    // Declaring process variables.
    ConnectionListenerInternal* internal=(ConnectionListenerInternal*)connectionListener;
    int server_sockfd, client_sockfd;
    int server_len ;
    int size ;
    unsigned client_len;
    struct sockaddr_in server_address;
    struct sockaddr_in client_address;
    void* readBuffer;
    void* writeBuffer;
    int commandSize;
    int currentSize;
    const int       optVal = 1;
    const socklen_t optLen = sizeof(optVal);
    // Remove any old socket and create an unnamed socket for the server.
    server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    int rtn = setsockopt(server_sockfd, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = INADDR_ANY;// htons(ipConverterToLong(internal->ip));
    server_address.sin_port = htons(internal->port);
    server_len = sizeof(server_address);

    int rc = bind(server_sockfd, (struct sockaddr *) &server_address, server_len);
    if(rc<0) {
        Throw (Fatal,1,"Failt to bind to listen port \n");
    }
    printf("RC from bind = %d\n", rc );

    //Create a connection queue and wait for clients
    rc = listen(server_sockfd, 5);
    printf("RC from listen = %d\n", rc ) ;

    client_len = sizeof(client_address);
    while(internal->active==TRUE) {
        client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_len);
        char* ip=inet_ntoa(client_address.sin_addr);
        size=0;
        size += read(client_sockfd,&commandSize+size, SIZE_OF_INT);
        if(size<SIZE_OF_INT) {
            continue;
        }
        commandSize=htonl(commandSize);
        readBuffer=memoryAlloc(commandSize-SIZE_OF_INT);
        currentSize=0;
        currentSize+=read(client_sockfd,readBuffer+currentSize, commandSize-SIZE_OF_INT);
        if(currentSize<commandSize-SIZE_OF_INT) {
            memoryFree(readBuffer);
            continue;
        }
        Command command=commandDeSerialize(readBuffer,commandSize-SIZE_OF_INT);
        memoryFree(readBuffer);
        // handle message
        handleMessage(client_sockfd,internal,command,ip);
        // Create the result message and send it to client
        commandSize=commandSerialize(command,&writeBuffer);
        int result = write(client_sockfd,writeBuffer,commandSize);
        if(result<0) {
            printf("ConnectionListener Fail to write to socket");
        }
        memoryFree(writeBuffer);
        //commandDestroy(command);
    }
    printf("Server connection listener stoped\n");
    close(client_sockfd);

}