Logo
~Sockets~
~Examples~
~Contact~


main.cpp

Go to the documentation of this file.
00001 #include "ServidorSAM.h"
00002 #include <ListenSocket.h>
00003 #include <iostream>
00004 #include <sstream>
00005 #include <string>
00006 #include <stdio.h>
00007 #include <pthread.h>
00008 #include <time.h>
00009 
00010 using namespace std;
00011 
00012 void Comando(void);
00013 void* SelectBucle(void*);
00014 
00015 MyEventHandler h;
00016 ListenSocket<ServidorSAM> l(h);
00017 
00018 int main(int argc, char* argv[])
00019 {
00020         int puerto=6667;
00021         string pass="123",ip="127.0.0.1";
00022         ServidorSAM::SetEstado("ACTIVO");
00023 
00024         if(argc>1)
00025         {
00026                 for(int i=1;i<argc;i++)
00027                 {
00028                         string opc(argv[i]);                            
00029                         if(opc=="-p")                                                                   
00030                                 puerto=atoi(argv[++i]);
00031                         else if(opc=="-s")
00032                                 pass=string(argv[++i]);
00033                         else if(opc=="-i")
00034                                 ip=string(argv[++i]);
00035                         else if(opc=="-e")
00036                         {
00037                                 if(string(argv[i+1])=="ACTIVO" || string(argv[i+1])=="PARADO")
00038                                         ServidorSAM::SetEstado(string(argv[++i]));
00039                         }
00040                         else if(opc=="-a")
00041                         {                                       
00042                                 cout << endl << "Opciones del programa:" << endl;
00043                                 cout << "-p para seleccionar el puerto donde arrancar" << endl;
00044                                 cout << "-i para seleccionar la direccion IP desde la que escuchar clientes" << endl;
00045                                 cout << "-s para seleccionar el password de administracion" << endl;
00046                                 cout << "-e para arrancar con un estado determinado [ACTIVO|PARADO]" << endl <<endl;
00047                                 exit(EXIT_SUCCESS);                                                     
00048                         }
00049                 }
00050         }
00051 
00052         ServidorSAM::SetPuerto(puerto);
00053         ServidorSAM::SetPass(pass);
00054         ServidorSAM::SetIpEscucha(ip);
00055                                 
00056         if (l.Bind(ServidorSAM::GetIpEscucha(),ServidorSAM::GetPuerto(),30))
00057         {
00058                 cout << "Error al abrir la escucha principal!!" << endl;
00059                 cout << "Puerto: " << ServidorSAM::GetPuerto() << endl;
00060                 exit(EXIT_FAILURE);
00061         }
00062 
00063         h.Add(&l);      
00064         h.Select(1,0);
00065         Comando();
00066 
00067         return EXIT_SUCCESS;
00068 }
00069 
00070 void* SelectBucle(void*)
00071 {               
00072         while(1)
00073         {
00074                 h.Select(1,0);
00075                 h.CheckEvents();
00076         }
00077         return 0;
00078 }
00079 
00080 void Comando(void)
00081 {
00082         string cmd;
00083         pthread_t slcthread;
00084         bool q=true;
00085 
00086         if(ServidorSAM::GetEstado()=="ACTIVO")
00087         {
00088                 int p=pthread_create(&slcthread,NULL,SelectBucle,NULL);
00089                 if(p)
00090                 {
00091                         cout << "Error al crear el hilo de eventos" << endl;
00092                         exit(EXIT_FAILURE);
00093                 }               
00094         }
00095          
00096         while(q)
00097         {
00098                 cout << "Servidor " << ServidorSAM::GetEstado() << ">";
00099                 getline(cin,cmd);
00100                 cmd=cmd.substr(0,20);
00101                 if(cmd=="quit" || cmd=="q")
00102                 {
00103                         l.Close();
00104                         pthread_cancel(slcthread);                        
00105                         //h.RemoveAllSockets();         
00106                         q=false;
00107                 }
00108                 else 
00109                 if(cmd=="estadistica" || cmd=="est")
00110                 {
00111                         list<string> b=ServidorSAM::GetBaneados();
00112                         cout << "\nServidor escuchando en " << ServidorSAM::GetIpEscucha() << endl;
00113                         cout << "Utilizando el puerto " << ServidorSAM::GetPuerto() << endl;
00114                         cout << "Estado " << ServidorSAM::GetEstado() << endl;
00115                         cout << "Usuarios activos:" << ServidorSAM::GetClientes() << endl;
00116                         cout << "Canales activos:" << ServidorSAM::GetCanales() << endl; 
00117                         cout << "Maximos usuarios permitidos por IP real:" << MAX_CON_IP << endl;
00118                         cout << "Password de administracion:" << ServidorSAM::GetPass() << endl << endl;
00119                         if(b.size()>0)
00120                         {
00121                                 cout << "Ips baneadas:";
00122                                 for(list<string>::iterator i=b.begin(); i!=b.end(); i++)
00123                                         cout << *i << endl;
00124                         }
00125                 }else
00126                 if(cmd=="parar" || cmd=="p")
00127                 {       
00128                     if(ServidorSAM::GetEstado()=="ACTIVO")
00129                     {   
00130                         ServidorSAM::SetEstado("PARADO");
00131                         pthread_cancel(slcthread);      
00132                         l.Close();                      
00133                     }
00134                 }else
00135                 if(cmd=="activar" || cmd=="a")
00136                 {
00137                     if(ServidorSAM::GetEstado()=="PARADO")
00138                     {                   
00139                         if (l.Bind(ServidorSAM::GetIpEscucha(),ServidorSAM::GetPuerto(),30))
00140                         {
00141                                 cout << "Error al abrir la escucha principal!!" << endl;
00142                                 cout << "Puerto: " << ServidorSAM::GetPuerto() << endl;
00143                                 exit(EXIT_FAILURE);
00144                         }                       
00145                         int p=pthread_create(&slcthread,NULL,SelectBucle,NULL);
00146                         if(p)
00147                         {
00148                                 cout << "Error al crear el hilo de eventos" << endl;
00149                                 exit(EXIT_FAILURE);
00150                         }                       
00151                         ServidorSAM::SetEstado("ACTIVO");
00152                     }
00153                 }else
00154                 if(cmd=="puerto" || cmd=="pu")
00155                 {               
00156                         cout << "Introduce el nuevo puerto:";
00157                         getline(cin,cmd);
00158                         if(cmd.length()>0)                      
00159                                 ServidorSAM::SetPuerto(atoi(cmd.c_str()));                      
00160                 }else
00161                 if(cmd=="pass" || cmd=="pa")
00162                 {
00163                         cout << "Introduce el nuevo password del administrador:";
00164                         getline(cin,cmd);
00165                         if(cmd.length()>0)
00166                                 ServidorSAM::SetPass(cmd);
00167                 }
00168                 else
00169                 if(cmd=="ip" || cmd=="i")
00170                 {
00171                         cout << "Introduce la direccion IP de escucha:";
00172                         getline(cin,cmd);
00173                         if(cmd.length()>0)
00174                                 ServidorSAM::SetIpEscucha(cmd);
00175                 }else
00176                 if(cmd=="qb" || cmd=="desbanear" || cmd=="quitar ban")
00177                 {
00178                         cout << "Introduce la ip para quitar el ban:";
00179                         getline(cin,cmd);
00180                         if(cmd.length()>0)
00181                                 ServidorSAM::QuitarBan(cmd);
00182                 }else
00183                 if(cmd=="pb" || cmd=="banear" || cmd=="poner ban")
00184                 {
00185                         cout << "Introduce la ip a banear:";
00186                         getline(cin,cmd);
00187                         if(cmd.length()>0)
00188                                 ServidorSAM::PonerBan(cmd);
00189                 }
00190                 else if(cmd=="ayuda" || cmd=="h")
00191                 {
00192                                 cout << endl << "Comandos disponibles:" << endl << endl;
00193                                 cout << "(q)uit para salir del programa" << endl;
00194                                 cout << "(est)adistica para ver las estadisticas" << endl;
00195                                 cout << "(p)arar para detener el sistema" << endl;
00196                                 cout << "(a)activar para iniciar el servidor" << endl;
00197                                 cout << "(pu)erto para modificar el puerto de escucha" << endl;
00198                                 cout << "(pa)ss para modificar el password de administracion" << endl;
00199                                 cout << "(i)p para modificar la direccion de escucha de clientes" << endl;
00200                                 cout << "qb | desbanear | quitar ban para quitar un ban de usuario" << endl;
00201                                 cout << "pb | banear | poner ban para poner un ban a un usuario" << endl << endl;                               
00202                 }
00203                 else if(cmd.length()>0)
00204                         cout << "Comando desconocido!!" << endl;
00205         }       
00206 }
00207 
00208 
00209 
00210 
Page, code, and content Copyright (C) 2005 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4