#include <SocketServer.hpp>
Collaboration diagram for SocketServer:

Public Member Functions | |
| SocketServer (unsigned maxSessions=~(unsigned) 0) | |
| ~SocketServer () | |
| bool | setup (Service *, unsigned short *port, const char *pinterface=0) |
| void | startServer () |
| void | stopServer () |
| void | stopSessions (bool wait=false) |
| void | foreachSession (void(*f)(Session *, void *), void *data) |
| void | checkSessions () |
Static Public Member Functions | |
| static bool | tryBind (unsigned short port, const char *intface=0) |
Private Member Functions | |
| void | doAccept () |
| void | checkSessionsImpl () |
| void | startSession (SessionInstance &) |
| void | doRun () |
Private Attributes | |
| NdbLockable | m_session_mutex |
| Vector< SessionInstance > | m_sessions |
| MutexVector< ServiceInstance > | m_services |
| unsigned | m_maxSessions |
| bool | m_stopThread |
| NdbThread * | m_thread |
| NdbLockable | m_threadLock |
Friends | |
| void * | socketServerThread_C (void *) |
| void * | sessionThread_C (void *) |
Classes | |
| class | Service |
| struct | ServiceInstance |
| class | Session |
| struct | SessionInstance |
Definition at line 31 of file SocketServer.hpp.
| SocketServer::SocketServer | ( | unsigned | maxSessions = ~(unsigned) 0 |
) |
Constructor / Destructor
Definition at line 30 of file SocketServer.cpp.
References m_maxSessions, m_stopThread, and m_thread.
00030 : 00031 m_sessions(10), 00032 m_services(5) 00033 { 00034 m_thread = 0; 00035 m_stopThread = false; 00036 m_maxSessions = maxSessions; 00037 }
| SocketServer::~SocketServer | ( | ) |
Definition at line 39 of file SocketServer.cpp.
References m_services, m_sessions, and NDB_CLOSE_SOCKET().
00039 { 00040 unsigned i; 00041 for(i = 0; i<m_sessions.size(); i++){ 00042 delete m_sessions[i].m_session; 00043 } 00044 for(i = 0; i<m_services.size(); i++){ 00045 if(m_services[i].m_socket) 00046 NDB_CLOSE_SOCKET(m_services[i].m_socket); 00047 delete m_services[i].m_service; 00048 } 00049 }
Here is the call graph for this function:

