Mercurial > cabal
diff sources/core.cpp @ 0:bafff9de2a76
Initial commit since summer'07
| author | Vlad Glagolev <enqlave@gmail.com> |
|---|---|
| date | Sun, 20 Jan 2008 19:25:25 +0300 |
| parents | |
| children | 19227b0b7cc1 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sources/core.cpp Sun Jan 20 19:25:25 2008 +0300 @@ -0,0 +1,142 @@ +/** core.cpp (2007-03-04) + * + * -- CABAL -- core module + * + * Copyright (c) 2007 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.h" + +Connection *C; + +Core::Core() + : + pid(PID), + commander(COMMANDER) +{ + 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::commandParse(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 = false; + break; + } else + shut ("unknown option detected: " + (string)argv[e]); + } + } +} + +void Core::configParse() +{ + 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, "irc.enqlave.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(); + } +}
