Import initial (GuiNuMo-server)

Projet d'origine, plein de bugs et pas fini.
Avant import du serveur GuiNuMo.
Plein de choses à faire.

git-svn-id: https://pif.pu-pm.univ-fcomte.fr/svn/loc@26 785a6c6c-259e-4ff1-8b91-dc31627914f0
This commit is contained in:
Frédéric Lassabe 2008-04-11 08:11:29 +00:00 committed by Matteo Cypriani
parent edd29b4ee3
commit f273cba513
19 changed files with 1384 additions and 0 deletions

35
GuiNuMo-server/Makefile Normal file
View File

@ -0,0 +1,35 @@
GXX=g++
GXXFLAGS=-Wall -O2
LD=g++
LDFLAGS= -lm -O2
all: test
clientinfo.o: clientinfo.cc clientinfo.hh point.hh
$(GXX) $(GXXFLAGS) clientinfo.cc -c -o clientinfo.o
server.o: server.cc server.hh clientinfo.hh referencepoint.hh accesspoint.hh point.hh measurement.hh
$(GXX) $(GXXFLAGS) server.cc -c -o server.o
referencepoint.o: referencepoint.cc referencepoint.hh measurement.hh point.hh
$(GXX) $(GXXFLAGS) referencepoint.cc -c -o referencepoint.o
accesspoint.o: accesspoint.cc accesspoint.hh point.hh
$(GXX) $(GXXFLAGS) accesspoint.cc -c -o accesspoint.o
point.o: point.cc point.hh
$(GXX) $(GXXFLAGS) point.cc -c -o point.o
measurement.o: measurement.cc measurement.hh
$(GXX) $(GXXFLAGS) measurement.cc -c -o measurement.o
test.o: test.cc point.hh measurement.hh accesspoint.hh referencepoint.hh clientinfo.hh server.hh
$(GXX) $(GXXFLAGS) test.cc -c -o test.o
test: test.o point.o measurement.o accesspoint.o referencepoint.o clientinfo.o server.o
$(LD) $(LDFLAGS) -o test test.o point.o measurement.o accesspoint.o referencepoint.o clientinfo.o server.o
clean:
rm -fv *~
rm -fv *.o
rm -fv test

View File

@ -0,0 +1,5 @@
#mac@;x;y;z;antenna;power
00:09:5b:42:ed:5b;4.50;22.00;0.00;5.00;20.00
00:13:10:1f:77:5f;1.50;5.50;3.00;2.00;20.00
00:18:39:c5:c3:26;25.00;20.00;0.00;2.00;20.00

View File

@ -0,0 +1,67 @@
#include "accesspoint.hh"
AccessPoint::AccessPoint()
{
coordinates.setX(0);
coordinates.setY(0);
coordinates.setZ(0);
ap_addr = "";
friis_index = 0;
freq = 2412;
antenna_gain = 2; //a good value
output_power = 20; //another good value, eq. to 100mW
}
AccessPoint::AccessPoint(string addr, float fidx, Point coords, unsigned int f, float antg, float outp)
{
coordinates = coords;
ap_addr = addr;
friis_index = fidx;
freq = f;
antenna_gain = antg;
output_power = outp;
}
AccessPoint::AccessPoint(const AccessPoint &ap)
{
coordinates = ap.coordinates;
ap_addr = ap.ap_addr;
friis_index = ap.friis_index;
freq = ap.freq;
antenna_gain = ap.antenna_gain;
output_power = ap.output_power;
}
bool AccessPoint::operator==(const AccessPoint &ap)const
{
if(ap_addr == ap.ap_addr)
return true;
return false;
}
bool AccessPoint::operator!=(const AccessPoint &ap)const
{
if(ap_addr != ap.ap_addr)
return true;
return false;
}
AccessPoint AccessPoint::operator=(const AccessPoint &ap)
{
if(this == &ap)
return *this;
ap_addr = ap.ap_addr;
friis_index = ap.friis_index;
return *this;
}
ostream &operator<<(ostream & os, AccessPoint & ap)
{
os << "MAC Address: " << ap.ap_addr << endl;
os << "Coordinates: " << ap.coordinates << endl;
os << "Frequency: " << ap.freq << " Hz" << endl;
os << "Antenna gain: " << ap.antenna_gain << "dBi" << endl;
os << "Output power: " << ap.output_power << "dBm" << endl;
return os;
}

View File

