Logo
~Sockets~
~Examples~
~Contact~


dchat.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 #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) /* save+quit */
00040 {
00041         fprintf(stderr,"received sigint: shutting down\n");
00042         quit = true;
00043 }
00044 
00045 
00046 void sighup(int s) /* quit */
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); // Hello / Goodbye
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()) // connected
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                                 // check OnConnect
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         // telnet
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         // minion
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         // main loop
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         // shutdown
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 
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4