[Client] Translation of comments, 72 col.

This commit is contained in:
Matteo Cypriani 2010-07-13 16:32:16 +02:00
parent a0be0f5bef
commit 3e80537c73
1 changed files with 157 additions and 110 deletions

View File

@ -6,81 +6,74 @@
#define DEBUG
/* Codes d'erreurs */
#define ERR_CREATING_SOCKET 1 // Erreur lors de la création de la socket d'envoi
#define ERR_BAD_USAGE 2 // Mauvais nombre d'arguments lors de l'appel au programme
#define ERR_SENDING_INFO 3 // Erreur lors de l'envoi de l'une des demandes de positionnement
/* Error codes */
#define ERR_CREATING_SOCKET 1 // Error when creating output socket
#define ERR_BAD_USAGE 2 // Bad program call (bad number of arguments)
#define ERR_SENDING_INFO 3 // Error sending a localisation request
/* Nombre de paquets envoyés */
#define DEFAULT_NBPKT_CALIB 20 // Rafale de 20 paquets lors de la calibration
#define DEFAULT_NBPKT_NORMAL 10 // Nombre de paquets envoyés dans le cas d'une demande de localisation
/* Number of packets to send */
#define DEFAULT_NBPKT_CALIB 20 // 20 packets when calibrating
#define DEFAULT_NBPKT_NORMAL 10 // 10 packets when requesting the position
/* Délai entre chaque paquet envoyé */
#define DEFAULT_DELAY_CALIB 50000 // Délai entre chaque émission de paquet lors de la calibration (en microsecondes)
#define DEFAULT_DELAY_NORMAL 25000 // Délai entre chaque émission de paquet dans le cas d'une demande de localisation (en microsecondes)
/* Delay between two packet transmissions (in microseconds) */
#define DEFAULT_DELAY_CALIB 50000 // Calibration request
#define DEFAULT_DELAY_NORMAL 25000 // Localisation request
/* Arguments du programme */
/* Program arguments (getopt string) */
#define OPTIONS "d:i:l::n:p:t:"
/* Affiche le mode d'emploi du programme */
void print_usage(char *prog)
{
printf("Usage :\n\
\t- Demande de localisation : %s -d dest_ip [-p dest_port] [-i iface] [-t delay] [-n nb_packets] [-l [port]]\n\
\t- Requête de calibration : %s -d dest_ip [-i iface] [-t delay] [-n nb_packets] direction x y z\n\
- dest_ip est l'adresse IP vers laquelle sera envoyée la demande de localisation.\n\
- dest_port est le port sur lequel sera envoyée la demande de localisation (défaut : %d).\n\
- delay est le délai d'attente entre chaque paquet émis.\n\
- nb_packets est le nombre de paquets émis pour la demande.\n\
- iface est une chaîne désignant le nom de interface réseau à partir de laquelle envoyer la demande (par exemple \"eth2\"). Si elle n'est pas spécifiée, le choix de l'interface source est automatique. Vous devez être root pour utiliser cette option.\n\
- L'option -l indique au programme qu'il doit attendre et afficher la position calculée par l'infrastructure avant de quitter. L'argument optionnel \"port\" permet de spécifier le port d'écoute ; s'il n'est pas présent, la valeur par défaut (%d) est utilisée.\n\
", prog, prog, LOC_REQUEST_DEFAULT_PORT, MOBILE_DEFAULT_PORT) ;
}
/* Function headers */
void print_usage(char *prog) ;
int main(int argc, char *argv[])
{
struct timeval request_time ;
char *buf = NULL ; // Paquet à envoyer
int buf_offset ; // Indice dans le paquet à envoyer, utilisé lors de sa création
int buf_size ; // Taille du paquet envoyé
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 ; // Descripteur de la socket d'envoi
ssize_t nsent ; // Retour de sendto
struct sockaddr_in
server,
client ;
int sockfd ; // Sending socket descriptor
ssize_t nsent ; // sendto() return value
int i ; // Compteur
BOOL is_calibration_request = FALSE ; // Sera positionné à TRUE si le paquet est une requête de calibration (FALSE pour une simple demande de localisation)
int i ; // Iterator
// TRUE if the packet is a calibration request, FALSE if it is a simple
// positioning request:
BOOL is_calibration_request = FALSE ;
struct {
char dest_ip[16] ; // Adresse IP de destination des paquets de la demande
char dest_ip[16] ; // Destination IP of the packets
long dest_port ;
char iface[IFNAMSIZ + 1] ; // Interface réseau depuis laquelle on envoit les paquets
long delay ; // Temps d'attente entre chaque émission de paquet
short nb_pkt ; // Nombre de paquets envoyés pour la demande
char iface[IFNAMSIZ + 1] ; // Source network interface
long delay ; // Time between two packet transmissions
short nb_pkt ; // Number of packets to send
long listening_port ;
// Données de calibration :
// Calibration data:
DIRECTION direction ;
float x ;
float y ;
float z ;
} options = {"",
LOC_REQUEST_DEFAULT_PORT,
"",
-1,
-1,
-1,
0, 0, 0, 0
} options = {
"",
LOC_REQUEST_DEFAULT_PORT,
"",
-1,
-1,
-1,
0, 0, 0, 0
} ;
int opt ; // Retour de getopt
int opt ; // getopt return value
float x, y, z ; // Position reçue en réponse du serveur
// Position of the mobile as computed by the infrastructure:
float x, y, z ;
/* Parcours des arguments de la ligne de commandes */
/* Parse command line */
while ((opt = getopt(argc, argv, OPTIONS)) != -1)
{
switch (opt)
@ -92,18 +85,23 @@ int main(int argc, char *argv[])
strncpy(options.iface, optarg, IFNAMSIZ + 1) ;
break ;
case 'l' :
if (optarg == 0) // Les options facultatives de getopt ne gèrent pas
{ // les valeurs séparées (du type : -l <port>)
if (argv[optind] == NULL // Si on est en bout de chaîne ou que l'optind
|| argv[optind][0] == '-') // suivant est une option, c'est qu'on a -l sans numéro de port
options.listening_port = MOBILE_DEFAULT_PORT ; // on prend donc la valeur par défaut.
/* Facultative getopt options does not handle separated
* values (like -l <port>) */
if (optarg == 0)
{
/* If we are at the end of the string, or the next optind
* is an option, we have -l without a port number */
if (argv[optind] == NULL || argv[optind][0] == '-')
// Take the default value:
options.listening_port = MOBILE_DEFAULT_PORT ;
else
{ // Sinon on prend la valeur de l'optind :
{
// Take the optind value:
options.listening_port = strtol(argv[optind], NULL, 0) ;
optind++ ;
}
}
else // On a une option de la forme -l<port>, tout va bien
else // We got an option like -l<port>, it's OK
options.listening_port = strtol(optarg, NULL, 0) ;
break ;
case 'n' :
@ -121,14 +119,16 @@ int main(int argc, char *argv[])
}
}
if (options.dest_ip[0] == '\0') // On vérifie si on a bien une adresse de destination
/* Check if we got a destination IP address */
if (options.dest_ip[0] == '\0')
{
fprintf(stderr, "Erreur ! Vous devez spécifier une adresse de destination (-d).\n") ;
fprintf(stderr, "Error! You must specify a destination IP address"
" (-d).\n") ;
print_usage(argv[0]) ;
return ERR_BAD_USAGE ;
}
/* Parcours des arguments restants (éventuelles données de calibration) */
/* Parse remaining arguments (possible calibration data) */
if (argc - optind != 0)
{
if (argc - optind == 4)
@ -139,54 +139,62 @@ int main(int argc, char *argv[])
options.y = strtod(argv[optind++], NULL) ;
options.z = strtod(argv[optind], NULL) ;
}
else
{ // Si le nombre d'arguments est mauvais, on quitte
else // Bad number of arguments
{
print_usage(argv[0]) ;
return ERR_BAD_USAGE ;
}
}
/* Vérifications des arguments et initialisation des données non initialisées */
if (options.delay < 0) // Délai non spécifié ou mauvais, application des valeurs par défaut
/* Check options and initialise uninitialised data */
// Delay not specified (or bad delay):
if (options.delay < 0)
{
#ifdef DEBUG
fprintf(stderr, "Attention ! delay : application de la valeur par défaut.\n") ;
fprintf(stderr,
"Warning! delay: failing back to default value.\n") ;
#endif // DEBUG
if (is_calibration_request)
options.delay = DEFAULT_DELAY_CALIB ;
else
options.delay = DEFAULT_DELAY_NORMAL ;
}
if (options.nb_pkt < 1) // Nombre de paquets non spécifié ou mauvais, application des valeurs par défaut
// Number of packet not specified (or bad number)
if (options.nb_pkt < 1)
{
#ifdef DEBUG
fprintf(stderr, "Attention ! nb_pkt : application de la valeur par défaut.\n") ;
fprintf(stderr,
"Warning! nb_pkt: failing back to default value.\n") ;
#endif // DEBUG
if (is_calibration_request)
options.nb_pkt = DEFAULT_NBPKT_CALIB ;
else
options.nb_pkt = DEFAULT_NBPKT_NORMAL ;
}
// We want to send a calibration request AND to be located, which is
// not allowed:
if (is_calibration_request && options.listening_port != -1)
{
#ifdef DEBUG
fprintf(stderr, "Attention ! Vous ne pouvez pas attendre de réponse du serveur lorsque vous effectuez une requête de calibration. Option -l ignorée...\n") ;
fprintf(stderr, "Warning! You cannot wait for a server answer when"
" you calibrate. Option -l ignored…\n") ;
#endif // DEBUG
options.listening_port = -1 ;
}
#ifdef DEBUG
fprintf(stderr, "Options :\n\
\tDestination IP : %s\n\
\tDestination port : %ld\n\
\tInterface : %s\n\
\tDelay : %ld\n\
\tNumber of packets : %d\n\
\tListening port : %ld\n\
\tDirection : %d\n\
\tX : %f\n\
\tY : %f\n\
\tZ : %f\n",
fprintf(stderr, "Options:\n"
"\tDestination IP: %s\n"
"\tDestination port: %ld\n"
"\tInterface: %s\n"
"\tDelay: %ld\n"
"\tNumber of packets: %d\n"
"\tListening port: %ld\n"
"\tDirection: %d\n"
"\tX: %f\n"
"\tY: %f\n"
"\tZ: %f\n"
,
options.dest_ip,
options.dest_port,
options.iface,
@ -200,47 +208,48 @@ int main(int argc, char *argv[])
) ;
#endif // DEBUG
/* Ouverture de la socket UDP vers le serveur d'aggrégation */
sockfd = create_udp_sending_socket(options.dest_ip, options.dest_port, &server, &client) ;
/* Open UDP socket to the aggregator */
sockfd = create_udp_sending_socket(options.dest_ip, options.dest_port,
&server, &client) ;
if (sockfd < 0)
{
perror("Erreur ! Impossible de créer la socket vers le serveur d'aggrégation \n");
perror("Error! Cannot create UDP sending socket to the aggregation"
" server") ;
return ERR_CREATING_SOCKET ;
}
if (options.iface[0] != '\0') // Si on a spécifié un nom d'interface
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)
if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, options.iface,
strlen(options.iface) + 1) == -1)
{
fprintf(stderr, "Erreur ! Impossible de sélectionner l'interface %s pour l'envoi du paquet : ", options.iface) ;
fprintf(stderr, "Error! Cannot select interface %s to send the"
" packet: ", options.iface) ;
perror("") ;
fprintf(stderr, "Envoi sur l'interface par défaut.\n") ;
fprintf(stderr, "Sending through the default interface.\n") ;
}
}
/* Création du paquet à envoyer */
/* Create packet to send */
gettimeofday(&request_time, NULL) ;
if (is_calibration_request) // Paquet calibration
if (is_calibration_request) // Calibration packet
{
printf("Envoi Calibration effectué à : %llu\n", timeval_to_ms(request_time)) ;
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_size =
sizeof(char) * 2 + sizeof(struct timeval) + sizeof(float) * 3 ;
buf = malloc(buf_size) ;
buf[buf_offset++] = PACKET_TYPE_CALIBRATION ; // Type de paquet = calibration
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
/*
float posX = options.x ;
float posY = options.y ;
float posZ = options.z ;
*/
#ifdef DEBUG
printf("Direction = %d, X = %f, Y = %f, Z = %f\n", buf[buf_offset - 1], options.x, options.y, options.z) ;
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) ;
@ -249,36 +258,41 @@ int main(int argc, char *argv[])
memcpy(&buf[buf_offset], &options.z, sizeof(float)) ;
}
else // Paquet normal
else // Standard packet
{
printf("Envoi normal effectué à : %llu\n", timeval_to_ms(request_time)) ;
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 ; // Type de paquet = demande
buf[0] = PACKET_TYPE_NORMAL ; // Packet type
memcpy(&buf[1], &request_time, sizeof(request_time)) ;
}
/* Envoi des infos au serveur d'aggrégation */
nsent = sendto(sockfd, (void *) buf, buf_size, 0, (struct sockaddr *) &server, (socklen_t) sizeof(server)) ; // Envoi d'un paquet.
/* 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("Erreur lors de l'envoi des informations au serveur ") ;
perror("Error sending data to the aggregation server") ;
return ERR_SENDING_INFO ;
}
#ifdef DEBUG
printf("Paquets envoyés : .") ;
printf("Sent packets: .") ;
fflush(stdout) ;
#endif // DEBUG
for (i = 0 ; i < options.nb_pkt - 1 ; i++) // Si tels sont les paramètres, on envoit éventuellement la suite de la rafale de paquets :
// Transmit remaining packets (if any)
for (i = 0 ; i < options.nb_pkt - 1 ; i++)
{
usleep(options.delay) ; // On attend le temps voulu entre chaque paquet.
usleep(options.delay) ; // Wait during the wanted delay
nsent = sendto(sockfd, (void *) buf, buf_size, 0, (struct sockaddr *) &server, (socklen_t) sizeof(server)) ;
nsent = sendto(sockfd, (void *) buf, buf_size, 0,
(struct sockaddr *) &server,
(socklen_t) sizeof(server)) ;
if (nsent != (ssize_t) buf_size)
{
perror("Erreur lors de l'envoi des infos au serveur ") ;
perror("Error sending data to the aggregation server") ;
return ERR_SENDING_INFO ;
}
@ -295,7 +309,7 @@ int main(int argc, char *argv[])
(void) close(sockfd) ;
/* Attente de la position du serveur */
/* Wait for the computed position */
if (options.listening_port != -1)
{
sockfd = create_udp_listening_socket(options.listening_port);
@ -303,8 +317,41 @@ int main(int argc, char *argv[])
recvfrom(sockfd, &y, sizeof(float), 0, NULL, NULL);
recvfrom(sockfd, &z, sizeof(float), 0, NULL, NULL);
(void) close(sockfd) ;
printf("Position reçue : (%.4f;%.4f;%.4f)\n", x, y, z);
printf("Computed position: (%.4f;%.4f;%.4f)\n", x, y, z);
}
return 0 ;
}
void print_usage(char *prog)
{
printf("Usage:\n"
"Localisation request:\n"
"\t%s -d dest_ip [-p dest_port] [-i iface] [-t delay]"
" [-n nb_packets] [-l [port]]\n"
"Calibration request:\n"
"\t%s -d dest_ip [-i iface] [-t delay] [-n nb_packets]"
" direction x y z\n"
"\n"
"Options:\n"
"\t-d dest_ip\tDestination IP address of the localisation request.\n"
"\t-p dest_port\tDestination port of the localisation request"
" (default: %d).\n"
"\t-t delay\tTime between each packet transmission.\n"
"\t-n nb_packets\tNumber of packet transmitted for the request.\n"
"\t-i iface\tName of the network interface used to transmit the"
" request (e.g. \"eth2\"). If this option is absent, interface"
" is selected automatically. You must be root to use this"
" option.\n"
"\t-l [port]\tWait for the computed position and display it."
" Optional argument 'port' allows to specify the listening"
" port (default: %d).\n"
,
prog,
prog,
LOC_REQUEST_DEFAULT_PORT,
MOBILE_DEFAULT_PORT
) ;
}