@ -0,0 +1,45 @@
#ifndef _ACCESSPOINT_HH_
#define _ACCESSPOINT_HH_
#include <iostream>
#include <string>
#include "point.hh"
using namespace std;
using std::string;
class AccessPoint
{
protected:
Point coordinates;
string ap_addr;
float friis_index;
unsigned int freq;
float antenna_gain;
float output_power;
public:
AccessPoint();
AccessPoint(string addr, float fidx, Point coords, unsigned int f, float antg, float outp);
AccessPoint(const AccessPoint &ap);
~AccessPoint() {};
string getApAddr()const { return ap_addr; };
float getFriisIndex()const { return friis_index; };
Point getCoordinates()const { return coordinates; };
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; };
bool operator==(const AccessPoint &ap)const;
bool operator!=(const AccessPoint &ap)const;
AccessPoint operator=(const AccessPoint &ap);
friend ostream &operator<<(ostream & os, AccessPoint & ap);
};
#endif

View File

@ -0,0 +1,3 @@
00:09:5b:42:ed:5b;3.50;22.50;0.00;2462000000;5.0;20.0
00:13:10:1f:77:5f;1.70;4.70;3.00;2462000000;2.1;20.0
00:18:39:c5:c3:26;27.0;20.0;0.00;2462000000;2.1;20.0

41
GuiNuMo-server/area.cc Normal file
View File

@ -0,0 +1,41 @@
#include "area.hh"
Area::Area()
{
area_name = "None";
x_min = 0;
x_max = 0;
y_min = 0;
y_max = 0;
z_min = 0;
z_max = 0;
}
Area::Area(string an, float x1, float x2, float y1, float y2, float z1, float z2)
{
area_name = an;
x_min = x1;
x_max = x2;
y_min = y1;
y_max = y2;
z_min = z1;
z_max = z2;
}
Area::Area(const Area &a)
{
area_name = a.area_name;
x_min = a.x_min;
x_max = a.x_max;
y_min = a.y_min;
y_max = a.y_max;
z_min = a.z_min;
z_max = a.z_max;
}
bool Area::containsPoint(Point p)const
{
if((p.getX()>=x_min)&&(p.getX()<=x_max)&&(p.getY()>=y_min)&&(p.getY()<=y_max)&&(p.getZ()>=z_min)&&(p.getZ()<=z_max))
return true;
return false;
}

43
GuiNuMo-server/area.hh Normal file
View File

@ -0,0 +1,43 @@
#ifndef _AREA_HH_
#define _AREA_HH_
#include <iostream>
#include <string>
using namespace std;
using std::string;
class Area
{
protected:
string area_name;
float x_min;
float x_max;
float y_min;
float y_max;
float z_min;
float z_max;
public:
Area();
Area(string an, float x1, float x2, float y1, float y2, float z1, float z2);
Area(const Area &a);
~Area() {};
bool containsPoint(Point p)const;
string getLabel()const { return label; };
float getXmin()const { return x_min; };
float getXmax()const { return x_max; };
float getYmin()const { return y_min; };
float getYmax()const { return y_max; };
float getZmin()const { return z_min; };
float getZmax()const { return z_max; };
void setLabel(string l) { label = l; };
void setXmin(float v) { x_min = v; };
void setXmax(float v) { x_max = v; };
void setYmin(float v) { y_min = v; };
void setYmax(float v) { y_max = v; };
void setZmin(float v) { z_min = v; };
void setZmax(float v) { z_max = v; };
};
#endif

View File

@ -0,0 +1,26 @@
#include "clientinfo.hh"
ClientInfo::ClientInfo()
{
client_ip = "127.0.0.1";
antenna_gain = 2; //good default value
client_listen_port = CLIENT_DEFAULT_PORT;
for(int i=0;i<5;i++)
viterbi_distances[i] = 0;
}
ClientInfo::ClientInfo(string ip, int port, float antg)
{
client_ip = ip;
antenna_gain = antg;
client_listen_port = port;
for(int i=0;i<5;i++)
viterbi_distances[i] = 0;
}
ClientInfo::~ClientInfo()
{
viterbi_vector1.clear();
viterbi_vector2.clear();
}

View File

@ -0,0 +1,31 @@
#ifndef _CLIENTINFO_HH_
#define _CLIENTINFO_HH_
#include <iostream>
#include <string>
#include <vector>
#include "point.hh"
using namespace std;
using std::string;
#define CLIENT_DEFAULT_PORT 7778
class ClientInfo
{
protected:
string client_ip;
float antenna_gain;
int client_listen_port;
vector<Point> viterbi_vector1; /* Previous vector */
vector<Point> viterbi_vector2; /* Last vector */
float viterbi_distances[5];
public:
ClientInfo();
ClientInfo(string ip, int port, float antg);
~ClientInfo();
float getAntennaGain()const { return antenna_gain; };
};
#endif

View File

