void NMEAEndpoint::add_midpoint_cmd(Command_ptr command){
    try{
        NMEAMidpoint_ptr midpoint = NMEAMidpoint::factory(getConnectedTo(),command->getArguments());
        unregisterEndpoint();
        setConnectedTo(midpoint);
        registerEndpoint();
        
        command->answer("Midpoint created and connected.", shared_from_this());
    }
    catch(std::exception& e){
        command->answer(Answer::WRONG_ARGS, "Cannot understand "+command->getArguments()+" for command "+command->getCommand()+". Not a midpoint class\n", shared_from_this());
    }
}
Example #2
0
void StringVectorCallback::execute(Command_ptr command, CommandEndpoint_ptr instance){
    std::ostringstream oss;
    if(command->getArguments().length()==0){
        oss << speakingName << " is ";
        if(vector->size()==0){
            oss << "empty!";
        }
        else{
            for(std::vector<std::string>::iterator stringId = vector->begin(); stringId != vector->end();){
                oss << *stringId;
                ++stringId;
                if(stringId != vector->end()){
                    oss << ", ";
                }
            }
        }
        oss << std::endl;
        command->answer(oss.str(), instance);
    }
    else if(unsigned int i = command->getArguments().find("add")!=std::string::npos){
        if(writeable){
            i+=std::string("add").length();
            std::string ids=command->getArguments().substr(i, std::string::npos);
            std::stringstream iss(ids);
            std::string id;
            while(std::getline(iss, id, ',')) {
                trim(id);
                if(id.length()>0 && std::find(vector->begin(), vector->end(), id) == vector->end()){
                    vector->push_back(id);
                }
            }
            oss << speakingName << " now is ";
            if(vector->size()==0){
                oss << "empty!";
            }
            else{
                for(std::vector<std::string>::iterator stringId = vector->begin(); stringId != vector->end();){
                    oss << *stringId;
                    ++stringId;
                    if(stringId != vector->end()){
                        oss << ", ";
                    }
                }
            }
            oss << std::endl;
            command->answer(oss.str(), instance);
        }
        else{
            oss << speakingName << " is read-only and cannot be changed!" << std::endl;
            command->answer(oss.str(), instance);
        }
    }
    else if(unsigned int i = command->getArguments().find("set")!=std::string::npos){
        if(writeable){
            vector->erase(vector->begin(),vector->end());
            i+=std::string("set").length();
            std::string ids=command->getArguments().substr(i, std::string::npos);
            std::stringstream iss(ids);
            std::string id;
            while(std::getline(iss, id, ',')) {
                trim(id);
                if(id.length()>0 && std::find(vector->begin(), vector->end(), id) == vector->end()){
                    vector->push_back(id);
                }
            }
            oss << speakingName << " now is ";
            if(vector->size()==0){
                oss << "empty!";
            }
            else{
                for(std::vector<std::string>::iterator stringId = vector->begin(); stringId != vector->end();){
                    oss << *stringId;
                    ++stringId;
                    if(stringId != vector->end()){
                        oss << ", ";
                    }
                }
            }
            oss << std::endl;
            command->answer(oss.str(), instance);
        }
        else{
            oss << speakingName << " is read-only and cannot be changed!" << std::endl;
            command->answer(oss.str(), instance);
        }
    }
    else if(unsigned int i = command->getArguments().find("remove")!=std::string::npos){
        if(writeable){
            i+=std::string("remove").length();
            std::string ids=command->getArguments().substr(i, std::string::npos);
            std::stringstream iss(ids);
            std::string id;
            while(std::getline(iss, id, ',')) {
                trim(id);
                if(id.length()>0){
                    std::vector<std::string>::iterator it = std::find(vector->begin(), vector->end(), id);
                    if(it != vector->end()){
                        vector->erase(it);
                    }
                }
            }
            oss << speakingName << " now is ";
            if(vector->size()==0){
                oss << "empty!";
            }
            else{
                for(std::vector<std::string>::iterator stringId = vector->begin(); stringId != vector->end();){
                    oss << *stringId;
                    ++stringId;
                    if(stringId != vector->end()){
                        oss << ", ";
                    }
                }
            }
            oss << std::endl;
            command->answer(oss.str(), instance);
        }
        else{
            oss << speakingName << " is read-only and cannot be changed!" << std::endl;
            command->answer(oss.str(), instance);
        }
    }
    else{
        command->answer(Answer::WRONG_ARGS, "Cannot understand "+command->getArguments()+" for command "+command->getCommand()+". Not one of add, set or remove \n", instance);
    }
}