void loading_squares_behaviour::init(resource_hub * resources) {
	init_rendering(resources);

	// random numbers
	generator.seed((uint32_t)time(nullptr));
	t_distribution = std::uniform_real_distribution<float>(0.f, 0.5f);
	dt_distribution = std::uniform_real_distribution<float>(0.2f, 0.5f);

	// init squares
	init_squares();

	upload = false;
}
Ejemplo n.º 2
0
int main(int argc, char **argv)
{
  pthread_t db_thread_id;

  int sockfd, newfd; /* Socket descriptor and new descriptor */
  struct sockaddr_in my_addr; // my address information
  struct sockaddr_in their_addr; // connector's address information
  socklen_t sin_size;
  int yes=1, quit;

  struct square_t square[MAX_SQUARES]; // Array that holds runtime data for squares
  init_squares(square,MAX_SQUARES);    // Init this array

  init_the_very_ugly_array (very_ugly_array, MAX_SQUARES);

  if(argc!=2) // Some error handling
  {
    fprintf(stderr,"usage: %s port", argv[0]);
    exit(EXIT_FAILURE);
  }

  if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1) // Create an internet TCP-socket
  {
    perror("socket");
    exit(EXIT_FAILURE);
  }

  if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int))==-1)
  {
    perror("setsockopt");
    exit(EXIT_FAILURE);
  }

  my_addr.sin_family = AF_INET; // host byte order
  my_addr.sin_port = htons( atoi(argv[1]) ); // short, network byte order
  my_addr.sin_addr.s_addr = INADDR_ANY; //Automatically fill in my IP
  memset(my_addr.sin_zero,'\0',sizeof(my_addr.sin_zero));

  if(bind(sockfd,(struct sockaddr*)&my_addr, sizeof(my_addr))==-1)
  {
    perror("bind");
    exit(EXIT_FAILURE);
  }

  if(listen(sockfd,BACKLOG)==-1)
  {
    perror("listen");
    exit(EXIT_FAILURE);
  }

  pthread_create(&db_thread_id,NULL,db_thread,square);

  /* Main loop waiting for connections */
  quit = 0;
  while (!quit)
  {

    int newIndex=-1;

    /* This check the sd if there is a pending connection.
    * If there is one, accept that, and open a new socket for communicating */
    sin_size = sizeof(their_addr);
    if ((newfd = accept(sockfd,(struct sockaddr*)&their_addr,&sin_size))==-1)
    {
      perror("accept");
      continue;         // Goto loop beginning immediately. This accept failed.
    }

    newIndex = find_free_slot(square,MAX_SQUARES);
    printf("New index: %d.\n", newIndex);
    if(newIndex==-1)
    {
      close(newfd); // Close the socket descriptor to indicate to client that
      continue;     // we have no more space for it. Then goto beginning of loop.
    }

    square[newIndex].free=0;             // Indicate that this slot is no longer free.
    square[newIndex].csd = newfd;        // Note the desciptor to communicate via.
    square[newIndex].myindex = newIndex; // Note which index the thread has

    /* Now we can communicate with the client using csd socket (from accept)
     * sd will remain opened waiting other connections */

    /* Get the remote address */
    printf("Got connection from %s.\n",inet_ntoa(their_addr.sin_addr));
    strcpy(square[newIndex].remote_ip_str, inet_ntoa(their_addr.sin_addr));

    // Set the server port in square struct
    sprintf(square[newIndex].server_port,"%d",atoi(argv[1]) );


    // Serve the client in a separate thread. Main program will accept other clients.
    pthread_create(&(square[newIndex].thread_id), NULL, do_square, &(square[newIndex]));

  }

  return EXIT_SUCCESS;

}