[Client] Create libowlps-client from owlps-client

Move code related to sending requests into the new library
libowlps-client. This library will also be used by the listener to send
autocalibration requests.
Currently, it is statically linked because a dynamic linking should be
useless (because client and listener aren't normally on the same
machine).
This commit is contained in:
Matteo Cypriani 2010-07-29 11:04:30 +02:00
parent ec566286b4
commit 45258e9f4c
5 changed files with 197 additions and 73 deletions

View File

@ -0,0 +1,67 @@
# Compilateur
CC = gcc
# Autres outils
AR = ar
RANLIB = ranlib
# Variables générales
LIB_CIBLE=libowlps-client
VERSION=1.0
# Cibles à construire
STATIC=$(LIB_CIBLE).a
HEADER=owlps-client.h
# Composition de la bibliothèque
OBJS=$(LIB_CIBLE).o
# Flags
CFLAGS=-O2 -W -Wall -Wstrict-prototypes -O -I.
DEPFLAGS=-MMD
XCFLAGS=$(CFLAGS) $(DEPFLAGS) $(WARN) $(HEADERS)
PICFLAG=-fPIC
LIBS=-liw
#STRIPFLAGS= -Wl,-s
#LDFLAGS=
## Cibles de compilation standard ##
.PHONY : all static clean purge help
all : static
static : $(STATIC)
% : %.o
$(CC) $(LDFLAGS) $(STRIPFLAGS) $(XCFLAGS) -o $@ $^
%.o : %.c $(HEADER)
$(CC) $(XCFLAGS) -c $<
# Compilation de la bibliothèque statique
$(STATIC) : $(OBJS)
$(RM) $@
$(AR) cru $@ $^
$(RANLIB) $@
## Nettoyage ##
clean :
@$(RM) *~ *.o *.d
purge : clean
@$(RM) $(STATIC)
## Aide ##
help :
@echo "Bibliothèques nécessaires à la compilation :\n\
libowlps-dev\n\
\n\
Cibles possibles :\n\
static (cible par défaut) : Compile la bibliothèque statique (.a).\n\
\n\
clean : Supprime les fichiers temporaires.\n\
purge : Supprime le résultat de la compilation.\n\"

View File

@ -0,0 +1,87 @@
#include "owlps-client.h"
/* Opens an UDP socket to the aggregator. */
int owlps_create_socket_to_aggregator(char *dest_ip, int dest_port,
struct sockaddr_in *server,
char *iface)
{
struct sockaddr_in client ;
int sockfd = create_udp_sending_socket(dest_ip, dest_port,
server, &client) ;
if (sockfd < 0)
{
perror("Error! Cannot create UDP sending socket to the aggregation"
" server") ;
exit(ERR_CREATING_SOCKET) ;
}
if (iface[0] != '\0') // If we specified an interface name
owlps_use_iface(sockfd, iface) ;
return sockfd ;
}
/* Selects 'iface' as sending interface for the socket 'sockfd'. */
void owlps_use_iface(int sockfd, char *iface)
{
if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, iface,
strlen(iface) + 1) == -1)
{
fprintf(stderr, "Error! Cannot select interface %s to send the"
" packet: ", iface) ;
perror("") ;
fprintf(stderr, "Sending through the default interface.\n") ;
}
}
void owlps_send_request(int sockfd, struct sockaddr_in *server,
char *buf, int buf_size,
short nb_pkt, long delay)
{
int i ;
#ifdef DEBUG
printf("Sent packets: ") ;
#endif // DEBUG
// Transmit first packet:
owlps_send_packet(sockfd, server, buf, buf_size) ;
// Transmit remaining packets (if any):
for (i = 0 ; i < nb_pkt - 1 ; ++i)
{
usleep(delay) ; // Wait during the wanted delay
owlps_send_packet(sockfd, server, buf, buf_size) ;
}
#ifdef DEBUG
putchar('\n') ;
#endif // DEBUG
}
void owlps_send_packet(int sockfd, struct sockaddr_in *server,
char *buf, int buf_size)
{
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
}

View File

@ -0,0 +1,28 @@
/*
* This library contains code used to send positioning requests (normal,
* calibration or autocalibration) to the aggregaton server.
*/
#ifndef _LIBOWLPS_CLIENT_
#define _LIBOWLPS_CLIENT_
#include "../../libowlps/owlps.h"
/* Error codes */
#define ERR_CREATING_SOCKET 151 // Error when creating output socket
#define ERR_SENDING_INFO 152 // Error sending a localisation request
/* Function headers */
int owlps_create_socket_to_aggregator(char *dest_ip, int dest_port,
struct sockaddr_in *server,
char *iface) ;
void owlps_use_iface(int sockfd, char *iface) ;
void owlps_send_request(int sockfd, struct sockaddr_in *server,
char *buf, int buf_size,
short nb_pkt, long delay) ;
void owlps_send_packet(int sockfd, struct sockaddr_in *server,
char *buf, int buf_size) ;
#endif // _LIBOWLPS_CLIENT_

View File

@ -21,7 +21,7 @@ CFLAGS=-O2 -W -Wall -Wstrict-prototypes -O -I.
DEPFLAGS=-MMD
XCFLAGS=$(CFLAGS) $(DEPFLAGS) $(WARN) $(HEADERS)
PICFLAG=-fPIC
LIBS=../../libowlps/libowlps.so.1.0
LIBS=../../libowlps/libowlps.so.1.0 ../libowlps-client/libowlps-client.a
STATIC_LIBS=-liw -lm

View File

@ -2,14 +2,12 @@
* This file is part of the rtap localisation project.
*/
#include "../../libowlps/owlps.h"
#include "../libowlps-client/owlps-client.h"
#define DEBUG
/* 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
#define ERR_BAD_USAGE 1 // Bad program call (bad number of arguments)
/* Number of packets to send */
#define DEFAULT_NBPKT_CALIB 20 // 20 packets when calibrating
@ -35,7 +33,6 @@ void print_configuration(void) ;
void create_socket(void) ;
void make_packet(void) ;
void send_request(void) ;
void send_packet(void) ;
void receive_position(void) ;
void print_usage(void) ;
@ -274,6 +271,15 @@ void print_configuration()
inline void create_socket()
{
sockfd =
owlps_create_socket_to_aggregator(options.dest_ip, options.dest_port,
&server, options.iface) ;
}
/* Creates the packet to send. */
void make_packet()
{
@ -319,74 +325,10 @@ void make_packet()
/* Opens an UDP socket to the aggregator. */
void create_socket()
inline void send_request()
{
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
owlps_send_request(sockfd, &server, buf, buf_size,
options.nb_pkt, options.delay) ;
}