Permalink
Please sign in to comment.
Showing
with
90 additions
and 0 deletions.
- +3 −0 .gitmodules
- +10 −0 SConstruct
- +76 −0 parhost.cc
- +1 −0 threadpool
@@ -0,0 +1,3 @@ | ||
+[submodule "threadpool"] | ||
+ path = threadpool | ||
+ url = https://github.com/afeinberg/threadpool |
10
SConstruct
@@ -0,0 +1,10 @@ | ||
+# -*-python-*- | ||
+ | ||
+import os | ||
+ | ||
+env = Environment() | ||
+env.Append(CPPFLAGS='-g -Wall -Wextra') | ||
+env.Append(CPPPATH=[os.path.join(os.curdir, "threadpool")]) | ||
+env.Program(target='parhost', | ||
+ source=['parhost.cc'], | ||
+ LIBS=['boost_thread-mt']) |
76
parhost.cc
@@ -0,0 +1,76 @@ | ||
+#include <cstdlib> | ||
+#include <cstring> | ||
+#include <netdb.h> | ||
+#include <sys/types.h> | ||
+#include <sys/socket.h> | ||
+#include <arpa/inet.h> | ||
+ | ||
+#include <iostream> | ||
+#include <string> | ||
+ | ||
+#include <boost/bind.hpp> | ||
+#include "boost/threadpool.hpp" | ||
+ | ||
+#ifndef NI_MAXHOST | ||
+#define NI_MAXHOST 1025 | ||
+#endif | ||
+ | ||
+namespace { | ||
+ | ||
+const int k_default_maxthreads = 32; | ||
+const int k_nameinfo_maxhost = NI_MAXHOST; | ||
+ | ||
+using namespace boost::threadpool; | ||
+ | ||
+void lookup(const std::string &buf); | ||
+void submit_tasks(int max_threads); | ||
+ | ||
+void lookup(const std::string &buf) { | ||
+ struct addrinfo hints; | ||
+ struct addrinfo *ai; | ||
+ int rc; | ||
+ | ||
+ memset(&hints, 0, sizeof(hints)); | ||
+ hints.ai_family = AF_UNSPEC; | ||
+ | ||
+ if ((rc = getaddrinfo(buf.c_str(), NULL, &hints, &ai)) != 0) { | ||
+ std::cerr << "getaddrinfo(): " << gai_strerror(rc) << std::endl; | ||
+ } else if (ai != NULL) { | ||
+ char hostname[k_nameinfo_maxhost] = ""; | ||
+ rc = getnameinfo(ai->ai_addr, | ||
+ ai->ai_addrlen, | ||
+ hostname, | ||
+ k_nameinfo_maxhost, | ||
+ NULL, | ||
+ 0, | ||
+ 0); | ||
+ if (rc != 0) { | ||
+ std::cerr << "getnameinfo(): " << gai_strerror(rc) << std::endl; | ||
+ } else { | ||
+ if (*hostname != '\0') { | ||
+ std::cout << buf << " -> " << hostname << std::endl; | ||
+ } else { | ||
+ std::cout << buf << " -> ? " << std::endl; | ||
+ } | ||
+ } | ||
+ } | ||
+ | ||
+ freeaddrinfo(ai); | ||
+} | ||
+ | ||
+void submit_tasks(int max_threads) { | ||
+ pool tp(max_threads); | ||
+ std::string line; | ||
+ while (std::getline(std::cin, line)) { | ||
+ tp.schedule(boost::bind(lookup, line)); | ||
+ } | ||
+} | ||
+ | ||
+} // namespace | ||
+ | ||
+ | ||
+int main(int argc, char **argv) { | ||
+ int max_threads = argc < 2 ? k_default_maxthreads : atoi(argv[1]); | ||
+ submit_tasks(max_threads); | ||
+ return EXIT_SUCCESS; | ||
+} |
Submodule threadpool
added at
438043
0 comments on commit
6fc5482