[Aggregator] Revert DB-related code

Revert database-related code introduced in
  b7823a52 Reconstitution du dépôt, étape 5 (et dernière)
This commit is contained in:
Matteo Cypriani 2010-07-09 16:57:37 +02:00
parent 4473b0dd3a
commit a955467c83
3 changed files with 7 additions and 250 deletions

View File

@ -21,7 +21,7 @@ CFLAGS=-O2 -W -Wall -Wstrict-prototypes -O -I.
DEPFLAGS=-MMD
XCFLAGS=$(CFLAGS) $(DEPFLAGS) $(WARN) $(HEADERS)
PICFLAG=-fPIC
LIBS=-lpthread -lconfuse ../../libowlps/libowlps.so.1.0 -lpq
LIBS=-lpthread -lconfuse ../../libowlps/libowlps.so.1.0
## Cibles de compilation standard ##

View File

@ -5,7 +5,6 @@
#ifndef _RTAPAGGREGATE_H
#define _RTAPAGGREGATE_H
#include <postgresql/libpq-fe.h>
#include "../../libowlps/owlps.h"
#include <confuse.h>
@ -15,16 +14,12 @@
/* Arguments & configuration du programme */
#define OPTIONS "a:c:f:H:i:k:l:N:o:p:P:U:" // Chaîne pour getopt
#define OPTIONS "a:c:f:i:k:l:o:p:" // Chaîne pour getopt
#define DEFAULT_CONFIG_FILE "/usr/local/etc/owlps/owlps-aggregator.conf"
#define DEFAULT_AGGREGATE_TIMEOUT 1500 // Timeout d'agrégation (en millisecondes)
#define DEFAULT_KEEP_TIMEOUT 3000 // Temps que l'on conserve les données dans la liste (en millisecondes)
#define DEFAULT_CHECK_INTERVAL 500000 // Temps entre deux vérifications de la liste (en micro-secondes)
#define POSITIONER_DEFAULT_IP "127.0.0.1"
#define DEFAULT_DB_HOST "127.0.0.1"
#define DEFAULT_DB_NAME "owlps_db"
#define DEFAULT_DB_USER "owlps"
#define DEFAULT_DB_PASS "owlps"
/* Codes d'erreur */
#define ERR_NO_MESSAGE_RECEIVED 1 // Erreur lors de la lecture sur la socket
@ -70,12 +65,6 @@ void print_couple_list(couple_list *couples) ;
void print_couple_info(couple_info_list *info) ;
#endif // DEBUG
void* monitor_couple_list(couple_list **couples) ;
int Db_write_request(couple_message message, BOOL ref) ;
int DbAccessPoint(couple_message message, PGconn *conn);
int DbMobile(couple_message message, PGconn *conn);
int DbRefPoint(couple_message message, PGconn *conn);
int DbRequest(couple_message message, PGconn *conn, int id_mobile, int *id_point, BOOL ref);
int DbPacket(couple_message message, PGconn *conn, int id_ap, int id_request, BOOL ref);
void print_usage(char *prog) ;
#endif

View File

