Commit fae12a16 authored by Andrea Zoli's avatar Andrea Zoli
Browse files

Add CTAPacketBufferQ and CTAPacketBufferV.

parent 390f6c3a
Loading
Loading
Loading
Loading
+79 −0
Original line number Diff line number Diff line
/***************************************************************************
 PacketBufferQ.h  -  A FIFO buffer class for Packets
 -------------------
 copyright            : (C) 2013-2014 Andrea Zoli
 email                : zoli@iasfbo.inaf.it
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software for non commercial purpose              *
 *   and for public research institutes; you can redistribute it and/or    *
 *   modify it under the terms of the GNU General Public License.          *
 *   For commercial purpose see appropriate license terms                  *
 *                                                                         *
 ***************************************************************************/
#ifndef _PACKETBUFFERQ_H
#define _PACKETBUFFERQ_H

#include <string>
#include <queue>
#include "PacketLibDefinition.h"
#include "InputPacketStream.h"
#include "Input.h"

namespace PacketLib {

/// A FIFO queue of raw packets.
class PacketBufferQ {

public:
	PacketBufferQ(const string& configFile, const string& inputFile);

	~PacketBufferQ();

	/// Load all the tmInputFile packets into the queue.
	void load();

	/// Load a subset of packets into the queue.
	/// \param first The first packet.
	/// \param last The last packet.
	void load(int first, int last);

	/// Push a raw packet to the queue.
	/// \param rawPacket The raw packet pointer.
	void push(ByteStreamPtr rawPacket)
	{
		queue.push(rawPacket);
	}

	/// Pop a raw packet from the queue.
	/// \return The raw packet pointer.
	ByteStreamPtr pop()
	{
		if(queue.size() == 0)
			return 0;

		ByteStreamPtr elem = queue.front();
		queue.pop();
		return elem;
	}

	/// Return the size of the queue.
	/// \return The size of the queue.
	int size()
	{
		return queue.size();
	}

private:

	std::queue<ByteStreamPtr> queue;

	InputPacketStream* _ips;
	Input* _in;
};

}

#endif
+80 −0
Original line number Diff line number Diff line
/***************************************************************************
 PacketBufferV.h  -  An array buffer class for Packets
 -------------------
 copyright            : (C) 2013-2014 Andrea Zoli
 email                : zoli@iasfbo.inaf.it
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software for non commercial purpose              *
 *   and for public research institutes; you can redistribute it and/or    *
 *   modify it under the terms of the GNU General Public License.          *
 *   For commercial purpose see appropriate license terms                  *
 *                                                                         *
 ***************************************************************************/
#ifndef _PACKETBUFFERV_H
#define _PACKETBUFFERV_H

#include <string>
#include <vector>
#include "PacketLibDefinition.h"
#include "ByteStream.h"
#include "InputPacketStream.h"
#include "Input.h"

namespace PacketLib {

/// An array of raw packets.
class PacketBufferV {

public:

	PacketBufferV(const string& configFile, const string& inputFile);

	~PacketBufferV();

	/// Load all the tmInputFile packets into the vector.
	void load();

	/// Load a subset of packets into the vector.
	/// \param first The first packet.
	/// \param last The last packet.
	void load(int first, int last);

	/// Get a raw packet.
	/// \index the index of the packet.
	ByteStreamPtr get(int index)
	{
		return vec[index];
	}

	ByteStreamPtr getByteStream(int index, dword sizeB = 0);

	ByteStreamPtr getNextByteStream(dword sizeB = 0);

	/// Get a raw packet (cyclic vector)
	ByteStreamPtr getNext();

	/// Return the size of the vector.
	/// \return The size of the vector.
	int size()
	{
		return vec.size();
	}

	bool isBigendian();

private:

	std::vector<ByteStreamPtr> vec;
	unsigned int currentIndex;
	long currentIndexBS;

	InputPacketStream* _ips;
	Input* _in;
};

}

#endif

src/PacketBufferQ.cpp

0 → 100644
+78 −0
Original line number Diff line number Diff line
/***************************************************************************
 PacketBufferQ.cpp  -  A FIFO buffer class for Packets
 -------------------
    copyright            : (C) 2013-2014 Andrea Zoli
    email                : zoli@iasfbo.inaf.it
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software for non commercial purpose              *
 *   and for public research institutes; you can redistribute it and/or    *
 *   modify it under the terms of the GNU General Public License.          *
 *   For commercial purpose see appropriate license terms                  *
 *                                                                         *
 ***************************************************************************/
