![]() |
MinderHandler Class ReferenceSpecialized sockethandler for a distributed network.
More...
|
Public Member Functions | |
MinderHandler () | |
~MinderHandler () | |
void | GenerateID () |
const std::string & | GetID () |
void | SetExternalAddress (const std::string &str) |
void | SetLocalPort (port_t s) |
port_t | GetLocalPort () |
void | SetHostId (unsigned long id) |
unsigned long | GetHostId () |
void | SendMessage (const std::string &, short ttl=30) |
void | SendMessage (const std::string &, const std::string &, short, const std::string &, std::list< std::string > &, ulong_v &) |
bool | Seen (unsigned long, unsigned long, bool=false) |
void | SendConnectList () |
void | KeepAlive () |
bool | MinderSockets () |
bool | FindMinion (const std::string &) |
int | Count () |
void | Store (long, long, const std::string &) |
bool | StoreGet (long, long, std::string &) |
void | SetDebug (bool x=true) |
bool | Debug () |
void | AddHost (ipaddr_t, port_t, const std::string &, long) |
bool | GetHost (ipaddr_t &, port_t &, std::string &, long &) |
void | SetMyIpPort (ipaddr_t a, port_t p) |
void | GetMyIpPort (ipaddr_t &a, port_t &p) |
void | SendTop (const std::string &) |
void | Tops (FILE *) |
time_t | GetMinderTime () |
void | SetMinderTime (time_t x) |
virtual std::string | GetVersion ()=0 |
virtual unsigned char * | GetKey_m2minion ()=0 |
Protected Attributes | |
Base64 | m_b |
Private Types | |
typedef std::vector< SEEN * > | seen_v |
typedef std::vector< STORE * > | store_v |
typedef std::vector< HOSTS * > | hosts_v |
Private Member Functions | |
MinderHandler (const MinderHandler &) | |
MinderHandler & | operator= (const MinderHandler &) |
Private Attributes | |
std::string | m_id |
std::string | m_external_ip |
unsigned long | m_message_id |
port_t | m_local_port |
unsigned long | m_host_id |
seen_v | m_seen |
store_v | m_store |
bool | m_bDebug |
hosts_v | m_hosts |
ipaddr_t | my_ip |
port_t | my_port |
time_t | m_tMinder |
Classes | |
struct | HOSTS |
Minion hosts list. More... | |
struct | SEEN |
Structure describing a message that has been received by a host. More... | |
struct | STORE |
Definition at line 45 of file MinderHandler.h.
typedef std::vector<SEEN *> MinderHandler::seen_v [private] |
Definition at line 59 of file MinderHandler.h.
typedef std::vector<STORE *> MinderHandler::store_v [private] |
Definition at line 68 of file MinderHandler.h.
typedef std::vector<HOSTS *> MinderHandler::hosts_v [private] |
Definition at line 78 of file MinderHandler.h.
MinderHandler::MinderHandler | ( | ) |
Definition at line 52 of file MinderHandler.cpp.
References GenerateID().
00053 :SocketHandler() 00054 ,m_id("") 00055 ,m_external_ip("") 00056 ,m_message_id(0) 00057 ,m_host_id(0) 00058 ,m_bDebug(false) 00059 ,m_tMinder(0) 00060 { 00061 GenerateID(); 00062 }
MinderHandler::~MinderHandler | ( | ) |
Definition at line 65 of file MinderHandler.cpp.
References m_hosts, m_seen, and m_store.
00066 { 00067 for (seen_v::iterator it = m_seen.begin(); it != m_seen.end(); it++) 00068 { 00069 SEEN *p = *it; 00070 delete p; 00071 } 00072 { 00073 for (store_v::iterator it = m_store.begin(); it != m_store.end(); it++) 00074 { 00075 STORE *p = *it; 00076 delete p; 00077 } 00078 } 00079 { 00080 for (hosts_v::iterator it = m_hosts.begin(); it != m_hosts.end(); it++) 00081 { 00082 HOSTS *p = *it; 00083 delete p; 00084 } 00085 } 00086 }
MinderHandler::MinderHandler | ( | const MinderHandler & | ) | [inline, private] |
void MinderHandler::GenerateID | ( | ) |
Definition at line 89 of file MinderHandler.cpp.
References Uid::GetUid(), and m_id.
Referenced by MinderHandler().
const std::string & MinderHandler::GetID | ( | ) |
void MinderHandler::SetExternalAddress | ( | const std::string & | str | ) | [inline] |
void MinderHandler::SetLocalPort | ( | port_t | s | ) | [inline] |
port_t MinderHandler::GetLocalPort | ( | ) | [inline] |
void MinderHandler::SetHostId | ( | unsigned long | id | ) | [inline] |
unsigned long MinderHandler::GetHostId | ( | ) | [inline] |
Definition at line 91 of file MinderHandler.h.
Referenced by SendTop(), and Tops().
00091 { return m_host_id; }
void MinderHandler::SendMessage | ( | const std::string & | , | |
short | ttl = 30 | |||
) |
Definition at line 102 of file MinderHandler.cpp.
References DEB, Uid::GetBuf(), GetKey_m2minion(), Utility::l2string(), m_host_id, m_message_id, SocketHandler::m_sockets, and Store().
Referenced by KeepAlive(), SendConnectList(), and SendTop().
00103 { 00104 std::string msg; 00105 long message_id = m_message_id++; 00106 00107 msg = "Message_" + Utility::l2string(m_host_id); 00108 msg += ":" + Utility::l2string(message_id); 00109 msg += ":" + Utility::l2string(ttl); 00110 msg += ":" + msg_in; 00111 00112 DEB( fprintf(stderr, "Message:\n%s\n",msg.c_str());) 00113 00114 if (msg.size() > 255) // try is good even here, because of possible bandwidth differences between nodes 00115 { 00116 Store(m_host_id, message_id, msg); 00117 msg = "Try_" + Utility::l2string(m_host_id) + ":" + Utility::l2string(message_id); 00118 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++) 00119 { 00120 MinionSocket *p = dynamic_cast<MinionSocket *>((*it).second); 00121 if (p && p -> Ready() ) 00122 { 00123 Uid ruid(p -> GetRemoteId()); 00124 memcpy(GetKey_m2minion() + 8,ruid.GetBuf(),16); 00125 p -> Send( p -> encrypt(GetKey_m2minion(),msg) + "\n" ); 00126 } 00127 } 00128 } 00129 else 00130 { 00131 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++) 00132 { 00133 MinionSocket *p = dynamic_cast<MinionSocket *>((*it).second); 00134 if (p && p -> Ready() ) 00135 { 00136 Uid ruid(p -> GetRemoteId()); 00137 memcpy(GetKey_m2minion() + 8,ruid.GetBuf(),16); 00138 // p -> Send( p -> Utility::base64(msg) + "\n" ); 00139 p -> Send( p -> encrypt(GetKey_m2minion(),msg) + "\n" ); 00140 } 00141 } 00142 } 00143 }
void MinderHandler::SendMessage | ( | const std::string & | , | |
const std::string & | , | |||
short | , | |||
const std::string & | , | |||
std::list< std::string > & | , | |||
ulong_v & | ||||
) |
Definition at line 146 of file MinderHandler.cpp.
References Uid::GetBuf(), GetKey_m2minion(), Utility::l2string(), SocketHandler::m_sockets, and Store().
00147 { 00148 std::string msg; 00149 long host_id = atol(hid.c_str()); 00150 long message_id = atol(mid.c_str()); 00151 00152 msg = "Message_" + hid; 00153 msg += ":" + mid; 00154 msg += ":" + Utility::l2string(ttl); // + ":" + ttlstr; 00155 msg += ":" + msg_in; 00156 00157 if (msg.size() > 255) 00158 { 00159 Store(host_id, message_id, msg); 00160 msg = "Try_" + Utility::l2string(host_id) + ":" + Utility::l2string(message_id); 00161 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++) 00162 { 00163 MinionSocket *p = dynamic_cast<MinionSocket *>((*it).second); 00164 if (p && p -> Ready() ) 00165 { 00166 bool ok = true; 00167 for (std::list<std::string>::iterator it = clist.begin(); it != clist.end() && ok; it++) 00168 { 00169 std::string id = *it; 00170 if (!strcmp(p -> GetRemoteId().c_str(),id.c_str())) 00171 { 00172 ok = false; 00173 } 00174 } 00175 for (ulong_v::iterator i2 = hosts.begin(); i2 != hosts.end() && ok; i2++) 00176 { 00177 if (*i2 == static_cast<unsigned long>(p -> GetRemoteHostId())) 00178 { 00179 ok = false; 00180 } 00181 } 00182 if (ok) 00183 { 00184 Uid ruid(p -> GetRemoteId()); 00185 memcpy(GetKey_m2minion() + 8,ruid.GetBuf(),16); 00186 p -> Send( p -> encrypt(GetKey_m2minion(), msg) + "\n" ); 00187 } 00188 } 00189 } 00190 } 00191 else 00192 { 00193 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++) 00194 { 00195 MinionSocket *p = dynamic_cast<MinionSocket *>((*it).second); 00196 if (p && p -> Ready() ) 00197 { 00198 bool ok = true; 00199 for (std::list<std::string>::iterator it = clist.begin(); it != clist.end(); it++) 00200 { 00201 std::string id = *it; 00202 if (!strcmp(p -> GetRemoteId().c_str(),id.c_str())) 00203 { 00204 ok = false; 00205 break; 00206 } 00207 } 00208 if (ok) 00209 { 00210 Uid ruid(p -> GetRemoteId()); 00211 memcpy(GetKey_m2minion() + 8,ruid.GetBuf(),16); 00212 // p -> Send( p -> Utility::base64(msg) + "\n" ); 00213 p -> Send( p -> encrypt(GetKey_m2minion(), msg) + "\n" ); 00214 } 00215 } 00216 } 00217 } 00218 }
bool MinderHandler::Seen | ( | unsigned | long, | |
unsigned | long, | |||
bool | = false | |||
) |
Definition at line 308 of file MinderHandler.cpp.
References m_host_id, and m_seen.
00309 { 00310 if (host_id == m_host_id) 00311 return true; 00312 for (seen_v::iterator it = m_seen.begin(); it != m_seen.end(); it++) 00313 { 00314 SEEN *p = *it; 00315 if (p -> Eq(host_id,message_id)) 00316 { 00317 if (p -> m_temp && !temp) 00318 { 00319 p -> m_temp = temp; 00320 return false; 00321 } 00322 return true; 00323 } 00324 } 00325 // %! while time - erase 00326 time_t t = time(NULL); 00327 while (m_seen.size()) 00328 { 00329 SEEN *p = m_seen[0]; 00330 if (t - p -> m_t > 120) 00331 { 00332 delete p; 00333 m_seen.erase(m_seen.begin()); 00334 } 00335 else 00336 { 00337 break; 00338 } 00339 } 00340 SEEN *p = new SEEN(host_id,message_id); 00341 p -> m_temp = temp; 00342 m_seen.push_back(p); 00343 return false; 00344 }
void MinderHandler::SendConnectList | ( | ) |
Definition at line 231 of file MinderHandler.cpp.
References Base64::encode(), m_b, SocketHandler::m_sockets, and SendMessage().
00232 { 00233 std::string msg; 00234 std::string tmp = "_"; 00235 msg = "ConnectList"; 00236 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++) 00237 { 00238 MinionSocket *p = dynamic_cast<MinionSocket*>((*it).second); 00239 if (p && p -> Ready() ) 00240 { 00241 msg += tmp + p -> GetRemoteId(); 00242 tmp = ":"; 00243 } 00244 } 00245 m_b.encode(msg, tmp, false); 00246 //fprintf(stderr, "ConnectList:\n%s\n",msg.c_str()); 00247 SendMessage(tmp, 0); 00248 }
void MinderHandler::KeepAlive | ( | ) |
Definition at line 221 of file MinderHandler.cpp.
References Base64::encode(), m_b, and SendMessage().
00222 { 00223 std::string msg; 00224 std::string tmp; 00225 msg = "KeepAlive"; 00226 m_b.encode(msg, tmp, false); 00227 SendMessage(tmp); 00228 }
bool MinderHandler::MinderSockets | ( | ) |
Definition at line 251 of file MinderHandler.cpp.
References SocketHandler::m_sockets.
00252 { 00253 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++) 00254 { 00255 MinderSocket *p = dynamic_cast<MinderSocket *>((*it).second); 00256 if (p) 00257 return true; 00258 } 00259 return false; 00260 }
bool MinderHandler::FindMinion | ( | const std::string & | ) |
strcasecmp(p -> GetRemoteId().c_str(),id.c_str()))
strcasecmp(p -> GetRemoteId().c_str(),id.c_str()))
Definition at line 263 of file MinderHandler.cpp.
References SocketHandler::m_add, and SocketHandler::m_sockets.
00264 { 00265 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++) 00266 { 00267 MinionSocket *p = dynamic_cast<MinionSocket*>((*it).second); 00268 if (p && p -> GetRemoteId() == id) 00269 { 00270 return true; 00271 } 00272 } 00273 for (socket_m::iterator i2 = m_add.begin(); i2 != m_add.end(); i2++) 00274 { 00275 MinionSocket *p = dynamic_cast<MinionSocket*>((*i2).second); 00276 if (p && p -> GetRemoteId() == id) 00277 { 00278 return true; 00279 } 00280 } 00281 return false; 00282 }
int MinderHandler::Count | ( | ) |
Definition at line 285 of file MinderHandler.cpp.
References SocketHandler::m_add, and SocketHandler::m_sockets.
00286 { 00287 int q = 0; 00288 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++) 00289 { 00290 MinionSocket *p = dynamic_cast<MinionSocket*>((*it).second); 00291 if (p) 00292 { 00293 q++; 00294 } 00295 } 00296 for (socket_m::iterator i2 = m_add.begin(); i2 != m_add.end(); i2++) 00297 { 00298 MinionSocket *p = dynamic_cast<MinionSocket*>((*i2).second); 00299 if (p) 00300 { 00301 q++; 00302 } 00303 } 00304 return q; 00305 }
void MinderHandler::Store | ( | long | , | |
long | , | |||
const std::string & | ||||
) |
Definition at line 347 of file MinderHandler.cpp.
References m_store.
Referenced by SendMessage().
00348 { 00349 // %! while time - erase 00350 time_t t = time(NULL); 00351 while (m_store.size()) 00352 { 00353 STORE *p = m_store[0]; 00354 if (t - p -> m_t > 10) 00355 { 00356 delete p; 00357 m_store.erase(m_store.begin()); 00358 } 00359 else 00360 { 00361 break; 00362 } 00363 } 00364 STORE *p = new STORE(hid,mid,msg); 00365 m_store.push_back(p); 00366 }
bool MinderHandler::StoreGet | ( | long | , | |
long | , | |||
std::string & | ||||
) |
Definition at line 369 of file MinderHandler.cpp.
References m_store.
00370 { 00371 for (store_v::iterator it = m_store.begin(); it != m_store.end(); it++) 00372 { 00373 STORE *p = *it; 00374 if (p -> m_hid == hid && p -> m_mid == mid) 00375 { 00376 msg = p -> m_message; 00377 return true; 00378 } 00379 } 00380 return false; 00381 }
void MinderHandler::SetDebug | ( | bool | x = true |
) | [inline] |
bool MinderHandler::Debug | ( | ) | [inline] |
Definition at line 384 of file MinderHandler.cpp.
00385 { 00386 DEB( fprintf(stderr, "AddHost: size %d\n",m_hosts.size());) 00387 if (m_hosts.size() >= 20) 00388 return; 00389 for (hosts_v::iterator it = m_hosts.begin(); it != m_hosts.end(); it++) 00390 { 00391 HOSTS *p2 = *it; 00392 if (p2 -> ip == a && p2 -> port == p) 00393 { 00394 p2 -> key == k; 00395 return; 00396 } 00397 } 00398 HOSTS *p2 = new HOSTS(a,p,k,r); 00399 m_hosts.push_back(p2); 00400 }
Definition at line 403 of file MinderHandler.cpp.
References m_hosts.
00404 { 00405 if (m_hosts.size()) 00406 { 00407 hosts_v::iterator it = m_hosts.begin(); 00408 HOSTS *p2 = *it; 00409 a = p2 -> ip; 00410 p = p2 -> port; 00411 k = p2 -> key; 00412 r = p2 -> remote_host_id; 00413 delete p2; 00414 m_hosts.erase(it); 00415 return true; 00416 } 00417 return false; 00418 }
void MinderHandler::SendTop | ( | const std::string & | ) |
Definition at line 421 of file MinderHandler.cpp.
References Base64::encode(), GetHostId(), GetVersion(), Utility::l2string(), m_b, SocketHandler::m_sockets, and SendMessage().
00422 { 00423 std::string msg; 00424 std::string tmp; 00425 msg = "Top_" + hid; 00426 msg += ":" + Utility::l2string(GetHostId()); 00427 m_b.encode(GetVersion(), tmp, false); 00428 msg += ":" + tmp; 00429 #ifdef _WIN32 00430 msg += ":Win32"; 00431 #else 00432 msg += ":Linux"; 00433 #endif 00434 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++) 00435 { 00436 MinionSocket *p = dynamic_cast<MinionSocket*>((*it).second); 00437 // TelnetSocket *p2 = dynamic_cast<TelnetSocket *>((*it).second); 00438 if (p && p -> Ready() ) 00439 { 00440 msg += ":" + Utility::l2string(p -> GetRemoteHostId() ); 00441 } 00442 } 00443 //fprintf(stderr, " Top reply: %s\n",msg.c_str()); 00444 m_b.encode(msg, tmp, false); 00445 SendMessage(tmp); 00446 }
void MinderHandler::Tops | ( | FILE * | ) |
Definition at line 449 of file MinderHandler.cpp.
References GetHostId(), and SocketHandler::m_sockets.
00450 { 00451 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++) 00452 { 00453 MinionSocket *p = dynamic_cast<MinionSocket*>((*it).second); 00454 if (p && p -> Ready() ) 00455 { 00456 fprintf(fil,"\t\"%ld\" -> \"%ld\"\n", 00457 GetHostId(), 00458 p -> GetRemoteHostId()); 00459 } 00460 } 00461 }
time_t MinderHandler::GetMinderTime | ( | ) | [inline] |
void MinderHandler::SetMinderTime | ( | time_t | x | ) | [inline] |
virtual std::string MinderHandler::GetVersion | ( | ) | [pure virtual] |
Referenced by SendTop().
virtual unsigned char* MinderHandler::GetKey_m2minion | ( | ) | [pure virtual] |
Referenced by SendMessage().
MinderHandler& MinderHandler::operator= | ( | const MinderHandler & | ) | [inline, private] |
Base64 MinderHandler::m_b [protected] |
Definition at line 128 of file MinderHandler.h.
Referenced by KeepAlive(), SendConnectList(), and SendTop().
std::string MinderHandler::m_id [private] |
std::string MinderHandler::m_external_ip [private] |
Definition at line 134 of file MinderHandler.h.
unsigned long MinderHandler::m_message_id [private] |
port_t MinderHandler::m_local_port [private] |
Definition at line 136 of file MinderHandler.h.
unsigned long MinderHandler::m_host_id [private] |
seen_v MinderHandler::m_seen [private] |
store_v MinderHandler::m_store [private] |
Definition at line 139 of file MinderHandler.h.
Referenced by Store(), StoreGet(), and ~MinderHandler().
bool MinderHandler::m_bDebug [private] |
Definition at line 140 of file MinderHandler.h.
hosts_v MinderHandler::m_hosts [private] |
Definition at line 141 of file MinderHandler.h.
Referenced by AddHost(), GetHost(), and ~MinderHandler().
ipaddr_t MinderHandler::my_ip [private] |
Definition at line 142 of file MinderHandler.h.
port_t MinderHandler::my_port [private] |
Definition at line 143 of file MinderHandler.h.
time_t MinderHandler::m_tMinder [private] |
Definition at line 144 of file MinderHandler.h.