00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <ListenSocket.h>
00024 #include <signal.h>
00025 #include "DChatHandler.h"
00026 #include "DChatMinderSocket.h"
00027 #include "DChatMinionSocket.h"
00028 #include "ChatSocket.h"
00029
00030 #define DEB(x)
00031
00032 #define MINDER_HOST "minder.alhem.net"
00033 #define MINDER_PORT 9696
00034 #define LISTEN_PORT 20020
00035
00036 bool quit = false;
00037
00038
00039 void sigint(int s)
00040 {
00041 fprintf(stderr,"received sigint: shutting down\n");
00042 quit = true;
00043 }
00044
00045
00046 void sighup(int s)
00047 {
00048 fprintf(stderr,"received sighup: shutting down\n");
00049 quit = true;
00050 }
00051
00052
00053 void sigpipe(int s)
00054 {
00055 }
00056
00057
00058 void reg_minder(DChatHandler& h,const std::string& cmd)
00059 {
00060 DChatMinderSocket *m = new DChatMinderSocket(h);
00061 m -> Init();
00062
00063 m -> Function(cmd);
00064 m -> SetDeleteByHandler(true);
00065 m -> Open(MINDER_HOST,MINDER_PORT);
00066 h.Add(m);
00067 m -> SetLocalIpPort(Utility::GetLocalAddress(),h.GetLocalPort());
00068 if (!m -> Connecting())
00069 {
00070 m -> SendHello();
00071 }
00072
00073 }
00074
00075
00076 void connect(DChatHandler& h)
00077 {
00078 if (h.Count() < 3)
00079 {
00080 ipaddr_t a;
00081 port_t p;
00082 std::string key;
00083 long host_id;
00084 if (h.GetHost(a,p,key,host_id))
00085 {
00086 MinionSocket *tmp = new DChatMinionSocket(h,key,a,p);
00087 tmp -> Init();
00088 ipaddr_t my_ip;
00089 port_t my_port;
00090 h.GetMyIpPort(my_ip, my_port);
00091 tmp -> SetMyIpPort(my_ip,my_port);
00092 tmp -> SetRemoteHostId(host_id);
00093 {
00094 std::string t;
00095 Utility::l2ip(a,t);
00096 DEB( printf("Connect to: %s:%d\n",t.c_str(),p);)
00097 }
00098 if (tmp -> Open(a,p))
00099 {
00100 tmp -> SetDeleteByHandler(true);
00101 h.Add(tmp);
00102
00103 tmp -> SendHello("Hello");
00104 }
00105 else
00106 if (tmp -> Connecting())
00107 {
00108 tmp -> SetDeleteByHandler(true);
00109
00110 h.Add(tmp);
00111 }
00112 else
00113 {
00114 delete tmp;
00115 }
00116 }
00117 else
00118 {
00119 DEB( printf("h.GetHost() failed?\n");)
00120 }
00121 }
00122 }
00123
00124
00125 int main(int argc,char *argv[])
00126 {
00127 DChatHandler h( "config.xml" );
00128 ListenSocket<DChatMinionSocket> l3(h);
00129 ListenSocket<ChatSocket> l(h);
00130
00131 signal(SIGINT, (__sighandler_t)sigint);
00132 signal(SIGHUP, (__sighandler_t)sighup);
00133 signal(SIGPIPE, (__sighandler_t)sigpipe);
00134
00135
00136 {
00137 port_t p = 4404;
00138 while (l.Bind(p) && p < 4410)
00139 {
00140 p++;
00141 }
00142 printf("Telnet port: %d\n",p);
00143 }
00144 h.Add(&l);
00145
00146 {
00147 port_t port = LISTEN_PORT;
00148 while (l3.Bind(port))
00149 {
00150 port++;
00151 }
00152 h.Add(&l3);
00153 h.SetLocalPort(port);
00154 printf("Minion port: %d\n",port);
00155 }
00156
00157 {
00158 time_t tm = time(NULL);
00159 time_t tc = time(NULL);
00160 reg_minder(h,"Hello");
00161 h.Select(1,0);
00162 while (h.GetCount() && !quit)
00163 {
00164 h.Select(1,0);
00165 time_t t2 = time(NULL);
00166 if (t2 - tc > 60)
00167 {
00168 tc = t2;
00169 connect(h);
00170 }
00171 if (t2 - tm > 15 * 60)
00172 {
00173 tm = t2;
00174 reg_minder(h,"Hello");
00175 }
00176 }
00177 }
00178
00179 {
00180 reg_minder(h,"Goodbye");
00181 h.Select(1,0);
00182 while (h.MinderSockets())
00183 {
00184 h.Select(1,0);
00185 }
00186 }
00187 }
00188
00189