Пример #1
0
//**************************************************************************************************************************************************//
PROCESS_THREAD(RSSI_SCAN, ev, data)
{
    static struct etimer rssitimer;
    static struct rtimer rt;
    PROCESS_BEGIN();
    // Set the local address
    uip_ip6addr(&my_addr, 0, 0, 0, 0, 0, 0, 0, 0);
    uip_ds6_set_addr_iid(&my_addr, &uip_lladdr);
    uip_ds6_addr_add(&my_addr, 0, ADDR_MANUAL);

    //listening to port 3001 from beacons and sensors
    listen_conn = udp_new(NULL, UIP_HTONS(0), NULL);

    //Server Listen connection is bound too port 4001
    udp_bind(listen_conn, UIP_HTONS(RECEIVER_PORT));

    while(1)
    {
        //printf("inside while\n\r");
        //etimer_set(&rssitimer, CLOCK_SECOND/200);
        //GPIO_SET_PIN(GPIO_C_BASE, GPIO_PIN_MASK(1));
        //PROCESS_YIELD();
        PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
        if ((ev == tcpip_event))
        {
            recv_handler();
        }
    }
    PROCESS_END();
}
Пример #2
0
inline void HardwareSerial::store_char(unsigned char c)
{
  int i = (_rx_buffer->head + 1) % RX_BUFFER_SIZE;

  // if we should be storing the received character into the location
  // just before the tail (meaning that the head would advance to the
  // current location of the tail), we're about to overflow the buffer
  // and so we don't write the character or advance the head.
  if (i != _rx_buffer->tail) {
    _rx_buffer->buffer[_rx_buffer->head] = c;
    _rx_buffer->head = i;
  }
  if (recv_handler) {
    recv_handler(this, c);
  }
}
Пример #3
0
static void *
recv_from_learner(void *arg)
{
  void *zmq_recv_sock;
  recv_thd_data *thd = arg;

  if ((zmq_recv_sock = zmq_socket(thd->zmq_ctx, ZMQ_SUB))) {
    if (!zmq_connect(zmq_recv_sock, thd->recv_endpoint)) {
      grn_ctx ctx;
      if (!grn_ctx_init(&ctx, 0)) {
        if ((!grn_ctx_use(&ctx, db))) {
          msgpack_zone *mempool;
          if ((mempool = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE))) {
            grn_obj cmd_buf;
            zmq_pollitem_t items[] = {
              { zmq_recv_sock, 0, ZMQ_POLLIN, 0}
            };
            GRN_TEXT_INIT(&cmd_buf, 0);
            zmq_setsockopt(zmq_recv_sock, ZMQ_SUBSCRIBE, "", 0);
            while (loop) {
              zmq_poll(items, 1, 10000);
              if (items[0].revents & ZMQ_POLLIN) {
                recv_handler(&ctx, zmq_recv_sock, mempool, &cmd_buf);
              }
            }
            grn_obj_unlink(&ctx, &cmd_buf);
            msgpack_zone_free(mempool);
          } else {
            print_error("cannot create msgpack zone.");
          }
          /* db_close */
        } else {
          print_error("error in grn_db_open() on recv thread.");
        }
        grn_ctx_fin(&ctx);
      } else {
        print_error("error in grn_ctx_init() on recv thread.");
      }
    } else {
      print_error("cannot create recv zmq_socket.");
    }
  } else {
    print_error("cannot connect zmq_socket.");
  }
  return NULL;
}
Пример #4
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(ipv6_process, ev, data)
{


  PROCESS_BEGIN();

  // Set the local address
  uip_ip6addr(&my_addr, 0, 0, 0, 0, 0, 0, 0, 0);
  uip_ds6_set_addr_iid(&my_addr, &uip_lladdr);
  uip_ds6_addr_add(&my_addr, 0, ADDR_MANUAL);

  // Setup the destination address
  uiplib_ipaddrconv(RECEIVER_ADDR, &dest_addr);

  // Add a "neighbor" for our custom route
  // Setup the default broadcast route
  uiplib_ipaddrconv(ADDR_ALL_ROUTERS, &bcast_ipaddr);
  uip_ds6_nbr_add(&bcast_ipaddr, &bcast_lladdr, 0, NBR_REACHABLE);
  uip_ds6_route_add(&dest_addr, 128, &bcast_ipaddr);

  // Setup a udp "connection"
  client_conn = udp_new(&dest_addr, UIP_HTONS(RECEIVER_PORT), NULL);
  if (client_conn == NULL) {
    // Too many udp connections
    // not sure how to exit...stupid contiki
  }
  udp_bind(client_conn, UIP_HTONS(3001));

  etimer_set(&periodic_timer, 10*CLOCK_SECOND);

  while(1) {
    PROCESS_YIELD();

    if (etimer_expired(&periodic_timer)) {
      send_handler(ev, data);
      etimer_restart(&periodic_timer);
    } else if (ev == tcpip_event) {
      recv_handler();
    }
  }

  PROCESS_END();
}
Пример #5
0
static void dhcp_run(void)
{
	static bool udp_open_fail = FALSE;

	if(di.state == DHCP_STATE_INIT && di.action != DHCP_ACT_START) {
		DBG("wrong attempt");
		return;
	} else if(GetUDPSocketStatus(di.sock) == SOCKSTAT_CLOSED) {
		if(udp_open_fail == TRUE && !IS_TIME_PASSED(dhcp_run_tick, DHCP_RETRY_DELAY)) 
			goto RET_ALARM;
		ClsNetInfo(NI_IP_ADDR);
		ClsNetInfo(NI_SN_MASK);
		ClsNetInfo(NI_GW_ADDR);
		ClsNetInfo(NI_DNS_ADDR);
		if(UDPOpen(di.sock, DHCP_CLIENT_PORT) == RET_OK) {
			if(dhcp_async) sockwatch_open(di.sock, dhcp_async_cb);
			udp_open_fail = FALSE;
			dhcp_run_tick = wizpf_get_systick();
			dhcp_run_cnt = 0;
		} else {
			ERR("UDPOpen fail");
			udp_open_fail = TRUE;
			dhcp_run_tick = wizpf_get_systick();
			goto RET_ALARM;
		}
	}

	switch(di.state) {
	case DHCP_STATE_INIT:
		if(dhcp_run_cnt==0 && !IS_TIME_PASSED(dhcp_run_tick, DHCP_OPEN_DELAY)) 
			goto RET_ALARM;

		if(dhcp_run_cnt < DHCP_SEND_RETRY_COUNT) {
			dhcp_run_cnt++;
			if(send_discover() == RET_OK) {	// Discover ok
				if(dhcp_async) {
					DBG("DHCP Discovery Send Async");
					sockwatch_set(di.sock, WATCH_SOCK_UDP_SEND);
					return;	// alarm set is not needed
				} else {
					DBG("DHCP Discovery Sent");
					SET_STATE(DHCP_STATE_SEARCHING);
					dhcp_run_tick = wizpf_get_systick();
				}
			} else {
				ERRA("DHCP Discovery SEND fail - (%d)times", dhcp_run_cnt);
				dhcp_run_tick = wizpf_get_systick();
			}
		} else {
			ERRA("DHCP Discovery SEND fail - (%d)times", dhcp_run_cnt);
			dhcp_run_cnt = 0;
			UDPClose(di.sock);
			if(dhcp_async) sockwatch_close(di.sock);
			dhcp_fail();
			return; // alarm set is not needed
		}
		break;
	case DHCP_STATE_SEARCHING:
		if(!IS_TIME_PASSED(dhcp_run_tick, DHCP_RETRY_DELAY)) {
			int8 ret = recv_handler();
			if(ret == DHCP_MSG_OFFER) {
				SET_STATE(DHCP_STATE_SELECTING);
				dhcp_run_tick = wizpf_get_systick();
				dhcp_run_cnt = 0;
			} else if(ret != RET_NOK) DBGCRTCA(TRUE, "recv wrong packet(%d)", ret);
		} else {
			ERRA("DHCP Offer RECV fail - for (%d)msec", DHCP_RETRY_DELAY);
			SET_STATE(DHCP_STATE_INIT);
			dhcp_run_tick = wizpf_get_systick();
		}
		break;
	case DHCP_STATE_SELECTING:
		if(dhcp_run_cnt < DHCP_SEND_RETRY_COUNT) {
			dhcp_run_cnt++;
			if(send_request() == RET_OK) {	// Request ok
				if(dhcp_async) {
					DBG("DHCP Request Send Async");
					sockwatch_set(di.sock, WATCH_SOCK_UDP_SEND);
					return;	// alarm set is not needed
				} else {
					DBG("DHCP Request Sent");
					SET_STATE(DHCP_STATE_REQUESTING);
					dhcp_run_tick = wizpf_get_systick();
				}
			} else {
				ERRA("DHCP Request SEND fail - (%d)times", dhcp_run_cnt);
				dhcp_run_tick = wizpf_get_systick();
			}
		} else {
			ERRA("DHCP Request SEND fail - (%d)times", dhcp_run_cnt);
			dhcp_run_cnt = 0;
			UDPClose(di.sock);
			if(dhcp_async) sockwatch_close(di.sock);
			dhcp_fail();
			return; // alarm set is not needed
		}
		break;
	case DHCP_STATE_REQUESTING:
		if(!IS_TIME_PASSED(dhcp_run_tick, DHCP_RETRY_DELAY)) {
			int8 ret = recv_handler();
			if(ret == DHCP_MSG_ACK) {	// Recv ACK
				LOG("DHCP Success");
				SET_STATE(DHCP_STATE_IP_CHECK);
				dhcp_run_tick = wizpf_get_systick();
				dhcp_run_cnt = 0;
			} else if(ret == DHCP_MSG_NAK) {	// Recv NAK
				if(di.action == DHCP_ACT_START) {
					SET_STATE(DHCP_STATE_INIT);
					dhcp_run_tick = wizpf_get_systick();
				} else {
					SET_STATE(DHCP_STATE_BOUND);
				}
				dhcp_run_cnt = 0;
			} else if(ret != RET_NOK) DBGCRTCA(TRUE, "recv wrong packet(%d)", ret);
		} else {
			ERRA("DHCP ACK RECV fail - for (%d)msec", DHCP_RETRY_DELAY);
			if(di.action == DHCP_ACT_START) {
				SET_STATE(DHCP_STATE_INIT);
				dhcp_run_tick = wizpf_get_systick();
			} else {
				SET_STATE(DHCP_STATE_BOUND);
			}
		}
		break;
	case DHCP_STATE_IP_CHECK:
		//if(send_checker() == RET_OK) {
			SET_STATE(DHCP_STATE_BOUND);
			SetNetInfo(&workinfo);
			if(di.ip_update) di.ip_update();
			LOGA("DHCP ok - New IP (%d.%d.%d.%d)", 
				workinfo.ip[0], workinfo.ip[1], workinfo.ip[2], workinfo.ip[3]);
		//} else {
		//	SET_STATE(DHCP_STATE_INIT);
		//	ERR("IP Addr conflicted - IP(%d.%d.%d.%d)", workinfo.ip[0], workinfo.ip[1], workinfo.ip[2], workinfo.ip[3]);
		//	send_rel_dec(DHCP_MSG_DECLINE);
		//	if(di.ip_conflict) (*di.ip_conflict)();
		//}
		break;
	case DHCP_STATE_BOUND:
		di.action = DHCP_ACT_NONE;
		UDPClose(di.sock);
		if(dhcp_async) sockwatch_close(di.sock);
		return; // alarm set is not needed
	case DHCP_STATE_FAILED:
		return; // alarm set is not needed
	default:
		ERRA("wrong state(%d)", di.state);
		return; // alarm set is not needed
	}

RET_ALARM:
	if(dhcp_alarm) alarm_set(10, dhcp_alarm_cb, 0);
}
Пример #6
0
void
alltoall(int len)
{
    int cc;
    int i, n;
    int m;
    fd_set wfds, rfds;
    struct request send_req;

    send_req.rank = comm_rank;
    send_req.len  = len;

    for (i = 0; i < comm_size; i++) {
	if (i != comm_rank) {
	    recv_req[i].count = 0;
	} else {
	    recv_req[i].count = len;
	}
    }

    for (n = 0; n < comm_size - 1; n++) {
	/* send phase */
	send_req.rank = GET_RANK(send_req.rank + 1);
	send_req.fd = hosts[send_req.rank].sock;
	send_req.count = 0;
	send_handler(&send_req);

	assert(send_req.rank != comm_rank);

	for (;;) {
	    int nsel = 0;
	    m = -1;
	    FD_ZERO(&wfds);
	    FD_ZERO(&rfds);
	    if (send_req.count != send_req.len) {
		FD_SET(send_req.fd, &wfds);
		m = MAX(m, send_req.fd);
		nsel++;
	    } else {
		if (n + 1 != comm_size - 1)
		    break;	/* We have another pending send requests. */
	    }

	    for (i = 0; i < comm_size; i++) {
		if (recv_req[i].count == recv_req[i].len)
		    continue;
		FD_SET(recv_req[i].fd, &rfds);
		m = MAX(m, recv_req[i].fd);
		nsel++;
	    }

	    if (nsel == 0)
		break;
	    cc = select(m + 1, &rfds, &wfds, NULL, NULL);
	    nr_select++;
	    if (cc < 0)
		perror_exit("select", 1);

	    for (i = 0; i < comm_size; i++) {
		if (recv_req[i].count == recv_req[i].len)
		    continue;
		if (FD_ISSET(recv_req[i].fd, &rfds))
		    recv_handler(&recv_req[i]);
	    }
	    if (FD_ISSET(send_req.fd, &wfds))
		send_handler(&send_req);
	}
    }
}
Пример #7
0
/*
MAIN
Envoyer et recevoir des donnes
Gere les fenetres du GUI
*/
int main (int argc, char* argv[]) {

	if ( argc < 2 ) {
		printf ("Usage: %s PORT\n", argv[0]);
		exit (EXIT_FAILURE);
	}

	initscr();	// Start curses mode
	cbreak();	// Line buffering disabled, Pass on everty thing to me

	//my_win = create_newwin(height, width, starty, startx);
	f_haut	= definirFenetre( f_haut_hauteur, COLS, 0, 0 );
	f_bas	= definirFenetre( f_bas_hauteur, COLS, (LINES - f_bas_hauteur - marge_bas), 0 );
	f_info	= definirFenetre( f_info_hauteur, COLS, (LINES - donnerHauteur(f_bas) - f_info_hauteur - marge_bas), 0 );
	f_cmd 	= definirFenetre( f_cmd_hauteur, COLS, (LINES - donnerHauteur(f_bas) - donnerHauteur(f_info) - marge_bas - f_cmd_hauteur), 0);
	f_chat	= definirFenetre( (LINES - donnerHauteur(f_haut) - donnerHauteur(f_cmd) - donnerHauteur(f_info) - donnerHauteur(f_bas) - marge_bas), COLS, donnerHauteur(f_haut), 0 );

	refresh();
	w_haut	= create_newwin_with_border( f_haut );
	w_bas	= create_newwin_no_border( f_bas );
	w_info	= create_newwin_with_border( f_info );
	w_cmd	= create_newwin_with_border( f_cmd );
	w_chat	= create_newwin_no_border( f_chat );

	scrollok( w_chat, 1 );
	wsetscrreg( w_chat, donnerStarty(f_chat), donnerHauteur(f_chat) );
	wtimeout(w_bas, 500);

	mvwprintw(w_haut, 1, 1, "CHAT CLIENT");
	mvwprintw(w_cmd, 1, 1, "");
	mvwprintw(w_info, 1, 1, "/nom usager\t/mp usager msg\t/creer   groupe type\t/info  groupe\t\t/accept  usager groupe");
	mvwprintw(w_info, 2, 1, "\t\t/mg groupe msg\t/joindre groupe\t\t/liste usagers\t\t/refuser usager groupe");
	mvwprintw(w_info, 3, 1, "/quitter\t\t\t/byebye  groupe\t\t/liste groupes\t\t/stats   groupe");
	wmove( w_bas, 0, 0 );
	wrefresh(w_haut);
	wrefresh(w_info);
	wrefresh(w_bas);
	wrefresh(w_cmd);

	
	struct sockaddr_in	serveur;
	struct hostent*		hp;

	socket_d = socket (AF_INET, SOCK_STREAM, 0);
	if (socket_d < 0) {
		endwin();
		printf("Erreur lors de la création de la socket !\n");
		return 1;
	}
	setnonblocking (socket_d);

	hp = gethostbyname("localhost");
	if (hp==0) {
		endwin();
		close (socket_d);
		printf("Hôte inconnu!\n");
		return 2;
	}

	serveur.sin_family = AF_INET;
	serveur.sin_port = htons(atoi(argv[1]));
	bcopy((char *)hp->h_addr, (char *)&serveur.sin_addr, hp->h_length);

	if ( connect(socket_d,(struct sockaddr *)&serveur,sizeof(struct sockaddr_in)) < 0 ) {
		endwin();
		close (socket_d);
		printf("Erreur lors de la création d'une nouvelle connexion !\n");
		return 3;
	}


	nom_usager_defini = 0;

	input = chaineCreerVide( COLS );
	while ( 1 ) {
		key_handler();

		if ( ! recv_handler() )
			break;
	}	

	endwin ();
	close (socket_d);
	return 0;
}