diff --git a/GuiNuMo-server/Makefile b/GuiNuMo-server/Makefile index 5a9635a..99a7751 100644 --- a/GuiNuMo-server/Makefile +++ b/GuiNuMo-server/Makefile @@ -1,7 +1,7 @@ GXX=g++ GXXFLAGS=-Wall -O2 LD=g++ -LDFLAGS= -lm -O2 +LDFLAGS=-Wall -lm -O2 all: test diff --git a/GuiNuMo-server/accesspoint.cc b/GuiNuMo-server/accesspoint.cc index 7667928..2eb3f3a 100644 --- a/GuiNuMo-server/accesspoint.cc +++ b/GuiNuMo-server/accesspoint.cc @@ -1,7 +1,7 @@ #include "accesspoint.hh" -AccessPoint::AccessPoint(string addr, float fidx, Point coords, unsigned int f, float antg, float outp) +AccessPoint::AccessPoint(const string &addr, const float &fidx, const Point &coords, const unsigned int &f, const float &antg, const float &outp) { coordinates = coords; ap_addr = addr; @@ -46,7 +46,7 @@ AccessPoint AccessPoint::operator=(const AccessPoint &ap) return *this; } -ostream &operator<<(ostream & os, AccessPoint & ap) +ostream &operator<<(ostream &os, const AccessPoint &ap) { os << "MAC Address: " << ap.ap_addr << endl; os << "Coordinates: " << ap.coordinates << endl; diff --git a/GuiNuMo-server/accesspoint.hh b/GuiNuMo-server/accesspoint.hh index 60f30c1..d92116b 100644 --- a/GuiNuMo-server/accesspoint.hh +++ b/GuiNuMo-server/accesspoint.hh @@ -25,7 +25,7 @@ protected: float output_power; public: - AccessPoint(string addr = AP_DEFAULT_ADDR, float fidx = AP_DEFAULT_FRIIS_INDEX, Point coords = Point(), unsigned int f = AP_DEFAULT_FREQ, float antg = AP_DEFAULT_ANTENNA_GAIN, float outp = AP_DEFAULT_OUTPUT_POWER); + AccessPoint(const string &addr = AP_DEFAULT_ADDR, const float &fidx = AP_DEFAULT_FRIIS_INDEX, const Point &coords = Point(), const unsigned int &f = AP_DEFAULT_FREQ, const float &antg = AP_DEFAULT_ANTENNA_GAIN, const float &outp = AP_DEFAULT_OUTPUT_POWER); AccessPoint(const AccessPoint &ap); ~AccessPoint() {}; string getApAddr()const { return ap_addr; }; @@ -34,17 +34,17 @@ public: float getAntennaGain()const { return antenna_gain; }; unsigned int getFrequency()const { return freq; }; float getOutputPower()const { return output_power; }; - void setApAddr(string addr) { ap_addr = addr; }; - void setFriisIndex(float fidx) { friis_index = fidx; }; - void setCoordinates(Point apc) { coordinates = apc; }; - void setCoordinates(float x, float y, float z) { Point apc(x, y, z); coordinates = apc; }; - void setFrequency(unsigned int f) { freq = f; }; - void setAntennaGain(float antg) { antenna_gain = antg; }; - void setOutputPower(float outp) {output_power = outp; }; + void setApAddr(const string &addr) { ap_addr = addr; }; + void setFriisIndex(const float &fidx) { friis_index = fidx; }; + void setCoordinates(const Point &apc) { coordinates = apc; }; + void setCoordinates(const float &x, const float &y, const float &z) { Point apc(x, y, z); coordinates = apc; }; + void setFrequency(const unsigned int &f) { freq = f; }; + void setAntennaGain(const float &antg) { antenna_gain = antg; }; + void setOutputPower(const float &outp) {output_power = outp; }; bool operator==(const AccessPoint &ap)const; bool operator!=(const AccessPoint &ap)const; AccessPoint operator=(const AccessPoint &ap); - friend ostream &operator<<(ostream & os, AccessPoint & ap); + friend ostream &operator<<(ostream &os, const AccessPoint &ap); }; #endif diff --git a/GuiNuMo-server/cfg/minipc.cfg b/GuiNuMo-server/cfg/minipc.cfg new file mode 100644 index 0000000..b82973f --- /dev/null +++ b/GuiNuMo-server/cfg/minipc.cfg @@ -0,0 +1,19 @@ +# Fichier de configuration pour les AP. +# Chaque ligne est de la forme : +# MAC;X;Y;Z;Fréquence d'émission (Hz);Gain d'antenne (dBi);Puissance d'émission (dBm) +# Les lignes vides ou commençant par un # sont ignorées. Les commentaires doivent obligatoirement être en début de ligne. + +# mini5 (Laseldi) +00:13:CE:95:E1:6F;4.93;25.81;3.00;2417000000;5.0;20.0 + +# mini6 (Laurent / Wassim Haimed) +00:13:CE:95:DE:7E;4.83;10.88;3.71;2417000000;5.0;20.0 + +# mini4 (Incubateur) +00:13:CE:97:78:79;20.05;28.31;3.74;2417000000;5.0;20.0 + +# mini3 (Dominique) +00:13:CE:8F:77:43;7.085;4.13;0.74;2417000000;5.0;20.0 + +# mini1 (Adrien) +00:13:CE:8F:78:D9;30.35;5.74;0.98;2417000000;5.0;20.0 diff --git a/GuiNuMo-server/clientinfo.cc b/GuiNuMo-server/clientinfo.cc index 6f4be6a..ff294e0 100644 --- a/GuiNuMo-server/clientinfo.cc +++ b/GuiNuMo-server/clientinfo.cc @@ -1,19 +1,31 @@ #include "clientinfo.hh" -ClientInfo::ClientInfo(string ip, int port, float antg) +ClientInfo::ClientInfo(const string &ip, const int &port, const float &antg) { client_ip = ip; antenna_gain = antg; client_listen_port = port; - for(int i=0 ; i < 5 ; i++) - viterbi_distances[i] = 0; + int N = VITERBI_N, K = VITERBI_K ; + viterbi_V = new float*[N-1] ; + for (int n=0 ; n < N-1 ; n++) + { + viterbi_V[n] = new float[K] ; + for (int k=0 ; k < K ; k++) + viterbi_V[n][k] = 0 ; + } + } ClientInfo::~ClientInfo() { - viterbi_vector1.clear(); - viterbi_vector2.clear(); + viterbi_Eprevious.clear(); + viterbi_Ecurrent.clear(); + + int N = VITERBI_N ; + for(int n=0; n < N-1 ; n++) + delete[] viterbi_V[n] ; + delete viterbi_V ; } diff --git a/GuiNuMo-server/clientinfo.hh b/GuiNuMo-server/clientinfo.hh index 4374a92..4e55a24 100644 --- a/GuiNuMo-server/clientinfo.hh +++ b/GuiNuMo-server/clientinfo.hh @@ -13,20 +13,27 @@ using std::string; #define CLIENT_DEFAULT_IP "127.0.0.1" #define CLIENT_DEFAULT_ANTENNA_GAIN 2 // Good default value +#define VITERBI_N 5 +#define VITERBI_K 8 + class ClientInfo { protected: string client_ip; float antenna_gain; int client_listen_port; - vector viterbi_vector1; // Previous vector - vector viterbi_vector2; // Last vector - float viterbi_distances[5]; - + vector viterbi_Ecurrent; // Last vector + vector viterbi_Eprevious; // Previous vector + // float viterbi_distances[5]; + float **viterbi_V ; + public: - ClientInfo(string ip = CLIENT_DEFAULT_IP, int port = CLIENT_DEFAULT_PORT, float antg = CLIENT_DEFAULT_ANTENNA_GAIN); + ClientInfo(const string &ip = CLIENT_DEFAULT_IP, const int &port = CLIENT_DEFAULT_PORT, const float &antg = CLIENT_DEFAULT_ANTENNA_GAIN); ~ClientInfo(); float getAntennaGain()const { return antenna_gain; }; + vector& getRef_viterbi_Ecurrent() { return viterbi_Ecurrent; } ; + vector& getRef_viterbi_Eprevious() { return viterbi_Eprevious; } ; + float** get_viterbi_V() { return viterbi_V; } ; }; #endif diff --git a/GuiNuMo-server/measurement.cc b/GuiNuMo-server/measurement.cc index a37ec26..b6a59b6 100644 --- a/GuiNuMo-server/measurement.cc +++ b/GuiNuMo-server/measurement.cc @@ -1,6 +1,6 @@ #include "measurement.hh" -void Measurement::addSsValue(int ssv) +void Measurement::addSsValue(const int &ssv) { float ss_mwatts = pow(10, (float) ssv / 10.0) + (ss_list.size() * pow(10, average_ss / 10.0)); @@ -8,12 +8,12 @@ void Measurement::addSsValue(int ssv) average_ss = 10 * log10(ss_mwatts / ss_list.size()); } -float Measurement::getSsSquareDistance(float ss)const +float Measurement::getSsSquareDistance(const float &ss)const { return ((ss - average_ss) * (ss - average_ss)); } -ostream &operator<<(ostream &os, Measurement &m) +ostream &operator<<(ostream &os, const Measurement &m) { unsigned int i; diff --git a/GuiNuMo-server/measurement.hh b/GuiNuMo-server/measurement.hh index 7518598..0fe50e6 100644 --- a/GuiNuMo-server/measurement.hh +++ b/GuiNuMo-server/measurement.hh @@ -17,16 +17,16 @@ protected: vector ss_list; public: - Measurement(string ma = "ff:ff:ff:ff:ff:ff", float avg = 0, vector ssl = vector()) { mac_addr = ma; average_ss = avg, ss_list = ssl; }; + Measurement(const string &ma = "ff:ff:ff:ff:ff:ff", const float &avg = 0, const vector &ssl = vector()) { mac_addr = ma; average_ss = avg, ss_list = ssl; }; Measurement(const Measurement &m) { mac_addr = m.mac_addr; average_ss = m.average_ss; ss_list = m.ss_list; }; ~Measurement() { ss_list.clear(); }; vector getSsList()const { return ss_list; }; float getAverage()const { return average_ss; }; string getMacAddr()const { return mac_addr; }; - void addSsValue(int ssv); - void setMacAddr(string ma) { mac_addr = ma; }; - float getSsSquareDistance(float ss)const; - friend ostream &operator<<(ostream &os, Measurement &m); + void addSsValue(const int &ssv); + void setMacAddr(const string &ma) { mac_addr = ma; }; + float getSsSquareDistance(const float &ss)const; + friend ostream &operator<<(ostream &os, const Measurement &m); }; #endif diff --git a/GuiNuMo-server/point.hh b/GuiNuMo-server/point.hh index 2e6921c..6e33b0e 100644 --- a/GuiNuMo-server/point.hh +++ b/GuiNuMo-server/point.hh @@ -14,17 +14,17 @@ protected: float z; public: - Point(float _x = 0, float _y = 0, float _z = 0) { x = _x; y = _y; z = _z; } ; + Point(const float &_x = 0, const float &_y = 0, const float &_z = 0) { x = _x; y = _y; z = _z; } ; Point(const Point &p) { x = p.x; y = p.y; z = p.z; } ; ~Point() {}; float getX()const { return x; }; float getY()const { return y; }; float getZ()const { return z; }; - void setX(float my_x) { x = my_x; }; - void setY(float my_y) { y = my_y; }; - void setZ(float my_z) { z = my_z; }; + void setX(const float &_x) { x = _x; }; + void setY(const float &_y) { y = _y; }; + void setZ(const float &_z) { z = _z; }; float squareDistance(const Point &p)const { return ((x-p.x)*(x-p.x))+((y-p.y)*(y-p.y))+((z-p.z)*(z-p.z)); }; - float squareDistance(float mx, float my, float mz)const { return ((x-mx)*(x-mx))+((y-my)*(y-my))+((z-mz)*(z-mz)); }; + float squareDistance(const float &mx, const float &my, const float &mz)const { return ((x-mx)*(x-mx))+((y-my)*(y-my))+((z-mz)*(z-mz)); }; float distance(const Point &p)const { return sqrt(((x-p.x)*(x-p.x))+((y-p.y)*(y-p.y))+((z-p.z)*(z-p.z))); }; Point operator=(const Point &p); bool operator==(const Point &p)const; diff --git a/GuiNuMo-server/referencepoint.cc b/GuiNuMo-server/referencepoint.cc index 0a56c24..8ad87d5 100644 --- a/GuiNuMo-server/referencepoint.cc +++ b/GuiNuMo-server/referencepoint.cc @@ -1,12 +1,12 @@ #include "referencepoint.hh" -float ReferencePoint::getSsSquareDistance(vector m)const +float ReferencePoint::getSsSquareDistance(const vector &m)const { unsigned int i, j; float ret = 0; bool found; - for(i=0;i m)const j++; } } + return ret; } -void ReferencePoint::addMeasurement(string mac_a, int value) +void ReferencePoint::addMeasurement(const string &mac_a, const int &value) { unsigned int i; Measurement m; bool inserted = false; - for(i=0;i measurement_list; public: - ReferencePoint(float x = 0, float y = 0, float z = 0) { coordinates.setX(x); coordinates.setY(y); coordinates.setZ(z); }; + ReferencePoint(const float &x = 0, const float &y = 0, const float &z = 0) { coordinates.setX(x); coordinates.setY(y); coordinates.setZ(z); }; ReferencePoint(const ReferencePoint &rp) { coordinates = rp.coordinates; measurement_list = rp.measurement_list; }; - ReferencePoint(Point c) { coordinates = c; }; + ReferencePoint(const Point &c) { coordinates = c; }; ~ReferencePoint() { measurement_list.clear(); }; - float getSsSquareDistance(vector m)const; + float getSsSquareDistance(const vector &m)const; Point getCoordinates()const { return coordinates; }; - void addMeasurement(string mac_a, int value); - void setCoordinates(Point p) { coordinates = p; }; + void addMeasurement(const string &mac_a, const int &value); + void setCoordinates(const Point &p) { coordinates = p; }; ReferencePoint operator=(const ReferencePoint &rp); friend ostream &operator<<(ostream &os, ReferencePoint &rp); vector getMeasurementList()const { return measurement_list; }; - bool getPowerForAp(string ap_mac, float * p)const; + bool getPowerForAp(const string &ap_mac, float *p)const; }; #endif diff --git a/GuiNuMo-server/server.cc b/GuiNuMo-server/server.cc index db3b09d..6b6f02e 100644 --- a/GuiNuMo-server/server.cc +++ b/GuiNuMo-server/server.cc @@ -1,9 +1,18 @@ #include "server.hh" -/* Misc. very usefull functions */ +#define TEST +#define DEBUG // Décommenter pour avoir de l'affichage en plus. +//#define DEBUG_2 // Décommenter pour avoir encore plus d'affichage. + + + + +/******* Misc. very usefull functions *******/ + + /* Explodes a string into substrings based on separator sep. Returns a string vector. */ -inline vector explode(string input, char sep) +inline vector explode(const string &input, const char &sep) { vector vs; string tmp; @@ -27,8 +36,10 @@ inline vector explode(string input, char sep) return vs; } + + /* Function to convert a string to an integer */ -inline int string2int(string nb) +inline int string2int(const string &nb) { istringstream iss(nb); @@ -37,8 +48,10 @@ inline int string2int(string nb) return tmp; } + + /* Function to convert a string to an unsigned integer */ -inline unsigned int string2uint(string nb) +inline unsigned int string2uint(const string &nb) { istringstream iss(nb); @@ -47,8 +60,10 @@ inline unsigned int string2uint(string nb) return tmp; } + + /* Function to convert a string to a float */ -inline float string2float(string nb) +inline float string2float(const string &nb) { istringstream iss(nb); @@ -57,6 +72,8 @@ inline float string2float(string nb) return tmp; } + + /* Function extracts ints from string */ inline vector extractValues(string buff) { @@ -81,7 +98,9 @@ inline vector extractValues(string buff) return ret; } -inline vector extractReferencePointInfoFromBuffer(string buffer_in) + + +inline vector extractReferencePointInfoFromBuffer(const string &buffer_in) { unsigned int i = 0; string tmp_field; @@ -96,7 +115,7 @@ inline vector extractReferencePointInfoFromBuffer(string buffer_in) } ret.push_back(tmp_field); tmp_field.clear(); - i++; //go after the ';' + i++; // go after the ';' /* y */ while (buffer_in[i] != ';') @@ -108,6 +127,18 @@ inline vector extractReferencePointInfoFromBuffer(string buffer_in) tmp_field.clear(); i++; +#ifndef FRED_CSV_FORMAT // Dans le format Fred, on n'a pas de coordonnée Z. + /* z */ + while (buffer_in[i] != ';') + { + tmp_field.push_back(buffer_in[i]); + i++; + } + ret.push_back(tmp_field); + tmp_field.clear(); + i++; +#endif + /* Extract direction (not used now) */ while (buffer_in[i] != ';') { @@ -118,6 +149,7 @@ inline vector extractReferencePointInfoFromBuffer(string buffer_in) tmp_field.clear(); i++; +#ifdef FRED_CSV_FORMAT /* Extract mac address */ while (buffer_in[i] != ';') { @@ -136,15 +168,36 @@ inline vector extractReferencePointInfoFromBuffer(string buffer_in) } ret.push_back(tmp_field); tmp_field.clear(); +#else + 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, + { +#ifdef DEBUG_2 + cout << "Ajout de la valeur lue : " << tmp_field << endl ; +#endif + ret.push_back(tmp_field) ; // on met la valeur lue dans les valeurs de retour. + tmp_field.clear() ; + } + else // Si on n'est pas sur un séparateur, + tmp_field.push_back(buffer_in[i]) ; // on ajoute le caractère courant à la suite de la valeur lue. + + i++ ; + } +#endif /* Return the vector with each data */ return ret; } + + + /* ***************************************************************** */ + //Server::Server(string ip_addr, int listen_port, int send_port) // FIXME : paramètre send_port inutilisé -Server::Server(string ip_addr, int listen_port) +Server::Server(const string &ip_addr, const int &listen_port) { /* Open socket */ sockListen = socket(PF_INET, SOCK_DGRAM, 0); @@ -158,8 +211,13 @@ Server::Server(string ip_addr, int listen_port) /* Bind */ bind(sockListen, (struct sockaddr *)&server_addr, sizeof(server_addr)); + + makeReferencePointListFromFile(DEFAULT_REF_POINT_FILE); + makeApListFromFile(DEFAULT_AP_FILE); } + + Server::~Server() { client_list.clear(); @@ -169,18 +227,24 @@ Server::~Server() close(sockSend); } -void Server::send_to_client(int cl) + + +void Server::send_to_client(const int &cl) { /* Do not forget to implement later: usefull for a demo */ } + + int Server::receive_data() { /* Do not forget to implement later: usefull for a demo */ return 0; } -bool Server::pointExists(float x, float y, float z)const + + +bool Server::pointExists(const float &x, const float &y, const float &z)const { Point p(x, y, z); unsigned int i; @@ -192,8 +256,10 @@ bool Server::pointExists(float x, float y, float z)const return false; } + + /* Do not forget to call pointExists() before this one */ -unsigned int Server::pointIndex(float x, float y, float z)const +unsigned int Server::pointIndex(const float &x, const float &y, const float &z)const { Point p(x, y, z); unsigned int i; @@ -205,7 +271,9 @@ unsigned int Server::pointIndex(float x, float y, float z)const return 0; // Should never happen } -bool Server::pointExists(Point p)const + + +bool Server::pointExists(const Point &p)const { unsigned int i; @@ -216,7 +284,9 @@ bool Server::pointExists(Point p)const return false; } -bool Server::apExists(string ap_addr)const + + +bool Server::apExists(const string &ap_addr)const { unsigned int i; @@ -227,7 +297,9 @@ bool Server::apExists(string ap_addr)const return false; } -unsigned int Server::apIndex(string ap_addr)const + + +unsigned int Server::apIndex(const string &ap_addr)const { unsigned int i; @@ -238,8 +310,10 @@ unsigned int Server::apIndex(string ap_addr)const return 0; // Should never happen } + + /* Do not forget to call pointExists() before this one */ -unsigned int Server::pointIndex(Point p)const +unsigned int Server::pointIndex(const Point &p)const { unsigned int i; @@ -250,8 +324,23 @@ unsigned int Server::pointIndex(Point p)const return 0; // Should never happen } -/* return -1 if point does not exist, which should never happen */ -vector Server::getkClosestInSs(vector m, unsigned int k, bool ignore_point, Point point_ignored)const + + +/* 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. + */ +vector Server::getkClosestInSs(const vector &m, const unsigned int &k)const +{ + return getkClosestInSs(m, k, NULL) ; +} + + + +/* Selects the "k" closest points from the measurement "m", in the SS space. + * If "point_ignored" is not NULL, the Point "*point_ignored" is ignored. + * Returns an empty vector if no points are found, which should never happen. + */ +vector Server::getkClosestInSs(const vector &m, const unsigned int &k, const Point *point_ignored)const { unsigned int i, j, min_idx; vector distances_vector; @@ -260,7 +349,7 @@ vector Server::getkClosestInSs(vector m, unsigned int k, boo float tmp_distance = 0, dist_max = 10000000, tmp_min; for (i = 0 ; i < reference_point_list.size() ; i++) - if (!ignore_point||(reference_point_list[i].getCoordinates() != point_ignored)) + if (point_ignored == NULL || (reference_point_list[i].getCoordinates() != *point_ignored)) { tmp_distance = reference_point_list[i].getSsSquareDistance(m); /* if not k points, add it */ @@ -268,7 +357,7 @@ vector Server::getkClosestInSs(vector m, unsigned int k, boo { distances_vector.push_back(tmp_distance); points_vector.push_back(reference_point_list[i].getCoordinates()); - dist_max = (dist_max > tmp_distance)?tmp_distance:dist_max; + dist_max = (dist_max > tmp_distance) ? tmp_distance : dist_max; } else { @@ -319,7 +408,18 @@ vector Server::getkClosestInSs(vector m, unsigned int k, boo return points_vector; } -Point Server::getkWeightedInSs(vector m, unsigned int k, bool ignore_point, Point point_ignored)const + + +Point Server::getkWeightedInSs(const vector &m, const unsigned int &k)const +{ + return getkWeightedInSs(m, k, NULL) ; +} + + + +/* If "point_ignored" is not NULL, the Point "*point_ignored" is ignored. + */ +Point Server::getkWeightedInSs(const vector &m, const unsigned int &k, const Point *point_ignored)const { unsigned int i, j; vector distances_vector; @@ -329,7 +429,7 @@ Point Server::getkWeightedInSs(vector m, unsigned int k, bool ignor float total = 0, x = 0, y = 0, z = 0; for (i = 0 ; i < reference_point_list.size() ; i++) - if (!ignore_point || (reference_point_list[i].getCoordinates() != point_ignored)) + if (point_ignored == NULL || (reference_point_list[i].getCoordinates() != *point_ignored)) { tmp_distance = reference_point_list[i].getSsSquareDistance(m); /* if not k points, add it */ @@ -376,7 +476,9 @@ Point Server::getkWeightedInSs(vector m, unsigned int k, bool ignor return ret; } -Point Server::kPointsAverage(vector vp)const + + +Point Server::kPointsAverage(const vector &vp)const { unsigned int i; float x=0, y=0, z=0; @@ -395,7 +497,9 @@ Point Server::kPointsAverage(vector vp)const return p; } -Point Server::fbcm(vector m, int client_idx)const + + +Point Server::fbcm(const vector &m, const int &client_idx)const { Point ret(0, 0, 0); vector addr; @@ -449,7 +553,9 @@ Point Server::fbcm(vector m, int client_idx)const return ret; } -Point Server::interlink(vector m, int client_idx)const + + +Point Server::interlink(const vector &m, const int &client_idx)const { Point ret(0, 0, 0); vector addr; @@ -498,58 +604,116 @@ Point Server::interlink(vector m, int client_idx)const return ret; } -void Server::makeReferencePointListFromFile(string filename) + + +void Server::makeReferencePointListFromFile(const string &filename) { ifstream input_file; char buffer[BUFFER_LENGTH]; string lecture_fichier; ReferencePoint rp; Point tmp_point; - float x, y; + float x, y, z ; // Coordonnées des points unsigned int i, pt_idx; string cpp_buffer, tmp_mes; - vector measures_vector; vector infos; input_file.open(filename.c_str()) ; + if (input_file.fail()) { cerr << "Error opening input file « " << filename << " » !" << endl ; return ; } +#ifdef DEBUG + cout << "Lecture du fichier « " << filename << " »..." ; + int nlines = 0 ; + int npoints = 0 ; +#endif + while (!input_file.eof()) { +#ifdef DEBUG + if (nlines % 100 == 0) + printf("\n%5d", nlines) ; + cout << '.' ; + nlines++ ; +#endif + input_file.getline(buffer, BUFFER_LENGTH); + if ((input_file.rdstate() & ifstream::eofbit) == 0) { /* Extract fields */ cpp_buffer = buffer; + if (cpp_buffer.size() == 0) // Ignorer une ligne vide + continue ; infos = extractReferencePointInfoFromBuffer(cpp_buffer); x = string2float(infos[0]); y = string2float(infos[1]); +#ifdef FRED_CSV_FORMAT + z = DEFAULT_Z ; +#else + z = string2float(infos[2]) ; +#endif + /* Set point coordinates */ tmp_point.setX(x); tmp_point.setY(y); - tmp_point.setZ(DEFAULT_Z); + tmp_point.setZ(z); + /* Use C++ string format */ if (!pointExists(tmp_point)) { rp.setCoordinates(tmp_point); reference_point_list.push_back(rp); +#ifdef DEBUG_2 + npoints++ ; + cout << tmp_point << " : ajouté." << endl ; + } + else + { + cout << tmp_point << " : existe déjà." << endl ; +#endif } pt_idx = pointIndex(tmp_point); - measures_vector = extractValues(infos[4]); + +#ifdef FRED_CSV_FORMAT + vector measures_vector = extractValues(infos[4]) ; for (i = 0 ; i < measures_vector.size() ; i++) reference_point_list[pt_idx].addMeasurement(infos[3], measures_vector[i]); + measures_vector.clear(); +#else + for (i = 4 ; i < infos.size() ; i++) + { +#ifdef DEBUG_2 + cout << "Lecture de la valeur : " << infos[i] << "... " ; +#endif + if (i + 1 < infos.size()) + { + reference_point_list[pt_idx].addMeasurement(infos[i], string2int(infos[i+1])) ; +#ifdef DEBUG_2 + cout << "Mesure ajoutée : AP = " << infos[i] << " | SS = " << string2int(infos[i+1]) << endl ; +#endif + i++ ; + } + } +#endif } } +#ifdef DEBUG + cout << '\n' << nlines << " lignes lues, " << npoints << " points différents ajoutés.\n" << endl ; +#endif + input_file.close(); - measures_vector.clear(); + infos.clear() ; } -void Server::makeApListFromFile(string filename) + + +void Server::makeApListFromFile(const string &filename) { ifstream input_file; char buffer[BUFFER_LENGTH]; @@ -563,15 +727,25 @@ void Server::makeApListFromFile(string filename) return ; } +#ifdef DEBUG + cout << "Lecture du fichier « " << filename << " »..." << endl ; +#endif + while (!input_file.eof()) { input_file.getline(buffer, BUFFER_LENGTH); + if ((input_file.rdstate() & ifstream::eofbit) == 0) { + /* Traitement basique des commentaires */ + if (buffer[0] == '\0' // Ligne vide + || buffer[0] == '#') // ou ligne commençant par # + continue ; // ignorer cette ligne. + ap_infos = explode(buffer, ';'); - cout << buffer << endl; - for (unsigned int i=0 ; i < ap_infos.size() ; i++) - cout << ap_infos[i] << endl; +#ifdef DEBUG + cout << "AP : " << buffer ; +#endif tmp_ap.setApAddr(ap_infos[0]); tmp_ap.setCoordinates(string2float(ap_infos[1]), string2float(ap_infos[2]), string2float(ap_infos[3])); tmp_ap.setFrequency(string2uint(ap_infos[4])); @@ -579,12 +753,22 @@ void Server::makeApListFromFile(string filename) tmp_ap.setOutputPower(string2float(ap_infos[6])); access_point_list.push_back(tmp_ap); ap_infos.clear(); +#ifdef DEBUG + cout << " ajouté." << endl ; +#endif } } +#ifdef DEBUG + cout << endl ; +#endif + + input_file.close(); } + + void Server::printReferencePointList() { unsigned int i; @@ -593,6 +777,8 @@ void Server::printReferencePointList() cout << reference_point_list[i] << endl; } + + void Server::printAccessPointList() { unsigned int i; @@ -601,6 +787,8 @@ void Server::printAccessPointList() cout << access_point_list[i] << endl; } + + void Server::computeFriisFromRefList() { unsigned int i, j; @@ -641,47 +829,118 @@ void Server::computeFriisFromRefList() } + /* - * Function computes new cumulative distances for each running viterbi instance, + * Computes new cumulative distances for each running viterbi instance, * Returns the solution (type: Point) of the last ended viterbi. * Distances are grouped by line corresponding to an instance. - * vk MUST be the same as last.size() and current.size() ! + * "K" MUST be the same as last.size() and current.size() ! */ -/* -Point Server::viterbiLike(float ** dists, unsigned short vk, unsigned short vn, vector last, vector current) +void Server::fastViterbiLike(const unsigned short &N, const unsigned short &K, const unsigned int &id_client) { - unsigned int i, j, k; - float min; - vector last_dists; - Point position; - - // Compute the ending viterbi: line 0 - for (i = 0 ; i < current.size() ; i++) +#ifdef TEST + ClientInfo cl ; + float **V = cl.get_viterbi_V() ; + vector + &E_current = cl.getRef_viterbi_Ecurrent(), + &E_previous = cl.getRef_viterbi_Eprevious() ; +#else + float **V = client_list[id_client].get_viterbi_V() ; + vector + &E_current = client_list[id_client].getRef_viterbi_Ecurrent(), + &E_previous = client_list[id_client].getRef_viterbi_Eprevious() ; +#endif + + int i = 1 ; // Nombre d'ensembles d'historique qu'on a déjà passés + + if (N < 2) { - min = dists[0][0] + last[0].distance(current[i]); - for (j = 1 ; j < last.size() ; j++) - if ((dists[0][j] + last[j].distance(current[i])) < min) - min = dists[0][j] + last[j].distance(current[i]); - last_dists.push_back(min); + cerr << "fastViterbiLike() : N ne peut être inférieur à 2 !" << endl ; + return ; } - // Find shortest dist - min = last_dists[0]; - j = 0; - for (i = 1 ; i < last_dists.size() ; i++) - if (last_dists[i] < min) - { - min = last_dists[i]; - j = i; - } +#ifdef TEST + int pt = 0 ; +#ifdef DEBUG + cout << reference_point_list.size() << " points à traiter." << endl ; +#endif + while (pt < reference_point_list.size()) +#else + while (true) +#endif + { + vector vm ; + Point *ref_coords = NULL ; // Coordonnées de référence (à ignorer). - // Shortest distance determines the true point - position = current[j]; +#ifdef TEST + /* Get point measurements */ + vm.clear(); + vm = reference_point_list[pt].getMeasurementList(); + // *ref_coords = reference_point_list[i].getCoordinates(); +#endif - // Now, compute the remaining of the distance matrix + E_previous = E_current ; + E_current = getkClosestInSs(vm, K, ref_coords) ; // Création de l'ensemble des K points les plus proches dans l'espace des puissances. + if (i > 1) // Si on n'est plus à la première itération + { + /* Recalcul des plus courtes distances */ + for (int n = N-1-i ; n < N-2 ; n++) // Pour chaque historique existant (sauf le dernier), + for (int k = 0 ; k < K ; k++) // pour chaque point de l'historique, + { // on met à jour le chemin minimum entre l'historique précédent et le point : + V[n][k] = V[n+1][0] + E_previous[0].distance(E_current[k]) ; + for (int l = 1 ; l < K ; l++) + { + float f = V[n+1][l] + E_previous[l].distance(E_current[k]) ; + if (f < V[n][k]) + V[n][k] = f ; + } + } + + /* Traitement du dernier historique */ + for (int k = 0 ; k < K ; k++) // Pour chaque point, + { // on récupère le chemin minimum entre l'historique précédent et le point : + V[N-2][k] = E_previous[0].distance(E_current[k]) ; + for (int l = 1 ; l < K ; l++) + { + float f = E_previous[l].distance(E_current[k]) ; + if (f < V[N-2][k]) + V[N-2][k] = f ; + } + } + + /* Choix du min à renvoyer */ + if (N-1-i == 0) + { + float V0_min = V[0][0] ; + for (int k=1 ; k < K ; k++) + { + if (V[0][k] < V0_min) + V0_min = V[0][k] ; + cout << "V[0][" << k << "]=" << V[0][k] << " ; V0_min=" << V0_min << " -- " ; + } + cout << "V0_min = " << V0_min << endl ; + } + } + + // cout << "(N=" << N << ") - 1 - (i=" << i << ") = " << N-1-i << endl ; + for (int n=0 ; n < N-1 ; n++) + { + for (int k=0 ; k < K ; k++) + cout << "[" << V[n][k] << "]" ; + cout << endl ; + } + cout << "\n--------------------------" << endl ; + + if (i < N-1) + i++ ; + +#ifdef TEST + pt++ ; +#endif + } } -*/ + /* For experimentation purpose only */ @@ -693,9 +952,7 @@ void Server::radar_exp() unsigned int i; vector vm; - makeReferencePointListFromFile(DEFAULT_REF_POINT_FILE); - makeApListFromFile(DEFAULT_AP_FILE); - computeFriisFromRefList(); + // computeFriisFromRefList(); /* Open a log file */ logfile.open(DEFAULT_LOGFILE); @@ -720,17 +977,17 @@ void Server::radar_exp() logfile << ref_coords << "\t"; /* From 2 to 5 K-weighted-SS */ - solution = getkWeightedInSs(vm, 2, true, ref_coords); + solution = getkWeightedInSs(vm, 2, &ref_coords); logfile << solution << "\t" << solution.distance(ref_coords) << "\t"; - solution = getkWeightedInSs(vm, 3, true, ref_coords); + solution = getkWeightedInSs(vm, 3, &ref_coords); logfile << solution << "\t" << solution.distance(ref_coords) << "\t"; - solution = getkWeightedInSs(vm, 4, true, ref_coords); + solution = getkWeightedInSs(vm, 4, &ref_coords); logfile << solution << "\t" << solution.distance(ref_coords) << "\t"; - solution = getkWeightedInSs(vm, 5, true, ref_coords); + solution = getkWeightedInSs(vm, 5, &ref_coords); logfile << solution << "\t" << solution.distance(ref_coords) << "\t"; /* Nearest in SS */ - solutions = getkClosestInSs(vm, 1, true, ref_coords); + solutions = getkClosestInSs(vm, 1, &ref_coords); logfile << solutions[0] << "\t" << solutions[0].distance(ref_coords) << "\t"; /* Interlink Networks */ diff --git a/GuiNuMo-server/server.hh b/GuiNuMo-server/server.hh index fa7f36d..ee30e3a 100644 --- a/GuiNuMo-server/server.hh +++ b/GuiNuMo-server/server.hh @@ -22,9 +22,10 @@ using namespace std; using std::string; -#define DEFAULT_LOGFILE "log/radar_exp.csv" -#define DEFAULT_REF_POINT_FILE "csv/complete-scan.csv" -#define DEFAULT_AP_FILE "cfg/accesspoints.cfg" +#define DEFAULT_LOGFILE "log/radar_exp.csv" // Fichier de sortie +#define DEFAULT_REF_POINT_FILE "csv/agreg/toutes.csv" // Fichier des points de référence +#define DEFAULT_AP_FILE "cfg/minipc.cfg" // Fichier de configuration des AP +#define DEFAULT_TRACKING_FILE "csv/agreg/divagation.csv" // Fichier de prérégrination #define DEFAULT_IP "127.0.0.1" #define DEFAULT_LISTEN_PORT 7777 #define LIGHT_SPEED 300000000 @@ -33,9 +34,10 @@ using std::string; #define MINMAX_Y_START -1 #define MINMAX_X_STOP 15 #define MINMAX_Y_STOP 45 -#define BUFFER_LENGTH 1024 -#define DEFAULT_Z 3 -#define DEFAULT_VITERBI_K 5 +#define BUFFER_LENGTH 5000 +//#define DEFAULT_Z 3 // Décommenter pour utiliser des fichiers d'entrée avec des coordonnées dans un seul plan (X, Y). +//#define FRED_CSV_FORMAT // Décommenter pour utiliser les fichiers CSV au « format Fred » (plusieurs lignes par mesure, avec un AP par ligne). + class Server { @@ -48,32 +50,33 @@ protected: int sockSend; public: - //Server(string ip_addr = DEFAULT_IP, int listen_port = DEFAULT_LISTEN_PORT, int send_port); - Server(string ip_addr = DEFAULT_IP, int listen_port = DEFAULT_LISTEN_PORT); + Server(const string &ip_addr = DEFAULT_IP, const int &listen_port = DEFAULT_LISTEN_PORT); ~Server(); - void send_to_client(int cl); + void send_to_client(const int &cl); int receive_data(); - bool pointExists(float x, float y, float z)const; - unsigned int pointIndex(float x, float y, float z)const; - bool pointExists(Point p)const; - bool apExists(string ap_addr)const; - unsigned int apIndex(string ap_addr)const; - unsigned int pointIndex(Point p)const; - vector getkClosestInSs(vector m, unsigned int k, bool ignore_point, Point point_ignored)const; - Point getkWeightedInSs(vector m, unsigned int k, bool ignore_point, Point point_ignored)const; - Point kPointsAverage(vector vp)const; - Point fbcm(vector m, int client_idx)const; - Point interlink(vector m, int client_idx)const; - void makeReferencePointListFromFile(string filename); - void makeApListFromFile(string filename); + 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 apExists(const string &ap_addr)const; + unsigned int apIndex(const string &ap_addr)const; + unsigned int pointIndex(const Point &p)const; + vector getkClosestInSs(const vector &m, const unsigned int &k)const ; + vector getkClosestInSs(const vector &m, const unsigned int &k, const Point *point_ignored)const ; + Point getkWeightedInSs(const vector &m, const unsigned int &k)const ; + Point getkWeightedInSs(const vector &m, const unsigned int &k, const Point *point_ignored)const ; + Point kPointsAverage(const vector &vp)const; + Point fbcm(const vector &m, const int &client_idx)const; + Point interlink(const vector &m, const int &client_idx)const; + void makeReferencePointListFromFile(const string &filename); + void makeApListFromFile(const string &filename); void printReferencePointList(); void printAccessPointList(); void computeFriisFromRefList(); unsigned int getNbReferencePoints()const { return reference_point_list.size(); }; /* For experimentation purpose only ! */ void radar_exp(); - /* For compilation purpose :-) */ - // Point viterbiLike(float**, short unsigned int, short unsigned int, std::vector >, std::vector >) ; + + void fastViterbiLike(const unsigned short &N, const unsigned short &K, const unsigned int &id_client) ; }; #endif diff --git a/GuiNuMo-server/test.cc b/GuiNuMo-server/test.cc index 67ba27e..4b3011b 100644 --- a/GuiNuMo-server/test.cc +++ b/GuiNuMo-server/test.cc @@ -11,7 +11,6 @@ using namespace std; using std::string; -#define BUFFER_LENGTH 1024 int main(int argc, char ** argv) { @@ -19,7 +18,8 @@ int main(int argc, char ** argv) string read_file; Server my_server; - my_server.radar_exp(); + // my_server.radar_exp(); + my_server.fastViterbiLike(VITERBI_N, VITERBI_K, 0) ; return 0; }