/**
 * core.cpp (2007-03-04)
 *
 * -- CABAL -- core
 *
 * Copyright (c) 2007, NOD Team. 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 the NOD Team 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.
 */

#include "core.h"

Connection *C;

Core::Core() : pid (FILE_PID), cmdc (CMDC)
{
	cout << "CORE: initializing...\n";

	debug = true;

	now = gettime();

	for (int e = 0; e < MAX_SERVERS; e++)
		connections[e] = 0;

	servers = 0;
}

Core::~Core()
{
	for (int e = 0; e < servers; e++)
		delete connections[e];

	servers = 0;

	remove (pid);

	cout << "CORE: shutdown complete\n";
}

void Core::cmdParse (int argc, char *argv[])
{
	for (int e = 1; e < argc; e++)
	{
		while (1)
		{
			if (!strcmp (argv[e], "-c") || !strcmp (argv[e], "--config"))
			{
				if (!argv[++e])
					shut ("missing configuration file");

				cfg = argv[e];
				break;
			}
			else if (!strcmp (argv[e], "-d") || !strcmp (argv[e], "--daemon"))
			{
				debug = 0;
				break;
			}
			else
				shut ("unknown option detected: " + (string)argv[e]);
		}
	}
}

void Core::cfgParse()
{
	servers = -1;

	C = 0;

	ifstream config (cfg, ios::in);

	if (!config)
		shut ("unable to open configuration file");
	else
	{
		cout << "CORE: parsing configuration file: " << cfg << endl;

		if (servers + 1 < MAX_SERVERS)
		{
			connections[++servers] = new Connection();
			C = connections[servers];
		}
		else
			cout << "Error: maximum connections exceed\n";
// TEST
		C->hosts = C->hostAdd (C->hosts, "slz.sytes.net", 6667, 0);
// TEST
	}

	servers++;

	config.close();
}

void Core::pidCheck()
{
	ifstream procid (pid, ios::in);

	if (procid)
	{
		pid_t pids;
		procid >> pids;
		kill (pids, SIGCHLD);

		if (errno != ESRCH)
			shut ("another Core of CABAL is running");
	}

	procid.close();
}

void Core::pidWrite()
{
	ofstream procid (pid, ios::out);

	if (procid)
		procid << getpid() << endl;
	else
	{
		cout << "Error: unable to write PID file\n";
		exit (1);
	}

	procid.close();
}

void Core::work()
{
	while (1)
	{
		now = gettime();

		usleep (100);

		for (int e = 0; e < servers; e++)
			connections[e]->establish();
	}
}
