comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:bafff9de2a76
1 /** core.cpp (2007-03-04)
2 *
3 * -- CABAL -- core module
4 *
5 * Copyright (c) 2007 Vlad Glagolev <enqlave@gmail.com>
6 * All rights reserved.
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21 #include "core.h"
22
23 Connection *C;
24
25 Core::Core()
26 :
27 pid(PID),
28 commander(COMMANDER)
29 {
30 cout << "CORE: initializing...\n";
31
32 debug = true;
33
34 now = gettime();
35
36 for (int e = 0; e < MAX_SERVERS; e++)
37 connections[e] = 0;
38
39 servers = 0;
40 }
41
42 Core::~Core()
43 {
44 for (int e = 0; e < servers; e++)
45 delete connections[e];
46
47 servers = 0;
48
49 remove (pid);
50
51 cout << "CORE: shutdown complete\n";
52 }
53
54 void Core::commandParse(int argc, char *argv[])
55 {
56 for (int e = 1; e < argc; e++) {
57 while (1) {
58 if (!strcmp(argv[e], "-c") || !strcmp(argv[e], "--config")) {
59 if (!argv[++e])
60 shut ("missing configuration file");
61
62 cfg = argv[e];
63 break;
64 } else if (!strcmp(argv[e], "-d") || !strcmp(argv[e], "--daemon")) {
65 debug = false;
66 break;
67 } else
68 shut ("unknown option detected: " + (string)argv[e]);
69 }
70 }
71 }
72
73 void Core::configParse()
74 {
75 servers = -1;
76
77 C = 0;
78
79 ifstream config(cfg, ios::in);
80
81 if (!config)
82 shut ("unable to open configuration file");
83 else {
84 cout << "CORE: parsing configuration file: " << cfg << endl;
85
86 if (servers + 1 < MAX_SERVERS) {
87 connections[++servers] = new Connection();
88 C = connections[servers];
89 }
90 else
91 cout << "Error: maximum connections exceed\n";
92 // TEST
93 C->hosts = C->hostAdd (C->hosts, "irc.enqlave.net", 6667, 0);
94 // TEST
95 }
96
97 servers++;
98
99 config.close();
100 }
101
102 void Core::pidCheck()
103 {
104 ifstream procid(pid, ios::in);
105
106 if (procid) {
107 pid_t pids;
108 procid >> pids;
109 kill(pids, SIGCHLD);
110
111 if (errno != ESRCH)
112 shut ("another Core of CABAL is running");
113 }
114
115 procid.close();
116 }
117
118 void Core::pidWrite()
119 {
120 ofstream procid(pid, ios::out);
121
122 if (procid)
123 procid << getpid() << endl;
124 else {
125 cout << "Error: unable to write PID file\n";
126 exit (1);
127 }
128
129 procid.close();
130 }
131
132 void Core::work()
133 {
134 while (1) {
135 now = gettime();
136
137 usleep (100);
138
139 for (int e = 0; e < servers; e++)
140 connections[e]->establish();
141 }
142 }