@ -0,0 +1,33 @@
#include "measurement.hh"
void Measurement::addSsValue(int ssv)
{
float ss_mwatts = pow(10, (float) ssv / 10.0) + (ss_list.size() * pow(10, average_ss / 10.0));
ss_list.push_back(ssv);
average_ss = 10 * log10(ss_mwatts / ss_list.size());
}
float Measurement::getSsSquareDistance(float ss)const
{
return ((ss - average_ss) * (ss - average_ss));
}
ostream &operator<<(ostream &os, Measurement &m)
{
unsigned int i;
os << m.mac_addr << "->";
if(m.ss_list.size() == 0)
os << "No values";
else
for(i=0;i<m.ss_list.size();i++)
{
os << m.ss_list[i];
if(i < m.ss_list.size() - 1)
os << ";";
}
os << " [AVG=" << m.average_ss << "]";
return os;
}

View File

@ -0,0 +1,33 @@
#ifndef _MEASUREMENT_HH_
#define _MEASUREMENT_HH_
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
using namespace std;
using std::string;
class Measurement
{
protected:
string mac_addr;
float average_ss;
vector<int> ss_list;
public:
Measurement() { mac_addr = "ff:ff:ff:ff:ff:ff"; average_ss = 0; };
Measurement(string ma, float avg, vector<int> ssl) { 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<int> 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);
};
#endif

31
GuiNuMo-server/point.cc Normal file
View File

@ -0,0 +1,31 @@
#include "point.hh"
Point Point::operator=(const Point &p)
{
if(this == &p)
return *this;
x = p.x;
y = p.y;
z = p.z;
return *this;
}
bool Point::operator==(const Point &p)const
{
if((x == p.x)&&(y == p.y)&&(z == p.z))
return true;
return false;
}
bool Point::operator!=(const Point &p)const
{
if((x == p.x)&&(y == p.y)&&(z == p.z))
return false;
return true;
}
ostream &operator<<(ostream &os, const Point &p)
{
os << "(" << p.x << ";" << p.y << ";" << p.z << ")";
return os;
}

36
GuiNuMo-server/point.hh Normal file
View File

@ -0,0 +1,36 @@
#ifndef _POINT_HH_
#define _POINT_HH_
#include <iostream>
#include <math.h>
using namespace std;
class Point
{
protected:
float x;
float y;
float z;
public:
Point() { x = 0; y = 0; z = 0; };
Point(const Point &p) { x = p.x; y = p.y; z = p.z; } ;
Point(float my_x, float my_y, float my_z) { x = my_x; y = my_y; z = my_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; };
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 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;
bool operator!=(const Point &p)const;
friend ostream &operator<<(ostream & os, const Point &p);
};
#endif

View File

@ -0,0 +1,82 @@
#include "referencepoint.hh"
float ReferencePoint::getSsSquareDistance(vector<Measurement> m)const
{
unsigned int i, j;
float ret = 0;
bool found;
for(i=0;i<m.size();i++)
{
j = 0;
found = false;
while((j < measurement_list.size())&&(found == false))
{
if(measurement_list[j].getMacAddr() == m[i].getMacAddr())
{
found = true;
ret += measurement_list[j].getSsSquareDistance(m[i].getAverage());
}
j++;
}
}
return ret;
}
void ReferencePoint::addMeasurement(string mac_a, int value)
{
unsigned int i;
Measurement m;
bool inserted = false;
for(i=0;i<measurement_list.size();i++)
if(measurement_list[i].getMacAddr() == mac_a)
{
measurement_list[i].addSsValue(value);
inserted = true;
break;
}
if(inserted == false)
{
m.setMacAddr(mac_a);
m.addSsValue(value);
measurement_list.push_back(m);
}
}
ReferencePoint ReferencePoint::operator=(const ReferencePoint &rp)
{
if(this == &rp)
return *this;
coordinates = rp.coordinates;
measurement_list = rp.measurement_list;
return *this;
}
ostream &operator<<(ostream &os, ReferencePoint &rp)
{
unsigned int i;
os << rp.coordinates << endl;
if(rp.measurement_list.size() == 0)
os << "No measurements" << endl;
else
for(i=0;i<rp.measurement_list.size();i++)
os << rp.measurement_list[i] << endl;
return os;
}
bool ReferencePoint::getPowerForAp(string ap_mac, float * p)const
{
unsigned int i;
for(i=0;i<measurement_list.size();i++)
if(measurement_list[i].getMacAddr() == ap_mac)
{
*p = measurement_list[i].getAverage();
return true;
}
return false;
}

View File

