Logo
~Sockets~
~Examples~
~Contact~


dchat.cpp File Reference

#include <ListenSocket.h>
#include <signal.h>
#include "DChatHandler.h"
#include "DChatMinderSocket.h"
#include "DChatMinionSocket.h"
#include "ChatSocket.h"

Include dependency graph for dchat.cpp:

Go to the source code of this file.


Defines

#define DEB(x)
#define MINDER_HOST   "minder.alhem.net"
#define MINDER_PORT   9696
#define LISTEN_PORT   20020

Functions

void sigint (int s)
void sighup (int s)
void sigpipe (int s)
void reg_minder (DChatHandler &h, const std::string &cmd)
void connect (DChatHandler &h)
int main (int argc, char *argv[])

Variables

bool quit = false

Define Documentation

#define DEB (  ) 

File ......... dchat.cpp Published .... 2004-04-19 Author ....... grymse@alhem.net

Definition at line 30 of file dchat.cpp.

Referenced by connect().

#define LISTEN_PORT   20020

Definition at line 34 of file dchat.cpp.

Referenced by main().

#define MINDER_HOST   "minder.alhem.net"

Definition at line 32 of file dchat.cpp.

Referenced by reg_minder().

#define MINDER_PORT   9696

Definition at line 33 of file dchat.cpp.

Referenced by reg_minder().


Function Documentation

void connect ( DChatHandler h  ) 

Definition at line 76 of file dchat.cpp.

References DEB.

Referenced by main().

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 }

int main ( int  argc,
char *  argv[] 
)

Definition at line 125 of file dchat.cpp.

References connect(), LISTEN_PORT, quit, reg_minder(), sighup(), sigint(), and sigpipe().

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 }

void reg_minder ( DChatHandler h,
const std::string &  cmd 
)

Definition at line 58 of file dchat.cpp.

References MINDER_HOST, and MINDER_PORT.

Referenced by main().

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 }

void sighup ( int  s  ) 

Definition at line 46 of file dchat.cpp.

References quit.

Referenced by main().

00047 {
00048         fprintf(stderr,"received sighup: shutting down\n");
00049         quit = true;
00050 }

void sigint ( int  s  ) 

Definition at line 39 of file dchat.cpp.

References quit.

Referenced by main().

00040 {
00041         fprintf(stderr,"received sigint: shutting down\n");
00042         quit = true;
00043 }

void sigpipe ( int  s  ) 

Definition at line 53 of file dchat.cpp.

Referenced by main().

00054 {
00055 }


Variable Documentation

bool quit = false

Definition at line 36 of file dchat.cpp.

Referenced by main(), sighup(), and sigint().

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