<commit_before>//=============================================================================================================
/**
* @file     ftbuffclient.cpp
* @author   <NAME> <<EMAIL>
*           <NAME> <<EMAIL>>
*           <NAME>
* @version  1.0
* @date     December, 2019
*
* @section  LICENSE
*
* Copyright (C) 2019, <NAME> and <NAME>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that
* the following conditions are met:
*     * Redistributions of source code must retain the above copyright notice, this list of conditions and the
*       following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
*       the following disclaimer in the documentation and/or other materials provided with the distribution.
*     * Neither the name of MNE-CPP authors nor the names of its contributors may be used
*       to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Based on viewer.cc example from ftbuffer reference implementation, under the GNU GENERAL PUBLIC LICENSE Version 2:
*
* Copyright (C) 2010, <NAME>
* Donders Institute for Donders Institute for Brain, Cognition and Behaviour,
* Centre for Cognitive Neuroimaging, Radboud University Nijmegen,
* Kapittelweg 29, 6525 EN Nijmegen, The Netherlands
*
* @brief    FtBuffClient class definition.
*
*/

//*************************************************************************************************************
//=============================================================================================================
// INCLUDES
//=============================================================================================================

#include "ftbuffclient.h"

//*************************************************************************************************************
//=============================================================================================================
// QT INCLUDES
//=============================================================================================================

#include <QTest>

//*************************************************************************************************************

FtBuffClient::FtBuffClient()
: m_iNumChannels(0)
, m_uiNumSamples(0)
, m_bnewData(false)
, m_pcAddrField("localhost:1972")
, m_bGotParams(false)
, m_bChunkData(false)
{
}

//*************************************************************************************************************

FtBuffClient::FtBuffClient(char* addr)
: m_pcAddrField(addr)
, m_iNumChannels(0)
, m_uiNumSamples(0)
, m_bnewData(false)
, m_bGotParams(false)
, m_bChunkData(false)
{
}

//*************************************************************************************************************

void FtBuffClient::getDataExample() {

    while(true) {

        //Starts connection with m_ftCon_Connector
        while(!this->isConnected()) {
            this->startConnection();
            QTest::qSleep(2000);
        }

        //Handle requests, responses, incoming data after connection is made
        this->idleCall();
        QTest::qSleep(10);
    }
}

//*************************************************************************************************************

