![]() |
MinderSocket Class ReferenceClient socket for connecting to a Minder.
More...
|
Public Member Functions | |
MinderSocket (ISocketHandler &, const std::string &) | |
~MinderSocket () | |
void | SetLocalIpPort (const std::string &x, port_t p) |
void | Function (const std::string &str) |
void | SendHello () |
void | SetExtraInfo (long x) |
virtual MinionSocket * | CreateMinionSocket (const std::string &, ipaddr_t, port_t)=0 |
virtual int | GetMaxConnections ()=0 |
virtual void | OnSwitchDatabase (const std::string &)=0 |
Static Public Attributes | |
static char | g_UpdateHost [256] |
static port_t | g_UpdatePort |
static char | g_UpdateUrl [256] |
Protected Member Functions | |
void | OnLine (const std::string &line) |
Callback fires when a socket in line protocol has read one full line. | |
void | OnConnect () |
Called when a connection has completed. | |
Private Member Functions | |
MinderSocket & | operator= (const MinderSocket &) |
Private Attributes | |
std::string | m_app |
std::string | local_ip |
port_t | local_port |
ipaddr_t | my_ip |
port_t | my_port |
std::string | m_function |
long | m_extra_info |
Definition at line 39 of file MinderSocket.h.
MinderSocket::MinderSocket | ( | ISocketHandler & | , | |
const std::string & | ||||
) |
Definition at line 51 of file MinderSocket.cpp.
References TcpSocket::SetLineProtocol().
00052 :TcpSocket(h) 00053 ,m_app(app) 00054 ,my_ip(0) 00055 ,my_port(0) 00056 ,m_function("") 00057 ,m_extra_info(0) 00058 { 00059 SetLineProtocol(); 00060 }
MinderSocket::~MinderSocket | ( | ) |
void MinderSocket::SetLocalIpPort | ( | const std::string & | x, | |
port_t | p | |||
) | [inline] |
void MinderSocket::Function | ( | const std::string & | str | ) | [inline] |
void MinderSocket::SendHello | ( | ) |
Definition at line 175 of file MinderSocket.cpp.
References Utility::base64(), Socket::Handler(), Utility::l2string(), local_ip, local_port, m_app, m_extra_info, m_function, TcpSocket::Send(), and Socket::SetCloseAndDelete().
Referenced by OnConnect(), and OnLine().
00176 { 00177 std::string msg = m_function + "_"; 00178 // assert(m_app.size()); 00179 if (!m_app.size()) 00180 { 00181 SetCloseAndDelete(); 00182 return; 00183 } 00184 msg += Utility::base64(m_app); 00185 msg += ":" + static_cast<MinderHandler&>(Handler()).GetID(); 00186 msg += ":" + local_ip; 00187 msg += ":" + Utility::l2string(local_port); 00188 msg += ":" + Utility::l2string(static_cast<MinderHandler&>(Handler()).GetHostId()); 00189 msg += ":" + Utility::l2string(m_extra_info); 00190 Send( Utility::base64(msg) + "\n" ); 00191 }
void MinderSocket::SetExtraInfo | ( | long | x | ) | [inline] |
virtual MinionSocket* MinderSocket::CreateMinionSocket | ( | const std::string & | , | |
ipaddr_t | , | |||
port_t | ||||
) | [pure virtual] |
Referenced by OnLine().
virtual int MinderSocket::GetMaxConnections | ( | ) | [pure virtual] |
Referenced by OnLine().
virtual void MinderSocket::OnSwitchDatabase | ( | const std::string & | ) | [pure virtual] |
void MinderSocket::OnLine | ( | const std::string & | line | ) | [protected, virtual] |
Callback fires when a socket in line protocol has read one full line.
line | Line read |
Reimplemented from TcpSocket.
Definition at line 68 of file MinderSocket.cpp.
References ISocketHandler::Add(), Utility::base64d(), Socket::Connecting(), CreateMinionSocket(), DEB, g_UpdateHost, g_UpdatePort, g_UpdateUrl, GetMaxConnections(), Parse::getvalue(), Parse::getword(), Socket::Handler(), my_ip, my_port, TcpSocket::Open(), SendHello(), Socket::SetCloseAndDelete(), Socket::SetDeleteByHandler(), and Utility::u2ip().
00069 { 00070 std::string cmd; 00071 std::string id; 00072 std::string ipstr; 00073 port_t port; 00074 ipaddr_t ip; 00075 int max = GetMaxConnections(); //atoi(config["max_connections"].c_str()); 00076 Parse pa(Utility::base64d(line),"_:"); 00077 00078 pa.getword(cmd); 00079 static_cast<MinderHandler&>(Handler()).SetMinderTime(time(NULL)); 00080 00081 if (cmd == "You") 00082 { 00083 pa.getword(id); 00084 pa.getword(ipstr); 00085 port = (port_t)pa.getvalue(); 00086 unsigned long hostid = pa.getvalue(); 00087 00088 Utility::u2ip(ipstr,ip); 00089 00090 DEB( fprintf(stderr, " received my id '%s' %s:%d - %lu\n",id.c_str(),ipstr.c_str(),port,hostid);) 00091 // this is ourselves 00092 my_ip = ip; 00093 my_port = port; 00094 static_cast<MinderHandler&>(Handler()).SetMyIpPort(my_ip, my_port); 00095 DEB( fprintf(stderr, "ignoring %s:%d\n",ipstr.c_str(),port);) 00096 static_cast<MinderHandler&>(Handler()).SetExternalAddress(ipstr); 00097 if (static_cast<MinderHandler&>(Handler()).GetHostId() == 0) 00098 { 00099 static_cast<MinderHandler&>(Handler()).SetHostId(hostid); 00100 } 00101 } 00102 else 00103 #ifdef _WIN32 00104 if (cmd == "Update") 00105 { 00106 std::string host = pa.getword(); 00107 port_t port = (port_t)pa.getvalue(); 00108 std::string url = pa.getword(); 00109 strncpy(g_UpdateHost, host.c_str(), 255); 00110 g_UpdatePort = port; 00111 strncpy(g_UpdateUrl, url.c_str(), 255); 00112 } 00113 else 00114 #endif 00115 if (cmd == "Minion") 00116 { 00117 pa.getword(id); 00118 pa.getword(ipstr); 00119 port = (port_t)pa.getvalue(); 00120 long remote_host_id = pa.getvalue(); 00121 00122 Utility::u2ip(ipstr,ip); 00123 00124 if (ip == my_ip && port == my_port) 00125 { 00126 return; 00127 } 00128 max = (max == 0) ? 4 : max; 00129 00130 if (!static_cast<MinderHandler&>(Handler()).FindMinion(id) ) 00131 { 00132 if (0 && static_cast<MinderHandler&>(Handler()).Count() < max) 00133 { 00134 DEB( fprintf(stderr, " connect to %s:%d\n",ipstr.c_str(),port);) 00135 //fprintf(stderr, "Minder List: %s:%d id %s\n",ipstr.c_str(),port,id.c_str()); 00136 MinionSocket *tmp = CreateMinionSocket(id,ip,port); //new MinionSocket(Handler(),id,ip,port); 00137 tmp -> SetMyIpPort(my_ip,my_port); 00138 if (tmp -> Open(ip,port)) 00139 { 00140 tmp -> SetDeleteByHandler(true); 00141 Handler().Add(tmp); 00142 // 00143 tmp -> SendHello("Hello"); 00144 } 00145 else 00146 if (tmp -> Connecting()) 00147 { 00148 tmp -> SetDeleteByHandler(true); 00149 // check OnConnect 00150 Handler().Add(tmp); 00151 } 00152 else 00153 { 00154 delete tmp; 00155 } 00156 } 00157 else 00158 { 00159 static_cast<MinderHandler&>(Handler()).AddHost(ip,port,id,remote_host_id); 00160 } 00161 } 00162 else 00163 { 00164 DEB( fprintf(stderr, " id found\n");) 00165 } 00166 } 00167 else 00168 if (cmd == "End") 00169 { 00170 SetCloseAndDelete( true ); 00171 } 00172 }
void MinderSocket::OnConnect | ( | ) | [protected, virtual] |
Called when a connection has completed.
Reimplemented from Socket.
Definition at line 194 of file MinderSocket.cpp.
References SendHello().
00195 { 00196 SendHello(); 00197 }
MinderSocket& MinderSocket::operator= | ( | const MinderSocket & | ) | [inline, private] |
char MinderSocket::g_UpdateHost[256] [static] |
port_t MinderSocket::g_UpdatePort [static] |
char MinderSocket::g_UpdateUrl[256] [static] |
std::string MinderSocket::m_app [private] |
std::string MinderSocket::local_ip [private] |
port_t MinderSocket::local_port [private] |
ipaddr_t MinderSocket::my_ip [private] |
port_t MinderSocket::my_port [private] |
std::string MinderSocket::m_function [private] |
long MinderSocket::m_extra_info [private] |