Example #1
0
void OSCHandler::ProcessMessage(const osc::ReceivedMessage& m, const IpEndpointName& /*remoteEndpoint*/)
{
    const String stripWildcard = OSCPrefix + "strip/*";

    try
    {
        String msgPattern = m.AddressPattern();

        if (msgPattern.equalsIgnoreCase(OSCPrefix + "press"))
        {
            // we need three arguments for button presses
            const int numArgs = m.ArgumentCount();
            if (numArgs != 3) throw osc::Exception();

            osc::ReceivedMessageArgumentStream args = m.ArgumentStream();

            // unpack the monome button, row and state (button up or down)
            osc::int32 row, col, state;
            args >> row >> col >> state >> osc::EndMessage;
            buttonPressCallback(row, col, state == 1);
        }
        else if (msgPattern.matchesWildcard(stripWildcard, false))
        {
            // strip off the /mlrvst/strip/ part of the message
            msgPattern = msgPattern.substring(stripWildcard.length() - 1);

            // and extract the SampleStrip rowID from the message
            const String rowIDStr = msgPattern.upToFirstOccurrenceOf("/", false, false);

            const int stripID = rowIDStr.getIntValue();

            handleStripMessage(stripID, m);
        }
    }
bool StandardRequestHandler::operator()(const std::string& request_path, const std::string& full_request_path, const osc::ReceivedMessage& m)
{
    try
    {
        std::string path = osgDB::getFilePath(full_request_path);
        std::string last_elem = osgDB::getSimpleFileName(full_request_path);

        osg::ref_ptr<osgGA::GUIEventAdapter> ea = getDevice()->getOrCreateUserDataEvent();
        osg::UserDataContainer* udc = ea->getOrCreateUserDataContainer();


        ea->setName(_treatFirstArgumentAsValueName ? full_request_path : path);
        udc->setName(ea->getName());

        if (m.ArgumentCount() == 0) {
            return true;
        }

        // if we have only one argument, get it and save it to the udc
        else if (m.ArgumentCount() == 1)
        {
            addArgumentToUdc(udc, last_elem, m.ArgumentsBegin());
            return true;
        }
        else
        {
            unsigned int i(0);
            osc::ReceivedMessageArgumentIterator start = m.ArgumentsBegin();
            if ((_treatFirstArgumentAsValueName) && (start->TypeTag() == osc::STRING_TYPE_TAG))
            {
                last_elem = start->AsString();
                ++start;
                // if we hav only 2 arguments, then save the value and return
                if (m.ArgumentCount() == 2)
                {
                    addArgumentToUdc(udc, last_elem, start);
                    return true;
                }
            }
            std::vector<float> float_vec;
            std::vector<double> double_vec;
            bool mixed_arguments(false);
            for(osc::ReceivedMessageArgumentIterator itr = start; itr != m.ArgumentsEnd(); ++itr, ++i)
            {
                if(itr->TypeTag() == osc::FLOAT_TYPE_TAG)
                {
                    float_vec.push_back(itr->AsFloat());
                }
                else if(itr->TypeTag() == osc::DOUBLE_TYPE_TAG)
                {
                    double_vec.push_back(itr->AsDouble());
                }
                else if(itr->TypeTag() == osc::INT32_TYPE_TAG)
                {
                    float_vec.push_back(itr->AsInt32());
                }
                else {
                    mixed_arguments = true;
                    break;
                }
            }
            if (!mixed_arguments)
            {
                unsigned int sum = float_vec.size() + double_vec.size();
                if (sum == float_vec.size())
                {
                    if (addNativeTypeFromVector(udc, last_elem, float_vec))
                        return true;
                }
                else if (sum == double_vec.size())
                {
                    if (addNativeTypeFromVector(udc, last_elem, double_vec))
                        return true;
                }
            }

            for(osc::ReceivedMessageArgumentIterator itr = start; itr != m.ArgumentsEnd(); ++itr, ++i)
            {
                std::ostringstream ss;
                ss << last_elem << "_" << i;
                addArgumentToUdc(udc, ss.str(), itr);
            }
        }
        return true;

    }
    catch(osc::Exception& e)
    {
        handleException(e);
        return false;
    }
    return false;
}
void TUIOMsgListener::ProcessMessage(const osc::ReceivedMessage &m, const IpEndpointName &remoteEndpoint) {
    try {
        const char *addr = m.AddressPattern();
        
        std::cout << "TUIOMsgListener: Received message "
            << addr << std::endl;
            
        const int prefixLen = strlen(tuioAddressPrefix);
        
        if (strncmp(addr, tuioAddressPrefix, prefixLen) == 0 && strlen(addr) > prefixLen + 1) {
            TUIOMsg *msg = new TUIOMsg;
            const char *type = (addr + prefixLen + 1);
            osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin();
            const unsigned long numArgs = m.ArgumentCount();

            if (strcmp(type, "2Dobj") == 0) {
                msg->type = kTUIOMsgTypeObj;
            } else if (strcmp(type, "2Dcur") == 0) {
                msg->type = kTUIOMsgTypeCur;
            } else if (strcmp(type, "2Dblb") == 0) {
                msg->type = kTUIOMsgTypeBlb;
            } else {
                std::cerr << "TUIOMsgListener: unknown TUIO message type "
                    << type << std::endl;
                return;
            }
            
            const char *cmd = (arg++)->AsString();

            if (strcmp(cmd, "source") == 0) {
                msg->cmd = kTUIOMsgCmdSrc;
            } else if (strcmp(cmd, "alive") == 0) {
                msg->cmd = kTUIOMsgCmdAlive;
            } else if (strcmp(cmd, "set") == 0) {
                msg->cmd = kTUIOMsgCmdSet;
            } else if (strcmp(cmd, "fseq") == 0) {
                msg->cmd = kTUIOMsgCmdFSeq;
            } else {
                std::cerr << "TUIOMsgListener: unknown TUIO message command "
                    << cmd << std::endl;
                return;
            }
            
            switch (msg->cmd) {
                case kTUIOMsgCmdSrc: {
                    const char *srcAddr = (arg++)->AsString();
                    const int addrLen = strlen(srcAddr + 1);
                    msg->data.source.addr = new char[addrLen];
                    strncpy(msg->data.source.addr, srcAddr, addrLen);
                }
                break;

                case kTUIOMsgCmdAlive: {
                    msg->data.alive.numSessIds = numArgs - 1;
                    if (msg->data.alive.numSessIds > 0) {
                        msg->data.alive.sessIds = new int[msg->data.alive.numSessIds];
                        
                        for (int i = 0; i < msg->data.alive.numSessIds; i++) {
                            msg->data.alive.sessIds[i] = (arg++)->AsInt32();
                        }
                    } else {
                        msg->data.alive.sessIds = NULL;
                    }
                }
                break;
                
                case kTUIOMsgCmdSet: {
                    msg->data.set.sessId = (arg++)->AsInt32();
                
                    switch (msg->type) {
                        case kTUIOMsgTypeObj:
                            msg->data.set.classId = (arg++)->AsInt32();
                            msg->data.set.pos.x = (arg++)->AsFloat();
                            msg->data.set.pos.y = (arg++)->AsFloat();
                            msg->data.set.angle = (arg++)->AsFloat();
                            msg->data.set.size.x = 0.0f;
                            msg->data.set.size.y = 0.0f;
                            msg->data.set.area = 0.0f;
                            msg->data.set.vel.x = (arg++)->AsFloat();
                            msg->data.set.vel.y = (arg++)->AsFloat();
                            msg->data.set.angleVel = (arg++)->AsFloat();
                            msg->data.set.motAccel = (arg++)->AsFloat();
                            msg->data.set.rotAccel = (arg++)->AsFloat();
                        break;
                        
                        case kTUIOMsgTypeCur:
                            msg->data.set.classId = 0;
                            msg->data.set.pos.x = (arg++)->AsFloat();
                            msg->data.set.pos.y = (arg++)->AsFloat();
                            msg->data.set.angle = 0.0f;
                            msg->data.set.size.x = 0.0f;
                            msg->data.set.size.y = 0.0f;
                            msg->data.set.area = 0.0f;
                            msg->data.set.vel.x = (arg++)->AsFloat();
                            msg->data.set.vel.y = (arg++)->AsFloat();
                            msg->data.set.angleVel = 0.0f;
                            msg->data.set.motAccel = (arg++)->AsFloat();
                            msg->data.set.rotAccel = 0.0f;
                        break;
                        
                        case kTUIOMsgTypeBlb:
                            msg->data.set.classId = 0;
                            msg->data.set.pos.x = (arg++)->AsFloat();
                            msg->data.set.pos.y = (arg++)->AsFloat();
                            msg->data.set.angle = (arg++)->AsFloat();
                            msg->data.set.size.x = (arg++)->AsFloat();
                            msg->data.set.size.y = (arg++)->AsFloat();
                            msg->data.set.area = 0.0f;
                            msg->data.set.vel.x = (arg++)->AsFloat();
                            msg->data.set.vel.y = (arg++)->AsFloat();
                            msg->data.set.angleVel = (arg++)->AsFloat();
                            msg->data.set.motAccel = (arg++)->AsFloat();
                            msg->data.set.rotAccel = (arg++)->AsFloat();
                        break;
                    }
                }   
                break;
                
                case kTUIOMsgCmdFSeq: {
                    msg->data.fseq.frameId = (arg++)->AsInt32();
                }
                break;
            }
            
            if (msgCallback) {
                msgCallback(msg);
            }
        } else {
            std::cerr << "TUIOMsgListener: Address prefix " << addr << " did not match to "
                << tuioAddressPrefix << std::endl;
        }
    } catch( osc::Exception& e ){
        // any parsing errors such as unexpected argument types, or
        // missing arguments get thrown as exceptions.
        std::cerr << "TUIOMsgListener: error while parsing message "
            << m.AddressPattern() << ": " << e.what() << "\n";
    }
}