nodes.c

00001 /*
00002  * Copyright (c) 2004, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * Author: Adam Dunkels <adam@sics.se>
00032  *
00033  * $Id: nodes.c,v 1.8 2008/05/14 19:22:58 adamdunkels Exp $
00034  */
00035 #include <signal.h>
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 
00040 #include "nodes.h"
00041 
00042 
00043 static int numnodes;
00044 
00045 static struct nodes_node nodes[2000];
00046 
00047 int nodes_base_node_port = 0;
00048 /*---------------------------------------------------------------------------*/
00049 void
00050 nodes_init(void)
00051 {
00052   numnodes = 0;
00053 }
00054 /*---------------------------------------------------------------------------*/
00055 void
00056 nodes_add(int pid, int x, int y, int port, int id)
00057 {
00058   nodes[numnodes].pid = pid;
00059   nodes[numnodes].x = x;
00060   nodes[numnodes].y = y;
00061   nodes[numnodes].port = port;
00062   nodes[numnodes].leds = 0;
00063   nodes[numnodes].done = 0;
00064   nodes[numnodes].id = id;
00065   ++numnodes;
00066 }
00067 /*---------------------------------------------------------------------------*/
00068 void
00069 nodes_kill(void)
00070 {
00071   int i;
00072   for(i = 0; i < numnodes; ++i) {
00073     kill(nodes[i].pid, SIGTERM);
00074   }
00075 }
00076 /*---------------------------------------------------------------------------*/
00077 int
00078 nodes_num(void)
00079 {
00080   return numnodes;
00081 }
00082 /*---------------------------------------------------------------------------*/
00083 struct nodes_node *
00084 nodes_node(int num)
00085 {
00086   if(num > numnodes) {
00087     fprintf(stderr, "nodes_node: request for %d > %d\n", num, numnodes);
00088     abort();
00089   }
00090   return &nodes[num];
00091 }
00092 /*---------------------------------------------------------------------------*/
00093 static struct nodes_node *
00094 find_node(int x, int y)
00095 {
00096   int i;
00097 
00098   for(i = numnodes; i >= 0; --i) {
00099     if(nodes[i].x == x && nodes[i].y == y) {
00100       return &nodes[i];
00101     }
00102   }
00103   return &nodes[0];
00104 }
00105 /*---------------------------------------------------------------------------*/
00106 void
00107 nodes_set_leds(int x, int y, int leds)
00108 {
00109   find_node(x, y)->leds = leds;
00110 }
00111 /*---------------------------------------------------------------------------*/
00112 void
00113 nodes_set_text(int x, int y, char *text)
00114 {
00115   strncpy(find_node(x, y)->text, text, NODES_TEXTLEN);
00116 }
00117 /*---------------------------------------------------------------------------*/
00118 void
00119 nodes_set_radio_status(int x, int y, int radio_status)
00120 {
00121   find_node(x, y)->radio_status = radio_status;
00122 }
00123 /*---------------------------------------------------------------------------*/
00124 void
00125 nodes_set_line(int x, int y, int linex, int liney)
00126 {
00127   struct nodes_node *n;
00128 
00129   n = find_node(x, y);
00130   n->linex = linex;
00131   n->liney = liney;
00132 }
00133 /*---------------------------------------------------------------------------*/
00134 struct nodes_node *
00135 nodes_find_pid(pid_t pid)
00136 {
00137   int i;
00138   printf("Nofodes %d\n", numnodes);
00139   for(i = 0; i < numnodes; ++i) {
00140     printf("%d == %d\n", pid, nodes[i].pid); fflush(NULL);
00141     if(nodes[i].pid == pid) {
00142       return &nodes[i];
00143     }
00144   }
00145   return NULL;
00146 }
00147 /*---------------------------------------------------------------------------*/
00148 void
00149 nodes_done(int id)
00150 {
00151   int i;
00152   int num_done = 0;
00153 
00154   for(i = numnodes; i >= 0; --i) {
00155     if(nodes[i].id == id) {
00156       nodes[i].done = 1;
00157     }
00158     if(nodes[i].done != 0) {
00159       num_done++;
00160     }
00161   }
00162 
00163   if(num_done == numnodes) {
00164     exit(0);
00165   }
00166 }
00167 /*---------------------------------------------------------------------------*/

Generated on Mon Apr 11 14:23:44 2011 for Contiki 2.5 by  doxygen 1.6.1