Example #1
0
void * getMessagesThread(void * arg) {
    // This code will be executed simultaneously with main()
    // Get messages to get last message number. Discard the initial Messages

    while (1) {
        // Get messages after last message number received.
        char response[MAX_RESPONSE];
        sendCommand(host, port, "GET-MESSAGES", user, password, "0 room", response);
        parseMessages(response);
        int i;
        for (i = 0; i < numMessages; i++) {
            printf("%s\n", messages[i]);
        }
        // Print messages

        // Sleep for ten seconds
        usleep(2*1000*1000);
    }
}
Example #2
0
void NodeJS::update() {
    
    
    // If ping hasn't returned, reconnect it.
    if((ofGetElapsedTimeMillis() - pingResponseTime) > pingResponseTimeout) {
        //disconnect client, then reconnect.
        ofLogVerbose() << "* FORCE CLOSE CONNECTION.";
        client.close();
        pingResponseTime = ofGetElapsedTimeMillis();
    }
    
    
    // check if we're connected.
    if(client.isConnected()) {
        
        // check for new data.
        string data = client.receiveRaw();
        if( client.getNumReceivedBytes() > -1) {
            // parse message.
            parseMessages(data);
        }
        
    } else {
        
        
        ofLogVerbose() << "* DELAY BEFORE RECONNECT";
        
        // attempt reconnect.
        deltaTime = ofGetElapsedTimeMillis() - connectTime;
        
        if( deltaTime > CONNECT_TIMEOUT) {
            ofLogVerbose() << "* RECONNECTING";
            //LOVEUtils::logWithTimeStamp("Attempting reconnect.");
            //client.close();
            client.setup(nodeIP, nodePort);
			connectTime = ofGetElapsedTimeMillis();
		}
        
    }
    
}
Example #3
0
void update()            
{
	static float t=0;
	t += 0.02f;
	frame++;

    if ( ! loaded ) return;
    
	PROFILE;
	w32::Lock lock( &(critSec) );

	for ( size_t i=0; i<numItems(); i++ )
	{
        Item * it = getItem(i);
		it->updateFrame( t, frame );
	}

//	doAudioDSound();

    parseMessages();
    
    if ( ! numItems() )
    {
        return;
    }
    
	updateConnections( matchRadius );

	if ( frame % 100 == 0 )
	{
		printf( "items(%d) alive(%d) ", numItems(), tuio.nAlive );
		for ( int i=0; i<tuio.nAlive; i++ )
		{
			printf( " %d ", tuio.alive[i] );
		}
		printf( "\n" );
	}
}
Example #4
0
// Parse routine BLR.
void Routine::parseBlr(thread_db* tdbb, CompilerScratch* csb, bid* blob_id)
{
	Jrd::Attachment* attachment = tdbb->getAttachment();

	UCharBuffer tmp;

	if (blob_id)
	{
		blb* blob = blb::open(tdbb, attachment->getSysTransaction(), blob_id);
		ULONG length = blob->blb_length + 10;
		UCHAR* temp = tmp.getBuffer(length);
		length = blob->BLB_get_data(tdbb, temp, length);
		tmp.resize(length);
	}

	parseMessages(tdbb, csb, BlrReader(tmp.begin(), (unsigned) tmp.getCount()));

	JrdStatement* statement = getStatement();
	PAR_blr(tdbb, NULL, tmp.begin(), (ULONG) tmp.getCount(), NULL, &csb, &statement, false, 0);
	setStatement(statement);

	if (!blob_id)
		setImplemented(false);
}
Example #5
0
//NOTE: UDP will drop anything that does not fit in the buffer size! so the buffer size must be the maximum
//possible size that this program will send a packet!
void UDP_Socket::receivePackets()
{
	int i = 0;
	while (true)
	{
		int protocolHandler = 0;
#if PLATFORM == WINDOWS
		int receiveLength = sizeof(receive);
#else
		unsigned int receiveLength = sizeof(receive);
#endif
		int bytes = recvfrom(socketHandle, messageBuffer, MAX_PACKET_SIZE, 0, (sockaddr*)&receive, &receiveLength);
		if (bytes <= 0 || (protocolHandler = checkProtocol()) == 0)
			break;
		messageBuffer[bytes] = '\0';
		sendACK();
		//TODO: Save these!
		unsigned int receiveAddress = ntohl(receive.sin_addr.s_addr);
		unsigned int receivePort = ntohs(receive.sin_port);
		parseMessages(messageBuffer, i, bytes);
		i++;
	}
	sortMessageBuffer();
}
/** 
The main state machine for the application.
Never exits.
*/
void stateMachine()
{
 //   while (1)
  //  {
        if (zigbeeNetworkStatus == NWK_ONLINE)
        {
            if(moduleHasMessageWaiting())      //wait until SRDY goes low indicating a message has been received. 
                stateFlags |= STATE_FLAG_MESSAGE_WAITING;
        }
        
        switch (state)
        {
        case STATE_IDLE:
            {
                if (stateFlags & STATE_FLAG_MESSAGE_WAITING & coordinator_on)    // If there is a message waiting...
                {
                    parseMessages();                            // ... then display it
                    trackingStateMachine(current_router_index);
                    stateFlags &= ~STATE_FLAG_MESSAGE_WAITING;
                }
                
                if (stateFlags & STATE_FLAG_BUTTON_PRESSED)     // If ISR set this flag...
                {
                    if (debounceButton(ANY_BUTTON))             // ...then debounce it
                    {
                        processButtonPress();                   // ...and process it
                    }
                    if (debounceButtonHold(ANY_BUTTON))
                    {
                        processButtonHold();
                    }
                    stateFlags &= ~STATE_FLAG_BUTTON_PRESSED;
                }
                
                /* Other flags (for different messages or events) can be added here */
                break;
            }
            
        case STATE_MODULE_STARTUP:              // Start the Zigbee Module on the network
            {
#define MODULE_START_DELAY_IF_FAIL_MS 5000
                moduleResult_t result;
                struct moduleConfiguration defaultConfiguration = DEFAULT_MODULE_CONFIGURATION_COORDINATOR;
                
                /* Uncomment below to restrict the device to a specific PANID
                defaultConfiguration.panId = 0x1234;
                */
                
                /* Below is an example of how to restrict the device to only one channel:
                defaultConfiguration.channelMask = CHANNEL_MASK_17;
                printf("DEMO - USING CUSTOM CHANNEL 17\r\n");
                */
                
                while ((result = startModule(&defaultConfiguration, GENERIC_APPLICATION_CONFIGURATION)) != MODULE_SUCCESS)
                {
                    printf("FAILED. Error Code 0x%02X. Retrying...\r\n", result);
                    delayMs(MODULE_START_DELAY_IF_FAIL_MS);
                }
                //printf("Success\r\n");
                zigbeeNetworkStatus = NWK_ONLINE;
                
                state = STATE_DISPLAY_NETWORK_INFORMATION;
                break;
            }
        case STATE_DISPLAY_NETWORK_INFORMATION:
            {
                printf("~ni~");
                /* On network, display info about this network */
                displayNetworkConfigurationParameters();
                displayDeviceInformation();
                if (sysGpio(GPIO_SET_DIRECTION, ALL_GPIO_PINS) != MODULE_SUCCESS)   //Set module GPIOs as output
                {
                    printf("ERROR\r\n");
                }
                /*
                printf("Press button to change which received value is displayed on RGB LED. D6 & D5 will indicate mode:\r\n");
                printf("    None = None\r\n");
                printf("    Yellow (D9) = IR Temp Sensor\r\n");
                printf("    Red (D8) = Color Sensor\r\n");
                */
                printf("Displaying Messages Received\r\n");
                setModuleLeds(RGB_LED_DISPLAY_MODE_NONE);
                
                /* Now the network is running - wait for any received messages from the ZM */
#ifdef VERBOSE_MESSAGE_DISPLAY    
                printAfIncomingMsgHeaderNames();
#endif                
                state = STATE_IDLE;
                break;
            }
            
        default:     //should never happen
            {
                printf("UNKNOWN STATE\r\n");
                state = STATE_MODULE_STARTUP;
            }
            break;
        }
 //   } 
}    
/** 
The main state machine for the application.
Never exits.
*/
void stateMachine()
{
    while (1)
    {
        if (zigbeeNetworkStatus == NWK_ONLINE)
        {
            if(moduleHasMessageWaiting())      //wait until SRDY goes low indicating a message has been received. 
                stateFlags |= STATE_FLAG_MESSAGE_WAITING;
        }
        
        switch (state)
        {
        case STATE_IDLE:
            {
                if (stateFlags & STATE_FLAG_MESSAGE_WAITING)    // If there is a message waiting...
                {
                    parseMessages();                            // ... then display it
                    stateFlags &= ~STATE_FLAG_MESSAGE_WAITING;
                }
                
                if (stateFlags & STATE_FLAG_BUTTON_PRESSED)     // If ISR set this flag...
                {
                    state = STATE_BUTTON_PRESSED;
                    stateFlags &= ~STATE_FLAG_BUTTON_PRESSED;
                }                
                /* Other flags (for different messages or events) can be added here */
            }
            break;            
            
        case STATE_BUTTON_PRESSED:
            {
                rgbLedColor++;
                if (rgbLedColor > RGB_LED_COLOR_MAX)
                {
                    rgbLedColor = 0; 
                }
                printf("Setting Color to %s (%u)\r\n", getRgbLedColorName(rgbLedColor), rgbLedColor);
                switch (rgbLedColor)
                {
                case RGB_LED_COLOR_WHITE: red=RGB_LED_MAX; blue=RGB_LED_MAX; green=RGB_LED_MAX; break;
                case RGB_LED_COLOR_RED: red=RGB_LED_MAX; blue=0; green=0; break;
                case RGB_LED_COLOR_VIOLET: red=RGB_LED_MAX; blue=RGB_LED_MAX; green=0; break;
                case RGB_LED_COLOR_BLUE: red=0; blue=RGB_LED_MAX; green=0; break;        
                case RGB_LED_COLOR_CYAN: red=0; blue=RGB_LED_MAX; green=RGB_LED_MAX; break;
                case RGB_LED_COLOR_GREEN: red=0; blue=0; green=RGB_LED_MAX; break;
                case RGB_LED_COLOR_YELLOW: red=RGB_LED_MAX; blue=0; green=RGB_LED_MAX; break;
                default: red=RGB_LED_MAX; blue=RGB_LED_MAX; green=RGB_LED_MAX; break;
                }
                halRgbSetLeds(red, blue, green);
                state = STATE_IDLE;                
            }
            break;
            
        case STATE_MODULE_STARTUP:              // Start the Zigbee Module on the network
            {
#define MODULE_START_DELAY_IF_FAIL_MS 5000
                moduleResult_t result;
                struct moduleConfiguration defaultConfiguration = DEFAULT_MODULE_CONFIGURATION_COORDINATOR;
                
                /* Change this if you wish to use a custom PAN */
                defaultConfiguration.panId = ANY_PAN;
                
                /*Example of how to use a custom channel:
                printf("DEMO - USING CUSTOM CHANNEL 25\r\n");
                defaultConfiguration.channelMask = CHANNEL_MASK_25; */
                
                /* Change this below to be your operating region - MODULE_REGION_NORTH_AMERICA or MODULE_REGION_EUROPE */
#define OPERATING_REGION    (MODULE_REGION_NORTH_AMERICA) // or MODULE_REGION_EUROPE
                
                while ((result = expressStartModule(&defaultConfiguration, GENERIC_APPLICATION_CONFIGURATION, OPERATING_REGION)) != MODULE_SUCCESS)
                {
                    SET_NETWORK_FAILURE_LED_ON();          // Turn on the LED to show failure
                    printf("FAILED. Error Code 0x%02X. Retrying...\r\n", result);
                    delayMs(MODULE_START_DELAY_IF_FAIL_MS/2);                    
                    SET_NETWORK_FAILURE_LED_OFF();
                    delayMs(MODULE_START_DELAY_IF_FAIL_MS/2);
                }                
                INIT_BOOSTER_PACK_LEDS();                
                SET_NETWORK_LED_ON();
                SET_NETWORK_FAILURE_LED_OFF();
                halRgbSetLeds(RGB_LED_MAX, RGB_LED_MAX, RGB_LED_MAX);
                printf("Module Start Complete\r\n"); 
                SET_NETWORK_STATUS_ONLINE();
                
                state = STATE_DISPLAY_NETWORK_INFORMATION;
            }
            break;            
            
        case STATE_DISPLAY_NETWORK_INFORMATION:
            {
                printf("~ni~");
                /* On network, display info about this network */
                displayNetworkConfigurationParameters();
                displayDeviceInformation();
                
                printf("Press button to change color configuration\r\n");                
                printf("Displaying Messages Received\r\n");
                
                /* Now the network is running - wait for any received messages from the ZM */
#ifdef VERBOSE_MESSAGE_DISPLAY    
                printAfIncomingMsgHeaderNames();
#endif                
                state = STATE_IDLE;
            }
            break;            
            
        default:     //should never happen
            {
                printf("UNKNOWN STATE\r\n");
                state = STATE_MODULE_STARTUP;
            }
            break;
        }
    } 
}