bool FtBuffClient::readHeader() {

    //c++ wrapper classes for ftbuffer implementation
    //handles connections, requests, and storage of incoming data
    qDebug() << "Creating request/response handlers...";
    SimpleStorage chunkBuffer;
    headerdef_t header_def;
    FtBufferRequest request;
    FtBufferResponse response;


    //set request command to GET_HDR, other member variables to approprit values
    qDebug() << "Preparing header...";
    request.prepGetHeader();

    qDebug() << "Attempting TCP connection...";
    //Attempt to establish TCP connection
    if (tcprequest(m_ftCon_Connector.getSocket(), request.out(), response.in()) < 0) {
        qDebug() << "Error in communication - check buffer server";
        m_ftCon_Connector.disconnect();
        m_iNumChannels = 0;
        return false;
    }

    //Attempt to retrieve and read header and chunks if applicable
    qDebug() << "Attempting to retrieve header...";
    if (!response.checkGetHeader(header_def, &chunkBuffer)) {
        qDebug() << "Could not read header.";
        return false;
    }

    //Updating channel and sample info
    m_iNumChannels = header_def.nchans;
    m_uiNumSamples = header_def.nsamples;
    m_iSampleFrequency = header_def.fsample;

    m_bGotParams = true;

    qDebug() << "***   Header Data   ***";
    qDebug() << "Channels:" << header_def.nchans;
    qDebug() << "Buffer size:" << header_def.bufsize;
    qDebug() << "Fsample:" << header_def.fsample;
    qDebug() << "NEvents:" << header_def.nevents;
    qDebug() << "NSamples" << header_def.nsamples;
    qDebug() << "Data Type:" << header_def.data_type;
    qDebug() << "***********************";

    //saving header chunks and updating extended header flag
    if (chunkBuffer.size() != 0) {
//        m_ssChunkData.reset(&chunkBuffer);

//        qDebug() << "";
//        qDebug() << "";
//        qDebug() << "Chunk Buffer size:" << chunkBuffer.size();
//        qDebug() << "";
//        qDebug() << "";

//        const ft_chunk_t* chanNames = find_chunk(chunkBuffer.data(), 0, chunkBuffer.size(), FT_CHUNK_CHANNEL_NAMES);
//        const ft_chunk_t* neuromagHead = find_chunk(chunkBuffer.data(), 0, chunkBuffer.size(), FT_CHUNK_NEUROMAG_HEADER);

//        if (chanNames != Q_NULLPTR) {
//            qDebug() << "Channel name chunk found, size" << chanNames->def.size;
//        }

//        if (neuromagHead != Q_NULLPTR) {
//            qDebug () << "Neuromag header found, size" << neuromagHead->def.size;
//        }

        m_bChunkData = true;

//        qDebug() << "";
//        qDebug() << "Set m_bChunkData to true";
//        qDebug() << "";
        qDebug() << "Extended Buffer";
        const ft_chunk_t* chanNames = find_chunk(chunkBuffer.data(), 0, chunkBuffer.size(), FT_CHUNK_CHANNEL_NAMES);
        //const ft_chunk_t* neuromagHead = find_chunk(chunkBuffer.data(), 0, chunkBuffer.size(), FT_CHUNK_NEUROMAG_HEADER);
    //    const ft_chunk_t* neuromagIso = find_chunk(chunkData.data(), 0, chunkData.size(), FT_CHUNK_NEUROMAG_ISOTRAK);
    //    const ft_chunk_t* neuromagHPI = find_chunk(chunkData.data(), 0, chunkData.size(), FT_CHUNK_NEUROMAG_HPIRESULT);
        qDebug() << "parsed header";<commit_msg>added detecting neuromag header in cient, still not passing it up<commit_after>//=============================================================================================================
/**
* @file     ftbuffclient.cpp
* @author   <NAME> <<EMAIL>
*           <NAME> <<EMAIL>>
*           <NAME>
* @version  1.0
* @date     December, 2019
*
* @section  LICENSE
*
* Copyright (C) 2019, <NAME> and <NAME>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that
* the following conditions are met:
*     * Redistributions of source code must retain the above copyright notice, this list of conditions and the
*       following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
*       the following disclaimer in the documentation and/or other materials provided with the distribution.
*     * Neither the name of MNE-CPP authors nor the names of its contributors may be used
*       to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Based on viewer.cc example from ftbuffer reference implementation, under the GNU GENERAL PUBLIC LICENSE Version 2:
*
* Copyright (C) 2010, <NAME>
* Donders Institute for Donders Institute for Brain, Cognition and Behaviour,
* Centre for Cognitive Neuroimaging, Radboud University Nijmegen,
* Kapittelweg 29, 6525 EN Nijmegen, The Netherlands
*
* @brief    FtBuffClient class definition.
*
*/

//*************************************************************************************************************
//=============================================================================================================
// INCLUDES
//=============================================================================================================

#include "ftbuffclient.h"

//*************************************************************************************************************
//=============================================================================================================
// QT INCLUDES
//=============================================================================================================

#include <QTest>

//*************************************************************************************************************

FtBuffClient::FtBuffClient()
: m_iNumChannels(0)
, m_uiNumSamples(0)
, m_bnewData(false)
, m_pcAddrField("localhost:1972")
, m_bGotParams(false)
, m_bChunkData(false)
{
}

//*************************************************************************************************************

FtBuffClient::FtBuffClient(char* addr)
: m_pcAddrField(addr)
, m_iNumChannels(0)
, m_uiNumSamples(0)
, m_bnewData(false)
, m_bGotParams(false)
, m_bChunkData(false)
{
}

//*************************************************************************************************************

void FtBuffClient::getDataExample() {

    while(true) {

        //Starts connection with m_ftCon_Connector
        while(!this->isConnected()) {
            this->startConnection();
            QTest::qSleep(2000);
        }

        //Handle requests, responses, incoming data after connection is made
        this->idleCall();
        QTest::qSleep(10);
    }
}

//*************************************************************************************************************

bool FtBuffClient::readHeader() {

    //c++ wrapper classes for ftbuffer implementation
    //handles connections, requests, and storage of incoming data
    qDebug() << "Creating request/response handlers...";
    SimpleStorage chunkBuffer;
    headerdef_t header_def;
    FtBufferRequest request;
    FtBufferResponse response;


    //set request command to GET_HDR, other member variables to approprit values
    qDebug() << "Preparing header...";
    request.prepGetHeader();

    qDebug() << "Attempting TCP connection...";
    //Attempt to establish TCP connection
    if (tcprequest(m_ftCon_Connector.getSocket(), request.out(), response.in()) < 0) {
        qDebug() << "Error in communication - check buffer server";
        m_ftCon_Connector.disconnect();
        m_iNumChannels = 0;
        return false;
    }

    //Attempt to retrieve and read header and chunks if applicable
    qDebug() << "Attempting to retrieve header...";
    if (!response.checkGetHeader(header_def, &chunkBuffer)) {
        qDebug() << "Could not read header.";
        return false;
    }

    //Updating channel and sample info
    m_iNumChannels = header_def.nchans;
    m_uiNumSamples = header_def.nsamples;
    m_iSampleFrequency = header_def.fsample;

    m_bGotParams = true;

    qDebug() << "***   Header Data   ***";
    qDebug() << "Channels:" << header_def.nchans;
    qDebug() << "Buffer size:" << header_def.bufsize;
    qDebug() << "Fsample:" << header_def.fsample;
    qDebug() << "NEvents:" << header_def.nevents;
    qDebug() << "NSamples" << header_def.nsamples;
    qDebug() << "Data Type:" << header_def.data_type;
    qDebug() << "***********************";

    //saving header chunks and updating extended header flag
    if (chunkBuffer.size() != 0) {
//        m_ssChunkData.reset(&chunkBuffer);

//        qDebug() << "";
//        qDebug() << "";
//        qDebug() << "Chunk Buffer size:" << chunkBuffer.size();
//        qDebug() << "";
//        qDebug() << "";

//        const ft_chunk_t* chanNames = find_chunk(chunkBuffer.data(), 0, chunkBuffer.size(), FT_CHUNK_CHANNEL_NAMES);
//        const ft_chunk_t* neuromagHead = find_chunk(chunkBuffer.data(), 0, chunkBuffer.size(), FT_CHUNK_NEUROMAG_HEADER);

//        if (chanNames != Q_NULLPTR) {
//            qDebug() << "Channel name chunk found, size" << chanNames->def.size;
//        }

//        if (neuromagHead != Q_NULLPTR) {
//            qDebug () << "Neuromag header found, size" << neuromagHead->def.size;
//        }

        m_bChunkData = true;

//        qDebug() << "";
//        qDebug() << "Set m_bChunkData to true";
//        qDebug() << "";
        qDebug() << "Extended Buffer";
        const ft_chunk_t* chanNames = find_chunk(chunkBuffer.data(), 0, chunkBuffer.size(), FT_CHUNK_CHANNEL_NAMES);
        const ft_chunk_t* neuromagHead = find_chunk(chunkBuffer.data(), 0, chunkBuffer.size(), FT_CHUNK_NEUROMAG_HEADER);
    //    const ft_chunk_t* neuromagIso = find_chunk(chunkData.data(), 0, chunkData.size(), FT_CHUNK_NEUROMAG_ISOTRAK);
    //    const ft_chunk_t* neuromagHPI = find_chunk(chunkData.data(), 0, chunkData.size(), FT_CHUNK_NEUROMAG_HPIRESULT);

        if (chanNames->def.type == FT_CHUNK_CHANNEL_NAMES)      qDebug() << "Found channel name chunk";
        if (neuromagHead->def.type ==FT_CHUNK_NEUROMAG_HEADER)  qDebug() << "Found neuromag cheader chunk";

        qDebug() << "parsed header";
{'fraction_non_alphanumeric': 0.26318952851609456, 'fraction_numerical': 0.00587685321223454, 'mean_word_length': 4.188842688842689, 'pattern_counts': {'":': 0, '<': 171, '<?xml version=': 0, '>': 35, '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': '255755', 'n_tokens_mistral': 4162, 'n_tokens_neox': 3815, 'n_words': 1137}