bool ofxOscReceiver::getParameter(ofAbstractParameter & parameter){ ofxOscMessage msg; if ( messages.size() == 0 ) return false; while(hasWaitingMessages()){ ofAbstractParameter * p = ¶meter; getNextMessage(&msg); vector<string> address = ofSplitString(msg.getAddress(),"/",true); for(int i=0;i<address.size();i++){ if(address[i]==p->getEscapedName()){ if(p->type()==typeid(ofParameterGroup).name()){ if(address.size()>i+1 && static_cast<ofParameterGroup*>(p)->contains(address[i+1])){ p = &static_cast<ofParameterGroup*>(p)->get(address[i+1]); } }else if(p->type()==typeid(ofParameter<int>).name() && msg.getArgType(0)==OFXOSC_TYPE_INT32){ p->cast<int>() = msg.getArgAsInt32(0); }else if(p->type()==typeid(ofParameter<float>).name() && msg.getArgType(0)==OFXOSC_TYPE_FLOAT){ ofParameter<float> * param = dynamic_cast<ofParameter<float>* >(p); float length = param->getMax() - param->getMin(); p->cast<float>() = ( msg.getArgAsFloat(0) * length ) + param->getMin(); }else if(p->type()==typeid(ofParameter<bool>).name() && msg.getArgType(0)==OFXOSC_TYPE_INT32){ p->cast<bool>() = msg.getArgAsInt32(0); }else if(msg.getArgType(0)==OFXOSC_TYPE_STRING){ p->fromString(msg.getArgAsString(0)); } } } } return true; }
void WaypointController::spin(const ros::TimerEvent & e) { if(hasNextWaypoint() && cur_wp_ == 0 && waypointPublisher_.getNumSubscribers() > 0) { ROS_INFO("Publishing first waypoint"); waypointPublisher_.publish(getNextMessage()); } }
void WaypointController::handleCurrentway(asctec_msgs::CurrentWay msg) { ROS_INFO("CurrentWay navigation_status: 0x%02x", msg.navigation_status); if(msg.navigation_status == 0x07 && hasNextWaypoint()) { ROS_INFO("Publishing next waypoint"); waypointPublisher_.publish (getNextMessage()); } }
void ofxDuration::update(ofEventArgs& args){ while(hasWaitingMessages()){ ofxOscMessage m; getNextMessage(&m); //duration will send an info package whenever play begins //we use this to set up all the track info if(m.getAddress() == "/duration/info"){ parseInfoMessage(m); } else { parseTrackMessage(m); } } }
void Entity::updateMessage(long elapsed){ MessageWindow& message_window = getMessageWindow(); if (!message_window.hasMessageTimeExpired()){//If message time exists, decrease its timer value message_window.setMessageTimer(message_window.getMessageTimer() - elapsed); } else{ getNextMessage(); } float x = getPosition().x; float y = (getPosition().y - sprite.getLocalBounds().height - 32); current_message.update(x, y, elapsed); }
void Network::dispatchMessages() { while (messageReady()) { MessageIn msg = getNextMessage(); MessageHandlerIterator iter = mMessageHandlers.find(msg.getId()); if (msg.getLength() == 0) logger->error("Zero length packet received. Exiting."); if (iter != mMessageHandlers.end()) { iter->second->handleMessage(msg); } else { logger->log("Unhandled packet: %x", msg.getId()); } skip(msg.getLength()); } }
int main( int inNumArgs, char **inArgs ) { if( inNumArgs != 5 ) { usage(); } char *address = inArgs[1]; int port = 8005; sscanf( inArgs[2], "%d", &port ); char *emailPrefix = inArgs[3]; int numClients = 1; sscanf( inArgs[4], "%d", &numClients ); Client *connections = new Client[ numClients ]; // first, connect each int numConnected = 0; for( int i=0; i<numClients; i++ ) { connections[i].i = i; connections[i].id = -1; connections[i].skipCompressedData = 0; connections[i].pendingCMData = false; connections[i].moving = false; connections[i].dead = false; connections[i].disconnected = false; HostAddress a( stringDuplicate( address ), port ); char timeout = false; connections[i].sock = SocketClient::connectToServer( &a, 5000, &timeout ); if( timeout ) { printf( "Client %d timed out when trying to connect\n", i ); delete connections[i].sock; connections[i].sock = NULL; continue; } if( connections[i].sock != NULL ) { numConnected ++; char *email = autoSprintf( "*****@*****.**", emailPrefix, i ); printf( "Client %d connected, logging in with email %s\n", i, email ); char *message = autoSprintf( "LOGIN %s aaaa aaaa#", email ); connections[i].sock->send( (unsigned char*)message, strlen( message ), true, false ); delete [] message; delete [] email; } else { printf( "Client %d failed to connect\n", i ); } } // process messages while( numConnected > 0 ) { numConnected = 0; for( int i=0; i<numClients; i++ ) { if( connections[i].sock == NULL ) { continue; } numConnected ++; if( connections[i].id == -1 ) { // still waiting for first PU if( connections[i].disconnected ) { printf( "Client %d lost connection\n", i ); delete connections[i].sock; connections[i].sock = NULL; continue; } char *message = getNextMessage( &( connections[i] ) ); if( message != NULL ) { //printf( "Client %d got message:\n%s\n\n", i, message ); if( strstr( message, "MC" ) == message ) { //printf( "Client %d got first map chunk\n", i ); int sizeX, sizeY, x, y, binarySize, compSize; sscanf( message, "MC\n%d %d %d %d\n%d %d\n", &sizeX, &sizeY, &x, &y, &binarySize, &compSize ); connections[i].skipCompressedData = compSize; } else if( strstr( message, "PU" ) == message ) { // PU message! // last line describes us int numLines; char **lines = split( message, "\n", &numLines ); if( numLines > 2 ) { // first line is about us parsePlayerUpdateMessage( &( connections[i] ), lines[ numLines - 2 ] ); } printf( "Client %d got first player update, " "pid = %d, pos = %d,%d\n", i, connections[i].id, connections[i].x, connections[i].y ); for( int p=0; p<numLines; p++ ) { delete [] lines[p]; } delete [] lines; } delete [] message; } } else { // player is live if( connections[i].dead ) { printf( "Client %d died, closing connection\n", i ); delete connections[i].sock; connections[i].sock = NULL; continue; } if( connections[i].disconnected ) { printf( "Client %d lost connection\n", i ); delete connections[i].sock; connections[i].sock = NULL; continue; } if( !connections[i].moving ) { // make a move connections[i].moving = true; //printf( "Client %d starting move\n", i ); int xDelt = 0; int yDelt = 0; while( xDelt == 0 && yDelt == 0 ) { xDelt = randSource.getRandomBoundedInt( -1, 1 ); yDelt = randSource.getRandomBoundedInt( -1, 1 ); } char *message = autoSprintf( "MOVE %d %d %d %d#", connections[i].x, connections[i].y, xDelt, yDelt ); connections[i].sock->send( (unsigned char*)message, strlen( message ), true, false ); delete [] message; } char *message = getNextMessage( &( connections[i] ) ); if( message != NULL ) { if( strstr( message, "MC" ) == message ) { int sizeX, sizeY, x, y, binarySize, compSize; sscanf( message, "MC\n%d %d %d %d\n%d %d\n", &sizeX, &sizeY, &x, &y, &binarySize, &compSize ); connections[i].skipCompressedData = compSize; } else if( strstr( message, "PU" ) == message ) { // PU message! int numLines; char **lines = split( message, "\n", &numLines ); for( int p=1; p<numLines-1; p++ ) { parsePlayerUpdateMessage( &( connections[i] ), lines[p] ); } for( int p=0; p<numLines; p++ ) { delete [] lines[p]; } delete [] lines; } delete [] message; } } } } printf( "No more clients connected\n" ); for( int i=0; i<numClients; i++ ) { if( connections[i].sock != NULL ) { delete connections[i].sock; } } delete [] connections; return 1; }
//-------------------------------------------------------------- void oscReceiver::update() { if (hasWaitingMessages()) { while(getNextMessage(&m_oscMessage)) { // int indexArg = 0; ofLog() << "oscReceiver::update() - received " << m_oscMessage.getAddress(); if (m_oscMessage.getAddress() == OSC_MOD_SET_WEIGHT) { apparelModManager* pModManager = GLOBALS->getModManager(); if (pModManager) { // Name of the mod string instanceName = m_oscMessage.getArgAsString(0); // Get Mod instance apparelMod* pMod = pModManager->getMod(instanceName); // Exists ?? if (pMod && pMod->m_isWeightManual) { pMod->setWeight( m_oscMessage.getArgAsFloat(1) ); } } } else if (m_oscMessage.getAddress() == OSC_MOD_SET_PARAMETER) { apparelModManager* pModManager = GLOBALS->getModManager(); if (pModManager) { // Name of the mod string instanceName = m_oscMessage.getArgAsString(0); // Get Mod instance apparelMod* pMod = pModManager->getMod(instanceName); // Exists ?? if (pMod) { if (m_oscMessage.getNumArgs() == 3) { string parameterName = m_oscMessage.getArgAsString(1); ofAbstractParameter& modParam = pMod->getParameter(parameterName); setParameterValue(modParam, m_oscMessage, 2); //OFAPPLOG->println("instanceName="+instanceName+"/parameterName="+parameterName); // OFAPPLOG->println("value for "+instanceName+"/"+parameterName+"="+modParam.toString()); } // else if (m_oscMessage.getNumArgs() == 4) { string groupName = m_oscMessage.getArgAsString(1); string parameterName = m_oscMessage.getArgAsString(2); ofAbstractParameter& modGroupParam = pMod->getParameter(groupName); if (modGroupParam.type()==typeid(ofParameterGroup).name()) { setParameterValue(modGroupParam, m_oscMessage, 3); } } } } } } } }
bool ofxOscReceiver::getNextMessage( ofxOscMessage * message ) { return getNextMessage(*message); }
//-------------------------------------------------------------- JSBool ofxJSOscReceiver::getNextMessage(jsval* retval){ ofxJSOscMessage* mjs = new ofxJSOscMessage(); // will be garbage collected ...? bool is = m_oscReceiver.getNextMessage(&mjs->m_oscMessage); *retval = is ? OBJECT_TO_JSVAL(mjs->getJSObject(ofxJSGetContext())) : JSVAL_NULL; return JS_TRUE; }
Message *MessageQueue::getNextMessage() { // Arbitrary - TODO manage differently in conjunction // with more sane thread controls once they're done. return getNextMessage(5); }
void ofxTSPSReceiver::update( ofEventArgs &e ){ if(hasWaitingMessages()){ // check for waiting messages while( hasWaitingMessages() ){ // get the next message ofxOscMessage m; getNextMessage( &m ); if(m.getAddress() == "TSPS/scene/") { scene.idleTime = m.getArgAsFloat(0); scene.percentCovered = m.getArgAsFloat(1); } if (m.getAddress() == "TSPS/personEntered/" || m.getAddress() == "TSPS/personMoved/" || m.getAddress() == "TSPS/personUpdated/" || m.getAddress() == "TSPS/personWillLeave/"){ int id = m.getArgAsInt32(0); bool personIsNew = false; ofxTSPSPerson* person = personWithID( id ); if(person == NULL){ person = new ofxTSPSPerson(id, trackedPeople.size()); trackedPeople.push_back(person); personIsNew = true; } person->update( &m ); if(eventListener != NULL){ if (m.getAddress() == "TSPS/personEntered/" || personIsNew){ eventListener->personEntered(person, &scene); } else if (m.getAddress() == "TSPS/personMoved/"){ eventListener->personMoved(person, &scene); } else if (m.getAddress() == "TSPS/personUpdated/"){ eventListener->personUpdated(person, &scene); } else if (m.getAddress() == "TSPS/personWillLeave/"){ eventListener->personWillLeave(person, &scene); } } if(m.getAddress() == "TSPS/personWillLeave/"){ for (int i = trackedPeople.size() - 1; i >= 0; i--){ if (trackedPeople[i]->pid == person->pid){ trackedPeople.erase(trackedPeople.begin() + i); break; } } delete person; } } } } //clear orphaned blobs for(int i = trackedPeople.size()-1; i >= 0; i--) { if(ofGetElapsedTimeMillis()-trackedPeople[i]->updatedAt > personTimeout) { trackedPeople.erase(trackedPeople.begin() + i); eventListener->personWillLeave(trackedPeople[i], &scene); } } }
//-------------------------------------------------------------- void oscReceiver::update() { DeviceManager* pDeviceManager = Globals::instance()->mp_deviceManager; if (hasWaitingMessages()) { while(getNextMessage(&m_oscMessage)) { int indexArg = 0; if (m_oscMessage.getAddress() == OSC_ADDRESS_SET_DEVICE_PROP) { Device* pDevice = pDeviceManager->getDeviceById( m_oscMessage.getArgAsString(indexArg++) ); if (pDevice) { // Property string propName = m_oscMessage.getArgAsString(indexArg++); if (propName == "volMax") { pDevice->setSoundInputVolumeMaxOSC( m_oscMessage.getArgAsFloat(indexArg) ); } else if (propName == "volHistorySize") { pDevice->setSoundInputVolHistorySizeOSC( m_oscMessage.getArgAsInt32(indexArg) ); } else if (propName == "volHistoryTh") { pDevice->setSoundInputVolHistoryThOSC( m_oscMessage.getArgAsFloat(indexArg) ); } else if (propName == "enableStandbyMode") { pDevice->setEnableStandbyModeOSC( m_oscMessage.getArgAsInt32(indexArg)==1 ? true : false ); } else if (propName == "timeStandby") { //printf("timeStandby = %.3f - ", m_oscMessage.getArgAsFloat(indexArg)); pDevice->setTimeStandbyOSC( m_oscMessage.getArgAsFloat(indexArg) ); } else if (propName == "sampleVolStandby") { pDevice->setSampleVolumeStandbyOSC( m_oscMessage.getArgAsFloat(indexArg) ); } else if (propName == "nbLEDsStandby") { // pDevice->setNbLEDsStandbyOSC( m_oscMessage.getArgAsInt32(indexArg) ); } else if (propName == "speedStandby") { // pDevice->setSpeedStandbyOSC( m_oscMessage.getArgAsFloat(indexArg) ); } } } else if (m_oscMessage.getAddress() == OSC_ADDRESS_SEND_PACKETS) { if (pDeviceManager) { string deviceId = m_oscMessage.getArgAsString(indexArg++); Device* pDevice = pDeviceManager->getDeviceById( deviceId ); // Not found, create the device now if (pDevice == 0) { // TODO : type ? -> factory if (Globals::instance()->mp_app->isSimulation == false) { int nbLEDs = m_oscMessage.getNumArgs() - indexArg; // WARNING with this printf("> New Device found ! id=%s, nbLEDs=%d\n", deviceId.c_str(), nbLEDs); DeviceEcho* pDeviceEcho = new DeviceEcho(deviceId, nbLEDs, 4.0f/float(nbLEDs-1)); pDeviceEcho->setupSenderOSC(m_oscMessage.getRemoteIp(), 1235); pDeviceEcho->loadXML("Config/devices/"); // Add device to list pDeviceManager->addDevice(pDeviceEcho); // TEMP / TODO : attach to surface main if (Globals::instance()->mp_app->mp_surfaceMain) Globals::instance()->mp_app->mp_surfaceMain->addDevice(pDeviceEcho); // Create a node in visualisation scene Globals::instance()->mp_app->createDeviceNodeInScene(pDeviceEcho); // Update interface //Globals::instance()->mp_app->guiMarkUpdateLayout(); Globals::instance()->mp_app->guiUpdateListDevices(); Globals::instance()->mp_app->selectDevice(pDeviceEcho->m_id); // TODO : Send configuration parameters to it (if any) } } // Decode new message if (pDevice) { pDevice->onReceivePacketBegin(); int nbPackets = m_oscMessage.getNumArgs() - indexArg; // TODO : Be careful with this for (int i=0;i<nbPackets;i++) { m_packetTemp.m_volume = m_oscMessage.getArgAsFloat(indexArg++); // TODO : Other arguments to decode here // Send to device pDevice->onReceivePacket(&m_packetTemp); } pDevice->onReceivePacketEnd(); // Get the surface of device and update animation Surface* pSurface = Globals::instance()->mp_app->getSurfaceForDevice(pDevice); if (pSurface){ pSurface->onNewPacket( pDevice->getLastPacket(), pDevice->m_id ); } } } } // New Device plugged // printf("osc %s - %d arguments\n", m_oscMessage.getAddress().c_str(), m_oscMessage.getNumArgs()); // New Device event } } }