00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef _WIN32
00024 #pragma warning(disable:4786)
00025 #endif
00026
00027
00028 #include <socket_include.h>
00029 #include "MyTcpSocket.h"
00030 #include "config.h"
00031 #include "MyHandler.h"
00032 #ifdef _WIN32
00033
00034 #endif
00035
00036 #define DEB(x)
00037
00038
00039
00040
00041 MyHandler::MyHandler()
00042 :SocketHandler()
00043 {
00044 }
00045
00046
00047 MyHandler::~MyHandler()
00048 {
00049 }
00050
00051
00052 void MyHandler::CheckTimeout()
00053 {
00054 bool ok = true;
00055 for (app_m::iterator it = m_app.begin(); it != m_app.end() && ok; it++)
00056 for (host_v::iterator i2 = (*it).second.begin(); i2 != (*it).second.end() && ok; i2++)
00057 {
00058 HOST *p = *i2;
00059 if (p)
00060 {
00061 if (time(NULL) - p -> t > 15 * 60 + 10)
00062 {
00063 unreg_host( (*it).first, p -> id,p -> ip,p -> port,p -> ip);
00064 ok = false;
00065 }
00066 }
00067 }
00068 }
00069
00070
00071 HOST * MyHandler::reg_host(const std::string& app,const std::string& id,ipaddr_t ip,port_t port,ipaddr_t ip_minion,unsigned long& hostid,long hid_from_host)
00072 {
00073 for (host_v::iterator it = m_app[app].begin(); it != m_app[app].end(); it++)
00074 {
00075 HOST *p = *it;
00076 if (!strcmp(id.c_str(),p -> id.c_str()) && p -> ip == ip && p -> port == port)
00077 {
00078 hostid = p -> host_id;
00079 p -> t = time(NULL);
00080 return p;
00081 }
00082 }
00083 HOST *p = new HOST(id,ip,port);
00084 if (hid_from_host)
00085 {
00086 p -> host_id = hostid = hid_from_host;
00087 }
00088 else
00089 {
00090 p -> host_id = hostid = atol(config["next_host_id"].c_str());
00091 config["next_host_id"] = Utility::l2string(hostid + 1);
00092 write_config();
00093 }
00094 p -> t = time(NULL);
00095 if (ip != ip_minion)
00096 p -> behind_firewall = true;
00097 m_app[app].push_back(p);
00098 ViewHosts();
00099 return p;
00100 }
00101
00102
00103 void MyHandler::unreg_host(const std::string& app,const std::string& id,ipaddr_t ip,port_t port,ipaddr_t ip_minion)
00104 {
00105 for (host_v::iterator it = m_app[app].begin(); it != m_app[app].end(); it++)
00106 {
00107 HOST *p = *it;
00108 if (!strcmp(id.c_str(),p -> id.c_str()) && p -> ip == ip && p -> port == port)
00109 {
00110 delete p;
00111 m_app[app].erase(it);
00112 ViewHosts();
00113 break;
00114 }
00115 }
00116 }
00117
00118
00119 HOST* MyHandler::GetRandomHOST(const std::string& app,bool must)
00120 {
00121 if (must)
00122 {
00123 int q = 0;
00124 for (host_v::iterator it = m_app[app].begin(); it != m_app[app].end(); it++)
00125 {
00126 HOST *p = *it;
00127 if (p -> connectable)
00128 {
00129 q++;
00130 break;
00131 }
00132 }
00133 if (q)
00134 {
00135 #ifdef _WIN32
00136 int x = rand() % m_app[app].size();
00137 #else
00138 int x = random() % m_app[app].size();
00139 #endif
00140 HOST *p = m_app[app][x];
00141 while (!p -> connectable)
00142 {
00143 #ifdef _WIN32
00144 x = rand() % m_app[app].size();
00145 #else
00146 x = random() % m_app[app].size();
00147 #endif
00148 p = m_app[app][x];
00149 }
00150 return p;
00151 }
00152 }
00153 else
00154 {
00155 if (m_app[app].size())
00156 {
00157 #ifdef _WIN32
00158 return m_app[app][rand() % m_app[app].size()];
00159 #else
00160 return m_app[app][random() % m_app[app].size()];
00161 #endif
00162 }
00163 }
00164 return NULL;
00165 }
00166
00167
00168 void MyHandler::reset_sent(const std::string& app)
00169 {
00170 for (host_v::iterator it = m_app[app].begin(); it != m_app[app].end(); it++)
00171 {
00172 HOST *p = *it;
00173 p -> sent = false;
00174 }
00175 }
00176
00177
00178 #define DEB(x) x
00179
00180 void MyHandler::ViewHosts()
00181 {
00182 DEB( printf("Current minion list\n");)
00183 for (app_m::iterator it = m_app.begin(); it != m_app.end(); it++)
00184 {
00185 DEB( printf(" %s\n",(*it).first.c_str());)
00186 for (host_v::iterator i2 = (*it).second.begin(); i2 != (*it).second.end(); i2++)
00187 {
00188 HOST *p = *i2;
00189 DEB( printf(" %ld %08x:%d %s%s%s\n",
00190 p -> host_id,p -> ip,p -> port,p -> id.c_str(),
00191 p -> behind_firewall ? " (nat)" : "",
00192 p -> connectable ? " Connectable" : "");)
00193 }
00194 }
00195 }
00196
00197