OPeNDAP Hyrax Back End Server (BES)
Updated for version 3.8.3
|
00001 // SocketListener.cc 00002 00003 // This file is part of bes, A C++ back-end server implementation framework 00004 // for the OPeNDAP Data Access Protocol. 00005 00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research 00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu> 00008 // 00009 // This library is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU Lesser General Public 00011 // License as published by the Free Software Foundation; either 00012 // version 2.1 of the License, or (at your option) any later version. 00013 // 00014 // This library is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 // Lesser General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU Lesser General Public 00020 // License along with this library; if not, write to the Free Software 00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 // 00023 // You can contact University Corporation for Atmospheric Research at 00024 // 3080 Center Green Drive, Boulder, CO 80301 00025 00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005 00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR. 00028 // 00029 // Authors: 00030 // pwest Patrick West <pwest@ucar.edu> 00031 // jgarcia Jose Garcia <jgarcia@ucar.edu> 00032 00033 #include <ctype.h> 00034 #include <sys/types.h> 00035 #include <sys/socket.h> 00036 00037 #include <cstring> 00038 #include <cerrno> 00039 00040 #include "SocketListener.h" 00041 #include "BESInternalError.h" 00042 #include "Socket.h" 00043 #include "SocketConfig.h" 00044 00045 SocketListener::SocketListener() 00046 : _accepting( false ) 00047 { 00048 } 00049 00050 SocketListener::~SocketListener() 00051 { 00052 } 00053 00054 void 00055 SocketListener::listen( Socket *s ) 00056 { 00057 if( _accepting ) 00058 { 00059 string err = (string)"Already accepting connections, " 00060 + "no more sockets can be added" ; 00061 throw BESInternalError( err, __FILE__, __LINE__ ) ; 00062 } 00063 00064 if( s && !s->isConnected() && !s->isListening() ) 00065 { 00066 s->listen() ; 00067 _socket_list[s->getSocketDescriptor()] = s ; 00068 } 00069 else 00070 { 00071 if( !s ) 00072 { 00073 throw BESInternalError( "null socket passed", __FILE__, __LINE__ ) ; 00074 } 00075 else if( s->isConnected() ) 00076 { 00077 string err( "socket already connected, cannot listen" ) ; 00078 throw BESInternalError( err, __FILE__, __LINE__ ) ; 00079 } 00080 else if( s->isListening() ) 00081 { 00082 string err( "socket already listening" ) ; 00083 throw BESInternalError( err, __FILE__, __LINE__ ) ; 00084 } 00085 } 00086 } 00087 00089 Socket * 00090 SocketListener::accept() 00091 { 00092 int msgsock ; 00093 00094 fd_set read_fd ; 00095 struct timeval timeout ; 00096 00097 int maxfd ; 00098 00099 for(;;) 00100 { 00101 timeout.tv_sec = 120 ; 00102 timeout.tv_usec = 0 ; 00103 00104 FD_ZERO( &read_fd ) ; 00105 00106 maxfd = 0 ; 00107 Socket_citer iter = _socket_list.begin() ; 00108 for( ; iter != _socket_list.end(); iter++ ) 00109 { 00110 Socket *s_ptr = (*iter).second ; 00111 int s = s_ptr->getSocketDescriptor() ; 00112 if( s > maxfd ) maxfd = s ; 00113 FD_SET( s, &read_fd ) ; 00114 } 00115 00116 if( select( maxfd+1, &read_fd, 00117 (fd_set*)NULL, (fd_set*)NULL, &timeout) < 0 ) 00118 { 00119 string err( "selecting sockets " ) ; 00120 const char *error_info = strerror( errno ) ; 00121 if( error_info ) 00122 err += " " + (string)error_info ; 00123 throw BESInternalError( err, __FILE__, __LINE__ ) ; 00124 } 00125 00126 iter = _socket_list.begin() ; 00127 for( ; iter != _socket_list.end(); iter++ ) 00128 { 00129 Socket *s_ptr = (*iter).second ; 00130 int s = s_ptr->getSocketDescriptor() ; 00131 if ( FD_ISSET( s, &read_fd ) ) 00132 { 00133 struct sockaddr from ; 00134 int len_from = sizeof( from ) ; 00135 #ifdef _ACCEPT_USES_SOCKLEN_T 00136 msgsock = ::accept( s, (struct sockaddr *)&from, 00137 (socklen_t *)&len_from ) ; 00138 #else 00139 msgsock = ::accept( s, (struct sockaddr *)&from, 00140 &len_from ) ; 00141 #endif 00142 return s_ptr->newSocket( msgsock, (struct sockaddr *)&from ); 00143 } 00144 } 00145 } 00146 return 0 ; 00147 } 00148 00155 void 00156 SocketListener::dump( ostream &strm ) const 00157 { 00158 strm << BESIndent::LMarg << "SocketListener::dump - (" 00159 << (void *)this << ")" << endl ; 00160 BESIndent::Indent() ; 00161 if( _socket_list.size() ) 00162 { 00163 strm << BESIndent::LMarg << "registered sockets:" << endl ; 00164 Socket_citer i = _socket_list.begin() ; 00165 Socket_citer ie = _socket_list.end() ; 00166 for( ; i != ie; i++ ) 00167 { 00168 strm << BESIndent::LMarg << "socket: " << (*i).first ; 00169 Socket *s_ptr = (*i).second ; 00170 s_ptr->dump( strm ) ; 00171 } 00172 } 00173 else 00174 { 00175 strm << BESIndent::LMarg << "registered sockets: none" << endl ; 00176 } 00177 BESIndent::UnIndent() ; 00178 } 00179