Commit 788df7d9 authored by Andrea Bulgarelli's avatar Andrea Bulgarelli
Browse files

compression structure

parent 734bb8e3
Loading
Loading
Loading
Loading
+20 −9
Original line number Diff line number Diff line
@@ -78,14 +78,27 @@ public:
	///\pre the ByteStream is set with one of set(ByteStream) methods
	virtual bool verify();
	
	///\return compress the data section (the variable part or the "data" part)
	virtual ByteStreamPtr compress(enum CompressionAlgorithms compressionAlgorithm, byte compressionLevel);
	///\return compress the data section and change the packet (the variable part of the "source data field")
	///\post the variable part of the "source data field" is compressed and should be sent or stored
	virtual ByteStreamPtr compressData(enum CompressionAlgorithms compressionAlgorithm, byte compressionLevel);
	
	virtual word getCompressionAlgorithm();
	///\return decompress the data section without changing the packet  (the variable part of the "source data field")
	virtual ByteStreamPtr decompressData();
	
	virtual enum CompressionAlgorithms getCompressionAlgorithm();
	
	virtual word getCompressionLevel();
	
	virtual void decompress();
	bool isCompressed() {
		if(compressible && getCompressionAlgorithm() != NONE)
			return true;
		else
			return false;
	}
	
	bool isCompressible() {
		return compressible;
	}
		
	///Get the prefix as a ByteStream
	ByteStreamPtr getBSPrefix();
@@ -106,7 +119,7 @@ public:
	ByteStreamPtr getBSSourceDataFieldsVariablePart();
	
	///Get the the variable part of the source data field as a ByteStream that contains the data
	ByteStreamPtr getBSData();
	ByteStreamPtr getData();
	
	///Get the the source data field as a ByteStream
	ByteStreamPtr getBSSourceDataField();
@@ -189,10 +202,6 @@ public:
    	return bigendian;
    }
	
	bool isCompressed() {
		return iscompressed;
	}
	
    /// Prints to stdout the value of packet data field in a formatted mode.
    virtual void printPacketValue();
	
@@ -352,6 +361,8 @@ private:
	
	int compressionLevelIndex;
	
	bool compressible;

};

}
+2 −2
Original line number Diff line number Diff line
@@ -42,10 +42,10 @@ public:
    /// Loads data header from configuration file.
    bool loadHeader(char* fileName) throw(PacketException*);

    /// Returns the total lenght of packet data field (data field header plus source data field)
    /// Returns the total lenght of "packet data field" (data field header plus source data field)
    dword getPacketLength();

    //set the length of the packet in the 'packet lenght' field (the data field dimension)
    //set the length of the packet in the 'packet lenght' field (the "packet data field" dimension)
	//It is encoded as "data field dimension - 1
    void setPacketLength(dword dim);

+31 −11
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ Packet::Packet(bool bigendian)
    tempPacketDataField = ByteStreamPtr(new ByteStream);
    tempTail = ByteStreamPtr(new ByteStream);
    first_output_stream_setted = false;
	iscompressed = false;
	compressible = false;

}

