207 lines
5.9 KiB
C++
207 lines
5.9 KiB
C++
/* vim: set sw=4 ts=4 si et: */
|
|
#include <netinet/in.h>
|
|
|
|
#include "timestamp.h"
|
|
|
|
#define DEBUG 1
|
|
|
|
TimeStamp::TimeStamp(Protocol::Type type, short index){
|
|
_index = index;
|
|
_type = type;
|
|
}
|
|
|
|
TimeStamp::TimeStamp(Protocol::Type type, char * raw, unsigned short raw_size){
|
|
int pos_idx = 0;
|
|
short stamp_len = -1;
|
|
|
|
_index = -1;
|
|
_type = type;
|
|
switch(_type){
|
|
case Protocol::TYPE_ABCAST :
|
|
{
|
|
memcpy(&_index, raw , 2);
|
|
_index = ntohs(_index);
|
|
pos_idx += 2;
|
|
stamp_len = 1;
|
|
}
|
|
break;
|
|
case Protocol::TYPE_CBCAST :
|
|
{
|
|
// on est super embétés...
|
|
// on regarde d'abord l'index...
|
|
// stamp_len = 4i
|
|
memcpy(&_index, raw , 2);
|
|
_index = ntohs(_index);
|
|
pos_idx += 2;
|
|
|
|
// on cherche ensuite la taille totale
|
|
// de l'horloge
|
|
memcpy(&stamp_len, (raw + pos_idx), 2);
|
|
stamp_len = ntohs(stamp_len);
|
|
pos_idx += 2;
|
|
|
|
// on choppe ensuite 'stamp_len' shorts
|
|
}
|
|
break;
|
|
case Protocol::TYPE_TEST :
|
|
{
|
|
// taille = 0;
|
|
stamp_len = 0;
|
|
}
|
|
break;
|
|
case Protocol::TYPE_UNKNOWN :
|
|
break;
|
|
default :
|
|
break;
|
|
}
|
|
|
|
if (DEBUG) {
|
|
printf("TimeStamp::TimeStamp(Protocol::Type, void *, int) -- stamp_index %d\n", _index);
|
|
printf("TimeStamp::TimeStamp(Protocol::Type, void *, int) -- stamp_len %d\n", stamp_len);
|
|
}
|
|
if (stamp_len < 0){
|
|
fprintf(stderr, "TimeStamp::TimeStamp -- Longueur du timestamp inconnue\n !");
|
|
exit(-1);
|
|
}
|
|
//_stamp = new TimeStamp(_type, stamp_index);
|
|
// on itere stamp_len fois sur 2 octets
|
|
for (int i = 0; i< stamp_len; i++){
|
|
short net_site_value, host_site_value;
|
|
memcpy(&net_site_value, (raw + pos_idx), 2);
|
|
host_site_value = ntohs(net_site_value);
|
|
|
|
if (DEBUG)
|
|
printf("TimeStamp::TimeStamp -- index %d horloge[%d] = %d -> %d\n",
|
|
pos_idx, i, net_site_value, host_site_value);
|
|
|
|
pos_idx += 2;
|
|
this->push_back(host_site_value);
|
|
}
|
|
|
|
}
|
|
|
|
Protocol::Type TimeStamp::getType(){
|
|
return _type;
|
|
}
|
|
|
|
short TimeStamp::getIndex(){
|
|
return _index;
|
|
}
|
|
|
|
char * TimeStamp::getRaw(){
|
|
int result_len = 0;
|
|
char * result = NULL;
|
|
switch(_type){
|
|
case Protocol::TYPE_TEST :
|
|
{
|
|
result = NULL;
|
|
}
|
|
break;
|
|
case Protocol::TYPE_ABCAST :
|
|
{
|
|
result_len = 2; // 2 bytes for site index
|
|
result_len += 2; // 2 bytes for clock value
|
|
result = new char[result_len];
|
|
|
|
short index_value = htons(_index);
|
|
if (DEBUG)
|
|
printf("TimeStamp::raw -- (ABCAST) index_value %d -> %d\n", _index, index_value);
|
|
|
|
memcpy(result, &index_value, 2); // on fixe l'index
|
|
|
|
short host_clock_value, net_clock_value;
|
|
host_clock_value = (*this)[0];
|
|
net_clock_value = htons(host_clock_value);
|
|
|
|
if (DEBUG)
|
|
printf("TimeStamp::raw -- (ABCAST) clock_value %d -> %d\n",host_clock_value, net_clock_value);
|
|
|
|
memcpy((result + 2),
|
|
&net_clock_value, 2); // on fixe l'index
|
|
|
|
}
|
|
break;
|
|
case Protocol::TYPE_CBCAST :
|
|
{
|
|
result_len = 2; // 2 bytes for site index
|
|
result_len += 2; // 2 bytes for site count
|
|
result_len += 2 * this->size(); // 2 bytes per site-clock value
|
|
result = new char[result_len];
|
|
|
|
short index_value = htons(_index);
|
|
short count_value = htons(this->size());
|
|
|
|
//FIXME: if (DEBUG)
|
|
printf("TimeStamp::raw -- (CBCAST) index_value %d -> %d\n", _index, index_value);
|
|
|
|
memcpy(result, &index_value, 2);
|
|
memcpy(result + 2, &count_value, 2);
|
|
|
|
short host_clock_value, net_clock_value;
|
|
for (int idx = 0; idx < this->size(); idx++){
|
|
host_clock_value = (*this)[idx];
|
|
net_clock_value = htons(host_clock_value);
|
|
|
|
//FIXME: if (DEBUG)
|
|
printf("TimeStamp::raw -- (CBCAST) clock_value %d -> %d\n",
|
|
host_clock_value,
|
|
net_clock_value);
|
|
|
|
memcpy((result + 4 + idx * 2),
|
|
&net_clock_value, 2); // on fixe l'index dans le résultat
|
|
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
printf("TimeStamp::raw -- undef type\n");
|
|
break;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
unsigned short TimeStamp::getRawSize(){
|
|
int result = -1;
|
|
switch(_type){
|
|
case Protocol::TYPE_TEST :
|
|
{
|
|
result = 0;
|
|
}
|
|
break;
|
|
case Protocol::TYPE_ABCAST :
|
|
{
|
|
result = 4;
|
|
}
|
|
break;
|
|
case Protocol::TYPE_CBCAST :
|
|
{
|
|
result = 4 + (2 * this->size()); // FIXME: plus la taille du vecteur * 2;
|
|
}
|
|
break;
|
|
default:
|
|
printf("TimeStamp::raw_len -- undef type\n");
|
|
break;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
bool TimeStamp::operator==(TimeStamp &stamp){
|
|
printf("TimeStamp::operator== -- \n");
|
|
bool ident = true;
|
|
if (this->_index != stamp._index){
|
|
ident = false;
|
|
}
|
|
if (this->size() != stamp.size()){
|
|
ident = false;
|
|
} else {
|
|
for (int i = 0; i < this->size(); i++){
|
|
if ((*this)[i] != stamp[i]){
|
|
ident = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return ident;
|
|
}
|
|
|
|
#undef DEBUG
|