/* * This file is part of the Owl Positioning System (OwlPS). * OwlPS is a project of the University of Franche-Comté * (Université de Franche-Comté), France. */ #include "request.hh" #include "mobile.hh" using namespace std ; using std::tr1::unordered_map ; /* *** Constructors *** */ /** * Note that the value pointed by #mobile is not deleted. */ Request::~Request() { measurements.clear() ; } /* *** Write accessors *** */ inline void Request::clear_real_position() { if (real_position) { delete real_position ; real_position = NULL ; } } void Request::set_real_position(const Point3D &_real_position) { if (real_position) *real_position = _real_position ; else real_position = new Point3D(_real_position) ; } /** * - #nb_packets is set to 1 (this is the default value when * constructing a Request). * - #mobile is NULLified, but the value it pointed to is not deleted. * - The fields of #time_sent are initialised to 0. * - #measurements is cleared. */ void Request::clear() { type = OWL_REQUEST_UNDEFINED ; nb_packets = 1 ; mobile = NULL ; time_sent.clear() ; measurements.clear() ; clear_real_position() ; } /* *** Operations *** */ float Request::ss_square_distance(const Request &source) const { unordered_map source_measurements(source.measurements), my_measurements(measurements) ; PosUtil::complete_with_dummy_measurements( my_measurements, source_measurements) ; return PosUtil::ss_square_distance( my_measurements, source_measurements) ; } /* *** Operators *** */ Request& Request::operator=(const Request &source) { if (this == &source) return *this ; type = source.type ; nb_packets = source.nb_packets ; mobile = source.mobile ; time_sent = source.time_sent ; measurements = source.measurements ; clear_real_position() ; if (source.real_position) real_position = new Point3D(*source.real_position) ; return *this ; } bool Request::operator==(const Request &source) const { if (this == &source) return true ; bool real_position_equal = real_position && source.real_position && *real_position == *source.real_position ; return real_position_equal && type == source.type && nb_packets == source.nb_packets && mobile == source.mobile && time_sent == source.time_sent && measurements == source.measurements ; } ostream& operator<<(ostream &os, const Request &r) { // Timestamp os << "At " << r.time_sent << "; " ; if (r.real_position) os << " Real coordinates : " << *r.real_position << "; " ; // MAC address os << "Type: " << static_cast(r.type) << ", Number of packets sent: " << r.nb_packets << ", Mobile: " << (r.mobile != NULL ? r.mobile->get_mac_addr() : "Unknown_Mobile") << ":" ; // List of Measurements if (r.measurements.empty()) os << " No values" ; else for (unordered_map::const_iterator i = r.measurements.begin() ; i != r.measurements.end() ; ++i) os << '\n' << i->first << ": " << i->second ; return os ; } /** * The Mobile MAC address and the Timestamp are sufficient to identify * uniquely a Request. */ size_t hash_value(const Request &source) { size_t seed = 0 ; boost::hash_combine(seed, source.type) ; boost::hash_combine(seed, source.nb_packets) ; boost::hash_combine(seed, source.time_sent) ; if (source.mobile) boost::hash_combine(seed, source.mobile->get_mac_addr()) ; if (source.real_position) boost::hash_combine(seed, source.real_position) ; return seed ; }