<commit_before>/*
 *   Copyright (C) 2014 Pelagicore AB
 *   All rights reserved.
 */
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>

#include "debug.h"
#include "controllerinterface.h"

ControllerInterface::ControllerInterface(const std::string &gatewayDir):
    m_fifo(0), m_fifoPath(gatewayDir + "/in_fifo")
{
}

ControllerInterface::~ControllerInterface()
{
}

bool ControllerInterface::startApp()
{
    if (m_fifo == 0)
        if (openFifo() == false)
            return false;

    char msg[] = {'1', '\0'};
    int ret = write(m_fifo, msg, sizeof(msg));
    if (ret == -1) {
        log_error("write: %s", strerror(errno));
        return false;
    }

    return true;
}

bool ControllerInterface::shutdown()
{
    if (m_fifo == 0)
        if (openFifo() == false)
            return false;

    char msg[] = {'2', '\0'};
    int ret = write(m_fifo, msg, sizeof(msg));
    if (ret == -1) {
        log_error("write: %s", strerror(errno));
        return false;
    }

    return true;
}

bool ControllerInterface::setEnvironmentVariable(const std::string &variable,
                                                 const std::string &value)
{
    if (m_fifo == 0)
        if (openFifo() == false)
            return false;

    std::string command = "3 " + variable + " " + value;
    int ret = write(m_fifo, command.c_str(), command.size() + 1);
    if (ret == -1) {
        log_error("write: %s", strerror(errno));
        return false;
    }

    return true;
}

bool ControllerInterface::systemCall(const std::string &cmd)
{
    if (m_fifo == 0)
        if (openFifo() == false)
            return false;

    std::string command = "4 " + cmd;
    int ret = write(m_fifo, command.c_str(), command.size() + 1);
    if (ret == -1) {
        log_error("write: %s", strerror(errno));
        return false;
    }

    return true;
}

bool ControllerInterface::openFifo()
{
    while (access (m_fifoPath.c_str(), F_OK) == -1) {
        log_error ("FIFO (%s) not available.. Spinning ..", m_fifoPath.c_str());
    }

    m_fifo = open(m_fifoPath.c_str(), O_WRONLY);
    if (m_fifo == -1) {
        log_error("open: %s erro=%s", m_fifoPath.c_str(), strerror(errno));
        return false;
    }

    return true;
}<commit_msg>Don't loop indefinitely when not finding fifo in container<commit_after>/*
 *   Copyright (C) 2014 Pelagicore AB
 *   All rights reserved.
 */
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>

#include "debug.h"
#include "controllerinterface.h"

ControllerInterface::ControllerInterface(const std::string &gatewayDir):
    m_fifo(0), m_fifoPath(gatewayDir + "/in_fifo")
{
}

ControllerInterface::~ControllerInterface()
{
}

bool ControllerInterface::startApp()
{
    if (m_fifo == 0)
        if (openFifo() == false)
            return false;

    char msg[] = {'1', '\0'};
    int ret = write(m_fifo, msg, sizeof(msg));
    if (ret == -1) {
        log_error("write: %s", strerror(errno));
        return false;
    }

    return true;
}

bool ControllerInterface::shutdown()
{
    if (m_fifo == 0)
        if (openFifo() == false)
            return false;

    char msg[] = {'2', '\0'};
    int ret = write(m_fifo, msg, sizeof(msg));
    if (ret == -1) {
        log_error("write: %s", strerror(errno));
        return false;
    }

    return true;
}

bool ControllerInterface::setEnvironmentVariable(const std::string &variable,
                                                 const std::string &value)
{
    if (m_fifo == 0)
        if (openFifo() == false)
            return false;

    std::string command = "3 " + variable + " " + value;
    int ret = write(m_fifo, command.c_str(), command.size() + 1);
    if (ret == -1) {
        log_error("write: %s", strerror(errno));
        return false;
    }

    return true;
}

bool ControllerInterface::systemCall(const std::string &cmd)
{
    if (m_fifo == 0)
        if (openFifo() == false)
            return false;

    std::string command = "4 " + cmd;
    int ret = write(m_fifo, command.c_str(), command.size() + 1);
    if (ret == -1) {
        log_error("write: %s", strerror(errno));
        return false;
    }

    return true;
}

bool ControllerInterface::openFifo()
{
    int max_spin = 1000; // Milliseconds
    while (access (m_fifoPath.c_str(), F_OK) == -1 && max_spin-- > 0) {
        log_error ("FIFO (%s) not available.. Spinning ..", m_fifoPath.c_str());
        usleep(1000);
    }

    if (max_spin == -1) {
        log_debug("Gave up trying to find %s", m_fifoPath.c_str());
    }


    m_fifo = open(m_fifoPath.c_str(), O_WRONLY);
    if (m_fifo == -1) {
        log_error("open: %s erro=%s", m_fifoPath.c_str(), strerror(errno));
        return false;
    }

    return true;
}
{'fraction_non_alphanumeric': 0.16077441077441076, 'fraction_numerical': 0.011784511784511785, 'mean_word_length': 2.518134715025907, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1677806', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1623, 'n_words': 391}