Skip to content
Snippets Groups Projects
Commit 63a0f977 authored by Andrea Bulgarelli's avatar Andrea Bulgarelli
Browse files

Packet::encode()

parent 7d6f604e
No related branches found
No related tags found
No related merge requests found
...@@ -107,10 +107,6 @@ public: ...@@ -107,10 +107,6 @@ public:
///Get the tail as a ByteStream ///Get the tail as a ByteStream
ByteStreamPtr getBSTail(); ByteStreamPtr getBSTail();
///Copy an array of bytes into the source data field. The size must be the same.
///It is necessary to set the number of blocks of each rblocks before.
void copyBSSourceDataField(byte* bytestream, dword size);
/// ///
PacketHeader* getPacketHeader(); PacketHeader* getPacketHeader();
...@@ -142,10 +138,13 @@ public: ...@@ -142,10 +138,13 @@ public:
ByteStreamPtr getInputStream(); ByteStreamPtr getInputStream();
/// Gets the encoded method. The output /// Encode the packet before write. The output packet is generated during this call.
/// packet is generated during this call. /// \param sourceDataVariable Is a ByteStream that is copied in the source data field fixed part
ByteStreamPtr encode(); /// \return the encoded stream
ByteStreamPtr encode(ByteStreamPtr sourceDataVariable = 0);
/// Get the ByteStream of the encoded/compressed packet
ByteStreamPtr getOutputStream();
/// Gets the name of packet. /// Gets the name of packet.
virtual char* getName() virtual char* getName()
...@@ -183,8 +182,6 @@ public: ...@@ -183,8 +182,6 @@ public:
return iscompressed; return iscompressed;
} }
/// Prints to stdout the value of packet data field in a formatted mode. /// Prints to stdout the value of packet data field in a formatted mode.
virtual void printPacketValue(); virtual void printPacketValue();
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#define DEBUGMODE 0 #define DEBUGMODE 0
#define ERRORMODE 1 #define ERRORMODE 1
#define EXCEPTION_ENABLED 1 #define EXCEPTION_ENABLED 1
#define PACKETNOTRECOGNIZED 0
/// define NULL 0 /// define NULL 0
#define ARCH_BIGENDIAN 0 #define ARCH_BIGENDIAN 0
......
...@@ -119,12 +119,12 @@ public: ...@@ -119,12 +119,12 @@ public:
/// determinated type is repeated) for each type of rblock present. /// determinated type is repeated) for each type of rblock present.
/// \param number The number of blocks /// \param number The number of blocks
/// \param rBlockIndex The number of rblock /// \param rBlockIndex The number of rblock
virtual void setNumberOfRealDataBlock(word number, word rblockIndex = 0) throw (PacketException*); virtual void setNumberOfBlocks(word number, word rblockIndex = 0) throw (PacketException*);
/// Get the number of blocks (the number of times that a block of a /// Get the number of blocks (the number of times that a block of a
/// determinate type is repeated) for each type of rblock present. /// determinate type is repeated) for each type of rblock present.
/// \param rBlockIndex The number of rblock /// \param rBlockIndex The number of rblock
virtual word getNumberOfRealDataBlock(word rblockIndex = 0); virtual word getNumberOfBlocks(word rblockIndex = 0);
virtual void setRBlockType(word rb) virtual void setRBlockType(word rb)
{ {
...@@ -164,6 +164,10 @@ public: ...@@ -164,6 +164,10 @@ public:
return fixed.getFields(index); return fixed.getFields(index);
}; };
virtual word getFieldIndex(string fieldname) {
return fixed.getFieldIndex(fieldname);
}
/// Returns the value of a field in the list of fields of the fixed part. /// Returns the value of a field in the list of fields of the fixed part.
/// See ParfOfPacket doc for more details /// See ParfOfPacket doc for more details
/// \param index Represent the index in the list. /// \param index Represent the index in the list.
......
...@@ -39,7 +39,7 @@ public: ...@@ -39,7 +39,7 @@ public:
/// with the index rBlockIndex. /// with the index rBlockIndex.
/// \param nblock the number of the block /// \param nblock the number of the block
/// \param rBlockIndex the number of the rblock /// \param rBlockIndex the number of the rblock
virtual SDFBlock* getBlock(word nblock, word rBlockIndex); virtual SDFBlock* getBlock(word nblock, word rBlockIndex=0);
///Get the fixed part of the source data field ///Get the fixed part of the source data field
virtual ByteStreamPtr getFixedPart() { return block[0].fixed.getByteStream(); }; virtual ByteStreamPtr getFixedPart() { return block[0].fixed.getByteStream(); };
...@@ -51,6 +51,8 @@ public: ...@@ -51,6 +51,8 @@ public:
/// \param index Represent the index in the list. /// \param index Represent the index in the list.
virtual Field* getFields(word index); virtual Field* getFields(word index);
virtual word getFieldIndex(string fieldname);
/// Returns the value of a field in the fixed part of this source data field. /// Returns the value of a field in the fixed part of this source data field.
/// See PartOfPacket for documentation /// See PartOfPacket for documentation
/// \param index Represent the index in the list. /// \param index Represent the index in the list.
...@@ -231,13 +233,13 @@ public: ...@@ -231,13 +233,13 @@ public:
/// determinated type is repeated) for each type of rblock present. /// determinated type is repeated) for each type of rblock present.
/// \param number The number of blocks /// \param number The number of blocks
/// \param rBlockIndex The number of rblock /// \param rBlockIndex The number of rblock
void setNumberOfRealDataBlock(word number, word rblockIndex = 0) throw (PacketException*); void setNumberOfBlocks(word number, word rblockIndex = 0) throw (PacketException*);
/// Get the number of blocks (the number of times that a block of a /// Get the number of blocks (the number of times that a block of a
/// determinated type is repeated) for each type of rblock present. /// determinated type is repeated) for each type of rblock present.
/// \param rBlockIndex The number of rblock /// \param rBlockIndex The number of rblock
word getNumberOfRealDataBlock(word rblockIndex = 0); word getNumberOfBlocks(word rblockIndex = 0);
/// Prints the value of each field of this part of packet /// Prints the value of each field of this part of packet
......
...@@ -63,6 +63,7 @@ Field::Field(char* n, char* dim, char* prVal, int count) : progressiv(count) ...@@ -63,6 +63,7 @@ Field::Field(char* n, char* dim, char* prVal, int count) : progressiv(count)
{ {
type->thereIsPredefinedValue = true; type->thereIsPredefinedValue = true;
type->predefinedValue = Utility::convertToInteger(prVal); type->predefinedValue = Utility::convertToInteger(prVal);
value = type->predefinedValue; //AB
} }
else else
{ {
......
...@@ -35,7 +35,7 @@ OutputPacketStream::OutputPacketStream(const char* fileNameConfig) : PacketStrea ...@@ -35,7 +35,7 @@ OutputPacketStream::OutputPacketStream(const char* fileNameConfig) : PacketStrea
void OutputPacketStream::writePacket(Packet* p) throw(PacketExceptionIO*) void OutputPacketStream::writePacket(Packet* p) throw(PacketExceptionIO*)
{ {
writePacket(p->encode()); writePacket(p->getOutputStream());
} }
void OutputPacketStream::writePacket(ByteStreamPtr bs) throw(PacketExceptionIO*) void OutputPacketStream::writePacket(ByteStreamPtr bs) throw(PacketExceptionIO*)
......
...@@ -517,12 +517,12 @@ bool Packet::setPacketValueSourceDataField(ByteStreamPtr packetDataField, int de ...@@ -517,12 +517,12 @@ bool Packet::setPacketValueSourceDataField(ByteStreamPtr packetDataField, int de
if(dataField->sourceDataField->isBlock()) if(dataField->sourceDataField->isBlock())
{ {
dword nrd = dataField->getNumberOfRealDataBlock(); dword nrd = dataField->getNumberOfRealDataBlock();
dataField->sourceDataField->setNumberOfRealDataBlock(nrd); dataField->sourceDataField->setNumberOfBlocks(nrd);
} }
/ /
/*if(dataField->sourceDataField->isRBlock()) { /*if(dataField->sourceDataField->isRBlock()) {
word nrd = dataField->sourceDataField->getNumberOfRealDataBlock(); word nrd = dataField->sourceDataField->getNumberOfRealDataBlock();
dataField->sourceDataField->setNumberOfRealDataBlock(nrd); dataField->sourceDataField->setNumberOfBlocks(nrd);
}*/ }*/
...@@ -597,11 +597,25 @@ bool Packet::setPacketValueHeader(ByteStreamPtr packetHeader) ...@@ -597,11 +597,25 @@ bool Packet::setPacketValueHeader(ByteStreamPtr packetHeader)
ByteStreamPtr Packet::encode() ByteStreamPtr Packet::encode(ByteStreamPtr sourceDataVariable)
{ {
//TODO: check //TODO: check
generateStream(); generateStream();
ByteStreamPtr b = ByteStreamPtr(new ByteStream(packet_output->stream, size() + (thereisprefix?dimPrefix:0), bigendian));
dword dimpacket = dimPacketHeader + header->getPacketLength();
ByteStreamPtr b = ByteStreamPtr(new ByteStream(packet_output->stream, dimpacket + (thereisprefix?dimPrefix:0), bigendian));
//copy the sourceDataVariable
if(sourceDataVariable != 0) {
//the variable part of the sourcedatafield
//cout << "packet size " << size() << " " << dimpacket << " " << dimPacketStartingFixedPart << " " << sourceDataVariable->size() << endl;
if(sourceDataVariable->size() != size() - dimPacketStartingFixedPart - dimPacketTail)
throw new PacketException("the size of the sourceDataVariable is wrong");
memcpy( packet_output->stream + dimPacketStartingFixedPart, sourceDataVariable->stream, sourceDataVariable->size());
b = ByteStreamPtr(new ByteStream(packet_output->stream, dimpacket + (thereisprefix?dimPrefix:0), bigendian));
}
//COMPRESSION HERE //COMPRESSION HERE
//add compression algorithm here of the variable part of the source data field and do not use size() of packet //add compression algorithm here of the variable part of the source data field and do not use size() of packet
...@@ -610,6 +624,14 @@ ByteStreamPtr Packet::encode() ...@@ -610,6 +624,14 @@ ByteStreamPtr Packet::encode()
return b; return b;
} }
ByteStreamPtr Packet::getOutputStream() {
dword dimpacket = dimPacketHeader + header->getPacketLength();
ByteStreamPtr b = ByteStreamPtr(new ByteStream(packet_output->stream, dimpacket + (thereisprefix?dimPrefix:0), bigendian));
return b;
}
ByteStreamPtr Packet::getInputStream() ByteStreamPtr Packet::getInputStream()
{ {
return packet; return packet;
...@@ -836,14 +858,6 @@ ByteStreamPtr Packet::getBSSourceDataField() { ...@@ -836,14 +858,6 @@ ByteStreamPtr Packet::getBSSourceDataField() {
return sdff; return sdff;
} }
void Packet::copyBSSourceDataField(byte* bytestream, dword size) {
ByteStreamPtr sdfbs = getBSSourceDataField();
if(sdfbs->size() != size)
throw new PacketException("Packet::copyBSSourceDataField(): size of the data is wrong");
byte* sdfbsp = sdfbs->getStream();
memcpy(sdfbsp, bytestream, size*sizeof(byte));
decodedPacketSourceDataField = false;
}
void Packet::compress() { void Packet::compress() {
......
...@@ -363,11 +363,11 @@ dword SDFBlock::size() ...@@ -363,11 +363,11 @@ dword SDFBlock::size()
{ {
bi = block[i].getID(); bi = block[i].getID();
rbi = block[i].getRBlockType(); rbi = block[i].getRBlockType();
word nrdb = getNumberOfRealDataBlock(rbi); word nrdb = getNumberOfBlocks(rbi);
if(bi < nrdb) if(bi < nrdb)
dim += block[i].size(); dim += block[i].size();
else else
i += type->maxNumberOfBlock[rbi] - getNumberOfRealDataBlock(rbi) - 1; i += type->maxNumberOfBlock[rbi] - getNumberOfBlocks(rbi) - 1;
} }
return dim; return dim;
} }
...@@ -387,19 +387,19 @@ SDFBlock* SDFBlock::getBlock(word nblock, word rBlockIndex) ...@@ -387,19 +387,19 @@ SDFBlock* SDFBlock::getBlock(word nblock, word rBlockIndex)
} }
void SDFBlock::setNumberOfRealDataBlock(word number, word rblockIndex) throw (PacketException*) void SDFBlock::setNumberOfBlocks(word number, word rblockIndex) throw (PacketException*)
{ {
/// In case the variable part is not present or rBlockVariable = false, /// In case the variable part is not present or rBlockVariable = false,
/// the field where to save the value is not present. The dimension is fixed. /// the field where to save the value is not present. The dimension is fixed.
if(!type->variablePresent || !type->rBlockVariable[rblockIndex]) if(!type->variablePresent || !type->rBlockVariable[rblockIndex])
{ {
throw new PacketException("It is not possible to set setNumberOfRealDataBlock for this rBlock: variable part not present"); throw new PacketException("It is not possible to set setNumberOfBlocks for this rBlock: variable part not present");
return; return;
} }
PartOfPacket* pop = &fixed; PartOfPacket* pop = &fixed;
if(number > type->maxNumberOfBlock[rblockIndex]) if(number > type->maxNumberOfBlock[rblockIndex])
throw new PacketException("It is not possible to set setNumberOfRealDataBlock: too much blocks"); throw new PacketException("It is not possible to set setNumberOfBlocks: too much blocks");
numberOfRealDataBlock[rblockIndex] = number; numberOfRealDataBlock[rblockIndex] = number;
for(int i=0; i< type->headerLevelOfNBlockIndex[rblockIndex]; i++) for(int i=0; i< type->headerLevelOfNBlockIndex[rblockIndex]; i++)
pop = pop->previous; pop = pop->previous;
...@@ -417,7 +417,7 @@ void SDFBlock::setNumberOfRealDataBlock(word number, word rblockIndex) throw (Pa ...@@ -417,7 +417,7 @@ void SDFBlock::setNumberOfRealDataBlock(word number, word rblockIndex) throw (Pa
} }
word SDFBlock::getNumberOfRealDataBlock(word rblockIndex) word SDFBlock::getNumberOfBlocks(word rblockIndex)
{ {
if(!type->variablePresent) if(!type->variablePresent)
return 0; return 0;
...@@ -452,7 +452,7 @@ word SDFBlock::getCurrentNumberOfBlocks() ...@@ -452,7 +452,7 @@ word SDFBlock::getCurrentNumberOfBlocks()
{ {
word nblock = 0; word nblock = 0;
for(int i=0; i< type->numberOfRBlocks; i++) for(int i=0; i< type->numberOfRBlocks; i++)
nblock = getNumberOfRealDataBlock(i); nblock = getNumberOfBlocks(i);
return nblock; return nblock;
} }
...@@ -474,14 +474,14 @@ bool SDFBlock::setOutputStream(ByteStreamPtr os, dword first) ...@@ -474,14 +474,14 @@ bool SDFBlock::setOutputStream(ByteStreamPtr os, dword first)
{ {
bi = block[i].getID(); bi = block[i].getID();
rbi = block[i].getRBlockType(); rbi = block[i].getRBlockType();
if(bi < getNumberOfRealDataBlock(rbi)) if(bi < getNumberOfBlocks(rbi))
{ {
/// Only for valid blocks /// Only for valid blocks
block[i].setOutputStream(os, start); block[i].setOutputStream(os, start);
start += block[i].size(); start += block[i].size();
} }
else else
i += type->maxNumberOfBlock[rbi] - getNumberOfRealDataBlock(rbi) - 1; i += type->maxNumberOfBlock[rbi] - getNumberOfBlocks(rbi) - 1;
} }
} }
return true; return true;
...@@ -500,13 +500,13 @@ ByteStreamPtr SDFBlock::generateStream(bool bigendian) ...@@ -500,13 +500,13 @@ ByteStreamPtr SDFBlock::generateStream(bool bigendian)
{ {
bi = block[i].getID(); bi = block[i].getID();
rbi = block[i].getRBlockType(); rbi = block[i].getRBlockType();
if(bi < getNumberOfRealDataBlock(rbi)) if(bi < getNumberOfBlocks(rbi))
{ {
/// Only for valid blocks /// Only for valid blocks
block[i].generateStream(bigendian); block[i].generateStream(bigendian);
} }
else else
i += type->maxNumberOfBlock[rbi] - getNumberOfRealDataBlock(rbi) - 1; i += type->maxNumberOfBlock[rbi] - getNumberOfBlocks(rbi) - 1;
} }
} }
...@@ -544,7 +544,7 @@ bool SDFBlock::setByteStream(ByteStreamPtr s, int decodeType) ...@@ -544,7 +544,7 @@ bool SDFBlock::setByteStream(ByteStreamPtr s, int decodeType)
{ {
bi = block[i].getID(); bi = block[i].getID();
rbi = block[i].getRBlockType(); rbi = block[i].getRBlockType();
word nrdb = getNumberOfRealDataBlock(rbi); word nrdb = getNumberOfBlocks(rbi);
if(bi < nrdb) if(bi < nrdb)
{ {
/// Only for valid blocks /// Only for valid blocks
...@@ -563,7 +563,7 @@ bool SDFBlock::setByteStream(ByteStreamPtr s, int decodeType) ...@@ -563,7 +563,7 @@ bool SDFBlock::setByteStream(ByteStreamPtr s, int decodeType)
bytestart = bytestop + 1; bytestart = bytestop + 1;
} }
else else
i += type->maxNumberOfBlock[rbi] - getNumberOfRealDataBlock(rbi) - 1; i += type->maxNumberOfBlock[rbi] - getNumberOfBlocks(rbi) - 1;
} }
} }
return true; return true;
...@@ -596,7 +596,7 @@ char** SDFBlock::printValue(char* addString) ...@@ -596,7 +596,7 @@ char** SDFBlock::printValue(char* addString)
{ {
bi = block[i].getID(); bi = block[i].getID();
rbi = block[i].getRBlockType(); rbi = block[i].getRBlockType();
if(bi < getNumberOfRealDataBlock(rbi)) if(bi < getNumberOfBlocks(rbi))
{ {
/// Only for valid blocks /// Only for valid blocks
ct = block[i].printValue(addString); ct = block[i].printValue(addString);
...@@ -607,7 +607,7 @@ char** SDFBlock::printValue(char* addString) ...@@ -607,7 +607,7 @@ char** SDFBlock::printValue(char* addString)
} }
} }
else else
i += type->maxNumberOfBlock[rbi] - getNumberOfRealDataBlock(rbi) - 1; i += type->maxNumberOfBlock[rbi] - getNumberOfBlocks(rbi) - 1;
} }
} }
cc[index] = '\0'; cc[index] = '\0';
...@@ -630,13 +630,13 @@ void SDFBlock::printValueStdout() ...@@ -630,13 +630,13 @@ void SDFBlock::printValueStdout()
{ {
bi = block[i].getID(); bi = block[i].getID();
rbi = block[i].getRBlockType(); rbi = block[i].getRBlockType();
if(bi < getNumberOfRealDataBlock(rbi)) if(bi < getNumberOfBlocks(rbi))
{ {
/// Only for valid blocks /// Only for valid blocks
block[i].printValueStdout(); block[i].printValueStdout();
} }
else else
i += type->maxNumberOfBlock[rbi] - getNumberOfRealDataBlock(rbi) - 1; i += type->maxNumberOfBlock[rbi] - getNumberOfBlocks(rbi) - 1;
} }
} }
} }
...@@ -659,10 +659,10 @@ word SDFBlock::getTotalNumberOfFields() ...@@ -659,10 +659,10 @@ word SDFBlock::getTotalNumberOfFields()
{ {
bi = block[j].getID(); bi = block[j].getID();
rbi = block[j].getRBlockType(); rbi = block[j].getRBlockType();
if(bi < getNumberOfRealDataBlock(rbi)) if(bi < getNumberOfBlocks(rbi))
dim += block[j].getNumberOfFields(); dim += block[j].getNumberOfFields();
else else
j += type->maxNumberOfBlock[rbi] - getNumberOfRealDataBlock(rbi) - 1; j += type->maxNumberOfBlock[rbi] - getNumberOfBlocks(rbi) - 1;
} }
return dim; return dim;
} }
......
...@@ -111,18 +111,18 @@ dword SourceDataField::sizeFixedPart() ...@@ -111,18 +111,18 @@ dword SourceDataField::sizeFixedPart()
return block[0].fixed.size(); return block[0].fixed.size();
} }
void SourceDataField::setNumberOfRealDataBlock(word number, word rblockIndex) throw (PacketException*) void SourceDataField::setNumberOfBlocks(word number, word rblockIndex) throw (PacketException*)
{ {
/// The block[0] is the only block present /// The block[0] is the only block present
block[0].setNumberOfRealDataBlock(number, rblockIndex); block[0].setNumberOfBlocks(number, rblockIndex);
reset_output_stream = true; reset_output_stream = true;
} }
word SourceDataField::getNumberOfRealDataBlock(word rblockIndex) word SourceDataField::getNumberOfBlocks(word rblockIndex)
{ {
/// The block[0] is the only block present /// The block[0] is the only block present
return block[0].getNumberOfRealDataBlock(rblockIndex); return block[0].getNumberOfBlocks(rblockIndex);
} }
...@@ -152,6 +152,10 @@ word SourceDataField::getFieldValue(word index) ...@@ -152,6 +152,10 @@ word SourceDataField::getFieldValue(word index)
return block[0].getFieldValue(index); return block[0].getFieldValue(index);
} }
word SourceDataField::getFieldIndex(string fieldname) {
return block[0].getFieldIndex(fieldname);
}
void SourceDataField::setFieldValue(word index, word value) void SourceDataField::setFieldValue(word index, word value)
{ {
block[0].setFieldValue(index, value); block[0].setFieldValue(index, value);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment