Example #1
0
void UIntCallback::execute(Command_ptr command, CommandEndpoint_ptr instance){
    std::ostringstream ss;
    if(command->getCommand()==name){
        if(command->getArguments().length()==0){
            ss<<speakingName<<" is " << *uint << '\n';
            command->answer(ss.str(), instance);
        }
        else{
            if(writeable){
                try
                {
                    unsigned int tmp_uint = boost::lexical_cast<unsigned int>(command->getArguments());
                    if(tmp_uint >= min && tmp_uint <= max){
                        *uint = tmp_uint;
                        ss<< speakingName << " changed to " <<  *uint << "\n";
                        command->answer(ss.str(), instance);
                    }
                    else{
                        ss << "Cannot set " << speakingName << " to " <<  tmp_uint << ". Must be between "<< min << " and " << max <<"\n";
                        command->answer(Answer::WRONG_ARGS, ss.str(), instance);
                    }
                    
                }
                catch(const boost::bad_lexical_cast&)
                {
                    command->answer(Answer::WRONG_ARGS, "Cannot understand "+command->getArguments()+" for command "+command->getCommand()+". Not a number!\n", instance);
                }
            }
            else{
                ss<<speakingName<< " is not writeable\n";
                command->answer(ss.str(), instance);
            }
        }
    }
}
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());
    }
}
void FileEndpoint::space_left_cmd(Command_ptr command){
    boost::filesystem::path filepath(filename);
    boost::filesystem::space_info space = boost::filesystem::space(filepath);
    std::ostringstream oss;
    oss << (space.available/1024/1024) << "MB left on device\n";
    command->answer(oss.str(), this->shared_from_this());
}
void NMEAEndpoint::print_stats_cmd(Command_ptr command){
    std::ostringstream oss;
    oss << "Statistics for " << getId() << std::endl << "---------------------------------------" << std::endl;
    if(stats_enabled){
        boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
        if(out_stat_list.size()>0){
            boost::posix_time::ptime out_oldest = out_stat_list.front().first;
            double out_time_passed = (((double)(now-out_oldest).total_microseconds())/1000000);
            double out_msg_per_sec = out_stat_list.size()/out_time_passed;
            double out_byte_per_sec = out_total_size/out_time_passed;
            oss << "output messages per second:" << '\t' << out_msg_per_sec << std::endl;
            oss << "output bytes per second:" << '\t' << out_byte_per_sec << std::endl;
            oss << "output messages total:" << '\t' << out_msg_from_start << std::endl;
            oss << "output MB total:" << '\t' << out_byte_from_start/1024/1024 << std::endl;
        }
        else{
            oss << "No messages sent yet" << std::endl;
        }
        if(in_stat_list.size()>0){
            boost::posix_time::ptime in_oldest = in_stat_list.front().first;
            double in_time_passed = (((double)(now-in_oldest).total_microseconds())/1000000);
            double in_msg_per_sec = in_stat_list.size()/in_time_passed;
            double in_byte_per_sec = in_total_size/in_time_passed;
            oss << "input messages per second:" << '\t' << in_msg_per_sec << std::endl;
            oss << "input bytes per second:" << '\t' << in_byte_per_sec << std::endl;
            oss << "input messages total:" << '\t' << in_msg_from_start << std::endl;
            oss << "input MB total:" << '\t' << in_byte_from_start/1024/1024 << std::endl;
        }
        else{
            oss << "No messages received yet" << std::endl;
        }
    }
    command->answer(oss.str(), this->shared_from_this());
}
Example #5
0
void StringCallback::execute(Command_ptr command, CommandEndpoint_ptr instance){
    std::ostringstream ss;
    if(command->getCommand()==name){
        if(command->getArguments().length()==0){
            ss<<speakingName<<" is " << *string << '\n';
            command->answer(ss.str(), instance);
        }
        else{
            if(writeable){
                *string = command->getArguments();
                ss<< speakingName << " changed to " <<  *string << "\n";
            }
            else{
                ss<<speakingName<< " is not writeable\n";
                command->answer(ss.str(), instance);
            }
        }
    }
}
void FileEndpoint::seek_cmd(Command_ptr command){
    std::ostringstream oss;
    std::istringstream iss(command->getArguments());
    boost::posix_time::time_input_facet *timefacet = new boost::posix_time::time_input_facet("%Y-%m-%d %H:%M:%S");
    iss.imbue(std::locale(iss.getloc(), timefacet));
    boost::posix_time::ptime timestamp;
    try{
        iss >> timestamp;
        if(timestamp.is_not_a_date_time()){
            oss << "Dont understand argument. Time must be in format \"%Y-%m-%d %H:%M:%S\": " << command->getArguments() <<std::endl;
        }
        else{
            boost::posix_time::ptime next_message_at = seek(timestamp);
            oss << "Seeking: " << timestamp << " Next Message will be from " << next_message_at <<std::endl;
        }
    }
    catch(const std::exception& e){
        oss << "Dont understand argument. Time must be in format \"%Y-%m-%d %H:%M:%S\": " << command->getArguments() <<std::endl;
    }
    command->answer(oss.str(), this->shared_from_this());
}
void ConfigEndpoint::receive(Command_ptr command){
    if(command->getCommand()=="exit" || command->getCommand()=="logout" || command->getCommand()=="close"){
        ConfigEndpoint_ptr this_ptr = this->shared_from_this();
        close();
        command->answer("closed config "+configname+"\n", this_ptr);
    }
    else if(command->getCommand()=="has_commands"){
        {
            boost::mutex::scoped_lock lock(commandsMutex);
            for (std::list<Command_ptr>::const_iterator tmp_command = commands.begin(), end = commands.end(); tmp_command != end; ++tmp_command) {
                std::string receiver = (*tmp_command)->getReceiver();
                boost::replace_all(receiver, "*", "(.*)");
                boost::regex reg("^"+receiver+"$");
                boost::cmatch matches;
                CommandEndpoint_ptr sender = boost::dynamic_pointer_cast<CommandEndpoint>(command->getSender());
                if(sender){
                    if(boost::regex_search(sender->getId().c_str(), matches, reg)){
                        sender->receive((*tmp_command));
                    }
                }
            }
        }
    }
    else if(command->getCommand()=="add_command"){
        try {
            Command_ptr newCommand(new Command(command->getArguments(), this->shared_from_this()));
            {
                boost::mutex::scoped_lock lock(commandsMutex);
                commands.push_back(newCommand);
            }
            deliver(newCommand);
        }
        catch (const std::invalid_argument& ia) {
            command->answer(Answer::UNKNOWN_CMD, "Command might not be conform with command format "+command->getArguments()+"\n", this->shared_from_this());
        }
        command->answer("added command "+command->getArguments()+"\n", this->shared_from_this());
    }
    else if(command->getCommand()=="save"){
        if(command->getArguments().length()>0){
            save(command->getArguments());
            command->answer("saved config to "+command->getArguments()+"\n", this->shared_from_this());
        }
        else{
            save(configname);
            command->answer("saved config to "+configname+"\n", this->shared_from_this());
        }
            
    }
    else if(command->getCommand()=="list"){
        std::ostringstream ss;
        {
            boost::mutex::scoped_lock lock(commandsMutex);
            ss << "Currently " << commands.size() << " commands in config" << std::endl << "---------------------------------------" << std::endl;
            for (std::list<Command_ptr>::const_iterator commandi = commands.begin(), end = commands.end(); commandi != end; ++commandi) {
                ss << (*commandi)->to_str() << '\n';
            }

        }
        command->answer(ss.str(), this->shared_from_this());
    }
    else{
        CommandEndpoint::receive(command);
    }
}
void FileEndpoint::receive(Command_ptr command){
    if(command->getCommand()=="exit" || command->getCommand()=="logout" || command->getCommand()=="close"){
        FileEndpoint_ptr this_ptr = boost::static_pointer_cast<FileEndpoint>(shared_from_this());
        close();
        command->answer("closed file "+filename+"\n", this_ptr);
    }
    else if(command->getCommand()=="restart"){
        stopPlayback();
        seek(boost::date_time::not_a_date_time);
        startPlayback();
        command->answer("playback of "+getId()+" restarted at beginning\n", this->shared_from_this());
    }
    else if(command->getCommand()=="stop"){
        stopPlayback();
        command->answer("playback of "+getId()+" stopped\n", this->shared_from_this());
    }
    else if(command->getCommand()=="play"){
        startPlayback();
        command->answer("playback of "+getId()+" started\n", this->shared_from_this());
    }
    else if(command->getCommand()=="record"){
        record();
        command->answer("record to "+getId()+" started\n", this->shared_from_this());
    }
    else{
        NMEAEndpoint::receive(command);
    }
}
Example #9
0
void NMEAServer::receiveCommand(Command_ptr command){
        if(command->getCommand()=="exit" || command->getCommand()=="logout" || command->getCommand()=="close"){
            //TODO call exit on all endpoints
            raise(SIGTERM);
        }
        else if(command->getCommand()=="endpoints"){
            std::ostringstream ss;
            ss << "Currently " << online.size() << " endpoints connected" << std::endl << "---------------------------------------" << std::endl;
            for (std::list<Endpoint_ptr>::const_iterator endpoint = online.begin(), end = online.end(); endpoint != end; ++endpoint) {
                std::string state = getEndpointState();
                if(state.length()>0){
                    state = "("+state+")";
                }
                ss << (*endpoint)->getId() << '\t' << state << std::endl;
            }
            command->answer(ss.str(), this->shared_from_this());
        }
        else if(command->getCommand()=="time"){
            command->answer(to_simple_string(boost::posix_time::microsec_clock::local_time())+"\n", this->shared_from_this());
        }
        else if(command->getCommand()=="add"){
            boost::regex reg("^([[:alnum:]]*)\\h?$");
            boost::regex regArg("^([[:alnum:]]*)\\s([[:print:]]+)\\h?$");
            boost::cmatch matches;
            std::string type;
            std::string args;
            bool matched=false;
            if(boost::regex_search(command->getArguments().c_str(), matches, regArg)){
                type=std::string(matches[1].first, matches[1].second);
                args=std::string(matches[2].first, matches[2].second);
                matched=true;
            }
            else if(boost::regex_search(command->getArguments().c_str(), matches, reg)){
                type=std::string(matches[1].first, matches[1].second);
                args=std::string("");
                matched=true;
            }
            if(matched){
                if(type=="file" || type=="File"){
                    try
                    {
                        FileEndpoint::factory(this->shared_from_this(), args);
                        command->answer("New file Endpoint successfully created\n", this->shared_from_this());
                    }
                    catch (std::exception& e)
                    {
                        std::ostringstream oss;
                        oss << "FileEndpoint Exception: " << e.what();
                        log(oss.str());
                    }
                }
                else if(type=="tcp" || type=="TCP"){
                    try
                    {
                        TCPServer::factory(this->shared_from_this(), std::atoi(args.c_str()));
                        command->answer("New tcp Endpoint successfully created\n", this->shared_from_this());
                    }
                    catch (std::exception& e)
                    {
                        std::ostringstream oss;
                        oss << "TCPSession Exception: " << e.what();
                        log(oss.str());
                    }
                }
                else if(type=="serial"){
                    std::vector<std::string> strs;
                    try
                    {
                        boost::split(strs, args, boost::is_any_of("\t "));
                        if(strs.size()==2){
                            unsigned int boud = lexical_cast<unsigned int>(strs[1]);
                            SerialPort::factory(this->shared_from_this(), strs[0] ,boud);
                            command->answer("New SerialPort successfully created\n", this->shared_from_this());
                        }
                        else{
                            command->answer(Answer::WRONG_ARGS,"Cannot create serial. Argument must be \"device boudrate\"\n", this->shared_from_this());
                        }
                    }
                    catch (std::exception& e)
                    {
                        std::cerr << "serial: " << e.what() << "\n";
                        command->answer(Answer::WRONG_ARGS,"Cannot create serial. Argument must be \"device boudrate\"\n", this->shared_from_this());
                    }
                }
                else if(type=="GPSreceiver"){
                    GPSEndpoint::factory(this->shared_from_this());
                    command->answer("New GPSEndpoint successfully created\n", this->shared_from_this());
                }
                else if(type=="TimeEndpoint"){
                    TimeEndpoint::factory(this->shared_from_this());
                    command->answer("New TimeEndpoint successfully created\n", this->shared_from_this());
                }
                else if(type=="AISreceiver"){
                    AISEndpoint::factory(this->shared_from_this());
                    command->answer("New AISEndpoint successfully created\n", this->shared_from_this());
                }
                else if(type=="virtualAIVDO"){
                    try
                    {
                        if(args.length()==9){
                            unsigned int mmsi = lexical_cast<unsigned int>(args);
                            AIVDOEndpoint::factory(this->shared_from_this(), mmsi);
                            command->answer("New virtualAIVDO successfully created\n", this->shared_from_this());
                            
                        }
                        else{
                            command->answer(Answer::WRONG_ARGS,"Cannot create virtualAIVDO. MMSI Arguemnt must be 9 digits\n", this->shared_from_this());
                        }
                    }
                    catch (std::exception& e)
                    {
                        std::cerr << "virtualAIVDO: " << e.what() << "\n";
                        command->answer(Answer::WRONG_ARGS,"Cannot create virtualAIVDO. MMSI Arguemnt must be 9 digits\n", this->shared_from_this());
                    }
                }
                else if(type=="MemoryStore"){
                    MemoryStoreEndpoint::factory(this->shared_from_this(), args);
                    command->answer("New MemoryStoreEndpoint successfully created\n", this->shared_from_this());
                }
                else if(type=="Compass"){
                    CompassEndpoint::factory(this->shared_from_this());
                    command->answer("New CompassEndpoint successfully created\n", this->shared_from_this());
                }
                else{
                    command->answer(Answer::WRONG_ARGS, "Cannot understand command Argument "+command->getArguments()+" for Command "+command->getCommand()+". No such endpoint type to create.\n", this->shared_from_this());
                }
            }
            else{
                command->answer(Answer::WRONG_ARGS, "Cannot understand command Argument "+command->getArguments()+" for Command "+command->getCommand()+"\n", this->shared_from_this());
            }
        }
        else{
            CommandEndpoint::receive(command);
        }
}
Example #10
0
void BoolCallback::execute(Command_ptr command, CommandEndpoint_ptr instance){
    std::ostringstream ss;
    if(command->getCommand()==name){
        if(command->getArguments().length()==0){
            ss<<speakingName<<" is ";
            if(*boolean){
                ss<<"on";
            }
            else{
                ss<<"off";
            }
            ss<<'\n';
            command->answer(ss.str(), instance);
        }
        else if(command->getArguments()=="on"){
            if(writeable){
                *boolean=true;
                ss<<speakingName<< " is now turned on\n";
                command->answer(ss.str(), instance);
            }
            else{
                ss<<speakingName<< " is not writeable\n";
                command->answer(ss.str(), instance);
            }
        }
        else if(command->getArguments()=="off"){
            if(writeable){
                *boolean=false;
                ss<<speakingName<< " is now turned off\n";
                command->answer(ss.str(), instance);
            }
            else{
                ss<<speakingName<< " is not writeable\n";
                command->answer(ss.str(), instance);
            }
        }
        else{
            command->answer(Answer::WRONG_ARGS, "Cannot understand "+command->getArguments()+" for command "+command->getCommand()+"\n", instance);
        }
    }
}
Example #11
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);
    }
}