X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=recv.c;h=4b84e8b600cfeeb7d3907c8b292f97c2338730f2;hb=39ef1da5509461b18beb5b8f16ff6118c20c0ac6;hp=aaccb06ac77fcd7bded8ffc46c300039227eabe1;hpb=2ed89c59f0efcd0a2763f47c7d3455663241e623;p=paraslash.git diff --git a/recv.c b/recv.c index aaccb06a..4b84e8b6 100644 --- a/recv.c +++ b/recv.c @@ -15,18 +15,21 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */ -#include "gcc-compat.h" #include "para.h" +#include "list.h" +#include "sched.h" #include "recv.h" #include "recv.cmdline.h" +#include "fd.h" #include "error.h" +#include "stdout.h" struct gengetopt_args_info conf; INIT_RECV_ERRLISTS; -__printf_2_3 void para_log(int ll, char* fmt,...) +__printf_2_3 void para_log(int ll, const char* fmt,...) { va_list argp; @@ -48,12 +51,14 @@ static void *parse_config(int argc, char *argv[], int *receiver_num) printf("available receivers: "); for (i = 0; receivers[i].name; i++) printf("%s%s", i? " " : "", receivers[i].name); - printf("\nTry para_recv -r:-h for help on \n"); + printf("\nTry\n\tpara_recv -r ' -h'\n" + "for help on .\n"); exit(EXIT_SUCCESS); } return check_receiver_arg(conf.receiver_arg, receiver_num); } +#if 0 int main(int argc, char *argv[]) { int ret, eof = 0, max, r_opened = 0, receiver_num; @@ -81,16 +86,14 @@ recv: FD_ZERO(&rfds); FD_ZERO(&wfds); timeout.tv_sec = 0; - timeout.tv_usec = 1000 * 1000; + timeout.tv_usec = 999 * 1000; max = -1; ret = r->pre_select(&rn, &rfds, &wfds, &timeout); - max = MAX(max, ret); + max = PARA_MAX(max, ret); - PARA_DEBUG_LOG("timeout: %lums\n", tv2ms(&timeout)); - ret = select(max + 1, &rfds, &wfds, NULL, &timeout); + PARA_DEBUG_LOG("timeout: %lums, max: %d\n", tv2ms(&timeout), max); + ret = para_select(max + 1, &rfds, &wfds, &timeout); if (ret < 0) { - if (errno == EINTR || errno == EAGAIN) - goto recv; ret = -E_RECV_SELECT; goto out; } @@ -105,13 +108,13 @@ recv: goto recv; } ret = write(STDOUT_FILENO, rn.buf, rn.loaded); - PARA_DEBUG_LOG("wrote %d/%d\n", ret, rn.loaded); + PARA_DEBUG_LOG("wrote %d/%zd\n", ret, rn.loaded); if (ret < 0) { ret = -E_WRITE_STDOUT; goto out; } if (ret != rn.loaded) { - PARA_INFO_LOG("short write %d/%d\n", ret, rn.loaded); + PARA_INFO_LOG("short write %d/%zd\n", ret, rn.loaded); memmove(rn.buf, rn.buf + ret, rn.loaded - ret); } rn.loaded -= ret; @@ -123,6 +126,80 @@ out: if (r) r->shutdown(); if (ret < 0) - PARA_NOTICE_LOG("%d: (%s)\n", ret, PARA_STRERROR(-ret)); + PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret)); + return ret; +} +#endif + +void rn_event_handler(struct task *t) +{ + PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret)); + unregister_task(t); +} + +void stdout_event_handler(struct task *t) +{ + PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret)); + unregister_task(t); +} + +int main(int argc, char *argv[]) +{ + int ret, eof = 0, max, r_opened = 0, receiver_num; + struct timeval timeout; + struct receiver *r = NULL; + fd_set rfds, wfds; + struct receiver_node rn; + struct stdout_task sot; + struct sched s; + + init_sched(); + s.default_timeout.tv_sec = 1; + s.default_timeout.tv_usec = 0; + + memset(&rn, 0, sizeof(struct receiver_node)); + for (ret = 0; receivers[ret].name; ret++) + receivers[ret].init(&receivers[ret]); + ret = -E_RECV_SYNTAX; + rn.conf = parse_config(argc, argv, &receiver_num); + if (!rn.conf) { + PARA_EMERG_LOG("%s", "parse failed\n"); + goto out; + } + r = &receivers[receiver_num]; + rn.receiver = r; + ret = r->open(&rn); + if (ret < 0) + goto out; + r_opened = 1; + + sot.task.private_data = &sot; + sot.task.pre_select = stdout_pre_select; + sot.task.post_select = stdout_post_select; + sot.task.event_handler = stdout_event_handler; + sot.task.flags = 0; + sprintf(sot.task.status, "stdout writer"); + sot.buf = rn.buf; + sot.loaded = &rn.loaded; + sot.eof = &rn.eof; + register_task(&sot.task); + + rn.task.private_data = &rn; + rn.task.pre_select = r->pre_select; + rn.task.post_select = r->post_select; + rn.task.event_handler = rn_event_handler; + rn.task.flags = 0; + sprintf(rn.task.status, "receiver node"); + register_task(&rn.task); + + + ret = sched(&s); +out: + if (r_opened) + r->close(&rn); + if (r) + r->shutdown(); + if (ret < 0) + PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret)); return ret; }