Added hack for letting nodes signal that they are 'done'. The simulator exits when all nodes are done. Fixed random initialization so that it is different for all nodes.

This commit is contained in:
adamdunkels 2006-10-23 09:01:06 +00:00
parent 0ed1d401d5
commit c67ce1c1b0
9 changed files with 89 additions and 21 deletions

View file

@ -30,7 +30,7 @@
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: contiki-main.c,v 1.3 2006/10/06 08:25:30 adamdunkels Exp $
* $Id: contiki-main.c,v 1.4 2006/10/23 09:01:06 adamdunkels Exp $
*/
#include "contiki.h"
@ -147,7 +147,7 @@ idle(void)
void
contiki_main(int flag)
{
random_init(0);
random_init(getpid());
leds_init();

View file

@ -24,7 +24,7 @@
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* $Id: display.c,v 1.3 2006/10/06 08:25:30 adamdunkels Exp $
* $Id: display.c,v 1.4 2006/10/23 09:01:06 adamdunkels Exp $
*
* Author: Adam Dunkels <adam@sics.se>
*
@ -251,8 +251,8 @@ display_tick(void)
} else {
e = NULL;
}
if(d->size > 40) {
d->size -= 8;
if(d->size > 20) {
d->size /= 2;
} else {
d->size -= 4;
}

View file

@ -30,7 +30,7 @@
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: ether.c,v 1.3 2006/10/06 08:25:30 adamdunkels Exp $
* $Id: ether.c,v 1.4 2006/10/23 09:01:06 adamdunkels Exp $
*/
/**
* \file
@ -88,6 +88,7 @@ static int s, sc;
#define PTYPE_SENSOR 3
#define PTYPE_LEDS 4
#define PTYPE_TEXT 5
#define PTYPE_DONE 6
struct ether_hdr {
int type;
@ -106,7 +107,23 @@ static int strength;
static int collisions = 1;
static int num_collisions = 0;
static int num_packets = 0;
#include <sys/time.h>
static struct timeval t1;
/*-----------------------------------------------------------------------------------*/
void
ether_print_stats(void)
{
unsigned long time;
struct timeval t2;
gettimeofday(&t2, NULL);
time = (t2.tv_sec * 1000 + t2.tv_usec / 1000) -
(t1.tv_sec * 1000 + t1.tv_usec / 1000);
printf("%d, %d, %f\n", num_packets, num_collisions, time/1000.0);
}
/*-----------------------------------------------------------------------------------*/
void
ether_set_collisions(int c)
@ -130,7 +147,9 @@ void
ether_server_init(void)
{
struct sockaddr_in sa;
gettimeofday(&t1, NULL);
memb_init(&packets);
list_init(active_packets);
@ -273,6 +292,9 @@ ether_server_poll(void)
case PTYPE_TEXT:
nodes_set_text(hdr->srcx, hdr->srcy, hdr->text);
break;
case PTYPE_DONE:
nodes_done(hdr->srcid);
break;
}
}
/* tv.tv_sec = 0;
@ -342,7 +364,9 @@ ether_tick(void)
/* Go through all active packets to see if anyone is sent within
range of this node. */
for(p = list_head(active_packets); p != NULL; p = p->next) {
num_packets++;
/* Update the node type. */
hdr = (struct ether_hdr *)p->data;
/* nodes_node(hdr->srcid)->type = hdr->srcnodetype;*/
@ -511,3 +535,17 @@ ether_send_sensor_data(struct sensor_data *d, int srcx, int srcy, int strength)
}
/*-----------------------------------------------------------------------------------*/
void
ether_send_done(void)
{
struct ether_hdr hdr;
hdr.srcx = node.x;
hdr.srcy = node.y;
hdr.type = PTYPE_DONE;
hdr.srcid = node.id;
node_send_packet((char *)&hdr, sizeof(struct ether_hdr));
}
/*-----------------------------------------------------------------------------------*/

View file

@ -30,7 +30,7 @@
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: ether.h,v 1.2 2006/09/26 22:10:12 adamdunkels Exp $
* $Id: ether.h,v 1.3 2006/10/23 09:01:06 adamdunkels Exp $
*/
#ifndef __ETHER_H__
#define __ETHER_H__
@ -46,6 +46,8 @@ struct ether_packet {
};
void ether_send_done(void);
u8_t ether_send(char *data, int len);
void ether_set_leds(int leds);
void ether_set_text(char *text);

