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);
    }
}