Exemplo n.º 1
0
void commandContainerCleanup(CommandContainer commandContainer){
	CommandContainerInternal* internal=(CommandContainerInternal*)commandContainer;
        lockLock(internal->lock);
        time_t currentTime;       // Stores seconds elapsed since 01-01-1970
        time(&currentTime);
        // Cleanup atomic commands
        if(linkedListSize(internal->atomicCommands)>0){
            int length=linkedListSize(internal->atomicCommands);
            Command atomicCommand;
            while(length>0){
                 linkedListGetFirst(internal->atomicCommands,(Object*)&atomicCommand);
                 int commandTime=commandGetInt(atomicCommand,SERVER_HANDLING_TIME);
                 int distributed=commandGetInt(atomicCommand,DISTRIBUTED);
                 if((currentTime-commandTime>MAX_TIME_TO_HOLD_COMMAND&&distributed==TRUE)||(currentTime-commandTime>MAX_TIME_TO_HOLD_UNCLEANED_COMMAND)){
                   commandFree(atomicCommand);
                   linkedListRemoveFirst(internal->atomicCommands);
                 }else{
                     break;
                 }
                 length=linkedListSize(internal->atomicCommands);
            }
        }
        
        // Cleanup commands
        int length=linkedListSize(internal->versions);
        int i;
        int* intBuffer;
        while(length>0){
            linkedListGetFirst(internal->versions,(Object*)&intBuffer);
            Command temp;
            hashmapGet(internal->versionMap,*intBuffer,(Object*)&temp);
            int commandTime=commandGetInt(temp,SERVER_HANDLING_TIME);            
            int persisted=commandGetInt(temp,PERSISTED);
            int distributed=commandGetInt(temp,DISTRIBUTED);
            if((currentTime-commandTime>MAX_TIME_TO_HOLD_COMMAND&&persisted==TRUE&&distributed==TRUE)||(currentTime-commandTime>MAX_TIME_TO_HOLD_UNCLEANED_COMMAND)){
                linkedListRemoveFirst(internal->versions);
                hashmapRemove(internal->versionMap,*intBuffer);
                memoryFree(intBuffer);
                commandFree(temp);
            }else{
                break;
            }
            length=linkedListSize(internal->versions);
        }
        lockUnlock(internal->lock);
        
        

}
Exemplo n.º 2
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;
}
Exemplo n.º 3
0
void testCommandSetArgs(void **state)
{
	command_t *command = commandNew();
	char *argv[] = {"a", "b", "cdef"};
	int argc = 3;
	assert_true(command->argv == argv);
	assert_int_equal(command->argc, argc);
	commandFree(command);
}
Exemplo n.º 4
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.º 5
0
void testCommandSetConnectionMask(void **state)
{
	command_t *command = commandNew();
	commandSetConnectionMask(command, kCommandConnectionBackground);
	assert_true(command->connectionMask == kCommandConnectionBackground);
	commandSetConnectionMask(command, kCommandConnectionPipe);
	assert_true(command->connectionMask == kCommandConnectionPipe);
	commandSetConnectionMask(command, kCommandConnectionNone);
	assert_true(command->connectionMask == kCommandConnectionNone);
	commandFree(command);
}
Exemplo n.º 6
0
void testCommandSetRedirectToPath(void **state)
{
	command_t *command = commandNew();
	char *path = "/dev/urandom";
	commandSetRedirectToPath(command, path);
	assert_string_equal(command->redirectToPath, path);
	assert_true(command->redirectToPath != path);
	commandSetRedirectToPath(command, NULL);
	assert_true(command->redirectToPath == NULL);
	commandFree(command);
}
Exemplo n.º 7
0
void testCommandSetPath(void **state)
{
	command_t *command = commandNew();
	char *path = "/bin/ls";
	commandSetPath(command, path);
	assert_string_equal(command->path, path);
	assert_false(command->path == path); /* should be deep copied */
	commandSetPath(command, NULL);
	assert_true(command->path == NULL);
	commandFree(command);
}
Exemplo n.º 8
0
void testCommandNew(void **state)
{
	command_t *command = commandNew();
	assert_true(command != NULL);
	assert_true(command->path == NULL);
	assert_true(command->argc == 0);
	assert_true(command->argv == NULL);
	assert_true(command->redirectToPath == NULL);
	assert_true(command->redirectFromPath == NULL);
	assert_true(command->connectionMask == kCommandConnectionNone);
	commandFree(command);
}
Exemplo n.º 9
0
void testCommandIsBuiltin(void **state)
{
	command_t *command = commandNew();
	commandSetPath(command, "ls");
	assert_false(commandIsBuiltIn(command));
	commandSetPath(command, "prompt");
	assert_true(commandIsBuiltIn(command));
	commandSetPath(command, "exit");
	assert_true(commandIsBuiltIn(command));
	commandSetPath(command, "cd");
	assert_true(commandIsBuiltIn(command));
	commandSetPath(command, "pwd");
	assert_true(commandIsBuiltIn(command));
	commandSetPath(command, "echo");
	assert_false(commandIsBuiltIn(command));
	commandFree(command);
}