@ -0,0 +1,33 @@
#ifndef _REFERENCEPOINT_HH_
#define _REFERENCEPOINT_HH_
#include <iostream>
#include <vector>
#include "point.hh"
#include "measurement.hh"
using namespace std;
class ReferencePoint
{
protected:
Point coordinates;
vector<Measurement> measurement_list;
public:
ReferencePoint() { coordinates.setX(0); coordinates.setY(0); coordinates.setZ(0); };
ReferencePoint(const ReferencePoint &rp) { coordinates = rp.coordinates; measurement_list = rp.measurement_list; };
ReferencePoint(float x, float y, float z) { coordinates.setX(x); coordinates.setY(y); coordinates.setZ(z); };
ReferencePoint(Point c) { coordinates = c; };
~ReferencePoint() { measurement_list.clear(); };
float getSsSquareDistance(vector<Measurement> m)const;
Point getCoordinates()const { return coordinates; };
void addMeasurement(string mac_a, int value);
void setCoordinates(Point p) { coordinates = p; };
ReferencePoint operator=(const ReferencePoint &rp);
friend ostream &operator<<(ostream &os, ReferencePoint &rp);
vector<Measurement> getMeasurementList()const { return measurement_list; };
bool getPowerForAp(string ap_mac, float * p)const;
};
#endif

738
GuiNuMo-server/server.cc Normal file
View File

