예제 #1
0
        //A factory method that accept a string representation of a command with arguments and a stomp client pointer
        // and returns an appropriate Command object that represents the string command
        Command * CommandFactory::createFactory(std::string const &command_string, stomp::TwitterClient *client) {

            Args argumentList;
            boost::split(argumentList, command_string, boost::is_any_of(" "), boost::token_compress_on);
            
            std::string command_name = argumentList[0];
            
            //Remove the first element - that being the command_name, leaving only the actual arguments in the list
            argumentList.erase(argumentList.begin());

            Command *cmd = 0;

            //Ugly, we know, but C++ can't do a switch-case on strings and we didn't have enought time to find a more elegant solution
            if (command_name == "login") {
                cmd = new LoginCommand(client, argumentList);
            } else if (command_name == "follow") {
                cmd = new FollowCommand(client, argumentList);
            } else if (command_name == "unfollow") {
                cmd = new UnfollowCommand(client, argumentList);
            } else if (command_name == "tweet") {
                cmd = new TweetCommand(client, argumentList);
            } else if (command_name == "clients") {
                cmd = new ClientsCommand(client, argumentList);
            } else if (command_name == "stats") {
                cmd = new StatsCommand(client, argumentList);
            } else if (command_name == "logout") {
                cmd = new LogoutCommand(client, argumentList);
            } else if (command_name == "stop") {
                cmd = new StopCommand(client, argumentList);
            } else {
                std::cerr << "Unknown command type" << std::endl;
            }
            
            return cmd;
        }
예제 #2
0
void FlagImpl<std::string>::Parse( Args & args )
{
     for( Args::iterator arg = args.begin(); arg != args.end(); ++arg )
     {
          if( !arg->compare( GetFlag() ))
          {
               if( IsAlreadySet() ) // если флаг задан( уже встречалс¤ среди переданных аргументов )
                    throw exception::ErrBadArgs();
               Args::iterator flag = arg; // первый аргумент - флаг
               Args::iterator opt = ++arg; // следущий за ним аргумент - опци¤ 
               if( opt == args.end() )
                    throw exception::ErrBadArgs();
               SetVal( *opt );
               SetIsAlreadySet(); // выставл¤ем признак того, что данный флаг уже задан( защита от дублировани¤ )
               // удал¤ем из списка аргументов считанные флаг и опцию
               args.erase( flag ); 
               args.erase( opt );
               break;
          }
     }
}
예제 #3
0
LastfmService::Result LastfmService::fetch(Args &args)
{
	Result result;
	
	std::string url = baseURL;
	url += methodName();
	for (Args::const_iterator it = args.begin(); it != args.end(); ++it)
	{
		url += "&";
		url += it->first;
		url += "=";
		url += Curl::escape(it->second);
	}
	
	std::string data;
	CURLcode code = Curl::perform(data, url);
	
	if (code != CURLE_OK)
	{
		result.second = curl_easy_strerror(code);
		return result;
	}
	
	if (actionFailed(data))
	{
		StripHtmlTags(data);
		result.second = data;
		return result;
	}
	
	if (!parse(data))
	{
		// if relevant part of data was not found and one of arguments
		// was language, try to fetch it again without that parameter.
		// otherwise just report failure.
		Args::iterator lang = args.find("lang");
		if (lang != args.end())
		{
			args.erase(lang);
			return fetch(args);
		}
		else
		{
			// parse should change data to error msg, if it fails
			result.second = data;
			return result;
		}
	}
	
	result.first = true;
	result.second = data;
	return result;
}
예제 #4
0
void FlagImpl<bool>::Parse( Args & args )
{
     for( Args::iterator arg = args.begin(); arg != args.end(); ++arg )
     {
          if( !arg->compare( GetFlag() ))
          {
               if( IsAlreadySet() ) // если флаг задан( уже встречалс¤ среди переданных аргументов )
                    throw exception::ErrBadArgs();
               // аргумент, ¤вл¤ющийс¤ булевым флагом, не предполагает следующей за ним опции
               SetVal( true ); 
               SetIsAlreadySet(); // выставл¤ем признак того, что данный флаг уже задан( защита от дублировани¤ )
               // удал¤ем из списка аргументов считанный флаг
               args.erase( arg ); 
               break;
          }
     }
}