34 #include <sys/types.h>
35 #include <sys/socket.h>
41 #include <sys/select.h>
67 throw BESInternalError(
"Already accepting connections, no more sockets can be added", __FILE__, __LINE__);
77 throw BESInternalError(
"socket already connected, cannot listen", __FILE__, __LINE__);
87 BESDEBUG(
"ppt",
"SocketListener::accept() - START" << endl);
93 for (Socket_citer i = _socket_list.begin(), e = _socket_list.end(); i != e; i++) {
94 Socket *s_ptr = (*i).second;
105 while ((cpid = wait4(0 , &stat, WNOHANG, 0)) > 0) {
107 BESDEBUG(
"ppt2", bes_exit_message(cpid, stat) <<
"; num children: " << bes_num_children << endl);
110 struct timeval timeout;
111 timeout.tv_sec = 120;
113 while (select(maxfd + 1, &read_fd, (fd_set*) NULL, (fd_set*) NULL, &timeout) < 0) {
116 BESDEBUG(
"ppt2",
"SocketListener::accept() - select encountered EAGAIN" << endl);
123 BESDEBUG(
"ppt2",
"SocketListener::accept() - select encountered EINTR" << endl);
127 throw BESInternalError(
string(
"select: ") + strerror(errno), __FILE__, __LINE__);
131 BESDEBUG(
"ppt",
"SocketListener::accept() - select() completed without error." << endl);
133 for (Socket_citer i = _socket_list.begin(), e = _socket_list.end(); i != e; i++) {
134 Socket *s_ptr = (*i).second;
136 struct sockaddr from;
137 socklen_t len_from =
sizeof(from);
140 "SocketListener::accept() - Attempting to accept on "<< s_ptr->
getIp() <<
":"<<s_ptr->
getPort() << endl);
143 if (errno == EINTR) {
144 BESDEBUG(
"ppt2",
"SocketListener::accept() - accept() was interrupted" << endl);
148 throw BESInternalError(
string(
"accept: ") + strerror(errno), __FILE__, __LINE__);
152 BESDEBUG(
"ppt",
"SocketListener::accept() - END (returning new Socket)" << endl);
153 return s_ptr->
newSocket(msgsock, (
struct sockaddr *) &from);
157 BESDEBUG(
"ppt",
"SocketListener::accept() - END (returning 0)" << endl);
169 strm <<
BESIndent::LMarg <<
"SocketListener::dump - (" << (
void *)
this <<
")" << endl;
171 if (_socket_list.size()) {
173 Socket_citer i = _socket_list.begin();
174 Socket_citer ie = _socket_list.end();
175 for (; i != ie; i++) {
177 Socket *s_ptr = (*i).second;
virtual bool isListening()
exception thrown if inernal error encountered
virtual bool isConnected()
virtual ~SocketListener()
virtual void dump(ostream &strm) const
dumps information about this object
virtual Socket * accept()
Use the select() system call to wait for an incoming connection.
static ostream & LMarg(ostream &strm)
virtual void dump(ostream &strm) const
dumps information about this object
virtual int getSocketDescriptor()
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
virtual void listen(Socket *s)
virtual Socket * newSocket(int socket, struct sockaddr *addr)=0