@ -0,0 +1,738 @@
#include "server.hh"
/* Misc. very usefull functions */
/* Explodes a string into substrings based on separator sep. Returns a string vector. */
inline vector<string> explode(string input, char sep)
{
vector<string> vs;
string tmp;
unsigned int i;
for(i=0;i<input.size();i++)
if(input[i] == sep)
{
vs.push_back(tmp);
tmp.clear();
}
else
{
tmp.push_back(input[i]);
}
//Last entry, did not encounter a separator.
vs.push_back(tmp);
tmp.clear();
return vs;
}
/* Function to convert a string to an integer */
inline int string2int(string nb)
{
istringstream iss(nb);
int tmp;
iss>>tmp;
return tmp;
}
/* Function to convert a string to an unsigned integer */
inline unsigned int string2uint(string nb)
{
istringstream iss(nb);
unsigned int tmp;
iss>>tmp;
return tmp;
}
/* Function to convert a string to a float */
inline float string2float(string nb)
{
istringstream iss(nb);
float tmp;
iss>>tmp;
return tmp;
}
/* Function extracts ints from string */
inline vector<int> extractValues(string buff)
{
unsigned int ptr = 0;
vector<int> ret;
string tmp_field;
if(buff[buff.size()-1] != ';')
buff.push_back(';');
while(ptr < buff.size())
{
if(buff[ptr] != ';')
tmp_field.push_back(buff[ptr]);
else
{
ret.push_back(string2int(tmp_field));
tmp_field.clear();
}
ptr++;
}
return ret;
}
inline vector<string> extractReferencePointInfoFromBuffer(string buffer_in)
{
unsigned int i = 0;
string tmp_field;
vector<string> ret;
/* Extract coordinates */
/* x */
while(buffer_in[i] != ';')
{
tmp_field.push_back(buffer_in[i]);
i++;
}
ret.push_back(tmp_field);
tmp_field.clear();
i++; //go after the ';'
/* y */
while(buffer_in[i] != ';')
{
tmp_field.push_back(buffer_in[i]);
i++;
}
ret.push_back(tmp_field);
tmp_field.clear();
i++;
/* Extract direction (not used now) */
while(buffer_in[i] != ';')
{
tmp_field.push_back(buffer_in[i]);
i++;
}
ret.push_back(tmp_field);
tmp_field.clear();
i++;
/* Extract mac address */
while(buffer_in[i] != ';')
{
tmp_field.push_back(buffer_in[i]);
i++;
}
ret.push_back(tmp_field);
tmp_field.clear();
i++;
/* Extract scan list */
while(i < buffer_in.size())
{
tmp_field.push_back(buffer_in[i]);
i++;
}
ret.push_back(tmp_field);
tmp_field.clear();
/* Return the vector with each data */
return ret;
}
/* ***************************************************************** */
Server::Server()
{
/* Open socket */
sockListen = socket(PF_INET, SOCK_DGRAM, 0);
sockSend = socket(PF_INET, SOCK_DGRAM, 0);
/* Set addr */
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(DEFAULT_LISTEN_PORT);
server_addr.sin_addr.s_addr = inet_addr(DEFAULT_IP);
memset(server_addr.sin_zero, '\0', sizeof(server_addr.sin_zero));
/* Bind */
bind(sockListen, (struct sockaddr *)&server_addr, sizeof(server_addr));
}
Server::Server(string ip_addr, int listen_port, int send_port)
{
/* Open socket */
sockListen = socket(PF_INET, SOCK_DGRAM, 0);
sockSend = socket(PF_INET, SOCK_DGRAM, 0);
/* Set addr */
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(listen_port);
server_addr.sin_addr.s_addr = inet_addr(ip_addr.c_str());
memset(server_addr.sin_zero, '\0', sizeof(server_addr.sin_zero));
/* Bind */
bind(sockListen, (struct sockaddr *)&server_addr, sizeof(server_addr));
}
Server::~Server()
{
client_list.clear();
reference_point_list.clear();
access_point_list.clear();
close(sockListen);
close(sockSend);
}
void Server::send_to_client(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
{
Point p(x, y, z);
unsigned int i;
for(i=0;i<reference_point_list.size();i++)
if(p == reference_point_list[i].getCoordinates())
return true;
return false;
}
/* Do not forget to call pointExists() before this one */
unsigned int Server::pointIndex(float x, float y, float z)const
{
Point p(x, y, z);
unsigned int i;
for(i=0;i<reference_point_list.size();i++)
if(p == reference_point_list[i].getCoordinates())
return i;
/* Should never happen */
return 0;
}
bool Server::pointExists(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(string ap_addr)const
{
unsigned int i;
for(i=0;i<access_point_list.size();i++)
if(access_point_list[i].getApAddr() == ap_addr)
return true;
return false;
}
unsigned int Server::apIndex(string ap_addr)const
{
unsigned int i;
for(i=0;i<access_point_list.size();i++)
if(access_point_list[i].getApAddr() == ap_addr)
return i;
/* Should never happen */
return 0;
}
/* Do not forget to call pointExists() before this one */
unsigned int Server::pointIndex(Point p)const
{
unsigned int i;
for(i=0;i<reference_point_list.size();i++)
if(p == reference_point_list[i].getCoordinates())
return i;
/* Should never happen */
return 0;
}
/* return -1 if point does not exist, which should never happen */
vector<Point> Server::getkClosestInSs(vector<Measurement> m, unsigned int k, bool ignore_point, Point point_ignored)const
{
unsigned int i, j, min_idx;
vector<float> distances_vector;
vector<Point> points_vector;
Point tmp_pt;
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))
{
tmp_distance = reference_point_list[i].getSsSquareDistance(m);
/* if not k points, add it */
if(distances_vector.size() < k)
{
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;
}
else
{
/* if tmp_dst < dist_max, should add it and remove previous greatest dist. */
if(dist_max > tmp_distance)
{
/* remove old max */
for(j=0;j<distances_vector.size();j++)
if(distances_vector[j] == dist_max)
{
dist_max = tmp_distance;
distances_vector.erase(distances_vector.begin() + j);
points_vector.erase(points_vector.begin() + j);
distances_vector.push_back(tmp_distance);
points_vector.push_back(reference_point_list[i].getCoordinates());
break;
}
}
/* Else nothing needs to be done */
}
}
/* Sorts the vector */
for(i=0;i<distances_vector.size()-1;i++)
{
tmp_min = distances_vector[i];
min_idx = i;
for(j=i+1;j<distances_vector.size();j++)
if(tmp_min > distances_vector[j])
{
tmp_min = distances_vector[j];
min_idx = j;
}
if(min_idx != i)
{
/* Swap points */
tmp_pt = points_vector[i];
points_vector[i] = points_vector[min_idx];
points_vector[min_idx] = tmp_pt;
/* Swap distances */
distances_vector[min_idx] = distances_vector[i];
distances_vector[i] = tmp_min;
}
}
return points_vector;
}
Point Server::getkWeightedInSs(vector<Measurement> m, unsigned int k, bool ignore_point, Point point_ignored)const
{
unsigned int i, j;
vector<float> distances_vector;
vector<Point> points_vector;
float tmp_distance = 0, dist_max = 10000000;
Point ret;
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))
{
tmp_distance = reference_point_list[i].getSsSquareDistance(m);
/* if not k points, add it */
if(distances_vector.size() < k)
{
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;
}
else
{
/* if tmp_dst < dist_max, should add it and remove previous greatest dist. */
if(dist_max > tmp_distance)
{
/* remove old max */
for(j=0;j<distances_vector.size();j++)
if(distances_vector[j] == dist_max)
{
dist_max = tmp_distance;
distances_vector.erase(distances_vector.begin() + j);
points_vector.erase(points_vector.begin() + j);
distances_vector.push_back(tmp_distance);
points_vector.push_back(reference_point_list[i].getCoordinates());
break;
}
}
/* Else nothing needs to be done */
}
}
for(i=0;i<distances_vector.size();i++)
total += distances_vector[i];
for(i=0;i<distances_vector.size();i++)
{
x += points_vector[i].getX() * distances_vector[i] / total;
y += points_vector[i].getY() * distances_vector[i] / total;
z += points_vector[i].getZ() * distances_vector[i] / total;
}
ret.setX(x);
ret.setY(y);
ret.setZ(z);
return ret;
}
Point Server::kPointsAverage(vector<Point> vp)const
{
unsigned int i;
float x=0, y=0, z=0;
Point p;
for(i=0;i<vp.size();i++)
{
x += vp[i].getX();
y += vp[i].getY();
z += vp[i].getZ();
}
p.setX(x / (float) vp.size());
p.setY(y / (float) vp.size());
p.setZ(z / (float) vp.size());
return p;
}
Point Server::fbcm(vector<Measurement> m, int client_idx)const
{
Point ret(0, 0, 0);
vector<string> addr;
vector<float> dist_vect;
vector<Point> centres;
unsigned int i, ap_idx;
float constant_term, minmax_res, minmax_max;
float x = MINMAX_X_START, y = MINMAX_Y_START;
i = 0;
//cout << "FBCM: ";
for(i=0;i<m.size();i++)
if(apExists(m[i].getMacAddr()))
{
ap_idx = apIndex(m[i].getMacAddr());
//cout << "AP idx: " << ap_idx << " ";
centres.push_back(access_point_list[ap_idx].getCoordinates());
addr.push_back(m[i].getMacAddr());
constant_term = access_point_list[ap_idx].getOutputPower() + access_point_list[ap_idx].getAntennaGain() + 2;
constant_term += 20 * log10((300000000.0/(float) access_point_list[ap_idx].getFrequency())/(4*M_PI));
//end of expr. should be: client_list[client_idx].getAntennaGain() instead of 2.
//cout << "20log(" << (300000000.0/(float) access_point_list[ap_idx].getFrequency())/(4*M_PI) << ") = ";
//cout << constant_term << " ";
dist_vect.push_back(pow(10, (constant_term - m[i].getAverage()) / (10 * access_point_list[ap_idx].getFriisIndex())));
//cout << endl;
}
/* Then: min-max */
minmax_res = 1000000;
for(x=MINMAX_X_START;x<MINMAX_X_STOP;x+=MINMAX_STEP)
for(y=MINMAX_Y_START;y<MINMAX_Y_STOP;y+=MINMAX_STEP)
{
minmax_max = 0;
for(i=0;i<centres.size();i++)
if((centres[i].squareDistance(x, y, 3) - (dist_vect[i]*dist_vect[i])) > minmax_max)
minmax_max = centres[i].squareDistance(x, y, 3) - (dist_vect[i]*dist_vect[i]);
if(minmax_max < minmax_res)
{
ret.setX(x);
ret.setY(y);
minmax_res = minmax_max;
}
}
/* Clear all vectors */
addr.clear();
dist_vect.clear();
centres.clear();
/* Return position */
return ret;
}
Point Server::interlink(vector<Measurement> m, int client_idx)const
{
Point ret(0, 0, 0);
vector<string> addr;
vector<float> dist_vect;
vector<Point> centres;
unsigned int i, ap_idx;
float constant_term, minmax_res, minmax_max;
float x = MINMAX_X_START, y = MINMAX_Y_START;
i = 0;
for(i=0;i<m.size();i++)
if(apExists(m[i].getMacAddr()))
{
ap_idx = apIndex(m[i].getMacAddr());
centres.push_back(access_point_list[ap_idx].getCoordinates());
addr.push_back(m[i].getMacAddr());
constant_term = access_point_list[ap_idx].getOutputPower() + access_point_list[ap_idx].getAntennaGain();
constant_term += 20 * log10((300000000.0/(float) access_point_list[ap_idx].getFrequency())/(4*M_PI)) + 2;
//end of expr. should be: client_list[client_idx].getAntennaGain() instead of 2.
dist_vect.push_back(pow(10, (constant_term - m[i].getAverage()) / 35));
}
/* Then: min-max */
minmax_res = 1000000;
for(x=MINMAX_X_START;x<MINMAX_X_STOP;x+=MINMAX_STEP)
for(y=MINMAX_Y_START;y<MINMAX_Y_STOP;y+=MINMAX_STEP)
{
minmax_max = 0;
for(i=0;i<centres.size();i++)
if((centres[i].squareDistance(x, y, 3) - (dist_vect[i]*dist_vect[i])) > minmax_max)
minmax_max = centres[i].squareDistance(x, y, 3) - (dist_vect[i]*dist_vect[i]);
if(minmax_max < minmax_res)
{
ret.setX(x);
ret.setY(y);
minmax_res = minmax_max;
}
}
/* Clear all vectors */
addr.clear();
dist_vect.clear();
centres.clear();
/* Return position */
return ret;
}
void Server::makeReferencePointListFromFile(string filename)
{
ifstream input_file;
char buffer[BUFFER_LENGTH];
string lecture_fichier;
ReferencePoint rp;
Point tmp_point;
float x, y;
unsigned int i, pt_idx;
string cpp_buffer, tmp_mes;
vector<int> measures_vector;
vector<string> infos;
input_file.open(filename.c_str());
while(!input_file.eof())
{
input_file.getline(buffer, BUFFER_LENGTH);
if ((input_file.rdstate() & ifstream::eofbit ) == 0 )
{
/* Extract fields */
cpp_buffer = buffer;
infos = extractReferencePointInfoFromBuffer(cpp_buffer);
x = string2float(infos[0]);
y = string2float(infos[1]);
/* Set point coordinates */
tmp_point.setX(x);
tmp_point.setY(y);
tmp_point.setZ(DEFAULT_Z);
/* Use C++ string format */
if(!pointExists(tmp_point))
{
rp.setCoordinates(tmp_point);
reference_point_list.push_back(rp);
}
pt_idx = pointIndex(tmp_point);
measures_vector = extractValues(infos[4]);
for(i=0;i<measures_vector.size();i++)
reference_point_list[pt_idx].addMeasurement(infos[3], measures_vector[i]);
}
}
input_file.close();
measures_vector.clear();
}
void Server::makeApListFromFile(string filename)
{
ifstream input_file;
char buffer[BUFFER_LENGTH];
vector<string> ap_infos;
AccessPoint tmp_ap;
input_file.open(filename.c_str());
while(!input_file.eof())
{
input_file.getline(buffer, BUFFER_LENGTH);
if ((input_file.rdstate() & ifstream::eofbit ) == 0 )
{
ap_infos = explode(buffer, ';');
cout << buffer << endl;
for(unsigned int i=0;i<ap_infos.size();i++)
cout << ap_infos[i] << endl;
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]));
tmp_ap.setAntennaGain(string2float(ap_infos[5]));
tmp_ap.setOutputPower(string2float(ap_infos[6]));
access_point_list.push_back(tmp_ap);
ap_infos.clear();
}
}
input_file.close();
}
void Server::printReferencePointList()
{
unsigned int i;
for(i=0;i<reference_point_list.size();i++)
cout << reference_point_list[i] << endl;
}
void Server::printAccessPointList()
{
unsigned int i;
for(i=0;i<access_point_list.size();i++)
cout << access_point_list[i] << endl;
}
void Server::computeFriisFromRefList()
{
unsigned int i, j;
vector<float> friis_idx_list;
Point pt_coords, ap_coords;
float ap_power, ap_gain, calib_gain = 2, const_term, mes_power, friis_sum;
unsigned int ap_freq;
string ap_mac;
for(i=0;i<access_point_list.size();i++)
{
ap_power = access_point_list[i].getOutputPower();
ap_coords = access_point_list[i].getCoordinates();
ap_freq = access_point_list[i].getFrequency();
ap_gain = access_point_list[i].getAntennaGain();
ap_mac = access_point_list[i].getApAddr();
/* Compute main general term, independant from scans */
const_term = calib_gain + ap_gain;
const_term -= 20 * log10(4 * M_PI);
const_term += 20 * log10 (300000000.0 / ap_freq) + ap_power;
/* Compute an index for each ref point. List stored in friis_idx_list */
for(j=0;j<reference_point_list.size();j++)
{
pt_coords = reference_point_list[j].getCoordinates();
if(reference_point_list[i].getPowerForAp(ap_mac, &mes_power))
{
friis_idx_list.push_back((const_term - mes_power) / (10 * log10(ap_coords.distance(pt_coords))));
}
}
/* Now, compute avg value */
friis_sum = 0;
for(j=0;j<friis_idx_list.size();j++)
friis_sum += friis_idx_list[j];
access_point_list[i].setFriisIndex(friis_sum / friis_idx_list.size());
cout << access_point_list[i].getApAddr() << " -> " << (friis_sum / friis_idx_list.size()) << endl;
friis_idx_list.clear();
}
}
/*
* Function 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() !
*/
Point Server::viterbiLike(float ** dists, unsigned short vk, unsigned short vn, vector<Point> last, vector<Point> current)
{
unsigned int i, j, k;
float min;
vector<float> last_dists;
Point position;
/* Compute the ending viterbi: line 0 */
for(i=0;i<current.size();i++)
{
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);
}
/* 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;
}
/* Shortest distance determines the true point */
position = current[j];
/* Now, compute the remaining of the distance matrix */
}
/* For experimentation purpose only */
void Server::radar_exp()
{
ofstream logfile;
vector<Point> solutions;
Point solution, solution_kw, ref_coords;
unsigned int i;
vector<Measurement> vm;
makeReferencePointListFromFile("complete-scan.csv");
makeApListFromFile("accesspoints.cfg");
computeFriisFromRefList();
/* Open a log file */
logfile.open("radar_exp.csv");
/* Column names */
logfile << "Coordinates\t2-kss\t(Error)\t3-kss\t(Error)\t4-kss\t(Error)\t5-kss\t(Error)\tnss\t(Error)\tInterlink\t(Error)\tFBCM\t(Error)" << endl;
for(i=0;i<reference_point_list.size();i++)
{
/* Get point measurements */
vm.clear();
vm = reference_point_list[i].getMeasurementList();
ref_coords = reference_point_list[i].getCoordinates();
/* Print point coordinates */
logfile << ref_coords << "\t";
/* From 2 to 5 K-weighted-SS */
solution = getkWeightedInSs(vm, 2, true, ref_coords);
logfile << solution << "\t" << solution.distance(ref_coords) << "\t";
solution = getkWeightedInSs(vm, 3, true, ref_coords);
logfile << solution << "\t" << solution.distance(ref_coords) << "\t";
solution = getkWeightedInSs(vm, 4, true, ref_coords);
logfile << solution << "\t" << solution.distance(ref_coords) << "\t";
solution = getkWeightedInSs(vm, 5, true, ref_coords);
logfile << solution << "\t" << solution.distance(ref_coords) << "\t";
/* Nearest in SS */
solutions = getkClosestInSs(vm, 1, true, ref_coords);
logfile << solutions[0] << "\t" << solutions[0].distance(ref_coords) << "\t";
/* Interlink Networks */
solution = interlink(vm, 0);
logfile << solution << "\t" << solution.distance(ref_coords) << "\t";
/* FBCM */
solution = fbcm(vm, 0);
logfile << solution << "\t" << solution.distance(ref_coords) << endl;
solutions.clear();
}
logfile.close();
}

