This commit is contained in:
glenux 2006-03-04 15:13:51 +00:00
parent c658ca0b31
commit 43d2843829

View file

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