void collect(socket_fd client_socket){
  packet_t anything;
  unsigned int usleep_time = (unsigned int) 1000000 / samples_per_second;
  struct sockaddr_in* sock = alloc(struct sockaddr_in, 1);
  char buffer[sizeof(packet_t)];
 
  sock->sin_family = AF_INET;
  sock->sin_port = htons(sink_dport);
  sock->sin_addr.s_addr = inet_addr(sink_dip);

  while(npackets > 0){
    memset(&anything, 0, sizeof(anything));
    init_packet(&anything);

    write_data(anything.accumulated_time, anything.in_time, anything.id, "me", anything.samples);
    
    swap_packet_byte_order(&anything);

    memcpy(buffer, &anything, sizeof(packet_t)); 

    if(sendto(client_socket, buffer, sizeof(packet_t), 0,(const struct sockaddr *) sock, sizeof(struct sockaddr_in)) == -1){
      log(E, "Could not send packet\n");
    }

    npackets--;

    usleep(usleep_time);
  }

  printf("Simulation has ended.\n");
}
void serve(int server_fd){
  char buff[sizeof(packet_t)];
  socklen_t client_length;  
  char source_ip_port[22] = {0};
  packet_t pk;
  struct sockaddr_in client;

  while(true){
    memset(buff, 0, sizeof(packet_t));
    memset(&client, 0, sizeof(client));

    client_length = sizeof(client);

    if(recvfrom(server_fd, buff, sizeof(packet_t), 0,(struct sockaddr *) & client, &client_length) != -1){
      memcpy(& pk, buff, sizeof(packet_t));
      
      swap_packet_byte_order(& pk);

      memset(source_ip_port,0, 22);
      sprintf(source_ip_port, "%s:%hu", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
      write_data(pk.accumulated_time, pk.in_time, pk.id, source_ip_port, pk.samples);
    }
    else{
      log(E, "##Fatal error: recvfrom returned without any packet arrival\n");
      exit(-1);
    }
  }
}
Пример #3
0
void send_packet(socket_list *destination_sockets){
  packet_t anything;

  printf("Sampling at %d samples per second.\n", samples_per_second);

  usleep_time = (unsigned int) 1000000 / samples_per_second;

  while(npackets > 0){
    memset(&anything, 0, sizeof(anything));
    init_packet(&anything);
    
    swap_packet_byte_order(&anything);

    send_to_all(anything, destination_sockets);
    npackets--;

    usleep(usleep_time);    //DO NOT REMOVE THIS!!!!
  }

  printf("All generated packets were sent.\n");
  pthread_exit(0);
}