예제 #1
0
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;
}
예제 #2
0
TcpConnection tcpConnectionCreate(ConnectionPool connectionPool,DistributionManager distributionManager,int coreManagerId,Properties properties){
    TcpConnectionInternal* m=(TcpConnectionInternal*)memoryAlloc(sizeof(TcpConnectionInternal));
    m->connectionPool=connectionPool;
    char* remoteIp=propertiesGet(properties,"ip");
    int remotePort=atoi(propertiesGet(properties,"port"));
    memset(&m->address, 0, sizeof(m->address));         /* Zero out structure */
    m->address.sin_family      = AF_INET;               /* Internet address family */
    m->address.sin_addr.s_addr = inet_addr(remoteIp);   /* Server IP address */
    m->address.sin_port        = htons(remotePort);     /* Server port */
    /* Establish the connection to the echo server */
    if((m->sockfd=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
        printf("Fail to establish tcp connection with the server - IP%s port %i ",remoteIp,remotePort);
        return NULL;
    }

    if (connect(m->sockfd, (struct sockaddr *) &m->address, sizeof(m->address)) < 0){
        printf("Fail to establish tcp connection with the server - IP%s port %i ",remoteIp,remotePort);
        return NULL;
    }
    m->coreManagerId=coreManagerId;
    m->remoteIp=strdup(remoteIp);
    m->remoteIpLenght=strlen(remoteIp);
    m->remotePort=remotePort;
    m->distributionManager=distributionManager;
    m->active=TRUE;
    int set = 1;
    setsockopt(m->sockfd, SOL_SOCKET, MSG_NOSIGNAL, (void *)&set, sizeof(int));
    
    char* userName=propertiesGet(properties,"userName");
    char* password=propertiesGet(properties,"password");
    char* ip=propertiesGet(properties,"ip");
    int port=atoi(propertiesGet(properties,"port"));
    int userNameLenght=strlen(userName);
    int passwordLenght=strlen(password);
    int ipLenght=strlen(ip);
    Command command= commandBuilderBuildTcpConnectionCommand(userName, userNameLenght,password,passwordLenght,coreManagerId,ip,ipLenght,port);
    tcpConnectionSend(m,command);
    int status = commandGetInt(command,COMMAND_STATUS);
    if( status!=VALID){
       printf("Fail to establish tcp connection with the server : cause is authentication failure.");
       return NULL;
    }
   
            
    return m;
}