Réorganisation du code dans server.cc

server.{cc,hh} :
* Mutualisation du code des deux fonctions pointExists().
* Idem avec les deux fonctions pointIndex().
* makePointListFromFile() : correction pour la généricité de la fonction
  (lecture pour la liste des points de référence / lecture pour la liste
  de pérégrination).

git-svn-id: https://pif.pu-pm.univ-fcomte.fr/svn/loc@31 785a6c6c-259e-4ff1-8b91-dc31627914f0
This commit is contained in:
Matteo Cypriani 2008-05-07 08:59:06 +00:00
parent 54d788586f
commit 6641aef72b
2 changed files with 131 additions and 78 deletions

View File

@ -137,7 +137,7 @@ inline vector<string> extractReferencePointInfoFromBuffer(const string &buffer_i
ret.push_back(tmp_field); ret.push_back(tmp_field);
tmp_field.clear(); tmp_field.clear();
i++; i++;
#endif #endif // FRED_CSV_FORMAT
/* Extract direction (not used now) */ /* Extract direction (not used now) */
while (buffer_in[i] != ';') while (buffer_in[i] != ';')
@ -168,14 +168,14 @@ inline vector<string> extractReferencePointInfoFromBuffer(const string &buffer_i
} }
ret.push_back(tmp_field); ret.push_back(tmp_field);
tmp_field.clear(); tmp_field.clear();
#else #else // FRED_CSV_FORMAT
while (i <= buffer_in.size()) while (i <= buffer_in.size())
{ {
if ((buffer_in[i] == ';' || i == buffer_in.size()) && !tmp_field.empty()) // Si on est sur un séparateur et que la valeur lue n'est pas vide, if ((buffer_in[i] == ';' || i == buffer_in.size()) && !tmp_field.empty()) // Si on est sur un séparateur et que la valeur lue n'est pas vide,
{ {
#ifdef DEBUG_2 #ifdef DEBUG_2
cout << "Ajout de la valeur lue : " << tmp_field << endl ; cout << "Ajout de la valeur lue : " << tmp_field << endl ;
#endif #endif // DEBUG_2
ret.push_back(tmp_field) ; // on met la valeur lue dans les valeurs de retour. ret.push_back(tmp_field) ; // on met la valeur lue dans les valeurs de retour.
tmp_field.clear() ; tmp_field.clear() ;
} }
@ -184,7 +184,7 @@ inline vector<string> extractReferencePointInfoFromBuffer(const string &buffer_i
i++ ; i++ ;
} }
#endif #endif // FRED_CSV_FORMAT
/* Return the vector with each data */ /* Return the vector with each data */
return ret; return ret;
@ -244,13 +244,39 @@ int Server::receive_data()
/* FONCTION POUR RÉTRO-COMPATIBILITÉ.
* Recherche un point de coordonnées (x;y;z) dans la liste reference_point_list, retourne true s'il existe, false sinon. */
bool Server::pointExists(const float &x, const float &y, const float &z)const bool Server::pointExists(const float &x, const float &y, const float &z)const
{ {
Point p(x, y, z); return pointExists(reference_point_list, Point(x, y, z)) ;
}
/* FONCTION POUR RÉTRO-COMPATIBILITÉ.
* Recherche un Point dans la liste reference_point_list, retourne true s'il existe, false sinon. */
bool Server::pointExists(const Point &p)const
{
return pointExists(reference_point_list, p) ;
}
/* Recherche un point de coordonnées (x;y;z) dans la liste "point_list", retourne true s'il existe, false sinon. */
bool Server::pointExists(const vector<ReferencePoint> &point_list, const float &x, const float &y, const float &z) const
{
return pointExists(point_list, Point(x, y, z)) ;
}
/* Recherche un Point dans la liste "point_list", retourne true s'il existe, false sinon. */
bool Server::pointExists(const vector<ReferencePoint> &point_list, const Point &p) const
{
unsigned int i; unsigned int i;
for (i = 0 ; i < reference_point_list.size() ; i++) for (i = 0 ; i < point_list.size() ; i++)
if (p == reference_point_list[i].getCoordinates()) if (p == point_list[i].getCoordinates())
return true; return true;
return false; return false;
@ -261,7 +287,28 @@ bool Server::pointExists(const float &x, const float &y, const float &z)const
/* Do not forget to call pointExists() before this one */ /* Do not forget to call pointExists() before this one */
unsigned int Server::pointIndex(const float &x, const float &y, const float &z)const unsigned int Server::pointIndex(const float &x, const float &y, const float &z)const
{ {
Point p(x, y, z); return pointIndex(reference_point_list, Point(x, y, z)) ;
}
/* Do not forget to call pointExists() before this one */
unsigned int Server::pointIndex(const Point &p)const
{
return pointIndex(reference_point_list, p) ;
}
unsigned int Server::pointIndex(const vector<ReferencePoint> &point_list, const float &x, const float &y, const float &z) const
{
return pointIndex(point_list, Point(x, y, z)) ;
}
unsigned int Server::pointIndex(const vector<ReferencePoint> &point_list, const Point &p) const
{
unsigned int i; unsigned int i;
for (i = 0 ; i < reference_point_list.size() ; i++) for (i = 0 ; i < reference_point_list.size() ; i++)
@ -273,19 +320,6 @@ unsigned int Server::pointIndex(const float &x, const float &y, const float &z)c
bool Server::pointExists(const Point &p)const
{
unsigned int i;
for (i = 0 ; i < reference_point_list.size() ; i++)
if (p == reference_point_list[i].getCoordinates())
return true;
return false;
}
bool Server::apExists(const string &ap_addr)const bool Server::apExists(const string &ap_addr)const
{ {
unsigned int i; unsigned int i;
@ -312,20 +346,6 @@ unsigned int Server::apIndex(const string &ap_addr)const
/* Do not forget to call pointExists() before this one */
unsigned int Server::pointIndex(const Point &p)const
{
unsigned int i;
for (i = 0 ; i < reference_point_list.size() ; i++)
if (p == reference_point_list[i].getCoordinates())
return i;
return 0; // Should never happen
}
/* Selects the "k" closest points from the measurement "m", in the SS space. /* Selects the "k" closest points from the measurement "m", in the SS space.
* Returns an empty vector if no points are found, which should never happen. * Returns an empty vector if no points are found, which should never happen.
*/ */
@ -412,7 +432,7 @@ vector<Point> Server::getkClosestInSs(const vector<Measurement> &m, const unsign
else else
for (i = 0 ; i < distances_vector.size() - 1 ; i++) for (i = 0 ; i < distances_vector.size() - 1 ; i++)
cout << distances_vector[i] << " : " << points_vector[i] << endl ; cout << distances_vector[i] << " : " << points_vector[i] << endl ;
#endif #endif // DEBUG
return points_vector; return points_vector;
} }
@ -619,13 +639,23 @@ Point Server::interlink(const vector<Measurement> &m, const int &client_idx)cons
* Crée la liste des points de référence dans la liste reference_point_list. */ * Crée la liste des points de référence dans la liste reference_point_list. */
void Server::makeReferencePointListFromFile(const string &filename) void Server::makeReferencePointListFromFile(const string &filename)
{ {
makePointListFromFile(reference_point_list, filename) ; makePointListFromFile(reference_point_list, filename, true) ;
} }
/* Lit le fichier de mesures (CSV) nommé "filename", et place les informations dans la liste "dest_point_list". */
void Server::makePointListFromFile(vector<ReferencePoint> &dest_point_list, const string &filename) void Server::makePointListFromFile(vector<ReferencePoint> &dest_point_list, const string &filename)
{
makePointListFromFile(dest_point_list, filename, true) ;
}
/* Lit le fichier de mesures (CSV) nommé "filename", et place les informations dans la liste "dest_point_list".
* Si "uniq_point" est vrai, on vérifiera l'existence d'un point avant tout ajout d'information, de manière à ne pas créer de doublon : si le point existe déjà, les informations lui seront ajoutées, sinon un nouveau point sera créé dans la liste.
* Si "uniq_point" est faux, un nouveau point sera créé pour chaque ligne du fichier.
*/
void Server::makePointListFromFile(vector<ReferencePoint> &dest_point_list, const string &filename, const bool uniq_point)
{ {
ifstream input_file ; // Flux d'entrée du fichier. ifstream input_file ; // Flux d'entrée du fichier.
char buffer[BUFFER_LENGTH]; // Buffer lu dans le fichier. char buffer[BUFFER_LENGTH]; // Buffer lu dans le fichier.
@ -633,7 +663,7 @@ void Server::makePointListFromFile(vector<ReferencePoint> &dest_point_list, cons
ReferencePoint rp; ReferencePoint rp;
Point tmp_point; Point tmp_point;
float x, y, z ; // Coordonnées des points. float x, y, z ; // Coordonnées des points.
unsigned int pt_idx ; // Position du point lu dans la liste. unsigned int pt_idx = 0 ; // Position du point lu dans la liste.
vector<string> infos ; // Liste des informations lues dans une ligne du fichier. vector<string> infos ; // Liste des informations lues dans une ligne du fichier.
input_file.open(filename.c_str()) ; input_file.open(filename.c_str()) ;
@ -648,7 +678,7 @@ void Server::makePointListFromFile(vector<ReferencePoint> &dest_point_list, cons
cout << "Lecture du fichier « " << filename << " »..." ; cout << "Lecture du fichier « " << filename << " »..." ;
int nlines = 0 ; int nlines = 0 ;
int npoints = 0 ; int npoints = 0 ;
#endif #endif // DEBUG
while (!input_file.eof()) while (!input_file.eof())
{ {
@ -657,7 +687,7 @@ void Server::makePointListFromFile(vector<ReferencePoint> &dest_point_list, cons
printf("\n%5d", nlines) ; printf("\n%5d", nlines) ;
cout << '.' ; cout << '.' ;
nlines++ ; nlines++ ;
#endif #endif // DEBUG
input_file.getline(buffer, BUFFER_LENGTH); input_file.getline(buffer, BUFFER_LENGTH);
@ -667,14 +697,15 @@ void Server::makePointListFromFile(vector<ReferencePoint> &dest_point_list, cons
cpp_buffer = buffer; cpp_buffer = buffer;
if (cpp_buffer.size() == 0) // Ignorer une ligne vide if (cpp_buffer.size() == 0) // Ignorer une ligne vide
continue ; continue ;
infos = extractReferencePointInfoFromBuffer(cpp_buffer); infos = extractReferencePointInfoFromBuffer(cpp_buffer);
x = string2float(infos[0]); x = string2float(infos[0]);
y = string2float(infos[1]); y = string2float(infos[1]);
#ifdef FRED_CSV_FORMAT #ifdef FRED_CSV_FORMAT
z = DEFAULT_Z ; z = DEFAULT_Z ;
#else #else // FRED_CSV_FORMAT
z = string2float(infos[2]) ; z = string2float(infos[2]) ;
#endif #endif // FRED_CSV_FORMAT
/* Set point coordinates */ /* Set point coordinates */
tmp_point.setX(x); tmp_point.setX(x);
@ -682,50 +713,53 @@ void Server::makePointListFromFile(vector<ReferencePoint> &dest_point_list, cons
tmp_point.setZ(z); tmp_point.setZ(z);
/* Use C++ string format */ /* Use C++ string format */
if (!pointExists(tmp_point)) if (!uniq_point || !pointExists(dest_point_list, tmp_point)) // Si on ne veut pas de points unique, ou que le point n'existe pas encore,
{ {
rp.setCoordinates(tmp_point); rp.setCoordinates(tmp_point);
dest_point_list.push_back(rp); dest_point_list.push_back(rp) ; // on le crée.
pt_idx = dest_point_list.size() - 1 ; // Le point que l'on vient d'ajouter est le dernier du vector.
#ifdef DEBUG #ifdef DEBUG
npoints++ ; npoints++ ;
#endif #endif // DEBUG
#ifdef DEBUG_2 #ifdef DEBUG_2
cout << tmp_point << " : ajouté." << endl ; cout << tmp_point << " : ajouté." << endl ;
#endif // DEBUG_2
} }
else else // Le point existe déjà :
{ {
#ifdef DEBUG_2
cout << tmp_point << " : existe déjà." << endl ; cout << tmp_point << " : existe déjà." << endl ;
#endif #endif // DEBUG_2
pt_idx = pointIndex(dest_point_list, tmp_point) ; // On recherche le point auquel on veut ajouter les informations.
} }
pt_idx = pointIndex(tmp_point);
#ifdef FRED_CSV_FORMAT #ifdef FRED_CSV_FORMAT
vector<int> measures_vector = extractValues(infos[4]) ; vector<int> measures_vector = extractValues(infos[4]) ;
for (unsigned int i = 0 ; i < measures_vector.size() ; i++) for (unsigned int i = 0 ; i < measures_vector.size() ; i++)
dest_point_list[pt_idx].addMeasurement(infos[3], measures_vector[i]); dest_point_list[pt_idx].addMeasurement(infos[3], measures_vector[i]);
measures_vector.clear(); measures_vector.clear();
#else #else // FRED_CSV_FORMAT
for (unsigned int i = 4 ; i < infos.size() ; i++) for (unsigned int i = 4 ; i < infos.size() ; i++)
{ {
#ifdef DEBUG_2 #ifdef DEBUG_2
cout << "Lecture de la valeur : " << infos[i] << "... " ; cout << "Lecture de la valeur : " << infos[i] << "... " ;
#endif #endif // DEBUG_2
if (i + 1 < infos.size()) if (i + 1 < infos.size())
{ {
dest_point_list[pt_idx].addMeasurement(infos[i], string2int(infos[i+1])) ; dest_point_list[pt_idx].addMeasurement(infos[i], string2int(infos[i+1])) ;
#ifdef DEBUG_2 #ifdef DEBUG_2
cout << "Mesure ajoutée : AP = " << infos[i] << " | SS = " << string2int(infos[i+1]) << endl ; cout << "Mesure ajoutée : AP = " << infos[i] << " | SS = " << string2int(infos[i+1]) << endl ;
#endif #endif // DEBUG_2
i++ ; i++ ;
} }
} }
#endif #endif // FRED_CSV_FORMAT
} }
} }
#ifdef DEBUG #ifdef DEBUG
cout << '\n' << nlines << " lignes lues, " << npoints << " points différents ajoutés.\n" << endl ; cout << '\n' << nlines << " lignes lues, " << npoints << " points différents ajoutés.\n" << endl ;
#endif #endif // DEBUG
input_file.close(); input_file.close();
infos.clear() ; infos.clear() ;
@ -749,7 +783,7 @@ void Server::makeApListFromFile(const string &filename)
#ifdef DEBUG #ifdef DEBUG
cout << "Lecture du fichier « " << filename << " »..." << endl ; cout << "Lecture du fichier « " << filename << " »..." << endl ;
#endif #endif // DEBUG
while (!input_file.eof()) while (!input_file.eof())
{ {
@ -765,7 +799,7 @@ void Server::makeApListFromFile(const string &filename)
ap_infos = explode(buffer, ';'); ap_infos = explode(buffer, ';');
#ifdef DEBUG #ifdef DEBUG
cout << "AP : " << buffer ; cout << "AP : " << buffer ;
#endif #endif // DEBUG
tmp_ap.setApAddr(ap_infos[0]); tmp_ap.setApAddr(ap_infos[0]);
tmp_ap.setCoordinates(string2float(ap_infos[1]), string2float(ap_infos[2]), string2float(ap_infos[3])); tmp_ap.setCoordinates(string2float(ap_infos[1]), string2float(ap_infos[2]), string2float(ap_infos[3]));
tmp_ap.setFrequency(string2uint(ap_infos[4])); tmp_ap.setFrequency(string2uint(ap_infos[4]));
@ -775,13 +809,13 @@ void Server::makeApListFromFile(const string &filename)
ap_infos.clear(); ap_infos.clear();
#ifdef DEBUG #ifdef DEBUG
cout << " ajouté." << endl ; cout << " ajouté." << endl ;
#endif #endif // DEBUG
} }
} }
#ifdef DEBUG #ifdef DEBUG
cout << endl ; cout << endl ;
#endif #endif // DEBUG
input_file.close(); input_file.close();
@ -873,13 +907,13 @@ void Server::fastViterbiLike(const unsigned short &N, const unsigned short &K, c
&E_current = cl.getRef_viterbi_Ecurrent(), &E_current = cl.getRef_viterbi_Ecurrent(),
&E_previous = cl.getRef_viterbi_Eprevious() ; &E_previous = cl.getRef_viterbi_Eprevious() ;
vector<ReferencePoint> peregrination_point_list ; vector<ReferencePoint> peregrination_point_list ;
makePointListFromFile(peregrination_point_list, DEFAULT_TRACKING_FILE) ; makePointListFromFile(peregrination_point_list, DEFAULT_TRACKING_FILE, false) ;
#else #else // TEST
float **V = client_list[id_client].get_viterbi_V() ; float **V = client_list[id_client].get_viterbi_V() ;
vector<Point> vector<Point>
&E_current = client_list[id_client].getRef_viterbi_Ecurrent(), &E_current = client_list[id_client].getRef_viterbi_Ecurrent(),
&E_previous = client_list[id_client].getRef_viterbi_Eprevious() ; &E_previous = client_list[id_client].getRef_viterbi_Eprevious() ;
#endif #endif // TEST
int i = 1 ; // Nombre d'ensembles d'historique qu'on a déjà passés int i = 1 ; // Nombre d'ensembles d'historique qu'on a déjà passés
@ -891,19 +925,20 @@ void Server::fastViterbiLike(const unsigned short &N, const unsigned short &K, c
#ifdef TEST #ifdef TEST
unsigned int pt = 0 ; unsigned int pt = 0 ;
#ifdef DEBUG #ifdef DEBUG
cout << reference_point_list.size() << " points de référence, " << peregrination_point_list.size() << " points de pérégrination." << endl ; cout << reference_point_list.size() << " points de référence, " << peregrination_point_list.size() << " points de pérégrination." << endl ;
cout << "***** Liste : *****" << endl ; cout << "***** Liste pts référence : *****" << endl ;
for (int c = 0 ; c < peregrination_point_list.size() ; c++) printReferencePointList() ;
cout << peregrination_point_list[c] << endl ; cout << "***** Liste pérégrination : *****" << endl ;
cout << "*******************" << endl ; printPointList(peregrination_point_list) ;
#endif cout << "*********************************" << endl ;
#endif // DEBUG
while (pt < peregrination_point_list.size()) while (pt < peregrination_point_list.size())
#else #else // TEST
while (true) while (true)
#endif #endif // TEST
{ {
vector<Measurement> vm ; vector<Measurement> vm ;
@ -911,14 +946,14 @@ void Server::fastViterbiLike(const unsigned short &N, const unsigned short &K, c
/* Get point measurements */ /* Get point measurements */
//vm.clear(); //vm.clear();
vm = peregrination_point_list[pt].getMeasurementList(); vm = peregrination_point_list[pt].getMeasurementList();
#endif #endif // TEST
E_previous = E_current ; E_previous = E_current ;
E_current = getkClosestInSs(vm, K) ; // Création de l'ensemble des K points les plus proches dans l'espace des puissances. E_current = getkClosestInSs(vm, K) ; // Création de l'ensemble des K points les plus proches dans l'espace des puissances.
#ifdef DEBUG #ifdef DEBUG
cout << "Point courant : " << peregrination_point_list[pt] ; cout << "Point courant : " << peregrination_point_list[pt] ;
#endif #endif // DEBUG
if (i > 1) // Si on n'est plus à la première itération if (i > 1) // Si on n'est plus à la première itération
{ {
@ -971,14 +1006,14 @@ void Server::fastViterbiLike(const unsigned short &N, const unsigned short &K, c
cout << endl ; cout << endl ;
} }
cout << "\n--------------------------" << endl ; cout << "\n--------------------------" << endl ;
#endif #endif // DEBUG
if (i < N-1) if (i < N-1)
i++ ; i++ ;
#ifdef TEST #ifdef TEST
pt++ ; pt++ ;
#endif #endif // TEST
} }
} }

View File

@ -52,34 +52,52 @@ protected:
public: public:
Server(const string &ip_addr = DEFAULT_IP, const int &listen_port = DEFAULT_LISTEN_PORT); Server(const string &ip_addr = DEFAULT_IP, const int &listen_port = DEFAULT_LISTEN_PORT);
~Server(); ~Server();
void send_to_client(const int &cl); void send_to_client(const int &cl);
int receive_data(); int receive_data();
bool pointExists(const float &x, const float &y, const float &z)const; bool pointExists(const float &x, const float &y, const float &z)const;
unsigned int pointIndex(const float &x, const float &y, const float &z)const;
bool pointExists(const Point &p)const; bool pointExists(const Point &p)const;
bool pointExists(const vector<ReferencePoint> &point_list, const float &x, const float &y, const float &z) const ;
bool pointExists(const vector<ReferencePoint> &point_list, const Point &p) const ;
unsigned int pointIndex(const float &x, const float &y, const float &z)const;
unsigned int pointIndex(const Point &p)const;
unsigned int pointIndex(const vector<ReferencePoint> &point_list, const float &x, const float &y, const float &z) const ;
unsigned int pointIndex(const vector<ReferencePoint> &point_list, const Point &p) const ;
bool apExists(const string &ap_addr)const; bool apExists(const string &ap_addr)const;
unsigned int apIndex(const string &ap_addr)const; unsigned int apIndex(const string &ap_addr)const;
unsigned int pointIndex(const Point &p)const;
vector<Point> getkClosestInSs(const vector<Measurement> &m, const unsigned int &k)const ; vector<Point> getkClosestInSs(const vector<Measurement> &m, const unsigned int &k)const ;
vector<Point> getkClosestInSs(const vector<Measurement> &m, const unsigned int &k, const Point *point_ignored)const ; vector<Point> getkClosestInSs(const vector<Measurement> &m, const unsigned int &k, const Point *point_ignored)const ;
Point getkWeightedInSs(const vector<Measurement> &m, const unsigned int &k)const ; Point getkWeightedInSs(const vector<Measurement> &m, const unsigned int &k)const ;
Point getkWeightedInSs(const vector<Measurement> &m, const unsigned int &k, const Point *point_ignored)const ; Point getkWeightedInSs(const vector<Measurement> &m, const unsigned int &k, const Point *point_ignored)const ;
Point kPointsAverage(const vector<Point> &vp)const; Point kPointsAverage(const vector<Point> &vp)const;
Point fbcm(const vector<Measurement> &m, const int &client_idx)const; Point fbcm(const vector<Measurement> &m, const int &client_idx)const;
Point interlink(const vector<Measurement> &m, const int &client_idx)const; Point interlink(const vector<Measurement> &m, const int &client_idx)const;
void makeReferencePointListFromFile(const string &filename); void makeReferencePointListFromFile(const string &filename);
void makePointListFromFile(vector<ReferencePoint> &dest_point_list, const string &filename) ; void makePointListFromFile(vector<ReferencePoint> &dest_point_list, const string &filename) ;
void makePointListFromFile(vector<ReferencePoint> &dest_point_list, const string &filename, const bool uniq_point) ;
void makeApListFromFile(const string &filename); void makeApListFromFile(const string &filename);
void printReferencePointList(); void printReferencePointList();
void printPointList(vector<ReferencePoint> &point_list) ; void printPointList(vector<ReferencePoint> &point_list) ;
void printAccessPointList(); void printAccessPointList();
void computeFriisFromRefList(); void computeFriisFromRefList();
unsigned int getNbReferencePoints()const { return reference_point_list.size(); }; unsigned int getNbReferencePoints()const { return reference_point_list.size(); };
void fastViterbiLike(const unsigned short &N, const unsigned short &K, const unsigned int &id_client) ;
/* For experimentation purpose only ! */ /* For experimentation purpose only ! */
void radar_exp(); void radar_exp();
void fastViterbiLike(const unsigned short &N, const unsigned short &K, const unsigned int &id_client) ;
}; };
#endif #endif