view sources/core.cpp @ 2:19227b0b7cc1

.h => .hpp for the headers added year 2008 to copyright notes renamed some functions for better meaning prepared parser module for rewriting added showHelp() function some code clean up
author Vlad Glagolev <enqlave@gmail.com>
date Mon, 21 Jan 2008 01:14:10 +0300
parents bafff9de2a76
children a7051ac7118b
line wrap: on
line source

/** core.cpp (2007-03-04)
 *
 * -- CABAL -- core module
 *
 * Copyright (c) 2007-2008 Vlad Glagolev <enqlave@gmail.com>
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "core.hpp"

Connection *C;

Core::Core():
	commander(COMMANDER)
{
	pid = PID;

	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::parseCommandLine(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");

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

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

	C = false;

	ifstream sysconfig(config, ios::in);

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

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

	servers++;

	sysconfig.close();
}

void Core::checkPID()
{
	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::writePID()
{
	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();
	}
}