Logo
~Sockets~
~Examples~
~Contact~


MHandler Class Reference

#include <MHandler.h>

List of all members.


Public Types

typedef std::list< SERVICE * > service_v

Public Member Functions

 MHandler (StdLog *=NULL)
 ~MHandler ()
MasterSocketGetMasterSocket ()
void SendToExt (unsigned short, const char *, size_t)
void CloseExtConn (unsigned short)
void LoadConfig ()
void SaveConfig ()
void SetListenPort (port_t x)
port_t GetListenPort ()
void StartServices ()
service_vGetServices ()
void AddService (port_t port, const std::string &remote_host, port_t remote_port)
void StartService (SERVICE *)
void SetValidIP (const std::string &x)
const std::string & GetValidIP ()
unsigned short GetUniqueID ()
void SetAdminPort (port_t x)
port_t GetAdminPort ()

Private Member Functions

 MHandler (const MHandler &)
MHandleroperator= (const MHandler &)

Private Attributes

service_v m_services
port_t m_listen_port
std::string m_valid_ip
port_t m_admin_port

Static Private Attributes

static unsigned short m_unique_id = 0

Classes

struct  SERVICE

Detailed Description

Definition at line 11 of file MHandler.h.


Member Typedef Documentation

typedef std::list<SERVICE *> MHandler::service_v

Definition at line 21 of file MHandler.h.


Constructor & Destructor Documentation

MHandler::MHandler ( StdLog *  = NULL  ) 

Definition at line 14 of file MHandler.cpp.

00015 :SocketHandler(p)
00016 ,m_listen_port(23)
00017 //,m_proxy_port(8005)
00018 ,m_valid_ip("193.15.240.60")
00019 ,m_admin_port(16667)
00020 {
00021 }

MHandler::~MHandler (  ) 

Definition at line 24 of file MHandler.cpp.

00025 {
00026 }

MHandler::MHandler ( const MHandler  )  [inline, private]

Definition at line 56 of file MHandler.h.

00056 {} // copy constructor


Member Function Documentation

MasterSocket * MHandler::GetMasterSocket (  ) 

Definition at line 29 of file MHandler.cpp.

00030 {
00031         for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
00032         {
00033                 Socket *p0 = (*it).second;
00034                 MasterSocket *p = dynamic_cast<MasterSocket *>(p0);
00035                 if (p)
00036                 {
00037                         return p;
00038                 }
00039         }
00040         return NULL;
00041 }

void MHandler::SendToExt ( unsigned  short,
const char *  ,
size_t   
)

Definition at line 44 of file MHandler.cpp.

00045 {
00046         for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
00047         {
00048                 Socket *p0 = (*it).second;
00049                 ExtSocket *p = dynamic_cast<ExtSocket *>(p0);
00050                 if (p && p -> GetID() == id)
00051                 {
00052                         p -> SendBuf(buf, len);
00053                 }
00054         }
00055 }

void MHandler::CloseExtConn ( unsigned  short  ) 

Definition at line 58 of file MHandler.cpp.

00059 {
00060         for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
00061         {
00062                 Socket *p0 = (*it).second;
00063                 ExtSocket *p = dynamic_cast<ExtSocket *>(p0);
00064                 if (p && p -> GetID() == id)
00065                 {
00066                         p -> SetCloseAndDelete();
00067                 }
00068         }
00069 }

void MHandler::LoadConfig (  ) 

Definition at line 89 of file MHandler.cpp.

References m_services, SetAdminPort(), SetListenPort(), and SetValidIP().

Referenced by main().

00090 {
00091         FILE *fil = fopen("services.txt", "rt");
00092         if (fil)
00093         {
00094                 port_t local_port = 0;
00095                 std::string remote_host;
00096                 port_t remote_port;
00097                 char slask[1000];
00098                 fgets(slask, 1000, fil);
00099                 while (!feof(fil))
00100                 {
00101                         slask[strlen(slask) - 1] = 0;
00102                         if (*slask && *slask != '#')
00103                         {
00104                                 Parse pa(slask);
00105                                 std::string key = pa.getword();
00106                                 std::string value = pa.getword();
00107                                 if (key == "local_port")
00108                                         local_port = atoi(value.c_str());
00109                                 else
00110                                 if (key == "remote_host" || key == "remote_ip")
00111                                         remote_host = value;
00112                                 else
00113                                 if (key == "remote_port" && local_port)
00114                                 {
00115                                         remote_port = atoi(value.c_str());
00116                                         m_services.push_back(new SERVICE(local_port, remote_host, remote_port));
00117                                         local_port = 0;
00118                                 }
00119                                 else
00120                                 if (key == "listen_port")
00121                                         SetListenPort(atoi(value.c_str()));
00122                                 else
00123 /*
00124                                 if (key == "proxy_port")
00125                                         SetProxyPort(atoi(value.c_str()));
00126                                 else
00127 */
00128                                 if (key == "valid_ip")
00129                                         SetValidIP(value);
00130                                 else
00131                                 if (key == "admin_port")
00132                                         SetAdminPort(atoi(value.c_str()));
00133                         }
00134                         //
00135                         fgets(slask, 1000, fil);
00136                 }
00137                 fclose(fil);
00138         }
00139 }

void MHandler::SaveConfig (  ) 

Definition at line 142 of file MHandler.cpp.