View file

@ -30,7 +30,7 @@
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: main.c,v 1.3 2006/10/06 08:25:30 adamdunkels Exp $
* $Id: main.c,v 1.4 2006/10/23 09:01:06 adamdunkels Exp $
*/
/**
@ -123,7 +123,7 @@ start_node(int x, int y, int b)
usleep(1000 * ((random() & 0x0f) << 6) );
node_init(port - NODES_PORTBASE + 1, x, y, b);
node_init(port - NODES_PORTBASE + 2, x, y, b);
ethernode_init(port);
@ -134,11 +134,11 @@ start_node(int x, int y, int b)
}
/* printf("Adding sensor %d at (%d,%d)\n", pid, x, y);*/
main_process = 1;
nodes_add(pid, x, y, port);
nodes_add(pid, x, y, port, port - NODES_PORTBASE + 2);
++port;
return port - NODES_PORTBASE;
return port - NODES_PORTBASE + 1;
}
/*---------------------------------------------------------------------------*/
int

View file

@ -30,7 +30,7 @@
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: ethernode.c,v 1.2 2006/10/06 08:25:31 adamdunkels Exp $
* $Id: ethernode.c,v 1.3 2006/10/23 09:01:06 adamdunkels Exp $
*/
/**
* \file
@ -181,7 +181,7 @@ ethernode_send(void)
dest = ID_BROADCAST;
/* usleep(800 * (random_rand() % 1000));*/
usleep(100 * (random_rand() % 1000));
do_send(TYPE_DATA, dest, hdr, len);

View file

@ -30,7 +30,7 @@
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: node.h,v 1.2 2006/09/26 22:10:12 adamdunkels Exp $
* $Id: node.h,v 1.3 2006/10/23 09:01:06 adamdunkels Exp $
*/
#ifndef __NODE_H__
#define __NODE_H__
@ -54,4 +54,5 @@ int node_y(void);
void node_log(const char *fmt, ...);
#endif /* __NODE_H__ */

View file

@ -30,7 +30,7 @@
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: nodes.c,v 1.2 2006/09/26 22:10:12 adamdunkels Exp $
* $Id: nodes.c,v 1.3 2006/10/23 09:01:06 adamdunkels Exp $
*/
#include <signal.h>
#include <stdio.h>
@ -51,13 +51,15 @@ nodes_init(void)
}
/*---------------------------------------------------------------------------*/
void
nodes_add(int pid, int x, int y, int port)
nodes_add(int pid, int x, int y, int port, int id)
{
nodes[numnodes].pid = pid;
nodes[numnodes].x = x;
nodes[numnodes].y = y;
nodes[numnodes].port = port;
nodes[numnodes].leds = 0;
nodes[numnodes].done = 0;
nodes[numnodes].id = id;
++numnodes;
}
/*---------------------------------------------------------------------------*/
@ -126,3 +128,24 @@ nodes_find_pid(pid_t pid)
return NULL;
}
/*---------------------------------------------------------------------------*/
void
nodes_done(int id)
{
int i;
int num_done = 0;
for(i = numnodes; i >= 0; --i) {
if(nodes[i].id == id) {
nodes[i].done = 1;
}
if(nodes[i].done != 0) {
num_done++;
}
}
if(num_done == numnodes) {
ether_print_stats();
exit(0);
}
}
/*---------------------------------------------------------------------------*/

View file

@ -30,30 +30,34 @@
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: nodes.h,v 1.2 2006/09/26 22:10:12 adamdunkels Exp $
* $Id: nodes.h,v 1.3 2006/10/23 09:01:06 adamdunkels Exp $
*/
#ifndef __NODES_H__
#define __NODES_H__
#include <sys/types.h>
#define NODES_TEXTLEN 4
#define NODES_TEXTLEN 10
void nodes_init(void);
void nodes_add(int pid, int x, int y, int port);
void nodes_add(int pid, int x, int y, int port, int id);
void nodes_kill(void);
void nodes_set_leds(int x, int y, int leds);
void nodes_set_text(int x, int y, char *text);
void nodes_done(int id);
int nodes_num(void);
struct nodes_node *nodes_node(int num);
struct nodes_node *nodes_find_pid(pid_t pid);
struct nodes_node {
int pid;
int id;
int x, y;
int port;
int leds;
int done;
char text[NODES_TEXTLEN];
};