summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: c4330d8f64b58de7f31e763078bfd4cec0ea9643 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * yChat++; Contact: www.yChat.org; Mail@yChat.org
 * Copyright (C) 2003 Paul C. Buetow, Volker Richter 
 * Copyright (C) 2004 Paul C. Buetow
 * -----------------------------------------------------------------
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

#include <unistd.h>
#include <signal.h>
#include "incl.h"

#ifndef NCURSES
#ifdef CLI
#include "cli/cli.h"
#endif
#endif

using namespace std;

map<string,string>*
parse_argc( int argc, char* argv[] )
{
 map<string,string>* start_params = new map<string,string>;

 string s_output = "";

 // Set to 1 if a config option key has to be read ( ./ychat -o key1 value1 -o key2 value2 ... );
 bool b_conf = 0;

 // Will store the key of an additional option value (see also b_conf)
 string s_key;

 for (int i=1; argv[i] != 0; i++)
 {

  if ( !s_key.empty() )
  {
   (*start_params)[s_key] = string(argv[i]);
   s_key.clear();
  }
  else if ( b_conf )
  {
   s_key = string(argv[i]);
   b_conf = 0;
  }

  else
  {
   if ( string(argv[i]).find("v") != string::npos )
    s_output.append(tool::ychat_version()+"\n");
   if ( string(argv[i]).find("h") != string::npos )
    s_output.append( "Usage: ./ychat {h|v}|{o confkey confvalue}\n" );
   if ( string(argv[i]).find("o") != string::npos )
    b_conf = 1;
  }
 }

 if ( s_output.compare("") != 0 )
 {
  cout << s_output;
  delete start_params;
  exit(0);
 }

 return start_params;
}

int
main(int argc, char* argv[])
{
 map<string,string>* p_start_params = parse_argc( argc, argv );

    cout << tool::ychat_version() << endl 
         << DESCRIP << endl
    	 << DESCRI2 << endl
         << CONTACT << endl
         << SEPERAT << endl;

    // ignore SIGPIPE. otherwise the server will shut down with "Broken pipe" if
    // a client unexpected disconnects himself from a SOCK_STREAM.
    signal( SIGPIPE, SIG_IGN );

    // all the static data classes have to be initialized once. otherwise they will
    // contain only empty pointers and the chat server won't work correctly.
    // the order of the initializations is very importand. for example the s_html::init()
    // invokations assumes an initialized s_conf class.
    // begin to draw the ncurses amdin interface in a new pthread.

    // init the dynamic wrapper (is needed to pass all wrapped objects through a single pointer).
    wrap::WRAP = new dynamic_wrap;

    // init the config manager.
    wrap::WRAP->CONF = wrap::CONF = new conf( CONFILE, p_start_params ); 
    delete p_start_params,


   // init the statistic manager.
    wrap::WRAP->STAT = wrap::STAT = new stats; 

    // init the html-template manager.
    wrap::WRAP->HTML = wrap::HTML = new html; 

    // init the system message logd 
    wrap::WRAP->LOGD = wrap::LOGD = new logd( wrap::CONF->get_elem("httpd.logging.systemfile"),
                                              wrap::CONF->get_elem("httpd.logging.systemlines") );
    //<<*
    // init the session manager.
    wrap::WRAP->SMAN = wrap::SMAN = new sman; 
    //*>>
    

    // init the socket manager.
    wrap::WRAP->SOCK = wrap::SOCK = new sock; 

#ifdef NCURSES
    wrap::WRAP->NCUR = wrap::NCUR = new ncur; 	// init the ncurses admin interface.
    wrap::NCUR->run();				// run the thread

    // wait until ncurses interface has been initialized.
    while ( ! wrap::NCUR->is_ready() )
     usleep(1000);
#endif

    //<<*
    // init the chat manager.
    wrap::WRAP->CHAT = wrap::CHAT = new chat; 
    //*>>

    // init the system timer.
    wrap::WRAP->TIMR = wrap::TIMR = new timr; 
    wrap::TIMR->run(); // run the thread

    // init the module-loader manager.
    wrap::WRAP->MODL = wrap::MODL = new modl; 

    //<<*
    // init the garbage collector 
    wrap::WRAP->GCOL = wrap::GCOL = new gcol; 

    // init the data manager. 
#ifdef DATABASE
    wrap::WRAP->DATA = wrap::DATA = new data; 
#endif
    //*>>

#ifndef NCURSES
#ifdef CLI
    cli* p_cli = new cli;
    p_cli->run();
#endif
#endif

    //<<*	
    // Initialize database connection queue
#ifdef DATABASE
    wrap::DATA->initialize_connections();
#endif
    //*>>

    // start the socket manager. this one will listen for incoming http requests and will
    // forward them to the specified routines which will generate a http response.
    wrap::SOCK->start();

    cout << DOWNMSG << endl;

    return 0;
}