Logo
~Sockets~
~Examples~
~Contact~


MinderSocket Class Reference
[Distributed network framework]

Client socket for connecting to a Minder. More...

#include <MinderSocket.h>

Inheritance diagram for MinderSocket:

Inheritance graph
[legend]
Collaboration diagram for MinderSocket:

Collaboration graph
[legend]
List of all members.

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 MinionSocketCreateMinionSocket (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

MinderSocketoperator= (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

Detailed Description

Client socket for connecting to a Minder.

Definition at line 39 of file MinderSocket.h.


Constructor & Destructor Documentation

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 (  ) 

Definition at line 63 of file MinderSocket.cpp.

00064 {
00065 }


Member Function Documentation

void MinderSocket::SetLocalIpPort ( const std::string &  x,
port_t  p 
) [inline]

Definition at line 45 of file MinderSocket.h.

00045 { local_ip = x; local_port = p; }

void MinderSocket::Function ( const std::string &  str  )  [inline]

Definition at line 47 of file MinderSocket.h.

00047 { m_function = str; }

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]

Definition at line 49 of file MinderSocket.h.

00049 { m_extra_info = x; }

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.

Parameters:
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]

Definition at line 65 of file MinderSocket.h.

00065 { return *this; }


Member Data Documentation

char MinderSocket::g_UpdateHost[256] [static]

Definition at line 56 of file MinderSocket.h.

Referenced by OnLine().

Definition at line 57 of file MinderSocket.h.

Referenced by OnLine().

char MinderSocket::g_UpdateUrl[256] [static]

Definition at line 58 of file MinderSocket.h.

Referenced by OnLine().

std::string MinderSocket::m_app [private]

Definition at line 66 of file MinderSocket.h.

Referenced by SendHello().

std::string MinderSocket::local_ip [private]

Definition at line 67 of file MinderSocket.h.

Referenced by SendHello().

Definition at line 68 of file MinderSocket.h.

Referenced by SendHello().

Definition at line 69 of file MinderSocket.h.

Referenced by OnLine().

Definition at line 70 of file MinderSocket.h.

Referenced by OnLine().

std::string MinderSocket::m_function [private]

Definition at line 71 of file MinderSocket.h.

Referenced by SendHello().

Definition at line 72 of file MinderSocket.h.

Referenced by SendHello().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4