| void SocketServer::checkSessions | ( | ) |
Definition at line 293 of file SocketServer.cpp.
References checkSessionsImpl(), and m_session_mutex.
Referenced by MgmApiSession::get_nodeid(), and MgmApiSession::purge_stale_sessions().
00294 { 00295 m_session_mutex.lock(); 00296 checkSessionsImpl(); 00297 m_session_mutex.unlock(); 00298 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketServer::checkSessionsImpl | ( | ) | [private] |
Definition at line 301 of file SocketServer.cpp.
References m_sessions, m_thread, NdbThread_Destroy(), and NdbThread_WaitFor().
Referenced by checkSessions(), doRun(), and stopSessions().
00302 { 00303 for(int i = m_sessions.size() - 1; i >= 0; i--) 00304 { 00305 if(m_sessions[i].m_session->m_stopped) 00306 { 00307 if(m_sessions[i].m_thread != 0) 00308 { 00309 void* ret; 00310 NdbThread_WaitFor(m_sessions[i].m_thread, &ret); 00311 NdbThread_Destroy(&m_sessions[i].m_thread); 00312 } 00313 m_sessions[i].m_session->stopSession(); 00314 delete m_sessions[i].m_session; 00315 m_sessions.erase(i); 00316 } 00317 } 00318 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketServer::doAccept | ( | ) | [private] |
Definition at line 157 of file SocketServer.cpp.
References DEBUG, SocketServer::SessionInstance::m_service, m_services, SocketServer::SessionInstance::m_session, m_session_mutex, m_sessions, NDB_INVALID_SOCKET, NDB_SOCKET_TYPE, and startSession().
Referenced by doRun().
00157 { 00158 fd_set readSet, exceptionSet; 00159 FD_ZERO(&readSet); 00160 FD_ZERO(&exceptionSet); 00161 00162 m_services.lock(); 00163 int maxSock = 0; 00164 for (unsigned i = 0; i < m_services.size(); i++){ 00165 const NDB_SOCKET_TYPE s = m_services[i].m_socket; 00166 FD_SET(s, &readSet); 00167 FD_SET(s, &exceptionSet); 00168 maxSock = (maxSock > s ? maxSock : s); 00169 } 00170 struct timeval timeout; 00171 timeout.tv_sec = 1; 00172 timeout.tv_usec = 0; 00173 00174 if(select(maxSock + 1, &readSet, 0, &exceptionSet, &timeout) > 0){ 00175 for (unsigned i = 0; i < m_services.size(); i++){ 00176 ServiceInstance & si = m_services[i]; 00177 00178 if(FD_ISSET(si.m_socket, &readSet)){ 00179 NDB_SOCKET_TYPE childSock = accept(si.m_socket, 0, 0); 00180 if(childSock == NDB_INVALID_SOCKET){ 00181 continue; 00182 } 00183 00184 SessionInstance s; 00185 s.m_service = si.m_service; 00186 s.m_session = si.m_service->newSession(childSock); 00187 if(s.m_session != 0) 00188 { 00189 m_session_mutex.lock(); 00190 m_sessions.push_back(s); 00191 startSession(m_sessions.back()); 00192 m_session_mutex.unlock(); 00193 } 00194 00195 continue; 00196 } 00197 00198 if(FD_ISSET(si.m_socket, &exceptionSet)){ 00199 DEBUG("socket in the exceptionSet"); 00200 continue; 00201 } 00202 } 00203 } 00204 m_services.unlock(); 00205 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketServer::doRun | ( | ) | [private] |
Definition at line 243 of file SocketServer.cpp.
References checkSessionsImpl(), doAccept(), m_maxSessions, m_session_mutex, m_sessions, m_stopThread, and NdbSleep_MilliSleep().
Referenced by socketServerThread_C().
00243 { 00244 00245 while(!m_stopThread){ 00246 m_session_mutex.lock(); 00247 checkSessionsImpl(); 00248 if(m_sessions.size() < m_maxSessions){ 00249 m_session_mutex.unlock(); 00250 doAccept(); 00251 } else { 00252 m_session_mutex.unlock(); 00253 NdbSleep_MilliSleep(200); 00254 } 00255 } 00256 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketServer::foreachSession | ( | void(*)(Session *, void *) | f, | |
| void * | data | |||
| ) |
Definition at line 283 of file SocketServer.cpp.
References m_session_mutex, and m_sessions.
Referenced by MgmApiSession::purge_stale_sessions().
00284 { 00285 m_session_mutex.lock(); 00286 for(int i = m_sessions.size() - 1; i >= 0; i--){ 00287 (*func)(m_sessions[i].m_session, data); 00288 } 00289 m_session_mutex.unlock(); 00290 }
Here is the caller graph for this function:

Setup socket bind & listen Returns false if no success
Definition at line 88 of file SocketServer.cpp.
References DBUG_ENTER, DBUG_PRINT, DBUG_RETURN, errno, m_maxSessions, SocketServer::ServiceInstance::m_service, m_services, SocketServer::ServiceInstance::m_socket, memset, NDB_CLOSE_SOCKET(), Ndb_getInAddr(), NDB_INVALID_SOCKET, NDB_SOCKET_TYPE, ndbout_c(), sock, and SOCKET_SIZE_TYPE.
Referenced by main(), and TransporterRegistry::start_service().
00090 { 00091 DBUG_ENTER("SocketServer::setup"); 00092 DBUG_PRINT("enter",("interface=%s, port=%u", intface, *port)); 00093 struct sockaddr_in servaddr; 00094 memset(&servaddr, 0, sizeof(servaddr)); 00095 servaddr.sin_family = AF_INET; 00096 servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 00097 servaddr.sin_port = htons(*port); 00098 00099 if(intface != 0){ 00100 if(Ndb_getInAddr(&servaddr.sin_addr, intface)) 00101 DBUG_RETURN(false); 00102 } 00103 00104 const NDB_SOCKET_TYPE sock = socket(AF_INET, SOCK_STREAM, 0); 00105 if (sock == NDB_INVALID_SOCKET) { 00106 DBUG_PRINT("error",("socket() - %d - %s", 00107 errno, strerror(errno))); 00108 DBUG_RETURN(false); 00109 } 00110 00111 DBUG_PRINT("info",("NDB_SOCKET: %d", sock)); 00112 00113 const int on = 1; 00114 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 00115 (const char*)&on, sizeof(on)) == -1) { 00116 DBUG_PRINT("error",("getsockopt() - %d - %s", 00117 errno, strerror(errno))); 00118 NDB_CLOSE_SOCKET(sock); 00119 DBUG_RETURN(false); 00120 } 00121 00122 if (bind(sock, (struct sockaddr*) &servaddr, sizeof(servaddr)) == -1) { 00123 DBUG_PRINT("error",("bind() - %d - %s", 00124 errno, strerror(errno))); 00125 NDB_CLOSE_SOCKET(sock); 00126 DBUG_RETURN(false); 00127 } 00128 00129 /* Get the port we bound to */ 00130 SOCKET_SIZE_TYPE sock_len = sizeof(servaddr); 00131 if(getsockname(sock,(struct sockaddr*)&servaddr,&sock_len)<0) { 00132 ndbout_c("An error occurred while trying to find out what" 00133 " port we bound to. Error: %s",strerror(errno)); 00134 NDB_CLOSE_SOCKET(sock); 00135 DBUG_RETURN(false); 00136 } 00137 00138 DBUG_PRINT("info",("bound to %u",ntohs(servaddr.sin_port))); 00139 if (listen(sock, m_maxSessions > 32 ? 32 : m_maxSessions) == -1){ 00140 DBUG_PRINT("error",("listen() - %d - %s", 00141 errno, strerror(errno))); 00142 NDB_CLOSE_SOCKET(sock); 00143 DBUG_RETURN(false); 00144 } 00145 00146 ServiceInstance i; 00147 i.m_socket = sock; 00148 i.m_service = service; 00149 m_services.push_back(i); 00150 00151 *port = ntohs(servaddr.sin_port); 00152 00153 DBUG_RETURN(true); 00154 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketServer::startServer | ( | ) |
start/stop the server
Definition at line 216 of file SocketServer.cpp.
References m_stopThread, m_thread, m_threadLock, NDB_THREAD_PRIO_LOW, NdbThread_Create(), and socketServerThread_C.
Referenced by main(), and TransporterFacade::threadMainSend().
00216 { 00217 m_threadLock.lock(); 00218 if(m_thread == 0 && m_stopThread == false){ 00219 m_thread = NdbThread_Create(socketServerThread_C, 00220 (void**)this, 00221 32768, 00222 "NdbSockServ", 00223 NDB_THREAD_PRIO_LOW); 00224 } 00225 m_threadLock.unlock(); 00226 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketServer::startSession | ( | SessionInstance & | ) | [private] |
Definition at line 259 of file SocketServer.cpp.
References SocketServer::SessionInstance::m_session, SocketServer::SessionInstance::m_thread, NDB_THREAD_PRIO_LOW, NdbThread_Create(), and sessionThread_C.
Referenced by doAccept().
00259 { 00260 si.m_thread = NdbThread_Create(sessionThread_C, 00261 (void**)si.m_session, 00262 32768, 00263 "NdbSock_Session", 00264 NDB_THREAD_PRIO_LOW); 00265 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketServer::stopServer | ( | ) |
Definition at line 229 of file SocketServer.cpp.
References m_stopThread, m_thread, m_threadLock, NdbThread_Destroy(), and NdbThread_WaitFor().
Referenced by main(), NdbShutdown(), and TransporterFacade::threadMainSend().
00229 { 00230 m_threadLock.lock(); 00231 if(m_thread != 0){ 00232 m_stopThread = true; 00233 00234 void * res; 00235 NdbThread_WaitFor(m_thread, &res); 00236 NdbThread_Destroy(&m_thread); 00237 m_thread = 0; 00238 } 00239 m_threadLock.unlock(); 00240 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void SocketServer::stopSessions | ( | bool | wait = false |
) |
stop sessions
Note: Implies stopServer
Definition at line 321 of file SocketServer.cpp.
References checkSessionsImpl(), m_services, m_session_mutex, m_sessions, and NdbSleep_MilliSleep().
Referenced by main(), NdbShutdown(), and TransporterFacade::threadMainSend().
00321 { 00322 int i; 00323 m_session_mutex.lock(); 00324 for(i = m_sessions.size() - 1; i>=0; i--) 00325 { 00326 m_sessions[i].m_session->stopSession(); 00327 m_sessions[i].m_session->m_stop = true; // to make sure 00328 } 00329 m_session_mutex.unlock(); 00330 00331 for(i = m_services.size() - 1; i>=0; i--) 00332 m_services[i].m_service->stopSessions(); 00333 00334 if(wait){ 00335 m_session_mutex.lock(); 00336 while(m_sessions.size() > 0){ 00337 checkSessionsImpl(); 00338 m_session_mutex.unlock(); 00339 NdbSleep_MilliSleep(100); 00340 m_session_mutex.lock(); 00341 } 00342 m_session_mutex.unlock(); 00343 } 00344 }
Here is the call graph for this function:

Here is the caller graph for this function:

| bool SocketServer::tryBind | ( | unsigned short | port, | |
| const char * | intface = 0 | |||
| ) | [static] |
Setup socket and bind it then close the socket Returns true if succeding in binding
Definition at line 52 of file SocketServer.cpp.
References DBUG_PRINT, memset, NDB_CLOSE_SOCKET(), Ndb_getInAddr(), NDB_INVALID_SOCKET, NDB_SOCKET_TYPE, and sock.
Referenced by MgmtSrvr::alloc_node_id(), Ndb_cluster_connection_impl::init_nodes_vector(), main(), and ConfigRetriever::verifyConfig().
00052 { 00053 struct sockaddr_in servaddr; 00054 memset(&servaddr, 0, sizeof(servaddr)); 00055 servaddr.sin_family = AF_INET; 00056 servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 00057 servaddr.sin_port = htons(port); 00058 00059 if(intface != 0){ 00060 if(Ndb_getInAddr(&servaddr.sin_addr, intface)) 00061 return false; 00062 } 00063 00064 const NDB_SOCKET_TYPE sock = socket(AF_INET, SOCK_STREAM, 0); 00065 if (sock == NDB_INVALID_SOCKET) { 00066 return false; 00067 } 00068 00069 DBUG_PRINT("info",("NDB_SOCKET: %d", sock)); 00070 00071 const int on = 1; 00072 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 00073 (const char*)&on, sizeof(on)) == -1) { 00074 NDB_CLOSE_SOCKET(sock); 00075 return false; 00076 } 00077 00078 if (bind(sock, (struct sockaddr*) &servaddr, sizeof(servaddr)) == -1) { 00079 NDB_CLOSE_SOCKET(sock); 00080 return false; 00081 } 00082 00083 NDB_CLOSE_SOCKET(sock); 00084 return true; 00085 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void* sessionThread_C | ( | void * | _sc | ) | [friend] |
Definition at line 350 of file SocketServer.cpp.
Referenced by startSession().
00350 { 00351 SocketServer::Session * si = (SocketServer::Session *)_sc; 00352 00353 if(!transfer(si->m_socket)){ 00354 si->m_stopped = true; 00355 return 0; 00356 } 00357 00362 if(!si->m_stopped) 00363 { 00364 if(!si->m_stop){ 00365 si->m_stopped = false; 00366 si->runSession(); 00367 } else { 00368 NDB_CLOSE_SOCKET(si->m_socket); 00369 } 00370 } 00371 00372 si->m_stopped = true; 00373 return 0; 00374 }
| void* socketServerThread_C | ( | void * | _ss | ) | [friend] |
Definition at line 209 of file SocketServer.cpp.
Referenced by startServer().
00209 { 00210 SocketServer * ss = (SocketServer *)_ss; 00211 ss->doRun(); 00212 return 0; 00213 }
unsigned SocketServer::m_maxSessions [private] |
Definition at line 124 of file SocketServer.hpp.
Referenced by doRun(), setup(), and SocketServer().
MutexVector<ServiceInstance> SocketServer::m_services [private] |
Definition at line 123 of file SocketServer.hpp.
Referenced by doAccept(), setup(), stopSessions(), and ~SocketServer().
NdbLockable SocketServer::m_session_mutex [private] |
Definition at line 121 of file SocketServer.hpp.
Referenced by checkSessions(), doAccept(), doRun(), foreachSession(), and stopSessions().
Vector<SessionInstance> SocketServer::m_sessions [private] |
Definition at line 122 of file SocketServer.hpp.
Referenced by checkSessionsImpl(), doAccept(), doRun(), foreachSession(), stopSessions(), and ~SocketServer().
bool SocketServer::m_stopThread [private] |
Note, this thread is only used when running interactive
Definition at line 134 of file SocketServer.hpp.
Referenced by doRun(), SocketServer(), startServer(), and stopServer().
struct NdbThread* SocketServer::m_thread [private] |
Definition at line 135 of file SocketServer.hpp.
Referenced by checkSessionsImpl(), SocketServer(), startServer(), and stopServer().
NdbLockable SocketServer::m_threadLock [private] |
1.4.7

