From 63a0f977a19e13448cd0ccf2076b534dcd2bf6f1 Mon Sep 17 00:00:00 2001 From: Andrea Bulgarelli Date: Thu, 22 May 2014 10:26:18 +0200 Subject: [PATCH] Packet::encode() --- include/Packet.h | 17 +++++++--------- include/PacketLibDefinition.h | 1 + include/SDFBlock.h | 8 ++++++-- include/SourceDataField.h | 8 +++++--- src/Field.cpp | 1 + src/OutputPacketStream.cpp | 2 +- src/Packet.cpp | 38 ++++++++++++++++++++++++----------- src/SDFBlock.cpp | 38 +++++++++++++++++------------------ src/SourceDataField.cpp | 12 +++++++---- 9 files changed, 74 insertions(+), 51 deletions(-) diff --git a/include/Packet.h b/include/Packet.h index 7982ae1..a1c7c96 100644 --- a/include/Packet.h +++ b/include/Packet.h @@ -107,10 +107,6 @@ public: ///Get the tail as a ByteStream 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(); @@ -142,11 +138,14 @@ public: ByteStreamPtr getInputStream(); - /// Gets the encoded method. The output - /// packet is generated during this call. - ByteStreamPtr encode(); + /// Encode the packet before write. The output packet is generated during this call. + /// \param sourceDataVariable Is a ByteStream that is copied in the source data field fixed part + /// \return the encoded stream + ByteStreamPtr encode(ByteStreamPtr sourceDataVariable = 0); - + /// Get the ByteStream of the encoded/compressed packet + ByteStreamPtr getOutputStream(); + /// Gets the name of packet. virtual char* getName() { @@ -183,8 +182,6 @@ public: return iscompressed; } - - /// Prints to stdout the value of packet data field in a formatted mode. virtual void printPacketValue(); diff --git a/include/PacketLibDefinition.h b/include/PacketLibDefinition.h index deb747b..e827d58 100644 --- a/include/PacketLibDefinition.h +++ b/include/PacketLibDefinition.h @@ -33,6 +33,7 @@ #define DEBUGMODE 0 #define ERRORMODE 1 #define EXCEPTION_ENABLED 1 +#define PACKETNOTRECOGNIZED 0 /// define NULL 0 #define ARCH_BIGENDIAN 0 diff --git a/include/SDFBlock.h b/include/SDFBlock.h index 58f938c..e6cc18b 100644 --- a/include/SDFBlock.h +++ b/include/SDFBlock.h @@ -119,12 +119,12 @@ public: /// determinated type is repeated) for each type of rblock present. /// \param number The number of blocks /// \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 /// determinate type is repeated) for each type of rblock present. /// \param rBlockIndex The number of rblock - virtual word getNumberOfRealDataBlock(word rblockIndex = 0); + virtual word getNumberOfBlocks(word rblockIndex = 0); virtual void setRBlockType(word rb) { @@ -163,6 +163,10 @@ public: { 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. /// See ParfOfPacket doc for more details diff --git a/include/SourceDataField.h b/include/SourceDataField.h index 1c78c45..a07deb6 100644 --- a/include/SourceDataField.h +++ b/include/SourceDataField.h @@ -39,7 +39,7 @@ public: /// with the index rBlockIndex. /// \param nblock the number of the block /// \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 virtual ByteStreamPtr getFixedPart() { return block[0].fixed.getByteStream(); }; @@ -50,6 +50,8 @@ public: /// Returns a pointer of a field in the fixed part of this source data field. /// \param index Represent the index in the list. 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. /// See PartOfPacket for documentation @@ -231,13 +233,13 @@ public: /// determinated type is repeated) for each type of rblock present. /// \param number The number of blocks /// \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 /// determinated type is repeated) for each type of rblock present. /// \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 diff --git a/src/Field.cpp b/src/Field.cpp index 4e71891..59bfa06 100644 --- a/src/Field.cpp +++ b/src/Field.cpp @@ -63,6 +63,7 @@ Field::Field(char* n, char* dim, char* prVal, int count) : progressiv(count) { type->thereIsPredefinedValue = true; type->predefinedValue = Utility::convertToInteger(prVal); + value = type->predefinedValue; //AB } else { diff --git a/src/OutputPacketStream.cpp b/src/OutputPacketStream.cpp index b3820e9..0997a25 100644 --- a/src/OutputPacketStream.cpp +++ b/src/OutputPacketStream.cpp @@ -35,7 +35,7 @@ OutputPacketStream::OutputPacketStream(const char* fileNameConfig) : PacketStrea void OutputPacketStream::writePacket(Packet* p) throw(PacketExceptionIO*) { - writePacket(p->encode()); + writePacket(p->getOutputStream()); } void OutputPacketStream::writePacket(ByteStreamPtr bs) throw(PacketExceptionIO*) diff --git a/src/Packet.cpp b/src/Packet.cpp index 99908b7..3f445f6 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -517,12 +517,12 @@ bool Packet::setPacketValueSourceDataField(ByteStreamPtr packetDataField, int de if(dataField->sourceDataField->isBlock()) { dword nrd = dataField->getNumberOfRealDataBlock(); - dataField->sourceDataField->setNumberOfRealDataBlock(nrd); + dataField->sourceDataField->setNumberOfBlocks(nrd); } / /*if(dataField->sourceDataField->isRBlock()) { word nrd = dataField->sourceDataField->getNumberOfRealDataBlock(); - dataField->sourceDataField->setNumberOfRealDataBlock(nrd); + dataField->sourceDataField->setNumberOfBlocks(nrd); }*/ @@ -597,11 +597,25 @@ bool Packet::setPacketValueHeader(ByteStreamPtr packetHeader) -ByteStreamPtr Packet::encode() +ByteStreamPtr Packet::encode(ByteStreamPtr sourceDataVariable) { //TODO: check 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 //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() 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() { return packet; @@ -836,14 +858,6 @@ ByteStreamPtr Packet::getBSSourceDataField() { 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() { diff --git a/src/SDFBlock.cpp b/src/SDFBlock.cpp index 58c468c..104e597 100644 --- a/src/SDFBlock.cpp +++ b/src/SDFBlock.cpp @@ -363,11 +363,11 @@ dword SDFBlock::size() { bi = block[i].getID(); rbi = block[i].getRBlockType(); - word nrdb = getNumberOfRealDataBlock(rbi); + word nrdb = getNumberOfBlocks(rbi); if(bi < nrdb) dim += block[i].size(); else - i += type->maxNumberOfBlock[rbi] - getNumberOfRealDataBlock(rbi) - 1; + i += type->maxNumberOfBlock[rbi] - getNumberOfBlocks(rbi) - 1; } return dim; } @@ -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, /// the field where to save the value is not present. The dimension is fixed. 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; } PartOfPacket* pop = &fixed; 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; for(int i=0; i< type->headerLevelOfNBlockIndex[rblockIndex]; i++) pop = pop->previous; @@ -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) return 0; @@ -452,7 +452,7 @@ word SDFBlock::getCurrentNumberOfBlocks() { word nblock = 0; for(int i=0; i< type->numberOfRBlocks; i++) - nblock = getNumberOfRealDataBlock(i); + nblock = getNumberOfBlocks(i); return nblock; } @@ -474,14 +474,14 @@ bool SDFBlock::setOutputStream(ByteStreamPtr os, dword first) { bi = block[i].getID(); rbi = block[i].getRBlockType(); - if(bi < getNumberOfRealDataBlock(rbi)) + if(bi < getNumberOfBlocks(rbi)) { /// Only for valid blocks block[i].setOutputStream(os, start); start += block[i].size(); } else - i += type->maxNumberOfBlock[rbi] - getNumberOfRealDataBlock(rbi) - 1; + i += type->maxNumberOfBlock[rbi] - getNumberOfBlocks(rbi) - 1; } } return true; @@ -500,13 +500,13 @@ ByteStreamPtr SDFBlock::generateStream(bool bigendian) { bi = block[i].getID(); rbi = block[i].getRBlockType(); - if(bi < getNumberOfRealDataBlock(rbi)) + if(bi < getNumberOfBlocks(rbi)) { /// Only for valid blocks block[i].generateStream(bigendian); } 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) { bi = block[i].getID(); rbi = block[i].getRBlockType(); - word nrdb = getNumberOfRealDataBlock(rbi); + word nrdb = getNumberOfBlocks(rbi); if(bi < nrdb) { /// Only for valid blocks @@ -563,7 +563,7 @@ bool SDFBlock::setByteStream(ByteStreamPtr s, int decodeType) bytestart = bytestop + 1; } else - i += type->maxNumberOfBlock[rbi] - getNumberOfRealDataBlock(rbi) - 1; + i += type->maxNumberOfBlock[rbi] - getNumberOfBlocks(rbi) - 1; } } return true; @@ -596,7 +596,7 @@ char** SDFBlock::printValue(char* addString) { bi = block[i].getID(); rbi = block[i].getRBlockType(); - if(bi < getNumberOfRealDataBlock(rbi)) + if(bi < getNumberOfBlocks(rbi)) { /// Only for valid blocks ct = block[i].printValue(addString); @@ -607,7 +607,7 @@ char** SDFBlock::printValue(char* addString) } } else - i += type->maxNumberOfBlock[rbi] - getNumberOfRealDataBlock(rbi) - 1; + i += type->maxNumberOfBlock[rbi] - getNumberOfBlocks(rbi) - 1; } } cc[index] = '\0'; @@ -630,13 +630,13 @@ void SDFBlock::printValueStdout() { bi = block[i].getID(); rbi = block[i].getRBlockType(); - if(bi < getNumberOfRealDataBlock(rbi)) + if(bi < getNumberOfBlocks(rbi)) { /// Only for valid blocks block[i].printValueStdout(); } else - i += type->maxNumberOfBlock[rbi] - getNumberOfRealDataBlock(rbi) - 1; + i += type->maxNumberOfBlock[rbi] - getNumberOfBlocks(rbi) - 1; } } } @@ -659,10 +659,10 @@ word SDFBlock::getTotalNumberOfFields() { bi = block[j].getID(); rbi = block[j].getRBlockType(); - if(bi < getNumberOfRealDataBlock(rbi)) + if(bi < getNumberOfBlocks(rbi)) dim += block[j].getNumberOfFields(); else - j += type->maxNumberOfBlock[rbi] - getNumberOfRealDataBlock(rbi) - 1; + j += type->maxNumberOfBlock[rbi] - getNumberOfBlocks(rbi) - 1; } return dim; } diff --git a/src/SourceDataField.cpp b/src/SourceDataField.cpp index 3dd6137..3b0c426 100644 --- a/src/SourceDataField.cpp +++ b/src/SourceDataField.cpp @@ -111,18 +111,18 @@ dword SourceDataField::sizeFixedPart() 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 - block[0].setNumberOfRealDataBlock(number, rblockIndex); + block[0].setNumberOfBlocks(number, rblockIndex); reset_output_stream = true; } -word SourceDataField::getNumberOfRealDataBlock(word rblockIndex) +word SourceDataField::getNumberOfBlocks(word rblockIndex) { /// 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) return block[0].getFieldValue(index); } +word SourceDataField::getFieldIndex(string fieldname) { + return block[0].getFieldIndex(fieldname); +} + void SourceDataField::setFieldValue(word index, word value) { block[0].setFieldValue(index, value); -- GitLab