Skip to content

Instantly share code, notes, and snippets.

@byzhang
Created August 22, 2013 19:12
Show Gist options
  • Save byzhang/6311515 to your computer and use it in GitHub Desktop.
Save byzhang/6311515 to your computer and use it in GitHub Desktop.
muduo::AtomicInt32 g_running_clients;
EventLoop* g_loop = nullptr;
class Client: boost::noncopyable {
private:
const char* server_;
ifstream input_;
muduo::AtomicInt32 running_reqs_;
curl::Curl curl_;
curl::RequestPtr req_user_, req_target_;
public:
// real constructor
Client(EventLoop* loop, const char* server, const char* file):
server_(server),
input_(file),
curl_(loop) {
create_reqs();
}
virtual ~Client() {
}
private:
void onData(const char* data, int len) const {
(void)data;
(void)len;
}
void done(curl::Request* c, int code) {
// TODO: add counters
(void)c;
(void)code;
if (running_reqs_.decrementAndGet() > 0) {
return;
}
// create new reqs
if (create_reqs()) {
return;
}
// no new reqs are created, check whether we can quit the loop now.
auto clients = g_running_clients.decrementAndGet();
if (clients == 0) {
g_loop->quit(); // crashed
}
}
};
int main(int ac, char* av[])
{
std::string server;
int threads = 0;
vector<std::string> files;
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "produce help message")
("server,S", po::value<std::string>(&server)->default_value("http://127.0.0.1:8006/"), "the url of the server")
("inputs,I", po::value<vector<std::string>>(), "the input data files")
("threads,T", po::value<int>(&threads)->default_value(4), "the number of threads")
;
po::positional_options_description p;
p.add("inputs", -1);
po::variables_map vm;
po::store(po::command_line_parser(ac, av).
options(desc).positional(p).run(), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << endl;
return 0;
}
if (vm.count("inputs")) {
files = vm["inputs"].as<vector<std::string>>();
} else {
cout << desc << endl;
return 1;
}
g_running_clients.getAndSet(files.size());
curl::Curl::initialize(curl::Curl::kCURLssl);
EventLoop loop;
EventLoopThreadPool loopPool(&loop);
loopPool.setThreadNum(threads);
loopPool.start();
boost::ptr_vector<Client> clients;
for (const auto& file: files)
{
clients.push_back(new Client(loopPool.getNextLoop(), server.c_str(), file.c_str()));
}
loop.loop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment