[Client] Refactor code
This commit is contained in:
parent
6ce8d695b0
commit
f8f6e51dd6
|
@ -32,6 +32,11 @@ void check_configuration(void) ;
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
void print_configuration(void) ;
|
void print_configuration(void) ;
|
||||||
#endif // DEBUG
|
#endif // DEBUG
|
||||||
|
void create_socket(void) ;
|
||||||
|
void make_packet(void) ;
|
||||||
|
void send_request(void) ;
|
||||||
|
void send_packet(void) ;
|
||||||
|
void receive_position(void) ;
|
||||||
void print_usage(void) ;
|
void print_usage(void) ;
|
||||||
|
|
||||||
|
|
||||||
|
@ -64,140 +69,26 @@ char *program_name = NULL ;
|
||||||
// positioning request:
|
// positioning request:
|
||||||
BOOL is_calibration_request = FALSE ;
|
BOOL is_calibration_request = FALSE ;
|
||||||
|
|
||||||
|
int sockfd ; // Sending socket descriptor
|
||||||
|
struct sockaddr_in server ; // Server info
|
||||||
|
char *buf = NULL ; // Packet to send
|
||||||
|
int buf_size ; // Packet size
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
struct timeval request_time ;
|
|
||||||
char *buf = NULL ; // Packet to send
|
|
||||||
int buf_offset ; // Index used to create the packet
|
|
||||||
int buf_size ; // Packet size
|
|
||||||
|
|
||||||
struct sockaddr_in
|
|
||||||
server,
|
|
||||||
client ;
|
|
||||||
int sockfd ; // Sending socket descriptor
|
|
||||||
ssize_t nsent ; // sendto() return value
|
|
||||||
|
|
||||||
int i ; // Iterator
|
|
||||||
|
|
||||||
// Position of the mobile as computed by the infrastructure:
|
|
||||||
float x, y, z ;
|
|
||||||
|
|
||||||
program_name = argv[0] ;
|
program_name = argv[0] ;
|
||||||
parse_command_line(argc, argv) ;
|
parse_command_line(argc, argv) ;
|
||||||
|
|
||||||
/* Open UDP socket to the aggregator */
|
create_socket() ;
|
||||||
sockfd = create_udp_sending_socket(options.dest_ip, options.dest_port,
|
make_packet() ;
|
||||||
&server, &client) ;
|
send_request() ;
|
||||||
if (sockfd < 0)
|
|
||||||
{
|
|
||||||
perror("Error! Cannot create UDP sending socket to the aggregation"
|
|
||||||
" server") ;
|
|
||||||
return ERR_CREATING_SOCKET ;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.iface[0] != '\0') // If we specified an interface name
|
|
||||||
{
|
|
||||||
if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, options.iface,
|
|
||||||
strlen(options.iface) + 1) == -1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error! Cannot select interface %s to send the"
|
|
||||||
" packet: ", options.iface) ;
|
|
||||||
perror("") ;
|
|
||||||
fprintf(stderr, "Sending through the default interface.\n") ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Create packet to send */
|
|
||||||
gettimeofday(&request_time, NULL) ;
|
|
||||||
|
|
||||||
if (is_calibration_request) // Calibration packet
|
|
||||||
{
|
|
||||||
printf("Calibration time: %llu\n", timeval_to_ms(request_time)) ;
|
|
||||||
|
|
||||||
buf_offset = 0 ;
|
|
||||||
buf_size =
|
|
||||||
sizeof(char) * 2 + sizeof(struct timeval) + sizeof(float) * 3 ;
|
|
||||||
buf = malloc(buf_size) ;
|
|
||||||
|
|
||||||
buf[buf_offset++] = PACKET_TYPE_CALIBRATION ; // Packet type
|
|
||||||
memcpy(&buf[buf_offset], &request_time, sizeof(request_time)) ;
|
|
||||||
buf_offset += sizeof(request_time) ;
|
|
||||||
buf[buf_offset++] = options.direction ; // Direction
|
|
||||||
#ifdef DEBUG
|
|
||||||
printf("Direction = %d, X = %f, Y = %f, Z = %f\n",
|
|
||||||
buf[buf_offset - 1], options.x, options.y, options.z) ;
|
|
||||||
#endif // DEBUG
|
|
||||||
memcpy(&buf[buf_offset], &options.x, sizeof(float)) ;
|
|
||||||
buf_offset += sizeof(float) ;
|
|
||||||
memcpy(&buf[buf_offset], &options.y, sizeof(float)) ;
|
|
||||||
buf_offset += sizeof(float) ;
|
|
||||||
memcpy(&buf[buf_offset], &options.z, sizeof(float)) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
else // Standard packet
|
|
||||||
{
|
|
||||||
printf("Request time: %llu\n", timeval_to_ms(request_time)) ;
|
|
||||||
buf_size = sizeof(char) + sizeof(struct timeval) ;
|
|
||||||
buf = malloc(buf_size) ;
|
|
||||||
buf[0] = PACKET_TYPE_NORMAL ; // Packet type
|
|
||||||
memcpy(&buf[1], &request_time, sizeof(request_time)) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Send data to the aggregator */
|
|
||||||
nsent = sendto(sockfd, (void *) buf, buf_size, 0,
|
|
||||||
(struct sockaddr *) &server,
|
|
||||||
(socklen_t) sizeof(server)) ;
|
|
||||||
if (nsent != (ssize_t) buf_size)
|
|
||||||
{
|
|
||||||
perror("Error sending data to the aggregation server") ;
|
|
||||||
return ERR_SENDING_INFO ;
|
|
||||||
}
|
|
||||||
#ifdef DEBUG
|
|
||||||
printf("Sent packets: .") ;
|
|
||||||
fflush(stdout) ;
|
|
||||||
#endif // DEBUG
|
|
||||||
|
|
||||||
// Transmit remaining packets (if any)
|
|
||||||
for (i = 0 ; i < options.nb_pkt - 1 ; i++)
|
|
||||||
{
|
|
||||||
usleep(options.delay) ; // Wait during the wanted delay
|
|
||||||
|
|
||||||
nsent = sendto(sockfd, (void *) buf, buf_size, 0,
|
|
||||||
(struct sockaddr *) &server,
|
|
||||||
(socklen_t) sizeof(server)) ;
|
|
||||||
if (nsent != (ssize_t) buf_size)
|
|
||||||
{
|
|
||||||
perror("Error sending data to the aggregation server") ;
|
|
||||||
return ERR_SENDING_INFO ;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
|
||||||
putchar('.') ;
|
|
||||||
fflush(stdout) ;
|
|
||||||
#endif // DEBUG
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
|
||||||
putchar('\n') ;
|
|
||||||
#endif // DEBUG
|
|
||||||
|
|
||||||
|
|
||||||
(void) close(sockfd) ;
|
(void) close(sockfd) ;
|
||||||
|
|
||||||
/* Wait for the computed position */
|
|
||||||
if (options.listening_port != -1)
|
if (options.listening_port != -1)
|
||||||
{
|
receive_position() ;
|
||||||
sockfd = create_udp_listening_socket(options.listening_port);
|
|
||||||
recvfrom(sockfd, &x, sizeof(float), 0, NULL, NULL);
|
|
||||||
recvfrom(sockfd, &y, sizeof(float), 0, NULL, NULL);
|
|
||||||
recvfrom(sockfd, &z, sizeof(float), 0, NULL, NULL);
|
|
||||||
(void) close(sockfd) ;
|
|
||||||
printf("Computed position: (%.4f;%.4f;%.4f)\n", x, y, z);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0 ;
|
return 0 ;
|
||||||
}
|
}
|
||||||
|
@ -383,6 +274,139 @@ void print_configuration()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Creates the packet to send. */
|
||||||
|
void make_packet()
|
||||||
|
{
|
||||||
|
int buf_offset ; // Index used to create the packet
|
||||||
|
int buf_size ; // Packet size
|
||||||
|
struct timeval request_time ;
|
||||||
|
|
||||||
|
gettimeofday(&request_time, NULL) ;
|
||||||
|
|
||||||
|
if (is_calibration_request) // Calibration packet
|
||||||
|
{
|
||||||
|
printf("Calibration time: %llu\n", timeval_to_ms(request_time)) ;
|
||||||
|
|
||||||
|
buf_offset = 0 ;
|
||||||
|
buf_size =
|
||||||
|
sizeof(char) * 2 + sizeof(struct timeval) + sizeof(float) * 3 ;
|
||||||
|
buf = malloc(buf_size) ;
|
||||||
|
|
||||||
|
buf[buf_offset++] = PACKET_TYPE_CALIBRATION ; // Packet type
|
||||||
|
memcpy(&buf[buf_offset], &request_time, sizeof(request_time)) ;
|
||||||
|
buf_offset += sizeof(request_time) ;
|
||||||
|
buf[buf_offset++] = options.direction ; // Direction
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("Direction = %d, X = %f, Y = %f, Z = %f\n",
|
||||||
|
buf[buf_offset - 1], options.x, options.y, options.z) ;
|
||||||
|
#endif // DEBUG
|
||||||
|
memcpy(&buf[buf_offset], &options.x, sizeof(float)) ;
|
||||||
|
buf_offset += sizeof(float) ;
|
||||||
|
memcpy(&buf[buf_offset], &options.y, sizeof(float)) ;
|
||||||
|
buf_offset += sizeof(float) ;
|
||||||
|
memcpy(&buf[buf_offset], &options.z, sizeof(float)) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
else // Standard packet
|
||||||
|
{
|
||||||
|
printf("Request time: %llu\n", timeval_to_ms(request_time)) ;
|
||||||
|
buf_size = sizeof(char) + sizeof(struct timeval) ;
|
||||||
|
buf = malloc(buf_size) ;
|
||||||
|
buf[0] = PACKET_TYPE_NORMAL ; // Packet type
|
||||||
|
memcpy(&buf[1], &request_time, sizeof(request_time)) ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Opens an UDP socket to the aggregator. */
|
||||||
|
void create_socket()
|
||||||
|
{
|
||||||
|
struct sockaddr_in client ;
|
||||||
|
|
||||||
|
sockfd = create_udp_sending_socket(options.dest_ip, options.dest_port,
|
||||||
|
&server, &client) ;
|
||||||
|
if (sockfd < 0)
|
||||||
|
{
|
||||||
|
perror("Error! Cannot create UDP sending socket to the aggregation"
|
||||||
|
" server") ;
|
||||||
|
exit(ERR_CREATING_SOCKET) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.iface[0] != '\0') // If we specified an interface name
|
||||||
|
{
|
||||||
|
if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, options.iface,
|
||||||
|
strlen(options.iface) + 1) == -1)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error! Cannot select interface %s to send the"
|
||||||
|
" packet: ", options.iface) ;
|
||||||
|
perror("") ;
|
||||||
|
fprintf(stderr, "Sending through the default interface.\n") ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void send_request()
|
||||||
|
{
|
||||||
|
int i ;
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("Sent packets: ") ;
|
||||||
|
#endif // DEBUG
|
||||||
|
|
||||||
|
send_packet() ; // Transmit first packet
|
||||||
|
|
||||||
|
// Transmit remaining packets (if any)
|
||||||
|
for (i = 0 ; i < options.nb_pkt - 1 ; ++i)
|
||||||
|
{
|
||||||
|
usleep(options.delay) ; // Wait during the wanted delay
|
||||||
|
send_packet() ;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
putchar('\n') ;
|
||||||
|
#endif // DEBUG
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void send_packet()
|
||||||
|
{
|
||||||
|
ssize_t nsent = sendto(sockfd, (void *) buf, buf_size, 0,
|
||||||
|
(struct sockaddr *) &server,
|
||||||
|
(socklen_t) sizeof(server)) ;
|
||||||
|
if (nsent != (ssize_t) buf_size)
|
||||||
|
{
|
||||||
|
perror("Error sending data to the aggregation server") ;
|
||||||
|
exit(ERR_SENDING_INFO) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
putchar('.') ;
|
||||||
|
fflush(stdout) ;
|
||||||
|
#endif // DEBUG
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void receive_position()
|
||||||
|
{
|
||||||
|
// Position of the mobile as computed by the infrastructure:
|
||||||
|
float x, y, z ;
|
||||||
|
|
||||||
|
sockfd = create_udp_listening_socket(options.listening_port) ;
|
||||||
|
recvfrom(sockfd, &x, sizeof(float), 0, NULL, NULL) ;
|
||||||
|
recvfrom(sockfd, &y, sizeof(float), 0, NULL, NULL) ;
|
||||||
|
recvfrom(sockfd, &z, sizeof(float), 0, NULL, NULL) ;
|
||||||
|
(void) close(sockfd) ;
|
||||||
|
|
||||||
|
printf("Computed position: (%.4f;%.4f;%.4f)\n", x, y, z) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void print_usage()
|
void print_usage()
|
||||||
{
|
{
|
||||||
printf("Usage:\n"
|
printf("Usage:\n"
|
||||||
|
|
Loading…
Reference in New Issue