diff --git a/Makefile b/Makefile index 0144ce154..baa162b4a 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # Executables -TARGETS = server pipe random test +TARGETS = server pipe signal test # Libraries LIBS = libs2ss.so @@ -79,7 +79,7 @@ all: $(LIBS) $(TARGETS) server: server.o $(OBJS) libs2ss.so pipe: pipe.o $(OBJS) libs2ss.so test: test.o $(OBJS) libs2ss.so -random: random.o msg.o utils.o timing.o log.o +signal: signal.o msg.o utils.o timing.o log.o # Libraries libs2ss.so: CFLAGS += -fPIC $(LIB_CFLAGS) @@ -90,14 +90,13 @@ libs2ss.so: $(LIB_OBJS) install: $(TARGETS) install -m 0644 libs2ss.so $(PREFIX)/lib install -m 0755 server -T $(PREFIX)/bin/s2ss - install -m 0755 send $(PREFIX)/bin/s2ss-send - install -m 0755 receive $(PREFIX)/bin/s2ss-receive - install -m 0755 random $(PREFIX)/bin/s2ss-random + install -m 0755 pipe $(PREFIX)/bin/s2ss-pipe + install -m 0755 signal $(PREFIX)/bin/s2ss-signal install -m 0755 test $(PREFIX)/bin/s2ss-test release: all tar czf s2ss-$(COMMIT)-docs.tar.gz documentation/html/ - tar czf s2ss-$(COMMIT).tar.gz server/server server/test server/send server/receive server/random server/etc/ + tar czf s2ss-$(COMMIT).tar.gz server/server server/test server/pipe server/signal server/etc/ rsync *.tar.gz $(DEPLOY_USER)@$(DEPLOY_HOST):$(DEPLOY_PATH)/ rsync --archive --delete documentation/html/ $(DEPLOY_USER)@$(DEPLOY_HOST):$(DEPLOY_PATH)/doc/ diff --git a/include/msg.h b/include/msg.h index afce6df70..fdd60705d 100644 --- a/include/msg.h +++ b/include/msg.h @@ -71,10 +71,4 @@ int msg_fprint(FILE *f, struct msg *m, int flags, double offset); */ int msg_fscan(FILE *f, struct msg *m, int *flags, double *offset); -/** Change the values of an existing message in a random fashion. - * - * @param m A pointer to the message whose values will be updated - */ -void msg_random(struct msg *m); - #endif /* _MSG_H_ */ diff --git a/lib/msg.c b/lib/msg.c index 837bd75a6..58b302d2e 100644 --- a/lib/msg.c +++ b/lib/msg.c @@ -165,11 +165,3 @@ skip: if (fgets(line, sizeof(line), f) == NULL) return m->length; } - -void msg_random(struct msg *m) -{ - for (int i = 0; i < m->length; i++) - m->data[i].f += box_muller(0, 1); - - m->endian = MSG_ENDIAN_HOST; -} diff --git a/src/random.c b/src/signal.c similarity index 53% rename from src/random.c rename to src/signal.c index e8a46e07a..7718be5b0 100644 --- a/src/random.c +++ b/src/signal.c @@ -11,6 +11,8 @@ **********************************************************************************/ #include +#include +#include #include "config.h" #include "utils.h" @@ -19,10 +21,19 @@ #define CLOCKID CLOCK_REALTIME +enum SIGNAL_TYPE { + TYPE_RANDOM, + TYPE_SINE, + TYPE_SQUARE, + TYPE_TRIANGLE, + TYPE_MIXED +}; + int main(int argc, char *argv[]) { - if (argc < 3 || argc > 4) { - printf("Usage: %s VALUES RATE [LIMIT]\n", argv[0]); + if (argc < 4 || argc > 5) { + printf("Usage: %s SIGNAL VALUES RATE [LIMIT]\n", argv[0]); + printf(" SIGNAL is on of: mixed random sine triangle square\n"); printf(" VALUES is the number of values a message contains\n"); printf(" RATE how many messages per second\n"); printf(" LIMIT only send LIMIT messages\n\n"); @@ -35,9 +46,22 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - struct msg m = MSG_INIT(atoi(argv[1])); - double rate = atof(argv[2]); - int limit = argc >= 4 ? atoi(argv[3]) : -1; + struct msg m = MSG_INIT(atoi(argv[2])); + double rate = atof(argv[3]); + int limit = argc >= 5 ? atoi(argv[4]) : -1; + int type = TYPE_MIXED; + + /* Parse signal type */ + if (!strcmp(argv[1], "random")) + type = TYPE_RANDOM; + else if (!strcmp(argv[1], "sine")) + type = TYPE_SINE; + else if (!strcmp(argv[1], "triangle")) + type = TYPE_TRIANGLE; + else if (!strcmp(argv[1], "square")) + type = TYPE_SQUARE; + else if (!strcmp(argv[1], "mixed")) + type = TYPE_MIXED; /* Setup timer */ struct itimerspec its = { @@ -55,14 +79,29 @@ int main(int argc, char *argv[]) /* Print header */ fprintf(stderr, "# %-20s\t\t%s\n", "sec.nsec(seq)", "data[]"); + struct timespec start = time_now(); + /* Block until 1/p->rate seconds elapsed */ - while (limit-- > 0 || argc < 4) { - struct timespec ts = time_now(); + while (limit-- > 0 || argc < 5) { + struct timespec now = time_now(); + double running = time_delta(&start, &now); - m.ts.sec = ts.tv_sec; - m.ts.nsec = ts.tv_nsec; + m.ts.sec = now.tv_sec; + m.ts.nsec = now.tv_nsec; - msg_random(&m); + for (int i = 0; i < m.length; i++) { + int rtype = type != TYPE_MIXED ? type : i % 4; + double ampl = i+1; + double freq = i+1; + + switch (rtype) { + case TYPE_RANDOM: m.data[i].f += box_muller(0, 0.02); break; + case TYPE_SINE: m.data[i].f = ampl * sin(running * freq * M_PI); break; + case TYPE_TRIANGLE: m.data[i].f = ampl * (fabs(fmod(running * freq, 1) - .5) - 0.25) * 4; break; + case TYPE_SQUARE: m.data[i].f = ampl * ( (fmod(running * freq, 1) < .5) ? -1 : 1); break; + } + } + msg_fprint(stdout, &m, MSG_PRINT_ALL & ~MSG_PRINT_OFFSET, 0); fflush(stdout);