Added Option -l ('l'oop) to make the canplayer replay a given file more than

one time. To replay a given file infinitely say '-l i'. Of course it is not
possible/reasonable to define '-l' when reading from stdin ...
pull/7/head
Oliver Hartkopp 2007-02-12 08:36:38 +00:00
parent 892c0d9e7d
commit aca93c6867
1 changed files with 122 additions and 78 deletions

View File

@ -62,6 +62,7 @@
#include "lib.h" #include "lib.h"
#define DEFAULT_GAP 1 /* ms */ #define DEFAULT_GAP 1 /* ms */
#define DEFAULT_LOOPS 1 /* only one replay */
#define CHANNELS 20 /* anyone using more than 20 CAN interfaces at a time? */ #define CHANNELS 20 /* anyone using more than 20 CAN interfaces at a time? */
#define BUFSZ 100 /* for one line in the logfile */ #define BUFSZ 100 /* for one line in the logfile */
@ -78,6 +79,10 @@ void print_usage(char *prg)
{ {
fprintf(stderr, "\nUsage: %s <options> [interface assignment]*\n\n", prg); fprintf(stderr, "\nUsage: %s <options> [interface assignment]*\n\n", prg);
fprintf(stderr, "Options: -I <infile> (default stdin)\n"); fprintf(stderr, "Options: -I <infile> (default stdin)\n");
fprintf(stderr, " -l <num> "
"(process input file <num> times)\n"
" "
"(Use 'i' for infinite loop - default: %d)\n", DEFAULT_LOOPS);
fprintf(stderr, " -t (ignore timestamps: " fprintf(stderr, " -t (ignore timestamps: "
"send frames immediately)\n"); "send frames immediately)\n");
fprintf(stderr, " -g <ms> (gap in milli " fprintf(stderr, " -g <ms> (gap in milli "
@ -213,12 +218,14 @@ int main(int argc, char **argv)
FILE *infile = stdin; FILE *infile = stdin;
unsigned long gap = DEFAULT_GAP; unsigned long gap = DEFAULT_GAP;
int use_timestamps = 1; int use_timestamps = 1;
static int verbose, opt, loops; static int verbose, opt, delay_loops;
static int infinite_loops = 0;
static int loops = DEFAULT_LOOPS;
int assignments; /* assignments defined on the commandline */ int assignments; /* assignments defined on the commandline */
int txidx; /* sendto() interface index */ int txidx; /* sendto() interface index */
int nbytes, i, j; int eof, nbytes, i, j;
while ((opt = getopt(argc, argv, "I:tg:v")) != -1) { while ((opt = getopt(argc, argv, "I:l:tg:v")) != -1) {
switch (opt) { switch (opt) {
case 'I': case 'I':
infile = fopen(optarg, "r"); infile = fopen(optarg, "r");
@ -228,6 +235,16 @@ int main(int argc, char **argv)
} }
break; break;
case 'l':
if (optarg[0] == 'i')
infinite_loops = 1;
else
if (!(loops = atoi(optarg))) {
fprintf(stderr, "Invalid argument for option -l !\n");
return 1;
}
break;
case 't': case 't':
use_timestamps = 0; use_timestamps = 0;
break; break;
@ -249,6 +266,17 @@ int main(int argc, char **argv)
assignments = argc - optind; /* find real number of user assignments */ assignments = argc - optind; /* find real number of user assignments */
if (infile == stdin) { /* no jokes with stdin */
infinite_loops = 0;
loops = 1;
}
if (verbose > 1) /* use -v -v to see this */
if (infinite_loops)
printf("infinite_loops\n");
else
printf("%d loops\n", loops);
sleep_ts.tv_sec = gap / 1000; sleep_ts.tv_sec = gap / 1000;
sleep_ts.tv_nsec = (gap % 1000) * 1000000; sleep_ts.tv_nsec = (gap % 1000) * 1000000;
@ -293,9 +321,19 @@ int main(int argc, char **argv)
} }
} }
while (infinite_loops || loops--) {
if (infile != stdin)
rewind(infile); /* for each loop */
if (verbose > 1) /* use -v -v to see this */
printf (">>>>>>>>> start reading file. remaining loops = %d\n", loops);
if (!fgets(buf, BUFSZ-1, infile)) /* read first frame from logfile */ if (!fgets(buf, BUFSZ-1, infile)) /* read first frame from logfile */
goto out; /* nothing to read */ goto out; /* nothing to read */
eof = 0;
if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec, if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
device, ascframe) != 4) device, ascframe) != 4)
return 1; return 1;
@ -317,7 +355,7 @@ int main(int argc, char **argv)
diff_tv.tv_sec--, diff_tv.tv_usec += 1000000; diff_tv.tv_sec--, diff_tv.tv_usec += 1000000;
} }
while (1) { while (!eof) {
while ((!use_timestamps) || while ((!use_timestamps) ||
(frames_to_send(&today_tv, &diff_tv, &log_tv) < 0)) { (frames_to_send(&today_tv, &diff_tv, &log_tv) < 0)) {
@ -364,8 +402,10 @@ int main(int argc, char **argv)
} }
/* read next frame from logfile */ /* read next frame from logfile */
if (!fgets(buf, BUFSZ-1, infile)) if (!fgets(buf, BUFSZ-1, infile)) {
goto out; /* nothing to read */ eof = 1; /* this file is completely processed */
break;
}
if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec, if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
device, ascframe) != 4) device, ascframe) != 4)
@ -373,14 +413,18 @@ int main(int argc, char **argv)
if (use_timestamps) /* save a syscall if possible */ if (use_timestamps) /* save a syscall if possible */
gettimeofday(&today_tv, NULL); gettimeofday(&today_tv, NULL);
}
} /* while frames_to_send ... */
if (nanosleep(&sleep_ts, NULL)) if (nanosleep(&sleep_ts, NULL))
return 1; return 1;
loops++; /* private statistics */ delay_loops++; /* private statistics */
gettimeofday(&today_tv, NULL); gettimeofday(&today_tv, NULL);
}
} /* while (!eof) */
} /* while (infinite_loops || loops--) */
out: out:
@ -388,7 +432,7 @@ int main(int argc, char **argv)
fclose(infile); fclose(infile);
if (verbose > 1) /* use -v -v to see this */ if (verbose > 1) /* use -v -v to see this */
printf("%d loops\n", loops); printf("%d delay_loops\n", delay_loops);
return 0; return 0;
} }