/*!
 * domo-iot.cpp
 *
 * Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 * 
 * @Author:  NADAL Jean-Baptiste
 * @Date: 13/11/2019
 *
 */

// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com

/*-------------------------------- INCLUDES ---------------------------------*/

#include "domo-server.h"

#define kDocumentRoot "."
#define kPort 8081

/*!  ----------------------------------------------------------------------------
 *  @fn DomoServer
 *
 *  @brief Constructor of the Domo Server Object.
 */
DomoServer::DomoServer(void) : m_evt_loop(NULL)
{
}

/*!  ----------------------------------------------------------------------------
 *  @fn ~DomoServer
 *
 *  @brief Destructor of the Domo Server Object.
 */
DomoServer::~DomoServer(void)
{
}

/*!  ----------------------------------------------------------------------------
 *  @fn setup
 *
 *  @brief setup the Domo Server.
 */
bool DomoServer::setup(void)
{
    // Create the event loop.
    m_evt_loop = uv_default_loop();
    if (m_evt_loop == NULL)
    {
        return false;
    }

    if (m_broker.setup(m_evt_loop))
    {
        fprintf(stderr, "Failed to setup the Nats Broker.\n");
        return false;
    }

    m_server.setup(kDocumentRoot, kPort, m_evt_loop);

    // TODO
    m_broker.connect();

    return true;
}

/*!  ----------------------------------------------------------------------------
 *  @fn terminate
 *
 *  @brief terminate the Domo Server.
 */
void DomoServer::terminate(void)
{
    m_broker.terminate();

    if (m_evt_loop != NULL)
        uv_loop_close(m_evt_loop);
}

/*!  ----------------------------------------------------------------------------
 *  @fn loop
 *
 *  @brief main loop function.
 */
int DomoServer::loop(void)
{
    return uv_run(m_evt_loop, UV_RUN_DEFAULT);;
}