74
GuiNuMo-server/server.hh Normal file
View File

@ -0,0 +1,74 @@
#ifndef _SERVER_HH_
#define _SERVER_HH_
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include "clientinfo.hh"
#include "referencepoint.hh"
#include "accesspoint.hh"
#include "point.hh"
#include "measurement.hh"
using namespace std;
using std::string;
#define DEFAULT_IP "127.0.0.1"
#define DEFAULT_LISTEN_PORT 7777
#define LIGHT_SPEED 300000000
#define MINMAX_STEP 0.5
#define MINMAX_X_START -1
#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
class Server
{
protected:
vector<ClientInfo> client_list;
vector<ReferencePoint> reference_point_list;
vector<AccessPoint> access_point_list;
struct sockaddr_in server_addr;
int sockListen;
int sockSend;
public:
Server();
Server(string ip_addr, int listen_port, int send_port);
~Server();
void send_to_client(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<Point> getkClosestInSs(vector<Measurement> m, unsigned int k, bool ignore_point, Point point_ignored)const;
Point getkWeightedInSs(vector<Measurement> m, unsigned int k, bool ignore_point, Point point_ignored)const;
Point kPointsAverage(vector<Point> vp)const;
Point fbcm(vector<Measurement> m, int client_idx)const;
Point interlink(vector<Measurement> m, int client_idx)const;
void makeReferencePointListFromFile(string filename);
void makeApListFromFile(string filename);
void printReferencePointList();
void printAccessPointList();
void computeFriisFromRefList();
unsigned int getNbReferencePoints()const { return reference_point_list.size(); };
/* For experimentation purpose only ! */
void radar_exp();
};
#endif

3
GuiNuMo-server/test.bash Normal file
View File

@ -0,0 +1,3 @@
#!/bin/bash
./test 1060-scan.csv 1080-scan.csv 1100-scan.csv 1070-scan.csv 1090-scan.csv couloir-scan.csv

25
GuiNuMo-server/test.cc Normal file
View File

@ -0,0 +1,25 @@
#include <iostream>
#include <fstream>
#include <string>
#include "referencepoint.hh"
#include "measurement.hh"
#include "accesspoint.hh"
#include "clientinfo.hh"
#include "server.hh"
#include "point.hh"
using namespace std;
using std::string;
#define BUFFER_LENGTH 1024
int main(int argc, char ** argv)
{
ofstream output_file;
string read_file;
Server my_server;
my_server.radar_exp();
return 0;
}