From 49d13ccae47958a2333c08d257ce6d2c5ebb5139 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Thu, 5 Jun 2014 09:34:58 +0000 Subject: [PATCH] a collection of smaller changes git-svn-id: https://zerberus.eonerc.rwth-aachen.de:8443/svn/s2ss/trunk@30 8ec27952-4edc-4aab-86aa-e87bb2611832 --- include/msg.h | 1 - include/path.h | 18 ++++++++++------- include/utils.h | 26 ++++++++++++++++++++++++- src/path.c | 52 ++++++++++++++++++++++++------------------------- src/random.c | 8 ++++---- 5 files changed, 66 insertions(+), 39 deletions(-) diff --git a/include/msg.h b/include/msg.h index ff6195c95..c4ae7e6c0 100644 --- a/include/msg.h +++ b/include/msg.h @@ -21,7 +21,6 @@ * This struct defines the format of a message (protocol version 0) * Its declared as "packed" because it represents the "on wire" data. */ - struct msg { /// Sender device ID diff --git a/include/path.h b/include/path.h index d5ba79038..2f0421c0c 100644 --- a/include/path.h +++ b/include/path.h @@ -34,13 +34,16 @@ struct path int (*hooks[MAX_HOOKS])(struct msg *m); /// Counter for received messages - int msg_received; + int received; - /// Counter for dropped messages - int msg_dropped; + /// Counter for messages which arrived reordered + int delayed; + + /// Counter for messages which arrived multiple times + int duplicated; /// Last known message number - int seq_no; + int sequence; /// The current path state enum path_state state; @@ -54,14 +57,15 @@ struct path * * Memory is allocated dynamically and has to be freed by path_destroy() * + * @param p A pointer to the path structure * @param in The node we are receiving messages from * @param out The nodes we are sending to * @param len count of outgoing nodes * @return - * - a pointer to the new path on success - * - NULL if an error occured + * - 0 on success + * - otherwise an error occured */ -struct path* path_create(struct node *in, struct node *out); +int path_create(struct path *p, struct node *in, struct node *out); /** * @brief Delete a path created by path_create() diff --git a/include/utils.h b/include/utils.h index 806cf353a..1edf5e0bb 100644 --- a/include/utils.h +++ b/include/utils.h @@ -8,7 +8,10 @@ #ifndef _UTILS_H_ #define _UTILS_H_ +#include #include +#include +#include #include @@ -32,11 +35,13 @@ void print(enum log_level lvl, const char *fmt, ...); * * @param addr A string containing the hostname/ip and port seperated by a colon * @param sa A pointer to the resolved address + * @param flags Flags for gai * @return * - 0 on success */ -int resolve(const char *addr, struct sockaddr_in *sa); +int resolve(const char *addr, struct sockaddr_in *sa, int flags); +/// Check assertion and exit if failed #define assert(exp) do { \ if (!(exp)) { \ print(ERROR, "Assertion failed: '%s' in %s, %s:%d", \ @@ -44,23 +49,42 @@ int resolve(const char *addr, struct sockaddr_in *sa); exit(EXIT_FAILURE); \ } } while (0) +/// Printf alike debug message with level #define debug(lvl, msg, ...) do { \ if (lvl <= V) \ print(DEBUG, msg, ##__VA_ARGS__); \ } while (0) +/// Printf alike info message #define info(msg, ...) do { \ print(INFO, msg, ##__VA_ARGS__); \ } while (0) +/// Printf alike warning message #define warn(msg, ...) do { \ print(WARN, msg, ##__VA_ARGS__); \ } while (0) +/// Print error and exit #define error(msg, ...) do { \ print(ERROR, msg, ##__VA_ARGS__); \ exit(EXIT_FAILURE); \ } while (0) +/// Print error and strerror(errno) +#define perror(msg, ...) do { \ + print(ERROR, msg ": %s", ##__VA_ARGS__, \ + strerror(errno)); \ + exit(EXIT_FAILURE); \ + } while (0) + +/// Print configuration error and exit +#define cerror(c, msg, ...) do { \ + print(ERROR, msg " in %s:%u", ##__VA_ARGS__, \ + config_setting_source_file(c), \ + config_setting_source_line(c)); \ + exit(EXIT_FAILURE); \ + } while (0) + #endif /* _UTILS_H_ */ diff --git a/src/path.c b/src/path.c index b9ae8b931..0bca8f3c0 100644 --- a/src/path.c +++ b/src/path.c @@ -7,60 +7,62 @@ #include #include -#include #include +#include "cfg.h" #include "utils.h" #include "path.h" -struct path* path_create(struct node *in, struct node *out) -{ - struct path *p = malloc(sizeof(struct path)); - if (!p) - return NULL; +extern struct config config; +int path_create(struct path *p, struct node *in, struct node *out) +{ memset(p, 0, sizeof(struct path)); p->in = in; p->out = out; - return p; + return 0; } void path_destroy(struct path *p) { - if (!p) - return; - - free(p); + assert(p); } static void * path_run(void *arg) { struct path *p = (struct path *) arg; - struct pollfd pfd; struct msg m; - pfd.fd = p->in->sd; - pfd.events = POLLIN; + assert(p); - debug(1, "Established path: %12s => %s => %-12s", p->in->name, NAME, p->out->name); + debug(1, "Established path: %12s => %s => %-12s", p->in->name, config.name, p->out->name); /* main thread loop */ while (p->state == RUNNING) { - /* wait for new incoming messages */ - //poll(&pfd, 1, 1); + /* Receive message */ + msg_recv(&m, p->in); - /* receive message */ - node_recv(p->in, &m); + /* Check message sequence number */ + if (m.sequence < p->sequence) { + p->delayed++; + continue; + } + else if (m.sequence == p->sequence) { + p->duplicated++; + } - /* call hooks */ + p->received++; + + + /* Call hooks */ for (int i = 0; i < MAX_HOOKS && p->hooks[i]; i++) { p->hooks[i](&m); } - /* send messages */ - node_send(p->out, &m); + /* Send message */ + msg_send(&m, p->out); } return NULL; @@ -68,8 +70,7 @@ static void * path_run(void *arg) int path_start(struct path *p) { - if (!p) - return -EFAULT; + assert(p); p->state = RUNNING; pthread_create(&p->tid, NULL, &path_run, (void *) p); @@ -79,8 +80,7 @@ int path_stop(struct path *p) { void * ret; - if (!p) - return -EFAULT; + assert(p); p->state = STOPPED; diff --git a/src/random.c b/src/random.c index 81d97221a..8206b7cb8 100644 --- a/src/random.c +++ b/src/random.c @@ -50,9 +50,9 @@ int main(int argc, char *argv[]) int rate = atoi(argv[3]); struct msg msg = { - .dev_id = atoi(argv[1]), - .msg_len = atoi(argv[2]) * sizeof(double), - .seq_no = 0, + .device = atoi(argv[1]), + .length = atoi(argv[2]) * sizeof(double), + .sequence = 0, .data = { 0 } }; @@ -96,7 +96,7 @@ int main(int argc, char *argv[]) }; /* Print header */ - printf("# %-6s %-8s %-12s\n", "dev_id", "seq_no", "data"); + fprintf(stderr, "# %-6s %-8s %-12s\n", "dev_id", "seq_no", "data"); timer_create(CLOCKID, &sev, &t); timer_settime(t, 0, &its, NULL);