#include "Packet.h"
#include "PacketBufferQ.h"
#include "InputFile.h"

namespace PacketLib
{

PacketBufferQ::PacketBufferQ(const string& configFile, const string& inputFile)
{
	_ips = new InputPacketStream(configFile.c_str());
	_ips->createStreamStructure();
	_in = (Input*) new InputFile(_ips->isBigEndian());
	char** param = new char*[2];
	param[0] = (char*) inputFile.c_str();
	param[1] = 0;
	_in->open(param);
	_ips->setInput(_in);
	delete param;
}

PacketBufferQ::~PacketBufferQ()
{
	delete _ips;

	if(_in)
		_in->close();
}

void PacketBufferQ::load()
{
	ByteStreamPtr packetPtr = _ips->readPacket();
	int counter=0;
	while(packetPtr != 0)
	{
		push(packetPtr);
		packetPtr = _ips->readPacket();
		counter++;
	}
}

void PacketBufferQ::load(int first, int last)
{
	int counter = 0;
	ByteStreamPtr packetPtr;

	// skip elements preceeding first
	while(counter < first) {
		packetPtr = _ips->readPacket();
		if(packetPtr == 0) break;
		counter++;
	}

	// enqueue elements from first to last
	do {
		packetPtr = _ips->readPacket();
		if(packetPtr == 0) break;
		queue.push(packetPtr);
		counter++;
	}
	while(counter <= last);
}

}

src/PacketBufferV.cpp

0 → 100644
+108 −0
Original line number Diff line number Diff line
/***************************************************************************
 PacketBufferV.cpp  -  A FIFO buffer class for Packets
 -------------------
    copyright            : (C) 2013-2014 Andrea Zoli
    email                : zoli@iasfbo.inaf.it
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software for non commercial purpose              *
 *   and for public research institutes; you can redistribute it and/or    *
 *   modify it under the terms of the GNU General Public License.          *
 *   For commercial purpose see appropriate license terms                  *
 *                                                                         *
 ***************************************************************************/
#include "PacketBufferV.h"
#include "InputFile.h"

namespace PacketLib
{

PacketBufferV::PacketBufferV(const string& configFile, const string& inputFile)
	: currentIndex(0), currentIndexBS(0)
{
	_ips = new InputPacketStream(configFile.c_str());
	_ips->createStreamStructure();
	_in = (Input*) new InputFile(_ips->isBigEndian());
	char** param = new char*[2];
	param[0] = (char*) inputFile.c_str();
	param[1] = 0;
	_in->open(param);
	_ips->setInput(_in);
	delete param;
}

PacketBufferV::~PacketBufferV()
{
}

void PacketBufferV::load()
{
	ByteStreamPtr packetPtr = _ips->readPacket();
	int counter=0;
	while(packetPtr != 0)
	{
		vec.push_back(packetPtr);
		packetPtr = _ips->readPacket();
		counter++;
	}
}

void PacketBufferV::load(int first, int last)
{
	int counter = 0;
	ByteStreamPtr packetPtr;

	// skip elements preceeding first
	while(counter < first) {
		packetPtr = _ips->readPacket();
		if(packetPtr == 0) break;
		counter++;
	}
	

	// envec elements from first to last
	do {
		packetPtr = _ips->readPacket();
		if(packetPtr == 0) break;
		vec.push_back(packetPtr);
		counter++;
	}
	while(counter <= last);
}

ByteStreamPtr PacketBufferV::getNext()
{
	if(currentIndex >= vec.size())
		currentIndex = 0;
	return vec[currentIndex++];
}



ByteStreamPtr PacketBufferV::getByteStream(int index, dword sizeB) {

	ByteStreamPtr stream = vec[index];
	dword sizep = sizeB;
	if(!sizep) sizep = _ips->getPacketDimension(stream);
	bool bigendian = _ips->isBigEndian();
	return ByteStreamPtr(new ByteStream(stream, sizep, bigendian));

}

ByteStreamPtr PacketBufferV::getNextByteStream(dword sizeB) {

	if(currentIndexBS >= size())
		currentIndexBS = 0;
	return getByteStream(currentIndexBS++, sizeB);

}

bool PacketBufferV::isBigendian() {
	return _ips->isBigEndian();
}


}