<commit_before>#include <netinet/in.h>
#include "network/ChunkFactory.h"

namespace Network
{

// PUBLIC

ChunkFactory::ChunkFactory():   _sizeChunks(0),
    _chunkID(1),
    _packetID(1) {}

ChunkFactory::~ChunkFactory()
{
    _chunks.clear();
}

/**
 * @brief Convert a vector of Landmark into string to put it in chunk(s)
 * @details Iterates on a vector of Landmarks using processData()
 * @param landmarks_ Vector of landmark_ containing informations about the captured points
 */
void ChunkFactory::processData(const std::vector< Landmarks::Landmark* >& landmarks_)
{
    std::vector<Landmarks::Landmark*>::const_iterator it;
    for (it = landmarks_.begin() ; it != landmarks_.end(); ++it)
    {
        if (*it != NULL)
            processData((Landmarks::Landmark&)(*(*it))); // Get the reference from the pointer.
    }
}

/**
 * @brief Convert a Landmark into string to put it in chunk
 * @details Create the chunk header and add a landmark_ packet
 * to it using fromLandmarkToString().
 * Then it push the temporary chunk into the deque of chunks.
 * @param landmarks_ Class landmark_ containing informations about the captured points
 */
void ChunkFactory::processData(const Landmarks::Landmark& landmark_)
{
    _tmpChunk = MAGIC_NB_LANDMARK; // The Magic
    _tmpChunk += getNewChunkID(); // The Chunk ID
    _tmpChunk += fromLandmarkToString(landmark_); // The packet

    pushTmpChunkToChunks();
}

/**
 * @brief Convert a PointCloud of PointXYZ into packets of string(s) to put in chunk(s)
 * @details
 * @param points PointCloud containing only 3D point informations about the captured points
 */
void ChunkFactory::processData(const pcl::PointCloud< pcl::PointXYZ >& pointCloud)
{
    unsigned int cloudIndex = 0;
    unsigned int packetDone = 0;
    unsigned int totalPacketNeeded;
    std::string packet;
    std::string metadataPacket;

    totalPacketNeeded = calculateTotalPacketNeeded(pointCloud);

    while (packetDone < totalPacketNeeded)
    {
        packet = convertDataPacket(pointCloud, cloudIndex);
        metadataPacket = createPacketMetadata(++packetDone, totalPacketNeeded, packet);
        _tmpChunk = MAGIC_NB_POINTCLOUD; // The Magic
        _tmpChunk += getNewChunkID(); // The Chunk ID
        _tmpChunk += metadataPacket + packet; // The Packet
        pushTmpChunkToChunks();
    }
}

// Getters
/**
 * @brief Return whether _chunks is empty or not
 * @return A boolean to tell you whether _chunks is empty or not.
 */
bool ChunkFactory::isFullChunkReady() const { return (_chunks.size() == 0) ? (false) : (true); }

/**
 * @brief Get a chunk already prepared by ChunkFactory
 * @return A chunk (string) ready to sent through UDP. If there is no chunk prepared, it will
 * send an empty string.
 */
std::string ChunkFactory::getChunk()
{
    std::string chunk = "";

    if (_chunks.size() != 0)
    {
        chunk = _chunks.back();
        _chunks.pop_back();
        decreaseSizeChunks(chunk.size());
    }
    return chunk;
}

// PRIVATE

/**
 * @brief Push the temporary chunk into the deque _chunks
 * @details If not empty, put the temporary chunk into the deque of
 * chunks and clear it. It also call increaseSizeChunks()
 */
void ChunkFactory::pushTmpChunkToChunks()
{
    if (_tmpChunk.size() > 0)
    {
        _chunks.push_front(_tmpChunk);
        increaseSizeChunks(_tmpChunk.size());
        _tmpChunk.erase();
    }
}

/**
 * @brief Convert the class Landmark into a string
 * @details The chosen variable converted into the string are:
 * pos, robotPos, id, life, totalTimeObserved, bearing, range.
 * @param landmark_ Class containing 3D information that we have to convert
 * @return Return a string containing chosen information of the class Landmark
 */
std::string ChunkFactory::fromLandmarkToString(const Landmarks::Landmark& landmark_)
{
    std::string strLandmark = "";
    uint32_t idConevert = htonl(landmark_.id);
    uint32_t lifeConevert = htonl(landmark_.life);
    uint32_t totalTimeObservedConevert = htonl(landmark_.totalTimeObserved);
    uint32_t bearingConevert = htonl(landmark_.bearing);
    uint32_t rangeConevert = htonl(landmark_.range);


    strLandmark += fromPclPointToString(landmark_.pos);
    strLandmark += fromPclPointToString(landmark_.robotPos);
    strLandmark += encodeNbIntoString((void*)&(landmark_.id), sizeof(landmark_.id));
    strLandmark += encodeNbIntoString((void*)&(landmark_.life), sizeof(landmark_.life));
    strLandmark += encodeNbIntoString((void*)&(landmark_.totalTimeObserved), sizeof(landmark_.totalTimeObserved));
    strLandmark += encodeNbIntoString((void*)&(landmark_.bearing), sizeof(landmark_.bearing));
    strLandmark += encodeNbIntoString((void*)&(landmark_.range), sizeof(landmark_.range));

    return strLandmark;
}

/*
 * Calculate the total number of packet we need to do to send the cloud
 */
int ChunkFactory::calculateTotalPacketNeeded(const pcl::PointCloud< pcl::PointXYZ >& cloud)
{
    unsigned int        totalPacketNeeded = 0;
    unsigned int        totalNbOfPoint = cloud.size();
    unsigned int        nbOfPointInPacket;

    nbOfPointInPacket = SIZE_IN_PACKET / SIZE_OF_POINT_XYZ;
    totalPacketNeeded = (totalNbOfPoint / nbOfPointInPacket);

    return ((totalNbOfPoint % nbOfPointInPacket) > 0) ? (++totalPacketNeeded) : (totalPacketNeeded) ;
}

std::string ChunkFactory::createPacketMetadata(unsigned int currentPacket, unsigned int totalPacket, std::string& packet)
{
    unsigned short      packetSize = (unsigned short)(packet.size());
    std::string         metadata = "";
    uint32_t packetIDConevert = htonl(_packetID);
    uint32_t currentPacketConevert = htonl(currentPacket);
    uint32_t totalPacketConevert = htonl(totalPacket);
    uint16_t packetSizeConevert = htons(packetSize);

    metadata += encodeNbIntoString((void*)&(_packetID), sizeof(_packetID));
    metadata += encodeNbIntoString((void*)&(currentPacket), sizeof(currentPacket));
    metadata += encodeNbIntoString((void*)&(totalPacket), sizeof(totalPacket));
    metadata += encodeNbIntoString((void*)&(packetSize), sizeof(packetSize));

    return metadata;
}

std::string ChunkFactory::convertDataPacket(const pcl::PointCloud< pcl::PointXYZ >& cloud, unsigned int& cloudIndex)
{
    std::string packetData = "";
    unsigned int nbOfPoint;

    nbOfPoint = SIZE_IN_PACKET / SIZE_OF_POINT_XYZ;

    packetData += convertRangeOfPoint(cloud, cloudIndex, nbOfPoint);

    return packetData;
}

std::string ChunkFactory::convertRangeOfPoint(const pcl::PointCloud< pcl::PointXYZ >& cloud, unsigned int& cloudIndex, unsigned int nbOfPoint)
{<commit_msg>FakeAgent: Delete unused variable<commit_after>#include <netinet/in.h>
#include "network/ChunkFactory.h"

namespace Network
{

// PUBLIC

ChunkFactory::ChunkFactory():   _sizeChunks(0),
    _chunkID(1),
    _packetID(1) {}

ChunkFactory::~ChunkFactory()
{
    _chunks.clear();
}

/**
 * @brief Convert a vector of Landmark into string to put it in chunk(s)
 * @details Iterates on a vector of Landmarks using processData()
 * @param landmarks_ Vector of landmark_ containing informations about the captured points
 */
void ChunkFactory::processData(const std::vector< Landmarks::Landmark* >& landmarks_)
{
    std::vector<Landmarks::Landmark*>::const_iterator it;
    for (it = landmarks_.begin() ; it != landmarks_.end(); ++it)
    {
        if (*it != NULL)
            processData((Landmarks::Landmark&)(*(*it))); // Get the reference from the pointer.
    }
}

/**
 * @brief Convert a Landmark into string to put it in chunk
 * @details Create the chunk header and add a landmark_ packet
 * to it using fromLandmarkToString().
 * Then it push the temporary chunk into the deque of chunks.
 * @param landmarks_ Class landmark_ containing informations about the captured points
 */
void ChunkFactory::processData(const Landmarks::Landmark& landmark_)
{
    _tmpChunk = MAGIC_NB_LANDMARK; // The Magic
    _tmpChunk += getNewChunkID(); // The Chunk ID
    _tmpChunk += fromLandmarkToString(landmark_); // The packet

    pushTmpChunkToChunks();
}

/**
 * @brief Convert a PointCloud of PointXYZ into packets of string(s) to put in chunk(s)
 * @details
 * @param points PointCloud containing only 3D point informations about the captured points
 */
void ChunkFactory::processData(const pcl::PointCloud< pcl::PointXYZ >& pointCloud)
{
    unsigned int cloudIndex = 0;
    unsigned int packetDone = 0;
    unsigned int totalPacketNeeded;
    std::string packet;
    std::string metadataPacket;

    totalPacketNeeded = calculateTotalPacketNeeded(pointCloud);

    while (packetDone < totalPacketNeeded)
    {
        packet = convertDataPacket(pointCloud, cloudIndex);
        metadataPacket = createPacketMetadata(++packetDone, totalPacketNeeded, packet);
        _tmpChunk = MAGIC_NB_POINTCLOUD; // The Magic
        _tmpChunk += getNewChunkID(); // The Chunk ID
        _tmpChunk += metadataPacket + packet; // The Packet
        pushTmpChunkToChunks();
    }
}

// Getters
/**
 * @brief Return whether _chunks is empty or not
 * @return A boolean to tell you whether _chunks is empty or not.
 */
bool ChunkFactory::isFullChunkReady() const { return (_chunks.size() == 0) ? (false) : (true); }

/**
 * @brief Get a chunk already prepared by ChunkFactory
 * @return A chunk (string) ready to sent through UDP. If there is no chunk prepared, it will
 * send an empty string.
 */
std::string ChunkFactory::getChunk()
{
    std::string chunk = "";

    if (_chunks.size() != 0)
    {
        chunk = _chunks.back();
        _chunks.pop_back();
        decreaseSizeChunks(chunk.size());
    }
    return chunk;
}

// PRIVATE

/**
 * @brief Push the temporary chunk into the deque _chunks
 * @details If not empty, put the temporary chunk into the deque of
 * chunks and clear it. It also call increaseSizeChunks()
 */
void ChunkFactory::pushTmpChunkToChunks()
{
    if (_tmpChunk.size() > 0)
    {
        _chunks.push_front(_tmpChunk);
        increaseSizeChunks(_tmpChunk.size());
        _tmpChunk.erase();
    }
}

/**
 * @brief Convert the class Landmark into a string
 * @details The chosen variable converted into the string are:
 * pos, robotPos, id, life, totalTimeObserved, bearing, range.
 * @param landmark_ Class containing 3D information that we have to convert
 * @return Return a string containing chosen information of the class Landmark
 */
std::string ChunkFactory::fromLandmarkToString(const Landmarks::Landmark& landmark_)
{
    std::string strLandmark = "";


    strLandmark += fromPclPointToString(landmark_.pos);
    strLandmark += fromPclPointToString(landmark_.robotPos);
    strLandmark += encodeNbIntoString((void*)&(landmark_.id), sizeof(landmark_.id));
    strLandmark += encodeNbIntoString((void*)&(landmark_.life), sizeof(landmark_.life));
    strLandmark += encodeNbIntoString((void*)&(landmark_.totalTimeObserved), sizeof(landmark_.totalTimeObserved));
    strLandmark += encodeNbIntoString((void*)&(landmark_.bearing), sizeof(landmark_.bearing));
    strLandmark += encodeNbIntoString((void*)&(landmark_.range), sizeof(landmark_.range));

    return strLandmark;
}

/*
 * Calculate the total number of packet we need to do to send the cloud
 */
int ChunkFactory::calculateTotalPacketNeeded(const pcl::PointCloud< pcl::PointXYZ >& cloud)
{
    unsigned int        totalPacketNeeded = 0;
    unsigned int        totalNbOfPoint = cloud.size();
    unsigned int        nbOfPointInPacket;

    nbOfPointInPacket = SIZE_IN_PACKET / SIZE_OF_POINT_XYZ;
    totalPacketNeeded = (totalNbOfPoint / nbOfPointInPacket);

    return ((totalNbOfPoint % nbOfPointInPacket) > 0) ? (++totalPacketNeeded) : (totalPacketNeeded) ;
}

std::string ChunkFactory::createPacketMetadata(unsigned int currentPacket, unsigned int totalPacket, std::string& packet)
{
    unsigned short      packetSize = (unsigned short)(packet.size());
    std::string         metadata = "";

    metadata += encodeNbIntoString((void*)&(_packetID), sizeof(_packetID));
    metadata += encodeNbIntoString((void*)&(currentPacket), sizeof(currentPacket));
    metadata += encodeNbIntoString((void*)&(totalPacket), sizeof(totalPacket));
    metadata += encodeNbIntoString((void*)&(packetSize), sizeof(packetSize));

    return metadata;
}

std::string ChunkFactory::convertDataPacket(const pcl::PointCloud< pcl::PointXYZ >& cloud, unsigned int& cloudIndex)
{
    std::string packetData = "";
    unsigned int nbOfPoint;

    nbOfPoint = SIZE_IN_PACKET / SIZE_OF_POINT_XYZ;

    packetData += convertRangeOfPoint(cloud, cloudIndex, nbOfPoint);

    return packetData;
}

std::string ChunkFactory::convertRangeOfPoint(const pcl::PointCloud< pcl::PointXYZ >& cloud, unsigned int& cloudIndex, unsigned int nbOfPoint)
{
{'fraction_non_alphanumeric': 0.1131450213034559, 'fraction_numerical': 0.00331387091683762, 'mean_word_length': 4.318925723877466, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5259372', 'n_tokens_mistral': 3847, 'n_tokens_neox': 3734, 'n_words': 1018}