1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

a collection of smaller changes

git-svn-id: https://zerberus.eonerc.rwth-aachen.de:8443/svn/s2ss/trunk@30 8ec27952-4edc-4aab-86aa-e87bb2611832
This commit is contained in:
Steffen Vogel 2014-06-05 09:34:58 +00:00
parent 6dd733334f
commit 49d13ccae4
5 changed files with 66 additions and 39 deletions

View file

@ -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

View file

@ -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()

View file

@ -8,7 +8,10 @@
#ifndef _UTILS_H_
#define _UTILS_H_
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <netinet/ip.h>
@ -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_ */

View file

@ -7,60 +7,62 @@
#include <string.h>
#include <stdlib.h>
#include <poll.h>
#include <errno.h>
#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;

View file

@ -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);