Google
Web alhem.net
Main Page | Alphabetical List | Class List | File List | Class Members | File Members

minder.cpp

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2004  Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 */
00023 #ifdef _WIN32
00024 #pragma warning(disable:4786)
00025 #endif
00026 #include <stdio.h>
00027 #include <string>
00028 #include <vector>
00029 #include <map>
00030 #include <signal.h>
00031 #include <socket_include.h>
00032 
00033 #include <ListenSocket.h>
00034 #include <SocketHandler.h>
00035 #include "config.h"
00036 #include "MyTcpSocket.h"
00037 #include "RedirWwwSocket.h"
00038 #include "MyHandler.h"
00039 
00040 
00041 #ifdef _DEBUG
00042 #define DEB(x) x
00043 #else
00044 #define DEB(x) 
00045 #endif
00046 
00047 
00048 // this little program keeps tabs on all hosts in a game
00049 // adjustable keep-alive frequency
00050 
00051 #ifdef WIN32
00052 WSADATA libmibWSAdata;
00053 class X
00054 {
00055 public:
00056         X() { SocketStartup(); }
00057         ~X() { SocketCleanup(); }
00058 };
00059 X x;
00060 #endif
00061 
00062 static  int quit = 0;
00063 
00064 
00065 #ifndef _WIN32
00066 void sigint(int s) /* save+quit */
00067 {
00068 DEB(    printf("sigint\n");)
00069         quit++;
00070 }
00071 
00072 void sighup(int s) /* quit */
00073 {
00074 DEB(    printf("sighup\n");)
00075         quit++;
00076 }
00077 
00078 void sigusr1(int s) /* save */
00079 {
00080 }
00081 
00082 void sigusr2(int s) /* reset all */
00083 {
00084 }
00085 
00086 void sigpipe(int s)
00087 {
00088 }
00089 
00090 void siginit(void)
00091 {
00092         signal(SIGINT, (__sighandler_t)sigint);
00093         signal(SIGHUP, (__sighandler_t)sighup);
00094         signal(SIGUSR1, (__sighandler_t)sigusr1);
00095         signal(SIGUSR2, (__sighandler_t)sigusr2);
00096         signal(SIGPIPE, (__sighandler_t)sigpipe);
00097 }
00098 #endif
00099 
00100 
00101 int main(int argc,char *argv[])
00102 {
00103         // open listen socket
00104         // commands:
00105         //  init (state = startup)
00106         //  online (state = online)
00107         //  keepalive
00108         //  exit
00109         //  route (traceroute from host to minder)
00110         // sent as:
00111         //  http://minder.alhem.net:9696/init?ip&port
00112         //  http://minder.alhem.net:9696/route?10.0.0.1&10.0.0.2&...
00113         time_t t = time(NULL);
00114         MyHandler h;
00115         ListenSocket<MyTcpSocket> l(h);
00116         ListenSocket<RedirWwwSocket> l2(h);
00117         port_t redir_port = 0;
00118 
00119 #ifndef _WIN32
00120         siginit();
00121 #endif
00122 
00123         // default values
00124         config["minder_port"] = "9696";
00125         config["next_host_id"] = "1";
00126         // config["redir_port"] = "9666";
00127 
00128         parse_config(argc,argv,"minder.cfg");
00129 //      config["progname"] = argv[0];
00130 
00131         if (l.Bind(atoi(config["minder_port"].c_str()), 20) != 0)
00132         {
00133 DEB(            printf("Bind() failed\n");)
00134                 exit(-1);
00135         }
00136         redir_port = atoi(config["redir_port"].c_str());
00137         if (redir_port > 0)
00138         {
00139                 if (l2.Bind(redir_port, 20))
00140                 {
00141 DEB(                    perror("Bind() failed");)
00142                         exit(-2);
00143                 }
00144         }
00145 
00146         h.Add(&l);
00147         if (redir_port > 0)
00148         {
00149                 h.Add(&l2);
00150         }
00151 
00152         while (!quit)
00153         {
00154                 h.Select(1,0);
00155                 if (time(NULL) - t > 60)
00156                 {
00157                         h.CheckTimeout();
00158                 }
00159         }
00160 
00161         return 0;
00162 }
00163 
00164 

Generated on Tue Oct 3 23:47:05 2006 for The Minder by doxygen 1.3.6