Author: tjansen Date: Tue Nov 10 12:02:04 2009 New Revision: 1599 Log: Moved {c,s}d_do_main() to the respective main(). Fixes #117. Modified: trunk/pisacd/cdmain.c trunk/pisasd/sdmain.c Modified: trunk/pisacd/cdmain.c ============================================================================== --- trunk/pisacd/cdmain.c Tue Nov 10 11:38:20 2009 (r1598) +++ trunk/pisacd/cdmain.c Tue Nov 10 12:02:04 2009 (r1599) @@ -90,66 +90,6 @@ } /** - * The core loop as a client daemon. - * All message processing happens here. - * This function takes care of adding all relevant file descriptors to - * a set of file descriptors, setting up call to the select function and - * processing of all incoming and outgoing packets. - */ -static void cd_do_main(void) -{ - const int nfds = POLL_FD_COUNT; - int i; - struct pollfd pfds[nfds]; - - cd_ctx.flag_running = true; - - pisa_servers_add_all(); - - /* initialize the poll descriptors - * sockets.tunnel needs to be always included in the reading fd list - */ - pfds[POLL_TUN].fd = cd_ctx.fd_tunnel; - pfds[POLL_CONF].fd = cd_ctx.fd_pisaconf; - pfds[POLL_CTL].fd = cd_ctx.fd_control; - pfds[POLL_DATA].fd = cd_ctx.fd_data; - pfds[POLL_SCHED].fd = cd_ctx.scheduler.pipe_main[0]; - - for (i = 0; i < nfds; i++) - pfds[i].events = POLLIN; - - PISA_INFO("\nEntering main loop\n"); - - while (cd_ctx.flag_running || cd_ctx.flag_pending) { - - if (poll(pfds, nfds, -1) > 0) { - if (cd_ctx.flag_running) { - if (pfds[POLL_TUN].revents & POLLIN) - pisa_cd_copy_from_tun_to_sock(); - - if (pfds[POLL_DATA].revents & POLLIN) - pisa_cd_copy_from_sock_to_tun(); - } else { - /* TODO: Incoming packets after - * flag_running is false will cause 100% CPU - * load as select will immediately return. - * Read (and ignore) from the fds mentioned - * above. */ - } - - if (pfds[POLL_CTL].revents & POLLIN) - pisa_ctrlhandler_dispatch(&cd_ctx.ctrlhandlers, cd_ctx.fd_control); - - if (pfds[POLL_CONF].revents & POLLIN) - pisa_conf_handle_packet(cd_ctx.fd_pisaconf); - - if (pfds[POLL_SCHED].revents & POLLIN) - pisa_handle_scheduler(); - } - } -} - -/** * static void cd_print_usage(char **argv) * * Print usage instructions of client daemon. @@ -446,15 +386,61 @@ */ int main(int argc, char *argv[]) { + const int nfds = POLL_FD_COUNT; + int i; + struct pollfd pfds[nfds]; + /* Initialize basic settings */ cd_init(argc, argv); - /* get into the main loop */ - cd_do_main(); + cd_ctx.flag_running = true; + + pisa_servers_add_all(); + + /* initialize the poll descriptors + * sockets.tunnel needs to be always included in the reading fd list + */ + pfds[POLL_TUN].fd = cd_ctx.fd_tunnel; + pfds[POLL_CONF].fd = cd_ctx.fd_pisaconf; + pfds[POLL_CTL].fd = cd_ctx.fd_control; + pfds[POLL_DATA].fd = cd_ctx.fd_data; + pfds[POLL_SCHED].fd = cd_ctx.scheduler.pipe_main[0]; + + for (i = 0; i < nfds; i++) + pfds[i].events = POLLIN; + + PISA_INFO("\nEntering main loop\n"); + + while (cd_ctx.flag_running || cd_ctx.flag_pending) { + + if (poll(pfds, nfds, -1) > 0) { + if (cd_ctx.flag_running) { + if (pfds[POLL_TUN].revents & POLLIN) + pisa_cd_copy_from_tun_to_sock(); + + if (pfds[POLL_DATA].revents & POLLIN) + pisa_cd_copy_from_sock_to_tun(); + } else { + /* TODO: Incoming packets after + * flag_running is false will cause 100% CPU + * load as select will immediately return. + * Read (and ignore) from the fds mentioned + * above. */ + } + + if (pfds[POLL_CTL].revents & POLLIN) + pisa_ctrlhandler_dispatch(&cd_ctx.ctrlhandlers, cd_ctx.fd_control); + + if (pfds[POLL_CONF].revents & POLLIN) + pisa_conf_handle_packet(cd_ctx.fd_pisaconf); + + if (pfds[POLL_SCHED].revents & POLLIN) + pisa_handle_scheduler(); + } + } /* exit, cleaning up existing data */ cd_deinit(); exit(EXIT_SUCCESS); } - Modified: trunk/pisasd/sdmain.c ============================================================================== --- trunk/pisasd/sdmain.c Tue Nov 10 11:38:20 2009 (r1598) +++ trunk/pisasd/sdmain.c Tue Nov 10 12:02:04 2009 (r1599) @@ -97,53 +97,6 @@ } /** - * The core loop as a server daemon. - * All message processing happens here. - * This function takes care of adding all relevant file descriptors to - * a set of file descriptors, setting up call to the select function and - * processing of all incoming and outgoing packets. - */ -static inline void sd_do_main(void) -{ - struct pollfd pfds[POLL_FD_COUNT]; - const int nfds = POLL_FD_COUNT; - int i; - - /* initialize the polling structures */ - pfds[POLL_CTL].fd = sd_ctx.fd_control; - pfds[POLL_DATA].fd = sd_ctx.fd_data; - pfds[POLL_TUN].fd = sd_ctx.fd_tunnel; - pfds[POLL_CONF].fd = sd_ctx.fd_pisaconf; - pfds[POLL_SCHED].fd = sd_ctx.scheduler.pipe_main[0]; - - for (i = 0; i < nfds; i++) - pfds[i].events = POLLIN; - - sd_ctx.flag_running = true; - PISA_INFO("\nEntering main loop\n"); - - while (sd_ctx.flag_running) { - - if (poll(pfds, nfds, -1) > 0) { - if (pfds[POLL_CTL].revents & POLLIN) - pisa_ctrlhandler_dispatch(&sd_ctx.ctrlhandlers, sd_ctx.fd_control); - - if (pfds[POLL_DATA].revents & POLLIN) - pisa_sd_copy_from_sock_to_tun(); - - if (pfds[POLL_TUN].revents & POLLIN) - pisa_sd_copy_from_tun_to_sock(); - - if (pfds[POLL_CONF].revents & POLLIN) - pisa_conf_handle_packet(sd_ctx.fd_pisaconf); - - if (pfds[POLL_SCHED].revents & POLLIN) - pisa_handle_scheduler(); - } - } -} - -/** * static void sd_print_usage(char **argv) * * Print usage instructions of server daemon. @@ -495,11 +448,45 @@ */ int main(int argc, char *argv[]) { - /* Initialize basic settings */ + struct pollfd pfds[POLL_FD_COUNT]; + const int nfds = POLL_FD_COUNT; + int i; + + /* initialize basic settings */ sd_init(argc, argv); - /* get into the main loop */ - sd_do_main(); + /* initialize the polling structures */ + pfds[POLL_CTL].fd = sd_ctx.fd_control; + pfds[POLL_DATA].fd = sd_ctx.fd_data; + pfds[POLL_TUN].fd = sd_ctx.fd_tunnel; + pfds[POLL_CONF].fd = sd_ctx.fd_pisaconf; + pfds[POLL_SCHED].fd = sd_ctx.scheduler.pipe_main[0]; + + for (i = 0; i < nfds; i++) + pfds[i].events = POLLIN; + + sd_ctx.flag_running = true; + PISA_INFO("\nEntering main loop\n"); + + while (sd_ctx.flag_running) { + + if (poll(pfds, nfds, -1) > 0) { + if (pfds[POLL_CTL].revents & POLLIN) + pisa_ctrlhandler_dispatch(&sd_ctx.ctrlhandlers, sd_ctx.fd_control); + + if (pfds[POLL_DATA].revents & POLLIN) + pisa_sd_copy_from_sock_to_tun(); + + if (pfds[POLL_TUN].revents & POLLIN) + pisa_sd_copy_from_tun_to_sock(); + + if (pfds[POLL_CONF].revents & POLLIN) + pisa_conf_handle_packet(sd_ctx.fd_pisaconf); + + if (pfds[POLL_SCHED].revents & POLLIN) + pisa_handle_scheduler(); + } + } sd_deinit();