Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • bias/daqs/base-daq
1 result
Show changes
Commits on Source (9)
...@@ -27,6 +27,7 @@ public: ...@@ -27,6 +27,7 @@ public:
virtual int open()=0; virtual int open()=0;
virtual int close()=0; virtual int close()=0;
virtual void setDest(std::string dest) = 0; virtual void setDest(std::string dest) = 0;
virtual void updateDest()=0;
virtual std::string getDest() = 0; virtual std::string getDest() = 0;
virtual ~BaseArchiver() = default; virtual ~BaseArchiver() = default;
......
...@@ -11,47 +11,53 @@ ...@@ -11,47 +11,53 @@
namespace inaf::oasbo::DAQ{ namespace inaf::oasbo::DAQ{
class BaseDAQ { class BaseDAQ {
public: public:
static const int STOP = -1; enum Status {
static const int READY = 0; STOP = -1,
static const int IDLE = 1; READY = 0,
static const int RUN = 2; IDLE = 1,
RUN = 2
};
protected: protected:
int currentState; Status currentState;
int nextState; Status nextState;
bool changeStateFlag = false; bool changeStateFlag = false;
ConnectionProtocols::BaseProtocol *socket; ConnectionProtocols::BaseProtocol *socket;
Archivers::BaseArchiver *archiver; Archivers::BaseArchiver *archiver;
Providers::BaseProvider *provider; Providers::BaseProvider *provider;
Monitors::BaseMonitor *monitor; Monitors::BaseMonitor *monitor;
PacketLib::BasePacket *packet;
public: public:
void setProtocol(ConnectionProtocols::BaseProtocol &socket){this->socket = &socket;} void setProtocol(ConnectionProtocols::BaseProtocol &socket){this->socket = &socket;}
void setArchiver(Archivers::BaseArchiver &archiver){this->archiver = &archiver;} void setArchiver(Archivers::BaseArchiver &archiver){this->archiver = &archiver;}
void setProvider(Providers::BaseProvider &provider){this->provider = &provider;} void setProvider(Providers::BaseProvider &provider){this->provider = &provider;}
void setMonitor(Monitors::BaseMonitor &monitor){this->monitor = &monitor;} void setMonitor(Monitors::BaseMonitor &monitor){this->monitor = &monitor;}
void setCurrentState(int currentState){this->currentState = currentState;} void setPacket(PacketLib::BasePacket &packet){this->packet = &packet;}
void setNextState(int nextState){this->nextState = nextState;} void setCurrentState(Status currentState){this->currentState = currentState;}
void setNextState(Status nextState){this->nextState = nextState;}
void setChangeStateFlag(bool flag){this->changeStateFlag = flag;} void setChangeStateFlag(bool flag){this->changeStateFlag = flag;}
ConnectionProtocols::BaseProtocol * getSocketPtr(){return this->socket;} ConnectionProtocols::BaseProtocol * getSocketPtr(){return this->socket;}
Archivers::BaseArchiver *getArchiverPtr(){return this->archiver;} Archivers::BaseArchiver *getArchiverPtr(){return this->archiver;}
Providers::BaseProvider * getProviderPtr(){return this->provider;} Providers::BaseProvider * getProviderPtr(){return this->provider;}
Monitors::BaseMonitor * getMonitorPtr(){return this->monitor;} Monitors::BaseMonitor * getMonitorPtr(){return this->monitor;}
int getCurrentState(){return this->currentState;} PacketLib::BasePacket * getPacketPrt(){return this->packet;}
int getNextState(){return this->nextState;} Status getCurrentState(){return this->currentState;}
Status getNextState(){return this->nextState;}
int getChangeStateFlag(){return this->changeStateFlag;} int getChangeStateFlag(){return this->changeStateFlag;}
virtual void init() = 0; virtual void init() = 0;
virtual void start(int initialState,PacketLib::BasePacket &) = 0; virtual void start(Status initialState) = 0;
virtual void stop() = 0; virtual void stop() = 0;
virtual void switchState(const int) = 0; virtual void switchState(const Status) = 0;
virtual int deliverPacket(PacketLib::BasePacket &) = 0; virtual int deliverPacket() = 0;
virtual ~BaseDAQ(){ virtual ~BaseDAQ(){
delete socket; delete socket;
delete provider; delete provider;
delete archiver; delete archiver;
delete monitor; delete monitor;
delete packet;
} }
}; };
} }
......
...@@ -4,9 +4,7 @@ ...@@ -4,9 +4,7 @@
* Created on: Nov 24, 2022 * Created on: Nov 24, 2022
* Author: valerio * Author: valerio
*/ */
#pragma once
#ifndef INCLUDE_BASEPACKET_H_
#define INCLUDE_BASEPACKET_H_
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
...@@ -24,33 +22,33 @@ namespace inaf::oasbo::PacketLib { ...@@ -24,33 +22,33 @@ namespace inaf::oasbo::PacketLib {
class BasePacketStructure { class BasePacketStructure {
protected: protected:
std::string source; std::string source;
size_t byteSize; uint byteSize;
std::vector<int> fieldSizes; std::vector<uint> fieldSizes;
std::unordered_map<size_t, size_t> fieldNameToOffsetsMap; std::unordered_map<uint, uint> indexToOffsetsMap;
std::unordered_map<std::string, int> fieldNameToIndexMap; std::unordered_map<std::string, uint> fieldNameToIndexMap;
std::unordered_map<int, std::string> indexToFieldNameMap; std::unordered_map<uint, std::string> indexToFieldNameMap;
virtual std::vector<std::tuple<int, std::string, int>> readStructureFromSource( virtual std::vector<std::tuple<uint, std::string, uint>> readStructureFromSource(
std::string source) = 0; std::string source) = 0;
void updateFieldSizes( void updateFieldSizes(
const std::vector<std::tuple<int, std::string, int>> &paramsTuple) { const std::vector<std::tuple<uint, std::string, uint>> &paramsTuple) {
std::for_each(paramsTuple.begin(), paramsTuple.end(), std::for_each(paramsTuple.begin(), paramsTuple.end(),
[&](const std::tuple<int, std::string, int> &tup) { [&](const std::tuple<uint, std::string, uint> &tup) {
this->fieldSizes.push_back(std::get<2>(tup)); this->fieldSizes.push_back(std::get<2>(tup));
}); });
} }
void updateFieldOffsets( void updateFieldOffsets(
const std::vector<std::tuple<int, std::string, int>> &paramsTuple) { const std::vector<std::tuple<uint, std::string, uint>> &paramsTuple) {
size_t offset = 0; uint offset = 0;
for (size_t i = 0; i < paramsTuple.size(); i++) { for (size_t i = 0; i < paramsTuple.size(); i++) {
fieldNameToOffsetsMap[i] = offset; indexToOffsetsMap[i] = offset;
offset += std::get<2>(paramsTuple[i]); offset += std::get<2>(paramsTuple[i]);
} }
} }
void updateFieldNameAndIndexMap( void updateFieldNameAndIndexMap(
const std::vector<std::tuple<int, std::string, int>> &paramsTuple) { const std::vector<std::tuple<uint, std::string, uint>> &paramsTuple) {
std::for_each(paramsTuple.begin(), paramsTuple.end(), std::for_each(paramsTuple.begin(), paramsTuple.end(),
[&](const std::tuple<int, std::string, int> &tup) { [&](const std::tuple<uint, std::string, uint> &tup) {
this->fieldNameToIndexMap[std::get<1>(tup)] = std::get<0>( this->fieldNameToIndexMap[std::get<1>(tup)] = std::get<0>(
tup); tup);
this->indexToFieldNameMap[std::get<0>(tup)] = std::get<1>( this->indexToFieldNameMap[std::get<0>(tup)] = std::get<1>(
...@@ -59,7 +57,7 @@ protected: ...@@ -59,7 +57,7 @@ protected:
} }
void updateStructure(std::string source) { void updateStructure(std::string source) {
this->source = source; this->source = source;
std::vector<std::tuple<int, std::string, int>> paramsTuple = std::vector<std::tuple<uint, std::string, uint>> paramsTuple =
readStructureFromSource(source); readStructureFromSource(source);
updateFieldSizes(paramsTuple); updateFieldSizes(paramsTuple);
updateFieldOffsets(paramsTuple); updateFieldOffsets(paramsTuple);
...@@ -73,22 +71,22 @@ public: ...@@ -73,22 +71,22 @@ public:
void changeSource(std::string source) { void changeSource(std::string source) {
updateStructure(source); updateStructure(source);
} }
size_t getByteSize() { uint getByteSize() {
return this->byteSize; return this->byteSize;
} }
std::string getSource() { std::string getSource() {
return this->source; return this->source;
} }
std::optional<size_t> bitOffsetOf(uint index) { std::optional<uint> bitOffsetOf(uint index) {
if (index <= this->numberOfFields()) { if (index <= this->numberOfFields()) {
return fieldNameToOffsetsMap.at(index); return indexToOffsetsMap.at(index);
} else { } else {
std::cerr << "No field at " << index << ", max is " std::cerr << "No field at " << index << ", max is "
<< numberOfFields() << ", returning nullopt." << std::endl; << numberOfFields() << ", returning nullopt." << std::endl;
return std::nullopt; return std::nullopt;
} }
} }
std::optional<size_t> bitSizeOf(uint index) { std::optional<uint> bitSizeOf(uint index) {
if (index <= this->numberOfFields()) { if (index <= this->numberOfFields()) {
return this->fieldSizes[index]; return this->fieldSizes[index];
} else { } else {
...@@ -98,7 +96,7 @@ public: ...@@ -98,7 +96,7 @@ public:
} }
} }
std::optional<size_t> indexOfField(std::string fieldName) { std::optional<uint> indexOfField(std::string fieldName) {
try { try {
return this->fieldNameToIndexMap.at(fieldName); return this->fieldNameToIndexMap.at(fieldName);
} catch (const std::out_of_range &oor) { } catch (const std::out_of_range &oor) {
...@@ -108,7 +106,7 @@ public: ...@@ -108,7 +106,7 @@ public:
} }
} }
std::optional<std::string> fieldNameOfIndex(size_t index) { std::optional<std::string> fieldNameOfIndex(uint index) {
try { try {
return this->indexToFieldNameMap.at(index); return this->indexToFieldNameMap.at(index);
} catch (const std::out_of_range &oor) { } catch (const std::out_of_range &oor) {
...@@ -117,11 +115,11 @@ public: ...@@ -117,11 +115,11 @@ public:
return std::nullopt; return std::nullopt;
} }
} }
size_t numberOfFields() { uint numberOfFields() {
return this->fieldSizes.size(); return this->fieldSizes.size();
} }
std::unordered_map<std::string, int> getFieldNameToIndexMap() const { std::unordered_map<std::string, uint> getFieldNameToIndexMap() const {
return this->fieldNameToIndexMap; return this->fieldNameToIndexMap;
} }
}; };
...@@ -132,7 +130,7 @@ class bit_iterator: public std::iterator<std::random_access_iterator_tag, ...@@ -132,7 +130,7 @@ class bit_iterator: public std::iterator<std::random_access_iterator_tag,
public: public:
bit_iterator(const uint8_t *data, int offset, bit_iterator(const uint8_t *data, int offset,
BasePacketStructure *structure, BasePacketStructure *structure,
std::function<ValueType(const uint8_t*, size_t, int)> func) : std::function<ValueType(const uint8_t*, uint, uint)> func) :
m_data(data), m_offset(offset), m_structure(structure), m_func(func) { m_data(data), m_offset(offset), m_structure(structure), m_func(func) {
} }
...@@ -193,7 +191,7 @@ private: ...@@ -193,7 +191,7 @@ private:
const uint8_t *m_data; const uint8_t *m_data;
int m_offset; int m_offset;
BasePacketStructure *m_structure; BasePacketStructure *m_structure;
std::function<ValueType(const uint8_t*, size_t, int)> m_func; std::function<ValueType(const uint8_t*, uint, uint)> m_func;
}; };
template<typename ValueType> template<typename ValueType>
...@@ -206,17 +204,17 @@ protected: ...@@ -206,17 +204,17 @@ protected:
// The binary value is represented by num_bits number of bits starting from the offset-th bit in the memory. // The binary value is represented by num_bits number of bits starting from the offset-th bit in the memory.
// The function returns the value of the binary as a ValueType. // The function returns the value of the binary as a ValueType.
static ValueType _readValueFromBinaryAt_(const uint8_t *binaryPointer, static ValueType _readValueFromBinaryAt_(const uint8_t *binaryPointer,
size_t offset, int num_bits) { uint offset, uint num_bits) {
// Calculate the bit offset from the byte offset: // Calculate the bit offset from the byte offset:
int bit_offset = offset % 8; auto bit_offset = offset % 8;
// Calculate the byte offset from the bit offset: // Calculate the byte offset from the bit offset:
int byte_offset = offset / 8; auto byte_offset = offset / 8;
ValueType value = 0; ValueType value = 0;
for (int i = 0; i < num_bits; i++) { for (uint i = 0; i < num_bits; i++) {
// Calculate the byte and bit index of the current bit: // Calculate the byte and bit index of the current bit:
int byte_index = byte_offset + (bit_offset + i) / 8; auto byte_index = byte_offset + (bit_offset + i) / 8;
int bit_index = (bit_offset + i) % 8; auto bit_index = (bit_offset + i) % 8;
uint8_t byte = binaryPointer[byte_index]; uint8_t byte = binaryPointer[byte_index];
// Create a bit mask to isolate the desired bit: // Create a bit mask to isolate the desired bit:
uint8_t bit_mask = 1 << (7 - bit_index); uint8_t bit_mask = 1 << (7 - bit_index);
...@@ -226,7 +224,7 @@ protected: ...@@ -226,7 +224,7 @@ protected:
return value; return value;
} }
int minBitsRequired(size_t value) { uint minBitsRequired(size_t value) const {
// Handle special cases // Handle special cases
if (value == 0) { if (value == 0) {
return 1; return 1;
...@@ -235,7 +233,7 @@ protected: ...@@ -235,7 +233,7 @@ protected:
} }
// Calculate the number of bits needed // Calculate the number of bits needed
int bitsNeeded = 0; auto bitsNeeded = 0;
bool isNegative = value < 0; bool isNegative = value < 0;
uint32_t absValue = isNegative ? -value : value; uint32_t absValue = isNegative ? -value : value;
...@@ -260,10 +258,18 @@ public: ...@@ -260,10 +258,18 @@ public:
virtual ~BasePacketTempl() = default; virtual ~BasePacketTempl() = default;
void updateStructure(BasePacketStructure &structure) { void updateStructure(BasePacketStructure &structure) {
size_t newSize = std::min(structure.getByteSize(), this->structure->getByteSize());
this->structure = &structure; this->structure = &structure;
uint8_t *buff = new uint8_t[newSize];
std::memcpy(buff, binaryPointer, newSize);
delete this->binaryPointer;
this->binaryPointer = new uint8_t[newSize];
std::memcpy(binaryPointer, buff, newSize);
delete buff;
} }
std::optional<ValueType> readValueFromBinaryAt(uint index) { std::optional<ValueType> readValueFromBinaryAt(uint index) const {
auto offset = structure->bitOffsetOf(index); // offset from the beginning of the byte auto offset = structure->bitOffsetOf(index); // offset from the beginning of the byte
auto num_bits = structure->bitSizeOf(index); //remaining bits to read auto num_bits = structure->bitSizeOf(index); //remaining bits to read
if (offset.has_value() && num_bits.has_value()) if (offset.has_value() && num_bits.has_value())
...@@ -281,35 +287,52 @@ public: ...@@ -281,35 +287,52 @@ public:
return binaryPointer; return binaryPointer;
} }
void copyToBinaryPointer(const uint8_t *from, uint size) { int copyToBinaryPointer(const uint8_t *from, uint size) {
if (size > this->structure->getByteSize()) { uint max_writable = this->structure->getByteSize();
if (size > max_writable) {
std::cerr << "Error: you are trying to copy " << size std::cerr << "Error: you are trying to copy " << size
<< "byte where the max size is: " << "byte where the max size is: "
<< this->structure->getByteSize() << std::endl; << max_writable << std::endl;
std::cerr << "\tI copy only until " std::cerr << "\tI copy only until "
<< this->structure->getByteSize() << " byte" << std::endl; << max_writable << " byte" << std::endl;
std::memcpy(binaryPointer, from, this->structure->getByteSize()); std::memcpy(binaryPointer, from, max_writable);
} else return max_writable;
}
else{
std::memcpy(binaryPointer, from, size); std::memcpy(binaryPointer, from, size);
return size;
}
} }
void copyToBinaryPointer(const uint8_t *from, uint size, uint offset) {
if (size + offset > this->structure->getByteSize()) { int copyToBinaryPointer(const uint8_t *from, uint size, uint offset) {
uint max_writable = this->structure->getByteSize();
if(offset > max_writable){
std::cerr << "Error: you are trying to copy starting from " << offset
<< "byte where the max size is: "
<< max_writable << std::endl;
return -1;
}
if (size + offset > max_writable) {
std::cerr << "Error: you are trying to copy " << size + offset std::cerr << "Error: you are trying to copy " << size + offset
<< "byte where the max size is: " << "byte where the max size is: "
<< this->structure->getByteSize() << std::endl; << max_writable << std::endl;
std::cerr << "\tI copy only until " std::cerr << "\tI copy only until "
<< this->structure->getByteSize() << " byte" << std::endl; << max_writable << " byte" << std::endl;
std::memcpy(&binaryPointer[offset], from, int to_write = max_writable-offset;
this->structure->getByteSize()); std::memcpy(&binaryPointer[offset], from, to_write);
} else return to_write;
}
else{
std::memcpy(&binaryPointer[offset], from, size); std::memcpy(&binaryPointer[offset], from, size);
return size;
}
} }
std::optional<ValueType> operator[](uint index) { std::optional<ValueType> operator[](uint index) const {
return readValueFromBinaryAt(index); return readValueFromBinaryAt(index);
} }
std::optional<ValueType> operator[](std::string fieldName) { std::optional<ValueType> operator[](std::string fieldName) const {
auto index = structure->indexOfField(fieldName); auto index = structure->indexOfField(fieldName);
if (index.has_value()) if (index.has_value())
return readValueFromBinaryAt(index.value()); return readValueFromBinaryAt(index.value());
...@@ -328,7 +351,7 @@ public: ...@@ -328,7 +351,7 @@ public:
&_readValueFromBinaryAt_); &_readValueFromBinaryAt_);
} }
std::optional<int> writeValueToBinaryAtIndex(size_t index, std::optional<int> writeValueToBinaryAtIndex(uint index,
ValueType value) { ValueType value) {
auto offset = this->structure->bitOffsetOf(index); auto offset = this->structure->bitOffsetOf(index);
auto numbits = this->structure->bitSizeOf(index); auto numbits = this->structure->bitSizeOf(index);
...@@ -364,11 +387,15 @@ public: ...@@ -364,11 +387,15 @@ public:
} }
return numbits_written; return numbits_written;
} }
uint getPacketStructureByteSize() const {return structure->getByteSize();}
BasePacketStructure & getPacketStructure() const { return *(this->structure);}
virtual size_t getHeaderSize()=0; virtual bool isRecognizedHeader() const = 0;
virtual size_t getPayloadSize()=0; virtual bool isRecognizedHeader(std::vector<uint8_t> buff) const = 0;
virtual size_t getTailSize()=0; virtual uint getHeaderSize() const = 0;
virtual size_t getMaxPacketSize()=0; virtual uint getPayloadSize() const = 0;
virtual uint getTailSize() const = 0;
}; };
using valueType = size_t; using valueType = size_t;
...@@ -384,4 +411,4 @@ public: ...@@ -384,4 +411,4 @@ public:
}; };
} }
#endif
...@@ -27,19 +27,19 @@ namespace inaf::oasbo::ConnectionProtocols{ ...@@ -27,19 +27,19 @@ namespace inaf::oasbo::ConnectionProtocols{
class BaseProtocol{ class BaseProtocol{
protected: protected:
std::string host; std::string host;
int port;
public: public:
std::string getHost(){return host;} std::string getHost(){return host;}
int getPort(){return port;}
void setHost(std::string host){ this->host = host;} void setHost(std::string host){ this->host = host;}
void setPort(int port){this->port = port;}
virtual int connectToClient()=0;
virtual int connectToServer()=0; virtual int connectToServer()=0;
virtual int connectToClient()=0;
virtual int rcvPacketFromCli(PacketLib::BasePacket &) = 0; virtual int closeConnectionToServer()=0;
virtual int sendPacketToSrv(PacketLib::BasePacket &) = 0; virtual int closeConnectionToClient()=0;
virtual bool isConnectedToClient() const =0;
virtual bool isConnectedToServer() const =0;
virtual int receiveFromClient(PacketLib::BasePacket &) = 0;
virtual int sendToServer(PacketLib::BasePacket &) = 0;
virtual ~BaseProtocol() = default; virtual ~BaseProtocol() = default;
}; };
} }
......