//
//  cysfnodedir.cpp
//  xlxd
//
//  Created by Jean-Luc Deltombe (LX3JL) on 08/10/2019.
//  Copyright © 2019 Jean-Luc Deltombe (LX3JL). All rights reserved.
//
// ----------------------------------------------------------------------------
//    This file is part of xlxd.
//
//    xlxd 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 3 of the License, or
//    (at your option) any later version.
//
//    xlxd 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------

#include <string.h>
#include "main.h"
#include "creflector.h"
#include "cysfnodedir.h"


////////////////////////////////////////////////////////////////////////////////////////
// constructor & destructor

CYsfNodeDir::CYsfNodeDir()
{
	keep_running = true;
}

CYsfNodeDir::~CYsfNodeDir()
{
	// kill threads
	Close();
}


////////////////////////////////////////////////////////////////////////////////////////
// init & close

bool CYsfNodeDir::Init(void)
{
	// load content
	Reload();

	// reset run flag
	keep_running = true;

	// start  thread;
	m_Future = std::async(std::launch::async, &CYsfNodeDir::Thread, this);

	return true;
}

void CYsfNodeDir::Close(void)
{
	keep_running = false;
	if ( m_Future.valid() )
	{
		m_Future.get();
	}
}

////////////////////////////////////////////////////////////////////////////////////////
// thread

void CYsfNodeDir::Thread()
{
	while (keep_running)
	{
		// Wait YSFNODEDB_REFRESH_RATE minutes
		for (int i=0; i<30*YSFNODEDB_REFRESH_RATE && keep_running; i++)
			CTimePoint::TaskSleepFor(2000);

		// have lists files changed ?
		if ( NeedReload() )
		{
			Reload();
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////
// Reload

bool CYsfNodeDir::Reload(void)
{
	CBuffer buffer;
	bool ok = false;

	if ( LoadContent(&buffer) )
	{
		Lock();
		{
			ok = RefreshContent(buffer);
		}
		Unlock();
	}
	return ok;
}

////////////////////////////////////////////////////////////////////////////////////////
// find

bool CYsfNodeDir::FindFrequencies(const CCallsign &callsign, uint32 *txfreq, uint32 *rxfreq)
{
	auto found = find(callsign);
	if ( found != end() )
	{
		*txfreq = found->second.GetTxFrequency();
		*rxfreq = found->second.GetRxFrequency();
		return true;
	}
	else
	{
		*txfreq = YSF_DEFAULT_NODE_TX_FREQ;
		*rxfreq = YSF_DEFAULT_NODE_RX_FREQ;
		return false;
	}
}
