Logo
~Sockets~
~Examples~
~Contact~


MHandler.cpp

Go to the documentation of this file.
00001 #include <Parse.h>
00002 #include <ListenSocket.h>
00003 
00004 #include "MHandler.h"
00005 #include "MasterSocket.h"
00006 #include "ExtSocket.h"
00007 #include "ProxyInSocket.h"
00008 
00009 
00010 // statics
00011 unsigned short MHandler::m_unique_id = 0;
00012 
00013 
00014 MHandler::MHandler(StdLog *p)
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 }
00022 
00023 
00024 MHandler::~MHandler()
00025 {
00026 }
00027 
00028 
00029 MasterSocket *MHandler::GetMasterSocket()
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 }
00042 
00043 
00044 void MHandler::SendToExt(unsigned short id,const char *buf,size_t len)
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 }
00056 
00057 
00058 void MHandler::CloseExtConn(unsigned short id)
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 }
00070 
00071 
00072 /*
00073 ProxyInSocket *MHandler::GetProxySocket(unsigned short id)
00074 {
00075         for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
00076         {
00077                 Socket *p0 = (*it).second;
00078                 ProxyInSocket *p = dynamic_cast<ProxyInSocket *>(p0);
00079                 if (p && p -> GetID() == id)
00080                 {
00081                         return p;
00082                 }
00083         }
00084         return NULL;
00085 }
00086 */
00087 
00088 
00089 void MHandler::LoadConfig()
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 }
00140 
00141 
00142 void MHandler::SaveConfig()
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 }
00172 
00173 
00174 void MHandler::StartService(SERVICE *p)
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 }
00188 
00189 
00190 void MHandler::StartServices()
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 }
00198 
00199 
00200 void MHandler::AddService(port_t port,const std::string& remote_host,port_t remote_port)
00201 {
00202         SERVICE *p = new SERVICE(port, remote_host, remote_port);
00203         m_services.push_back(p);
00204         StartService(p);
00205 }
00206 
00207 
00208 unsigned short MHandler::GetUniqueID()
00209 {
00210         return ++m_unique_id;
00211 }
00212 
00213 
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