evhiperfifo.c 12 KB

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