Show scaled_main.c syntax highlighted
/*
*
* Copyright (c) 2003 The Regents of the University of California. All
* rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
* scaled.c: A daemon that sends and receives data packets of different
* programmable parameters for network measurement, using the generic
* link interface.
*
* $Id: scaled_main.c,v 1.3 2005/10/31 09:25:51 cerpa Exp $
*/
char scaled_c_id[] = "$Id: scaled_main.c,v 1.3 2005/10/31 09:25:51 cerpa Exp $";
#define __MAIN_C__
#include "scaled_i.h"
/********************** main() etc ***************************************/
/* Callback when we are asked (by emrun) to shut down */
static void scaled_shutdown(void *data)
{
neighbor_list_t *nl = (neighbor_list_t *) data;
elog(LOG_INFO, "shutting down scaled on %s", lu_name(nl->link, NULL));
exit(0);
}
static void usage(char *name)
{
misc_print_usage
(name, "-U <device> -T <time> [-r <root node>] [-s <pkt size>] "
"[-t <total pkts>] [-p <period>] [-w <tx window>]",
" --uses <device>: specify device to use\n"
" --gpstime <gps time>: specify gps reference time\n"
" --root <root node>: specify the id of the first node (assumes sequential numbering)\n"
" --pktsize <pkt size>: specify packet size (in bytes)\n"
" --totalpkts <total pkts>: specify total packets sent per tx window\n"
" --period <time period>: specify period used for the next transmission cycle (in ms)\n"
" --txwindow <tx window>: specify period used for packet transmission (in ms)\n"
);
exit(1);
}
int main(int argc, char *argv[])
{
neighbor_list_t nl;
char filename[1024];
struct timeval log_ctime;
struct timeval log_gtime;
emrun_opts_t emrun_opts = {
shutdown: scaled_shutdown,
data: &nl
};
/* generic initialization common to all programs */
misc_init(&argc, argv, CVSTAG);
memset(&nl, 0, sizeof(nl));
/* set defaults arguments */
nl.pkt_size = DEFAULT_PKT_SIZE;
nl.total_pkts = DEFAULT_TOTAL_PKTS;
nl.period = DEFAULT_PERIOD;
nl.tx_window = DEFAULT_TX_WINDOW;
/********** Parse command-line arguments ************/
/* link interface being used */
nl.link_name = link_parse_uses(&argc, argv, NULL);
if (nl.link_name == NULL) {
elog(LOG_ERR, "Please specify a link to use with --uses");
usage(argv[0]);
}
/* the gps reference time */
nl.string_gps_time = misc_parse_out_option(&argc, argv, "gpstime", 'T');
if (nl.string_gps_time == NULL) {
elog(LOG_ERR, "Please specify a gps reference time with -T or --gpstime");
usage(argv[0]);
}
misc_str_to_tv(nl.string_gps_time, &(nl.gps_time));
/* configurable options */
misc_parse_option_as_int(&argc, argv, "root", 'r', &nl.root_node_id);
misc_parse_option_as_int(&argc, argv, "pktsize", 's', &nl.pkt_size);
misc_parse_option_as_int(&argc, argv, "totalpkts", 't', &nl.total_pkts);
misc_parse_option_as_int(&argc, argv, "period", 'p', &nl.period);
misc_parse_option_as_int(&argc, argv, "txwindow", 'w', &nl.tx_window);
/* enable logging */
nl.log_dir = misc_parse_out_option(&argc, argv, "log", 'l');
if (nl.log_dir != NULL) {
nl.use_logging = 1;
/* open (truncate if exists) connectivity info file */
memset(filename, 0, 1024);
sprintf(filename, "%s/scale-node-%d-%s.log", nl.log_dir, my_node_id,
misc_tv_to_str(&(nl.gps_time)));
if ((nl.fp = fopen(filename, "a+")) == NULL) {
elog(LOG_WARNING, "Error opening %s: %m", filename);
exit(1);
} else {
gettimeofday(&log_ctime, NULL);
scaled_cpu_to_gps_time(&log_ctime, &log_gtime);
/* print experiment header */
fprintf(nl.fp, "\n\n\n");
fprintf(nl.fp, "*****************************************************\n");
fprintf(nl.fp, "NEW SCALE EXPERIMENT\n");
fprintf(nl.fp, "DATE: %s - %s\n", misc_print_date(&log_gtime), misc_tv_to_str(&log_gtime));
fprintf(nl.fp, "*****************************************************\n\n");
fflush(nl.fp);
}
}
if (misc_args_remain(&argc, argv))
usage(argv[0]);
/* Initialize other modules */
scaled_net_init(&nl);
emrun_init(&emrun_opts); /* this should be the last initialization */
elog_g(LOG_NOTICE, "Running, sending with packet size %d, total "
"packets %d, period %d to %s", nl.pkt_size, nl.total_pkts,
nl.period, lu_name(nl.link, NULL));
/* start experiment */
scaled_net_start(&nl);
/* actually, I never released control to glib */
g_main();
elog(LOG_CRIT, "event loop exited unexpectedly!");
return 1;
}
See more files for this project here