Exemplo n.º 1
0
void Client::startRead()
{
    //@puts("reading");
    char *buffer = (char*)malloc(sizeof(char)*10240);
    int bytesAvail = client.bytesAvailable();
    client.read(buffer, bytesAvail);
    buffer[bytesAvail] = '\0';
    int bytesPushed=0;
    int *size=(int*)malloc(sizeof(int));
    Action *action = (Action*)malloc(sizeof(int));
    char *msg;

    //loop through the available messages
    while(bytesPushed<bytesAvail){
    *size=0;
    popMetadata(&buffer,action,size,&msg);
    bytesPushed+=(*size+8);
    if(bytesPushed<=bytesAvail){

        //@printf("Bytes pushed: %i, Bytes available: %i\n",bytesPushed,bytesAvail);
        if(bytesPushed<bytesAvail){
            //@printf("Msg: %s\nData remaining: %s\n\n",msg,buffer);
        } else {
            //@printf("Msg: %s\n\n",msg);
        }
        switch(*action) {
            case KEY_EVENT:
                receiveEvent(stringToEvent(msg));
                break;
            case INSERT_STRING:
                insertString(msg);
                break;
            case REMOVE_STRING:
                removeString(msg);
                break;
            case INITIAL_SEND:
                initialRead(msg);
                break;
            case CURSOR_MOVE:
                if(moveRemoteCursor(msg)) cursorPositionChanged();
                break;
            default:
                puts("We don't take your kind here.");
                break;
        }
    } else {
        puts("Well, that went badly.");
    }
    }
}
Exemplo n.º 2
0
int StateMachine::Run()
{   
    //initialize database
    ptDb->init();
    connectionCount = false;
    int statusCode = 0;
    // Run the program until SIG_KILL is not received
    do
    {
        //initialize the state machine
        statusCode = initializeStateMachine();
        //set next state
        setNextState(statusCode);
        if(statusCode == SUCCESS)
        {
            //turn the timer off --- as here objects will reconstruct themselves
            StateMachine::isTimerEvent = false;
            // Run the program until connection is alive.
            do 
            {
                // poll for bro events.
                PollData tempQueue = waitForEvents();
                if(signalHandler->gotExitSignal())
                {
                    broker::message temp;
                    currentEvent = SIG_KILL_EVENT;
                    //pass kill signal to clean up the resources
                    extractAndProcessEvents(currentEvent,temp);
                   
                }
                else if(!ptBCM->isConnectionAlive())
                {
                    broker::message temp;
                    extractAndProcessEvents(CONNECTION_BROKEN_EVENT,temp);
                }
                // bro events processing 
                else if(!tempQueue.empty())
                {
                    for(auto& msg : tempQueue)
                    {
                        if(signalHandler->gotExitSignal())
                        {
                            currentEvent = SIG_KILL_EVENT;
                            extractAndProcessEvents(currentEvent,msg);
                            break;
                        }
                        else
                        {
                            auto ev = broker::to_string(msg[0]);
                            statusCode = extractAndProcessEvents(
                                    stringToEvent(ev),msg);
                            if(statusCode == SIG_KILL_EVENT)
                            {
                                break;
                            }
                        }
                    }
                }
                // if timer is up.
                else if(isTimerEvent)
                {
                    broker::message temp;
                    extractAndProcessEvents(TIMER_EVENT,temp);
                }
                
       
            }while(ptBCM->isConnectionAlive() &&
                    !signalHandler->gotExitSignal());
            
            //if disconnected then break the connection.
            LOG(WARNING) <<"Connection Broken";
            
            if(signalHandler->gotExitSignal())
            {
                doActionsForKillSignalEvent();
            }
            
        }
        
    }while(!signalHandler->gotExitSignal());
    
    return SUCCESS;                
}