hiperfifo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /* Example application source code using the multi socket interface to
  23. download many files at once.
  24. Written by Jeff Pohlmeyer
  25. Requires libevent version 2 and a (POSIX?) system that has mkfifo().
  26. This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c"
  27. sample programs.
  28. When running, the program creates the named pipe "hiper.fifo"
  29. Whenever there is input into the fifo, the program reads the input as a list
  30. of URL's and creates some new easy handles to fetch each URL via the
  31. curl_multi "hiper" API.
  32. Thus, you can try a single URL:
  33. % echo http://www.yahoo.com > hiper.fifo
  34. Or a whole bunch of them:
  35. % cat my-url-list > hiper.fifo
  36. The fifo buffer is handled almost instantly, so you can even add more URL's
  37. while the previous requests are still being downloaded.
  38. Note:
  39. For the sake of simplicity, URL length is limited to 1023 char's !
  40. This is purely a demo app, all retrieved data is simply discarded by the write
  41. callback.
  42. */
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <stdlib.h>
  46. #include <sys/time.h>
  47. #include <time.h>
  48. #include <unistd.h>
  49. #include <sys/poll.h>
  50. #include <curl/curl.h>
  51. #include <event2/event.h>
  52. #include <fcntl.h>
  53. #include <sys/stat.h>
  54. #include <errno.h>
  55. #define MSG_OUT stdout /* Send info to stdout, change to stderr if you want */
  56. /* Global information, common to all connections */
  57. typedef struct _GlobalInfo
  58. {
  59. struct event_base *evbase;
  60. struct event *fifo_event;
  61. struct event *timer_event;
  62. CURLM *multi;
  63. int still_running;
  64. FILE* input;
  65. } GlobalInfo;
  66. /* Information associated with a specific easy handle */
  67. typedef struct _ConnInfo
  68. {
  69. CURL *easy;
  70. char *url;
  71. GlobalInfo *global;
  72. char error[CURL_ERROR_SIZE];
  73. } ConnInfo;
  74. /* Information associated with a specific socket */
  75. typedef struct _SockInfo
  76. {
  77. curl_socket_t sockfd;
  78. CURL *easy;
  79. int action;
  80. long timeout;
  81. struct event *ev;
  82. int evset;
  83. GlobalInfo *global;
  84. } SockInfo;
  85. /* Update the event timer after curl_multi library calls */
  86. static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
  87. {
  88. struct timeval timeout;
  89. (void)multi; /* unused */
  90. timeout.tv_sec = timeout_ms/1000;
  91. timeout.tv_usec = (timeout_ms%1000)*1000;
  92. fprintf(MSG_OUT, "multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms);
  93. evtimer_add(g->timer_event, &timeout);
  94. return 0;
  95. }
  96. /* Die if we get a bad CURLMcode somewhere */
  97. static void mcode_or_die(const char *where, CURLMcode code)
  98. {
  99. if ( CURLM_OK != code ) {
  100. const char *s;
  101. switch (code) {
  102. case CURLM_BAD_HANDLE: s="CURLM_BAD_HANDLE"; break;
  103. case CURLM_BAD_EASY_HANDLE: s="CURLM_BAD_EASY_HANDLE"; break;
  104. case CURLM_OUT_OF_MEMORY: s="CURLM_OUT_OF_MEMORY"; break;
  105. case CURLM_INTERNAL_ERROR: s="CURLM_INTERNAL_ERROR"; break;
  106. case CURLM_UNKNOWN_OPTION: s="CURLM_UNKNOWN_OPTION"; break;
  107. case CURLM_LAST: s="CURLM_LAST"; break;
  108. default: s="CURLM_unknown";
  109. break;
  110. case CURLM_BAD_SOCKET: s="CURLM_BAD_SOCKET";
  111. fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
  112. /* ignore this error */
  113. return;
  114. }
  115. fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
  116. exit(code);
  117. }
  118. }
  119. /* Check for completed transfers, and remove their easy handles */
  120. static void check_multi_info(GlobalInfo *g)
  121. {
  122. char *eff_url;
  123. CURLMsg *msg;
  124. int msgs_left;
  125. ConnInfo *conn;
  126. CURL *easy;
  127. CURLcode res;
  128. fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running);
  129. while ((msg = curl_multi_info_read(g->multi, &msgs_left))) {
  130. if (msg->msg == CURLMSG_DONE) {
  131. easy = msg->easy_handle;
  132. res = msg->data.result;
  133. curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn);
  134. curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
  135. fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error);
  136. curl_multi_remove_handle(g->multi, easy);
  137. free(conn->url);
  138. curl_easy_cleanup(easy);
  139. free(conn);
  140. }
  141. }
  142. }
  143. /* Called by libevent when we get action on a multi socket */
  144. static void event_cb(int fd, short kind, void *userp)
  145. {
  146. GlobalInfo *g = (GlobalInfo*) userp;
  147. CURLMcode rc;
  148. int action =
  149. (kind & EV_READ ? CURL_CSELECT_IN : 0) |
  150. (kind & EV_WRITE ? CURL_CSELECT_OUT : 0);
  151. rc = curl_multi_socket_action(g->multi, fd, action, &g->still_running);
  152. mcode_or_die("event_cb: curl_multi_socket_action", rc);
  153. check_multi_info(g);
  154. if ( g->still_running <= 0 ) {
  155. fprintf(MSG_OUT, "last transfer done, kill timeout\n");
  156. if (evtimer_pending(g->timer_event, NULL)) {
  157. evtimer_del(g->timer_event);
  158. }
  159. }
  160. }
  161. /* Called by libevent when our timeout expires */
  162. static void timer_cb(int fd, short kind, void *userp)
  163. {
  164. GlobalInfo *g = (GlobalInfo *)userp;
  165. CURLMcode rc;
  166. (void)fd;
  167. (void)kind;
  168. rc = curl_multi_socket_action(g->multi,
  169. CURL_SOCKET_TIMEOUT, 0, &g->still_running);
  170. mcode_or_die("timer_cb: curl_multi_socket_action", rc);
  171. check_multi_info(g);
  172. }
  173. /* Clean up the SockInfo structure */
  174. static void remsock(SockInfo *f)
  175. {
  176. if (f) {
  177. if (f->evset)
  178. event_free(f->ev);
  179. free(f);
  180. }
  181. }
  182. /* Assign information to a SockInfo structure */
  183. static void setsock(SockInfo*f, curl_socket_t s, CURL*e, int act, GlobalInfo*g)
  184. {
  185. int kind =
  186. (act&CURL_POLL_IN?EV_READ:0)|(act&CURL_POLL_OUT?EV_WRITE:0)|EV_PERSIST;
  187. f->sockfd = s;
  188. f->action = act;
  189. f->easy = e;
  190. if (f->evset)
  191. event_free(f->ev);
  192. f->ev = event_new(g->evbase, f->sockfd, kind, event_cb, g);
  193. f->evset = 1;
  194. event_add(f->ev, NULL);
  195. }
  196. /* Initialize a new SockInfo structure */
  197. static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g)
  198. {
  199. SockInfo *fdp = calloc(sizeof(SockInfo), 1);
  200. fdp->global = g;
  201. setsock(fdp, s, easy, action, g);
  202. curl_multi_assign(g->multi, s, fdp);
  203. }
  204. /* CURLMOPT_SOCKETFUNCTION */
  205. static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
  206. {
  207. GlobalInfo *g = (GlobalInfo*) cbp;
  208. SockInfo *fdp = (SockInfo*) sockp;
  209. const char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE" };
  210. fprintf(MSG_OUT,
  211. "socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
  212. if (what == CURL_POLL_REMOVE) {
  213. fprintf(MSG_OUT, "\n");
  214. remsock(fdp);
  215. }
  216. else {
  217. if (!fdp) {
  218. fprintf(MSG_OUT, "Adding data: %s\n", whatstr[what]);
  219. addsock(s, e, what, g);
  220. }
  221. else {
  222. fprintf(MSG_OUT,
  223. "Changing action from %s to %s\n",
  224. whatstr[fdp->action], whatstr[what]);
  225. setsock(fdp, s, e, what, g);
  226. }
  227. }
  228. return 0;
  229. }
  230. /* CURLOPT_WRITEFUNCTION */
  231. static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
  232. {
  233. size_t realsize = size * nmemb;
  234. ConnInfo *conn = (ConnInfo*) data;
  235. (void)ptr;
  236. (void)conn;
  237. return realsize;
  238. }
  239. /* CURLOPT_PROGRESSFUNCTION */
  240. static int prog_cb (void *p, double dltotal, double dlnow, double ult,
  241. double uln)
  242. {
  243. ConnInfo *conn = (ConnInfo *)p;
  244. (void)ult;
  245. (void)uln;
  246. fprintf(MSG_OUT, "Progress: %s (%g/%g)\n", conn->url, dlnow, dltotal);
  247. return 0;
  248. }
  249. /* Create a new easy handle, and add it to the global curl_multi */
  250. static void new_conn(char *url, GlobalInfo *g )
  251. {
  252. ConnInfo *conn;
  253. CURLMcode rc;
  254. conn = calloc(1, sizeof(ConnInfo));
  255. memset(conn, 0, sizeof(ConnInfo));
  256. conn->error[0]='\0';
  257. conn->easy = curl_easy_init();
  258. if (!conn->easy) {
  259. fprintf(MSG_OUT, "curl_easy_init() failed, exiting!\n");
  260. exit(2);
  261. }
  262. conn->global = g;
  263. conn->url = strdup(url);
  264. curl_easy_setopt(conn->easy, CURLOPT_URL, conn->url);
  265. curl_easy_setopt(conn->easy, CURLOPT_WRITEFUNCTION, write_cb);
  266. curl_easy_setopt(conn->easy, CURLOPT_WRITEDATA, &conn);
  267. curl_easy_setopt(conn->easy, CURLOPT_VERBOSE, 1L);
  268. curl_easy_setopt(conn->easy, CURLOPT_ERRORBUFFER, conn->error);
  269. curl_easy_setopt(conn->easy, CURLOPT_PRIVATE, conn);
  270. curl_easy_setopt(conn->easy, CURLOPT_NOPROGRESS, 0L);
  271. curl_easy_setopt(conn->easy, CURLOPT_PROGRESSFUNCTION, prog_cb);
  272. curl_easy_setopt(conn->easy, CURLOPT_PROGRESSDATA, conn);
  273. fprintf(MSG_OUT,
  274. "Adding easy %p to multi %p (%s)\n", conn->easy, g->multi, url);
  275. rc = curl_multi_add_handle(g->multi, conn->easy);
  276. mcode_or_die("new_conn: curl_multi_add_handle", rc);
  277. /* note that the add_handle() will set a time-out to trigger very soon so
  278. that the necessary socket_action() call will be called by this app */
  279. }
  280. /* This gets called whenever data is received from the fifo */
  281. static void fifo_cb(int fd, short event, void *arg)
  282. {
  283. char s[1024];
  284. long int rv=0;
  285. int n=0;
  286. GlobalInfo *g = (GlobalInfo *)arg;
  287. (void)fd; /* unused */
  288. (void)event; /* unused */
  289. do {
  290. s[0]='\0';
  291. rv=fscanf(g->input, "%1023s%n", s, &n);
  292. s[n]='\0';
  293. if ( n && s[0] ) {
  294. new_conn(s,arg); /* if we read a URL, go get it! */
  295. } else break;
  296. } while ( rv != EOF);
  297. }
  298. /* Create a named pipe and tell libevent to monitor it */
  299. static const char *fifo = "hiper.fifo";
  300. static int init_fifo (GlobalInfo *g)
  301. {
  302. struct stat st;
  303. curl_socket_t sockfd;
  304. fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
  305. if (lstat (fifo, &st) == 0) {
  306. if ((st.st_mode & S_IFMT) == S_IFREG) {
  307. errno = EEXIST;
  308. perror("lstat");
  309. exit (1);
  310. }
  311. }
  312. unlink(fifo);
  313. if (mkfifo (fifo, 0600) == -1) {
  314. perror("mkfifo");
  315. exit (1);
  316. }
  317. sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0);
  318. if (sockfd == -1) {
  319. perror("open");
  320. exit (1);
  321. }
  322. g->input = fdopen(sockfd, "r");
  323. fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo);
  324. g->fifo_event = event_new(g->evbase, sockfd, EV_READ|EV_PERSIST, fifo_cb, g);
  325. event_add(g->fifo_event, NULL);
  326. return (0);
  327. }
  328. static void clean_fifo(GlobalInfo *g)
  329. {
  330. event_free(g->fifo_event);
  331. fclose(g->input);
  332. unlink(fifo);
  333. }
  334. int main(int argc, char **argv)
  335. {
  336. GlobalInfo g;
  337. (void)argc;
  338. (void)argv;
  339. memset(&g, 0, sizeof(GlobalInfo));
  340. g.evbase = event_base_new();
  341. init_fifo(&g);
  342. g.multi = curl_multi_init();
  343. g.timer_event = evtimer_new(g.evbase, timer_cb, &g);
  344. /* setup the generic multi interface options we want */
  345. curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
  346. curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g);
  347. curl_multi_setopt(g.multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
  348. curl_multi_setopt(g.multi, CURLMOPT_TIMERDATA, &g);
  349. /* we don't call any curl_multi_socket*() function yet as we have no handles
  350. added! */
  351. event_base_dispatch(g.evbase);
  352. /* this, of course, won't get called since only way to stop this program is
  353. via ctrl-C, but it is here to show how cleanup /would/ be done. */
  354. clean_fifo(&g);
  355. event_free(g.timer_event);
  356. event_base_free(g.evbase);
  357. curl_multi_cleanup(g.multi);
  358. return 0;
  359. }