References GetAdminPort(), GetListenPort(), GetValidIP(), and m_services.

Referenced by main().

00143 {
00144         FILE *fil = fopen("services.txt", "wt");
00145         if (fil)
00146         {
00147                 fprintf(fil, "# master listen port & valid IP that can connect\n");
00148                 fprintf(fil, "listen_port %u\n", GetListenPort());
00149                 fprintf(fil, "valid_ip %s\n", GetValidIP().c_str());
00150                 fprintf(fil, "\n");
00151 /*
00152                 fprintf(fil, "# proxy listen port\n");
00153                 fprintf(fil, "proxy_port %u\n", GetProxyPort());
00154                 fprintf(fil, "\n");
00155 */
00156                 fprintf(fil, "# admin listen port\n");
00157                 fprintf(fil, "admin_port %u\n", GetAdminPort());
00158                 fprintf(fil, "\n");
00159 
00160                 fprintf(fil, "# defined remote services (always end with remote_port because I'm lazy)\n");
00161                 for (service_v::iterator it = m_services.begin(); it != m_services.end(); it++)
00162                 {
00163                         SERVICE *p = *it;
00164                         fprintf(fil, "local_port %u\n", p -> port);
00165                         fprintf(fil, "remote_host %s\n", p -> remote_host.c_str());
00166                         fprintf(fil, "remote_port %u\n", p -> remote_port);
00167                         fprintf(fil, "\n");
00168                 }
00169                 fclose(fil);
00170         }
00171 }

void MHandler::SetListenPort ( port_t  x  )  [inline]

Definition at line 36 of file MHandler.h.

References m_listen_port.

Referenced by LoadConfig().

00036 { m_listen_port = x; }

port_t MHandler::GetListenPort (  )  [inline]

Definition at line 37 of file MHandler.h.

References m_listen_port.

Referenced by main(), and SaveConfig().

00037 { return m_listen_port; }

void MHandler::StartServices (  ) 

Definition at line 190 of file MHandler.cpp.

References m_services, and StartService().

Referenced by main().

00191 {
00192         for (service_v::iterator it = m_services.begin(); it != m_services.end(); it++)
00193         {
00194                 SERVICE *p = *it;
00195                 StartService(p);
00196         }
00197 }

service_v& MHandler::GetServices (  )  [inline]

Definition at line 43 of file MHandler.h.

References m_services.

Referenced by AdminSocket::Exec().

00043 { return m_services; }

void MHandler::AddService ( port_t  port,
const std::string &  remote_host,
port_t  remote_port 
)

Definition at line 200 of file MHandler.cpp.

References m_services, and StartService().

Referenced by AdminSocket::Exec().

00201 {
00202         SERVICE *p = new SERVICE(port, remote_host, remote_port);
00203         m_services.push_back(p);
00204         StartService(p);
00205 }

void MHandler::StartService ( SERVICE  ) 

Definition at line 174 of file MHandler.cpp.

Referenced by AddService(), and StartServices().

00175 {
00176         ListenSocket<ExtSocket> *l = new ListenSocket<ExtSocket>(*this);
00177         l -> SetDeleteByHandler();
00178         if (!l -> Bind(p -> port))
00179         {
00180                 printf("%s:%u on port %u started\n", p -> remote_host.c_str(), p -> remote_port, p -> port);
00181                 Add(l);
00182         }
00183         else
00184         {
00185                 delete l;
00186         }
00187 }

void MHandler::SetValidIP ( const std::string &  x  )  [inline]

Definition at line 47 of file MHandler.h.

References m_valid_ip.

Referenced by LoadConfig().

00047 { m_valid_ip = x; }

const std::string& MHandler::GetValidIP (  )  [inline]

Definition at line 48 of file MHandler.h.

References m_valid_ip.

Referenced by SaveConfig().

00048 { return m_valid_ip; }

unsigned short MHandler::GetUniqueID (  ) 

Definition at line 208 of file MHandler.cpp.

References m_unique_id.

00209 {
00210         return ++m_unique_id;
00211 }

void MHandler::SetAdminPort ( port_t  x  )  [inline]

Definition at line 52 of file MHandler.h.

References m_admin_port.

Referenced by LoadConfig().

00052 { m_admin_port = x; }

port_t MHandler::GetAdminPort (  )  [inline]

Definition at line 53 of file MHandler.h.

References m_admin_port.

Referenced by main(), and SaveConfig().

00053 { return m_admin_port; }

MHandler& MHandler::operator= ( const MHandler  )  [inline, private]

Definition at line 57 of file MHandler.h.

00057 { return *this; } // assignment operator


Member Data Documentation

Definition at line 58 of file MHandler.h.

Referenced by AddService(), GetServices(), LoadConfig(), SaveConfig(), and StartServices().

port_t MHandler::m_listen_port [private]

Definition at line 59 of file MHandler.h.

Referenced by GetListenPort(), and SetListenPort().

std::string MHandler::m_valid_ip [private]

Definition at line 61 of file MHandler.h.

Referenced by GetValidIP(), and SetValidIP().

unsigned short MHandler::m_unique_id = 0 [static, private]

Definition at line 62 of file MHandler.h.

Referenced by GetUniqueID().

port_t MHandler::m_admin_port [private]

Definition at line 63 of file MHandler.h.

Referenced by GetAdminPort(), and SetAdminPort().


The documentation for this class was generated from the following files:
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