2014-07-14 11:49:44 +00:00
|
|
|
/** Message related functions.
|
2014-06-05 09:34:29 +00:00
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2014, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-06-05 09:34:55 +00:00
|
|
|
#include <errno.h>
|
2014-07-18 16:05:49 +00:00
|
|
|
#include <byteswap.h>
|
2014-06-05 09:34:55 +00:00
|
|
|
#include <arpa/inet.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
#include "msg.h"
|
2014-07-07 07:53:42 +00:00
|
|
|
#include "node.h"
|
2014-06-05 09:34:55 +00:00
|
|
|
#include "utils.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-07-18 16:05:49 +00:00
|
|
|
void msg_swap(struct msg *m)
|
2014-06-05 09:34:29 +00:00
|
|
|
{
|
2014-07-18 16:05:49 +00:00
|
|
|
uint32_t *data = (uint32_t *) m->data;
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-07-18 16:05:49 +00:00
|
|
|
/* Swap sequence number */
|
|
|
|
m->sequence = bswap_16(m->sequence);
|
|
|
|
|
|
|
|
/* Swap data */
|
|
|
|
for (int i = 0; i < m->length; i++)
|
|
|
|
data[i] = bswap_32(data[i]);
|
|
|
|
|
|
|
|
m->endian ^= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int msg_fprint(FILE *f, struct msg *m)
|
|
|
|
{
|
|
|
|
assert(m->endian == MSG_ENDIAN_HOST);
|
|
|
|
|
|
|
|
fprintf(f, "%-8hu", m->sequence);
|
|
|
|
|
|
|
|
for (int i = 0; i < m->length; i++)
|
|
|
|
fprintf(f, "%-12.6f ", m->data[i].f);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
fprintf(f, "\n");
|
2014-06-05 09:34:53 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-04 15:58:11 +00:00
|
|
|
int msg_fscan(FILE *f, struct msg *m)
|
2014-06-05 09:34:53 +00:00
|
|
|
{
|
2014-07-04 15:58:11 +00:00
|
|
|
fscanf(f, "%8hu ", &m->sequence);
|
2014-06-05 09:34:53 +00:00
|
|
|
|
2014-07-04 15:58:11 +00:00
|
|
|
for (int i = 0; i < m->length; i++)
|
2014-07-18 16:05:49 +00:00
|
|
|
fscanf(f, "%12f ", &m->data[i].f);
|
2014-06-05 09:34:53 +00:00
|
|
|
|
|
|
|
fscanf(f, "\n");
|
2014-07-18 16:05:49 +00:00
|
|
|
|
|
|
|
m->endian = MSG_ENDIAN_HOST;
|
|
|
|
|
2014-06-05 09:34:53 +00:00
|
|
|
return 0;
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 09:34:53 +00:00
|
|
|
void msg_random(struct msg *m)
|
2014-06-05 09:34:29 +00:00
|
|
|
{
|
2014-07-18 16:05:49 +00:00
|
|
|
assert(m->endian == MSG_ENDIAN_HOST);
|
|
|
|
|
2014-07-04 15:58:11 +00:00
|
|
|
for (int i = 0; i < m->length; i++)
|
2014-07-18 16:05:49 +00:00
|
|
|
m->data[i].f += (float) random() / RAND_MAX - .5;
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:53 +00:00
|
|
|
m->sequence++;
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 09:34:55 +00:00
|
|
|
int msg_send(struct msg *m, struct node *n)
|
|
|
|
{
|
2014-07-18 16:05:49 +00:00
|
|
|
/* We dont care about the endianess of outgoing messages */
|
|
|
|
|
2014-07-04 15:58:11 +00:00
|
|
|
if (sendto(n->sd, m, (m->length+1) * 4, 0,
|
2014-07-18 16:05:49 +00:00
|
|
|
(struct sockaddr *) &n->remote,
|
|
|
|
sizeof(struct sockaddr_in)) < 0)
|
2014-06-05 09:34:55 +00:00
|
|
|
perror("Failed sendto");
|
|
|
|
|
2014-07-04 15:58:11 +00:00
|
|
|
debug(10, "Message sent to node '%s'", n->name);
|
2014-06-25 01:53:37 +00:00
|
|
|
|
|
|
|
return 0;
|
2014-06-05 09:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int msg_recv(struct msg *m, struct node *n)
|
|
|
|
{
|
2014-07-18 16:05:49 +00:00
|
|
|
/** @todo Fix this for multiple paths calling msg_recv. */
|
|
|
|
|
|
|
|
/* Receive message from socket */
|
2014-06-05 09:34:55 +00:00
|
|
|
if (recv(n->sd, m, sizeof(struct msg), 0) < 0)
|
|
|
|
perror("Failed recv");
|
|
|
|
|
2014-07-18 16:05:49 +00:00
|
|
|
/* Convert message to host endianess */
|
|
|
|
if (m->endian != MSG_ENDIAN_HOST)
|
|
|
|
msg_swap(m);
|
|
|
|
|
2014-07-04 15:58:11 +00:00
|
|
|
debug(10, "Message received from node '%s'", n->name);
|
2014-06-25 01:53:37 +00:00
|
|
|
|
|
|
|
return 0;
|
2014-06-05 09:34:55 +00:00
|
|
|
}
|