@@ -140,6 +140,7 @@ bool Packet::createPacketType(char* fileName, bool isprefix, word dimprefix) thr
													dimPacketTail = dataField->getPacketTail()->size();
													line = file.getLastLineRead();
													if(strcmp(line, "[Compression]") == 0) {
														compressible = true;
														compressionAlgorithmsIndex = atoi(file.getLine());
														compressionAlgorithmsSection = atoi(file.getLine());
														compressionLevelIndex = atoi(file.getLine());
@@ -532,6 +533,7 @@ bool Packet::setPacketValueSourceDataField(ByteStreamPtr packetDataField, int de
    b = tempPacketDataField->setStream(packetDataField, packetLength, packetLength+packetLength2-1);
    if(b)
    {
		/*
		if(isCompressed()) {
			decompress();
			//6) generate a new tempPacketDataField
@@ -542,6 +544,7 @@ bool Packet::setPacketValueSourceDataField(ByteStreamPtr packetDataField, int de
			packetLength2 = pl1 - pl3 -pl4;
			b = tempPacketDataField->setStream(packetDataField, packetLength, packetLength+packetLength2-1);
		}
		*/
		
		decodedPacketSourceDataField = true;
        bool ret = dataField->getPacketSourceDataField()->setByteStream(tempPacketDataField, decodeType);
@@ -858,7 +861,7 @@ ByteStreamPtr Packet::getBSSourceDataFieldsVariablePart() {
	return sdff;
}

ByteStreamPtr Packet::getBSData() {
ByteStreamPtr Packet::getData() {
	return getBSSourceDataFieldsVariablePart();
}

@@ -869,14 +872,14 @@ ByteStreamPtr Packet::getBSSourceDataField() {
}


word Packet::getCompressionAlgorithm() {
enum CompressionAlgorithms Packet::getCompressionAlgorithm() {
	switch(compressionAlgorithmsSection)
	{
		case 0:
			return header->getFieldValue(compressionAlgorithmsIndex);
			return (enum CompressionAlgorithms) header->getFieldValue(compressionAlgorithmsIndex);
			break;
		case 1:
			return dataField->getPacketDataFieldHeader()->getFieldValue(compressionAlgorithmsIndex);
			return (enum CompressionAlgorithms) dataField->getPacketDataFieldHeader()->getFieldValue(compressionAlgorithmsIndex);
			break;
		default:
			throw new PacketException("Error in the compression algorithm section field specification");
@@ -897,8 +900,10 @@ word Packet::getCompressionLevel() {
	}
}

ByteStreamPtr Packet::compress(enum CompressionAlgorithms compressionAlgorithm, byte compressionLevel) {
ByteStreamPtr Packet::compressData(enum CompressionAlgorithms compressionAlgorithm, byte compressionLevel) {
	
	if(!isCompressible())
		return 0;
	
	//set the algorithm
	switch(compressionAlgorithmsSection)
@@ -941,10 +946,16 @@ ByteStreamPtr Packet::compress(enum CompressionAlgorithms compressionAlgorithm,
	}
	//cout << compressionAlgorithmsSection << " " << compressionAlgorithmsIndex << " " << compressionAlgorithm << " - " << compressionLevelSection << " " << compressionLevelIndex << " " << (int)compressionLevel << endl;
	
	ByteStreamPtr original = getBSSourceDataFieldsVariablePart();
	dword dimvariablepart = header->getPacketLength() + dimPacketHeader - dimPacketStartingFixedPart - dimPacketTail;
	ByteStreamPtr original = ByteStreamPtr(new ByteStream(packet_output->stream  + (thereisprefix?dimPrefix:0) + dimPacketStartingFixedPart, dimvariablepart, bigendian));

	ByteStreamPtr compressed = original->compress(compressionAlgorithm, compressionLevel);
	cout << "original: " << original->size() << " compressed: " << compressed->size() << endl;
	ByteStreamPtr tail = getBSTail();
	//cout << "original: " << original->size() << " compressed: " << compressed->size() << endl;
	
	ByteStreamPtr tail = 0;
	if(dimPacketTail > 0)
		tail = ByteStreamPtr(new ByteStream(packet_output->stream + (thereisprefix?dimPrefix:0) + header->getPacketLength() + dimPacketHeader - dimPacketTail , dimPacketTail, bigendian));
	
	ByteStreamPtr b;
	
	if(compressed != 0) {
@@ -954,7 +965,7 @@ ByteStreamPtr Packet::compress(enum CompressionAlgorithms compressionAlgorithm,
		memcpy( packet_output->stream + (thereisprefix?dimPrefix:0) + dimPacketStartingFixedPart, compressed->stream, compressed->size());
		dword dimpacket = dimPacketStartingFixedPart + compressed->size() + dimPacketTail;
		if(dimPacketTail != 0)
			memcpy( packet_output->stream + (thereisprefix?dimPrefix:0) + dimpacket - dimPacketTail, tail->stream, tail->size());
			memcpy( packet_output->stream + (thereisprefix?dimPrefix:0) + dimpacket - dimPacketTail, tail->stream, dimPacketTail);
		
		//set packet length
		dword dimDataField = dimPacketDataFieldHeader + dimPacketSourceDataFieldFixed + dimPacketTail + compressed->size();
@@ -1012,7 +1023,8 @@ PartOfPacket* Packet::getPacketTail() {
	return dataField->getPacketTail();
}

void Packet::decompress() {
ByteStreamPtr Packet::decompressData() {
	//ALGORITHM FOR AUTOMATIC DECOMPRESSION: NOT IMPLEMENTED NOW. TODO
	//decompression algorithm here
	//1) get the fixed and variable part of the source data field, get the tail
	//2) decompress the variable part of the source data field into a new ByteStream
@@ -1021,5 +1033,13 @@ void Packet::decompress() {
	//5) change the field the indicate if the packet is compressed = 0
	//5) call decode(newstream)
	
	//DECOMPRESS ONLY THE DATA
	if(!isCompressed())
		return getData();
	ByteStreamPtr compressed = getData();
	ByteStreamPtr decompressed = compressed->decompress(getCompressionAlgorithm(), getCompressionLevel());

	return decompressed;
	
}