<commit_before>/*
* kinetic-c
* Copyright (C) 2014 Seagate Technology.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

#include "kinetic_types_internal.h"
#include "kinetic_client.h"
#include "kinetic_session.h"
#include "kinetic_controller.h"
#include "kinetic_operation.h"
#include "kinetic_logger.h"
#include "kinetic_pdu.h"
#include "kinetic_memory.h"
#include <stdlib.h>
#include <sys/time.h>

KineticClient * KineticClient_Init(const char* log_file, int log_level)
{
    KineticLogger_Init(log_file, log_level);
    KineticClient * client = KineticCalloc(1, sizeof(*client));
    if (client == NULL) { return NULL; }
    bool success = KineticPDU_InitBus(1, client);
    if (!success)
    {
        KineticFree(client);
        return NULL;
    }
    return client;
}

void KineticClient_Shutdown(KineticClient * const client)
{
    KineticPDU_DeinitBus(client);
    KineticFree(client);
    KineticLogger_Close();
}

KineticStatus KineticClient_CreateConnection(KineticSession* const session, KineticClient * const client)
{
    if (session == NULL) {
        LOG0("KineticSession is NULL!");
        return KINETIC_STATUS_SESSION_EMPTY;
    }

    if (strlen(session->config.host) == 0) {
        LOG0("Host is empty!");
        return KINETIC_STATUS_HOST_EMPTY;
    }

    if (session->config.hmacKey.len < 1 || session->config.hmacKey.data == NULL) {
        LOG0("HMAC key is NULL or empty!");
        return KINETIC_STATUS_HMAC_EMPTY;
    }

    KineticSession_Create(session, client);
    if (session->connection == NULL) {
        LOG0("Failed to create connection instance!");
        return KINETIC_STATUS_CONNECTION_ERROR;
    }

    // Create the connection
    KineticStatus status = KineticSession_Connect(session);
    if (status != KINETIC_STATUS_SUCCESS) {
        LOGF0("Failed creating connection to %s:%d", session->config.host, session->config.port);
        KineticSession_Destroy(session);
        session->connection = NULL;
        return status;
    }

    return status;
}

KineticStatus KineticClient_DestroyConnection(KineticSession* const session)
{
    if (session == NULL) {
        LOG0("KineticSession is NULL!");
        return KINETIC_STATUS_SESSION_INVALID;<commit_msg>Check result of KineticSession_Create, don't rely on session->connection.<commit_after>/*
* kinetic-c
* Copyright (C) 2014 Seagate Technology.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

#include "kinetic_types_internal.h"
#include "kinetic_client.h"
#include "kinetic_session.h"
#include "kinetic_controller.h"
#include "kinetic_operation.h"
#include "kinetic_logger.h"
#include "kinetic_pdu.h"
#include "kinetic_memory.h"
#include <stdlib.h>
#include <sys/time.h>

KineticClient * KineticClient_Init(const char* log_file, int log_level)
{
    KineticLogger_Init(log_file, log_level);
    KineticClient * client = KineticCalloc(1, sizeof(*client));
    if (client == NULL) { return NULL; }
    bool success = KineticPDU_InitBus(1, client);
    if (!success)
    {
        KineticFree(client);
        return NULL;
    }
    return client;
}

void KineticClient_Shutdown(KineticClient * const client)
{
    KineticPDU_DeinitBus(client);
    KineticFree(client);
    KineticLogger_Close();
}

KineticStatus KineticClient_CreateConnection(KineticSession* const session, KineticClient * const client)
{
    if (session == NULL) {
        LOG0("KineticSession is NULL!");
        return KINETIC_STATUS_SESSION_EMPTY;
    }

    if (strlen(session->config.host) == 0) {
        LOG0("Host is empty!");
        return KINETIC_STATUS_HOST_EMPTY;
    }

    if (session->config.hmacKey.len < 1 || session->config.hmacKey.data == NULL) {
        LOG0("HMAC key is NULL or empty!");
        return KINETIC_STATUS_HMAC_EMPTY;
    }

    KineticStatus res = KineticSession_Create(session, client);
    if (res != KINETIC_STATUS_SUCCESS) {
        LOGF0("Failed to create connection instance: %s",
            Kinetic_GetStatusDescription(res));
        return res;
    }

    if (session->connection == NULL) {
        LOG0("Failed to create connection instance!");
        return KINETIC_STATUS_CONNECTION_ERROR;
    }

    // Create the connection
    KineticStatus status = KineticSession_Connect(session);
    if (status != KINETIC_STATUS_SUCCESS) {
        LOGF0("Failed creating connection to %s:%d", session->config.host, session->config.port);
        KineticSession_Destroy(session);
        session->connection = NULL;
        return status;
    }

    return status;
}

KineticStatus KineticClient_DestroyConnection(KineticSession* const session)
{
    if (session == NULL) {
        LOG0("KineticSession is NULL!");
        return KINETIC_STATUS_SESSION_INVALID;
{'fraction_non_alphanumeric': 0.09737017310252996, 'fraction_numerical': 0.008821571238348869, 'mean_word_length': 3.7804295942720763, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 22, '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': '780237', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1777, 'n_words': 536}