Пример #1
0
int
main (int argc, char ** argv)
{
  char * device = DEFAULT_FLOCK_DEVICE;
  int number_of_birds = atoi (DEFAULT_NUMBER_OF_BIRDS);
  double noise_level = atof (DEFAULT_NOISE_LEVEL);
  flock_t flock = NULL;
  bird_data_t data_of_birds = NULL;
  int flockfd = -1;

  int port = 0;
  int sockfd = -1;
  char * host = NULL;
  struct sockaddr_in host_addr;

  fd_set input_fd_set;
  int maxfd;

  OSC_space_t space = NULL;

  int c;
  int count;
  int result = EXIT_SUCCESS;

  /* Parse arguments. */
  opterr = 0;

  while ((c = getopt (argc, argv, "d:b:n:")) != -1)
    switch (c)
      {
      case 'd':
        device = optarg;
        break;
      case 'b':
        number_of_birds = atoi (optarg);
        break;
      case 'n':
        noise_level = atof (optarg);
        break;
      default:
        break;
      }

  if (argc - optind != 2)
    {
      usage (argv[0]);
      exit (EXIT_FAILURE);
    }

  host = argv[optind++];
  port = atoi (argv[optind++]);

  get_more_priority ();
  signal (SIGINT, handle_signal);

  fprintf (stderr, "Preparing flock device: %s, number of birds: %d.\n",
           device, number_of_birds);

  if ((flock = flock_hl_open (device,
                              number_of_birds,
                              flock_bird_record_mode_position_angles,
                              1, 1)) == NULL)
    {
      result = EXIT_FAILURE;
      goto terminate;
    }

  flockfd = flock_get_file_descriptor (flock);

  /* TODO: use a command-line option for the local port. */
  fprintf (stderr, "Opening configuration port: %d.\n", port + 1);

  if ((sockfd = open_socket (port + 1)) == -1)
    {
      result = EXIT_FAILURE;
      goto terminate;
    }

  fprintf (stderr, "Preparing communication with host: %s, port: %d.\n",
           host, port);

  if ((fill_host_addr (host, port, &host_addr)) == -1)
    {
      result = EXIT_FAILURE;
      goto terminate;
    }

  FD_ZERO (&input_fd_set);
  FD_SET (sockfd, &input_fd_set);
  FD_SET (flockfd, &input_fd_set);
  maxfd = (sockfd < flockfd) ? flockfd : sockfd;

  data_of_birds = (bird_data_t)
    malloc (number_of_birds * sizeof (struct bird_data_s));

  OSC_init ();
  space = OSC_space_make ();
  OSC_space_register_method
    (space, OSC_method_make ("zthreshold", ",f", set_zthreshold, NULL));

  count = 0;

  /* First values. */
  {
    bird_data_t data;
    int bird;

    if (flock_next_record (flock, 1) == 0)
      {
        fprintf (stderr, "Can't get response from flock.\n");
        result = EXIT_FAILURE;
        goto terminate;
      }

    count++;

    for (bird = 0, data = data_of_birds;
         bird < number_of_birds;
         bird++, data++)
      {
        memcpy (&data->rec,
                flock_get_record (flock, bird + 1),
                sizeof (data->rec));
        data->zset = 0;
        data->zcount = 0;
        data->maxdz = 0;
        data->lastbump = 0;
        data->bumpcount = 0;
      }
  }

  fprintf (stderr, "Running... (Hit Ctrl-C to stop.)\n");

  while (!terminate)
    {
      fd_set read_fd_set;

      read_fd_set = input_fd_set;

      /* Block until new data is available, either from the flock or
         from peer. */
      if (select (maxfd + 1, &read_fd_set, NULL, NULL, NULL) == -1)
        {
          perror ("select");
          result = EXIT_FAILURE;
          goto terminate;
        }

      if (FD_ISSET (sockfd, &read_fd_set))
        /* Get data from peer. */
        receive (space, sockfd);

      if (FD_ISSET (flockfd, &read_fd_set))
        {
          bird_data_t data;
          int bird;

          /* Get data from flock. */
          if (flock_next_record (flock, 1) == 0)
            {
              result = EXIT_FAILURE;
              goto terminate;
            }

          count++;

          for (bird = 0, data = data_of_birds;
               bird < number_of_birds;
               bird++, data++)
            {
              double dx, dy, dz;

              /* Shift previous record. */
              memcpy (&data->prev_rec, &data->rec, sizeof (data->rec));

              /* Copy bird's record. */
              memcpy (&data->rec,
                      flock_get_record (flock, bird + 1),
                      sizeof (data->rec));

              dx = data->rec.values.pa.x - data->prev_rec.values.pa.x;
              dy = data->rec.values.pa.y - data->prev_rec.values.pa.y;
              dz = data->rec.values.pa.z - data->prev_rec.values.pa.z;

              {
                /* Coordinates. */

                double distance = sqrt ((dx * dx) + (dy * dy) + (dz * dz));

                if (distance > noise_level)
                  send_record (sockfd, &host_addr, bird + 1, &data->rec);
              }

              {
                /* Bumps. */

                if ((count - data->lastbump) < after_bump_delay)
                  continue;

                if (dz > zthreshold)
                  {
                    data->zset = 1;

                    if (data->maxdz < dz)
                      data->maxdz = dz;
                  }

                if (!data->zset)
                  {
                    data->maxdz = 0;
                    continue;
                  }

                /* Proposition: delay could depend on maxdz. */
                if (dz < 0.5 * zthreshold)
                  {
                    send_bump (sockfd, &host_addr, bird + 1, data->maxdz);
                    /*              fprintf (stderr, "bird %d bumps (%g).\n", */
                    /*                       bird + 1, data->maxdz); */
                    data->zset = 0;
                    data->maxdz = 0;
                    data->lastbump = count;
                    data->bumpcount++;
                  }
              }
            }
        }
    }

 terminate:
  fprintf (stderr, "Closing device and connection.\n");

  if (sockfd != -1)
    close_socket (sockfd);

  if (flock)
    flock_hl_close (flock);

  if (data_of_birds)
    free (data_of_birds);

  if (space)
    OSC_space_free (space);

  return result;
}
Пример #2
0
int connectFtpServer(char * server_ip, int port, char * user, char * password)
{
    int socket_control;
    giLogLine++;
    if( server_ip == NULL || user == NULL)
    {
        plog( "%s\n\n", "Fun(connectFtpServer): server_ip or user is NULL." );
        return -1;
    }

    int error;
    char log[1000]={0};

    error = fill_host_addr( server_ip, &ftp_server, port );
    if( error == 254 )
    {
        plog("%s\n\n", "Fun(connectFtpServer): Invalid port!");
        return -1;
    }

    if( error == 253 )
    {
        plog("%s\n\n", "Fun(connectFtpServer): Invalid address!");
        return -1;
    }

    struct timeval outtime;
    int type = 1;
    socket_control = socket( AF_INET,SOCK_STREAM, 0 );
    if( socket_control < 0 )
    {
        plog("%s\n\n", "Fun(connectFtpServer): Creat socket error.");
        return -1;
    }

    if( type == 1 )
    {
        outtime.tv_sec = 10;
        outtime.tv_usec = 0;// 300000;
    }
    else
    {
        outtime.tv_sec = 5;
        outtime.tv_usec = 0;
    }

    //fcntl(socket_control ,F_SETFL, ~O_NONBLOCK);
    //set = setsockopt( socket_control, SOL_SOCKET, SO_RCVTIMEO, &outtime, sizeof(outtime) );

    //connect to the server
    if(connect( socket_control, (struct sockaddr *) &ftp_server, sizeof(struct sockaddr_in) ) <0 )
    {
        memset( log, 0, 1000 );
        sprintf(log,"Fun(connectFtpServer): Can't connet to the server:%s,port:%d\n\n",inet_ntoa(ftp_server.sin_addr),ntohs(ftp_server.sin_port));
        plog( "%s", log );
        close(socket_control);
        return -1;
    }
    else
    {
        memset( log, 0, 1000 );

        sprintf( log, "Fun(connectFtpServer): Successfully connect to server:%s,port:%d\n",inet_ntoa(ftp_server.sin_addr),ntohs(ftp_server.sin_port));
        plog( "%s", log );
    }
	error = ftp_get_reply( socket_control, NULL );
	if( error == 421 )//There are too many connections from your internet address.
	{
        plog("%s\n\n","Fun(connectFtpServer): 421 too many connections.");
        close(socket_control);
        return -1;
	}
	else if( error == 220)//Successfully connect
	{
        if( (error = ftp_login(socket_control, user, password)) == -1 )
        {
            plog("%s\n\n", "Fun(connectFtpServer): user or password error.");
            close(socket_control);
            return -1;
        }

        plog("Fun(connectFtpServer): socket_control = %d\n",socket_control);
        return socket_control;
	}
	else
	{
        plog("%s\n\n", "Fun(connectFtpServer): Connect error!");
        close(socket_control);
        return -1;
	}

}