@ -42,7 +42,6 @@ int main(int argc, char **argv)
pthread_t thread ; // Thread pour la fonction de surveillance de la liste d'informations agrégées
char *ap_mac_string, *mobile_mac_string ; // Pointeurs pour retour de mac_bytes_to_string()
char *mobile_ip_string ;
BOOL ref;
cfg_opt_t opts[] = { // Options reconnues par confuse dans le fichier de config
CFG_INT("listening_port", AGGREGATE_DEFAULT_PORT, CFGF_NONE), // Port d'écoute
@ -52,10 +51,6 @@ int main(int argc, char **argv)
CFG_INT("aggregate_timeout", DEFAULT_AGGREGATE_TIMEOUT, CFGF_NONE), // Timeout d'agrégation (en millisecondes)
CFG_INT("keep_timeout", DEFAULT_KEEP_TIMEOUT, CFGF_NONE), // Temps que l'on conserve les données dans la liste (en millisecondes)
CFG_INT("check_interval", DEFAULT_CHECK_INTERVAL, CFGF_NONE), // Temps entre deux vérifications de la liste (en micro-secondes)
CFG_STR("host_db", DEFAULT_DB_HOST, CFGF_NONE),
CFG_STR("db_name", DEFAULT_DB_NAME, CFGF_NONE),
CFG_STR("user_db", DEFAULT_DB_USER, CFGF_NONE),
CFG_STR("pass_db", DEFAULT_DB_PASS, CFGF_NONE),
CFG_END() } ;
char *config_file ; // Nom du fichier de configuration
int opt ; // Retour de getopt
@ -101,10 +96,7 @@ int main(int argc, char **argv)
case 'f' : // Fichier de configuration
// Déjà traité.
break ;
case 'H' :
cfg_setstr(cfg, "host_db", optarg) ;
break ;
case 'i' :
case 'i' :
cfg_setstr(cfg, "positioner_ip", optarg) ;
break ;
case 'k' :
@ -113,21 +105,12 @@ int main(int argc, char **argv)
case 'l' :
cfg_setint(cfg, "listening_port", strtol(optarg, NULL, 0)) ;
break ;
case 'N' :
cfg_setstr(cfg, "db_name", optarg) ;
break ;
case 'o' :
cfg_setstr(cfg, "output_file", optarg) ;
break ;
case 'p' :
cfg_setint(cfg, "positioner_port", strtol(optarg, NULL, 0)) ;
break ;
case 'P' :
cfg_setstr(cfg, "pass_db", optarg) ;
break ;
case 'U' :
cfg_setstr(cfg, "user_db", optarg) ;
break ;
default :
print_usage(argv[0]) ;
return ERR_BAD_USAGE ;
@ -243,11 +226,8 @@ int main(int argc, char **argv)
free(ap_mac_string) ;
free(mobile_mac_string) ;
free(mobile_ip_string) ;
ref = (message.x_position != 0 && message.y_position != 0 && message.z_position != 0)? TRUE : FALSE;
Db_write_request(message, ref);
if(!ref) got_couple_info(&couples, message) ;
got_couple_info(&couples, message) ;
}
(void) close(sockfd) ; // Fermeture de la socket
@ -270,7 +250,7 @@ void* monitor_couple_list(couple_list **couples)
FILE *fd = NULL ;
char *ap_mac_string ;
unsigned long sub ; // Résultat de sub_date().
struct sockaddr_in serv;
struct sockaddr_in client ;
socklen_t serv_len = sizeof(serv);
@ -576,214 +556,11 @@ void print_couple_info(couple_info_list *info)
int Db_write_request(couple_message message, BOOL ref)
{
char *buf = (char *) malloc(100 * sizeof(char));
sprintf(buf, "hostaddr = '%s' dbname = '%s' user = '%s' password = '%s'", cfg_getstr(cfg, "host_db"), cfg_getstr(cfg, "db_name"), cfg_getstr(cfg, "user_db"), cfg_getstr(cfg, "pass_db"));
const char* conninfo = buf;
PGconn *conn;
int id_ap, id_mobile, id_request, id_point = -1;
conn = PQconnectdb(conninfo);
if(PQstatus(conn) != CONNECTION_OK){
printf("Connection à la base de donnée échouée : %s\n", PQerrorMessage(conn));
PQfinish(conn);
return 1;
}
if((id_ap = DbAccessPoint(message, conn)) < 0) return 1;
if((id_mobile = DbMobile(message, conn)) < 0) return 1;
if((id_request = DbRequest(message, conn, id_mobile, &id_point, ref)) < 0) return 1;
DbPacket(message, conn, id_ap, id_request, ref);
PQfinish(conn);
return 0;
}
int DbAccessPoint(couple_message message, PGconn *conn)
{
char *buf = (char *) malloc(200 * sizeof(char));
char *ap_mac_string;
int id_ap;
PGresult *res;
/** Recherche de l'AP conserné **/
ap_mac_string = mac_bytes_to_string(message.ap_mac_addr_bytes) ;
sprintf(buf, "SELECT id_ap, ap_addr FROM ap WHERE ap_addr = '%s';", ap_mac_string);
res = PQexec(conn, buf);
if(PQresultStatus(res) != PGRES_TUPLES_OK){
printf("Commande incorrect : %s\n", PQerrorMessage(conn));
return -1;
}else if(PQntuples(res) == 0){
PQclear(res);
sprintf(buf, "SELECT * FROM ap;");
res = PQexec(conn, buf);
id_ap = (PQntuples(res) != 0)? atoi(PQgetvalue(res, PQntuples(res) - 1, 0)) + 1 : 1;
sprintf(buf, "INSERT INTO ap VALUES('%d','%s', '0', '0', '0', '0', '2417000000', '5', '20');",id_ap, ap_mac_string);
PQexec(conn, buf);
}else{
id_ap = atoi(PQgetvalue(res, 0, 0));
}
PQclear(res);
free(buf);
return id_ap;
}
int DbMobile(couple_message message, PGconn *conn)
{
char *mobile_mac_string, *mobile_ip_string;
char *buf = (char *) malloc(200 * sizeof(char));
int id_mobile;
PGresult *res;
/** Recherche du mobile conserné **/
mobile_mac_string = mac_bytes_to_string(message.mobile_mac_addr_bytes);
mobile_ip_string = ip_bytes_to_string(message.mobile_ip_addr_bytes);
sprintf(buf, "SELECT * FROM mobile WHERE mobile_addr = '%s';", mobile_mac_string);
res = PQexec(conn, buf);
if(PQresultStatus(res) != PGRES_TUPLES_OK){
printf("Commande incorrect : %s\n", PQerrorMessage(conn));
return -1;
}else if(PQntuples(res) == 0){
PQclear(res);
sprintf(buf, "SELECT * FROM mobile;");
res = PQexec(conn, buf);
id_mobile = (PQntuples(res) != 0)? atoi(PQgetvalue(res, PQntuples(res) - 1, 0)) + 1 : 1;
sprintf(buf, "INSERT INTO mobile(id_mobile, mobile_addr, ip_mobile, last_connect) VALUES('%d','%s','%s','%llu');",id_mobile, mobile_mac_string, mobile_ip_string, timeval_to_ms(message.request_time));
PQexec(conn, buf);
}else{
id_mobile = atoi(PQgetvalue(res, 0, 0));
sprintf(buf, "UPDATE mobile SET last_connect = %llu, ip_mobile = '%s' WHERE id_mobile = %d;", timeval_to_ms(message.request_time), mobile_ip_string, id_mobile);
PQexec(conn, buf);
}
PQclear(res);
free(buf);
return id_mobile;
}
int DbRefPoint(couple_message message, PGconn *conn)
{
char *buf = (char *) malloc(200 * sizeof(char));
int id_ref_point;
char d = ' ';
PGresult *res;
switch(message.direction){
case 1:
d = 'N';
break;
case 2:
d = 'E';
break;
case 3:
d = 'S';
break;
case 4:
d = 'W';
break;
default :
d = ' ';
printf("Erreur : direction inconnue\n");
}
strcpy(buf, "SELECT * FROM ref_point;");
res = PQexec(conn, buf);
id_ref_point = (PQntuples(res) != 0)? atoi(PQgetvalue(res, PQntuples(res) - 1, 0)) + 1: 1;
sprintf(buf, "INSERT INTO ref_point(id_ref_point, x, y, z, direction) VALUES('%d','%f','%f','%f','%c');", id_ref_point, message.x_position, message.y_position, message.z_position, d);
PQexec(conn, buf);
return id_ref_point;
}
int DbRequest(couple_message message, PGconn *conn, int id_mobile, int *id_point, BOOL ref)
{
char *buf = (char *) malloc(200 * sizeof(char));
int id_request;
PGresult *res;
/** Ajout de la requete si elle n'existe pas **/
if(ref) sprintf(buf, "SELECT * FROM ref_request WHERE mobile_time = %llu AND id_mobile = %d;", timeval_to_ms(message.request_time), id_mobile);
else sprintf(buf, "SELECT * FROM request WHERE mobile_time = %llu AND id_mobile = %d;", timeval_to_ms(message.request_time), id_mobile);
res = PQexec(conn, buf);
if(PQresultStatus(res) != PGRES_TUPLES_OK){
printf("Commande incorrect : %s\n", PQerrorMessage(conn));
return -1;
}else if(PQntuples(res) == 0){
PQclear(res);
if(ref) sprintf(buf, "SELECT * FROM ref_request;");
else sprintf(buf, "SELECT * FROM request;");
res = PQexec(conn, buf);
id_request = (PQntuples(res) != 0)? atoi(PQgetvalue(res, PQntuples(res) - 1, 0)) + 1: 1;
if(ref){
if((*id_point = DbRefPoint(message, conn)) < 0) return -1;
sprintf(buf, "INSERT INTO ref_request(id_ref_request, mobile_time, id_mobile, id_ref_point) VALUES('%d','%llu','%d','%d');", id_request, timeval_to_ms(message.request_time), id_mobile, *id_point);
}else sprintf(buf, "INSERT INTO request(id_request, mobile_time, id_mobile) VALUES('%d','%llu','%d');", id_request, timeval_to_ms(message.request_time), id_mobile);
PQexec(conn, buf);
}else{
id_request = atoi(PQgetvalue(res, 0, 0));
if(ref) *id_point = atoi(PQgetvalue(res, 0, 3));
}
PQclear(res);
free(buf);
return id_request;
}
int DbPacket(couple_message message, PGconn *conn, int id_ap, int id_request, BOOL ref)
{
char *buf = (char *) malloc(200 * sizeof(char));
/** Ajout du paquet **/
if(ref) sprintf(buf, "INSERT INTO ref_packet(ap_time, power, id_ap, id_ref_request) VALUES('%llu','%d','%d','%d');", timeval_to_ms(message.start_time), message.antenna_signal_dbm - 0x100, id_ap, id_request);
else sprintf(buf, "INSERT INTO packet(ap_time, power, id_ap, id_request) VALUES('%llu','%d','%d','%d');", timeval_to_ms(message.start_time), message.antenna_signal_dbm - 0x100, id_ap, id_request);
PQexec(conn, buf);
free(buf);
return 0;
}
/* Affiche le mode d'emploi du programme */
void print_usage(char *prog)
{
printf("Usage :\n\
\t%s [-f config_file] [-l listening_port] [-i positionner_ip] [-p positioner_port] [-a aggregate_timeout] [-k keep_timeout] [-c check_interval] [-o output_file] [-H db_addr] [-N db_name] [-U db_user] [-P db_passwd]\n\
\t%s [-f config_file] [-l listening_port] [-i positionner_ip] [-p positioner_port] [-a aggregate_timeout] [-k keep_timeout] [-c check_interval] [-o output_file]\n\
Options générales :\n\
\t-f config_file : spécifie un fichier de configuration à lire à la place du fichier par défaut.\n\
Options serveur :\n\
@ -794,11 +571,6 @@ Options d'agrégation :\n\
\t-a aggregate_timeout : temps d'agrégation (défaut : %d millisecondes)\n\
\t-a keep_timeout : temps de garde des demandes (défaut : %d millisecondes)\n\
\t-a check_interval : délai entre deux vérifications des demandes en mémoire, et agrégation (défaut : %d micro-secondes)\n\
Options relatives à la base de données :\n\
\t-H db_addr : adresse du serveur de BdD (défaut : %s).\n\
\t-N db_name : nom de la base (défaut : %s).\n\
\t-U db_user : nom d'utilisateur pour l'accès à la BdD (défaut : %s).\n\
\t-P db_passwd : mot de passe pour l'accès à la BdD (défaut : %s).\n\
",
prog,
AGGREGATE_DEFAULT_PORT,
@ -806,10 +578,6 @@ Options relatives à la base de données :\n\
POSITIONER_DEFAULT_PORT,
DEFAULT_AGGREGATE_TIMEOUT,
DEFAULT_KEEP_TIMEOUT,
DEFAULT_CHECK_INTERVAL,
DEFAULT_DB_HOST,
DEFAULT_DB_NAME,
DEFAULT_DB_USER,
DEFAULT_DB_PASS
DEFAULT_CHECK_INTERVAL
) ;
}