SocketListener.cc

Go to the documentation of this file.
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,2005 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 <errno.h>
00035 #include <sys/types.h>
00036 #include <sys/socket.h>
00037 
00038 #include "SocketListener.h"
00039 #include "SocketException.h"
00040 #include "Socket.h"
00041 #include "SocketConfig.h"
00042 
00043 SocketListener::SocketListener()
00044     : _accepting( false )
00045 {
00046 }
00047 
00048 SocketListener::~SocketListener()
00049 {
00050 }
00051 
00052 void
00053 SocketListener::listen( Socket *s )
00054 {
00055     if( _accepting )
00056     {
00057         string err( "Already accepting connections ... no more sockets can be added" ) ;
00058         throw SocketException( err, __FILE__, __LINE__ ) ;
00059     }
00060 
00061     if( s && !s->isConnected() && !s->isListening() )
00062     {
00063         s->listen() ;
00064         _socket_list[s->getSocketDescriptor()] = s ;
00065     }
00066     else
00067     {
00068         if( !s )
00069         {
00070             throw SocketException( "null socket passed", __FILE__, __LINE__ ) ;
00071         }
00072         else if( s->isConnected() )
00073         {
00074             string err( "socket already connected, cannot listen" ) ;
00075             throw SocketException( err, __FILE__, __LINE__ ) ;
00076         }
00077         else if( s->isListening() )
00078         {
00079             string err( "socket already listening" ) ;
00080             throw SocketException( err, __FILE__, __LINE__ ) ;
00081         }
00082     }
00083 }
00084 
00086 Socket *
00087 SocketListener::accept()
00088 {
00089     int msgsock ;
00090 
00091     fd_set read_fd ;
00092     struct timeval timeout ;
00093 
00094     int maxfd ;
00095 
00096     for(;;)
00097     {
00098         timeout.tv_sec = 120 ;
00099         timeout.tv_usec = 0 ;
00100 
00101         FD_ZERO( &read_fd ) ;
00102 
00103         maxfd = 0 ;
00104         Socket_citer iter = _socket_list.begin() ;
00105         for( ; iter != _socket_list.end(); iter++ )
00106         {
00107             Socket *s_ptr = (*iter).second ;
00108             int s = s_ptr->getSocketDescriptor() ;
00109             if( s > maxfd ) maxfd = s ;
00110             FD_SET( s, &read_fd ) ;
00111         }
00112 
00113         if( select( maxfd+1, &read_fd,
00114                     (fd_set*)NULL, (fd_set*)NULL, &timeout) < 0 )
00115         {
00116             string err( "selecting sockets " ) ;
00117             const char *error_info = strerror( errno ) ;
00118             if( error_info )
00119                 err += " " + (string)error_info ;
00120             throw SocketException( err, __FILE__, __LINE__ ) ;
00121         }
00122 
00123         iter = _socket_list.begin() ;
00124         for( ; iter != _socket_list.end(); iter++ )
00125         {
00126             Socket *s_ptr = (*iter).second ;
00127             int s = s_ptr->getSocketDescriptor() ;
00128             if ( FD_ISSET( s, &read_fd ) )  
00129             {    
00130                 struct sockaddr_in from ;
00131                 int len_from = sizeof( from ) ;
00132 #ifdef _ACCEPT_USES_SOCKLEN_T 
00133                 msgsock = ::accept( s, (struct sockaddr *)&from,
00134                                     (socklen_t *)&len_from ) ;
00135 #else
00136                 msgsock = ::accept( s, (struct sockaddr *)&from,
00137                                     &len_from ) ;
00138 #endif
00139                 return s_ptr->newSocket( msgsock, from ) ;
00140             }
00141         }
00142     }
00143     return 0 ;
00144 }
00145 
00152 void
00153 SocketListener::dump( ostream &strm ) const
00154 {
00155     strm << BESIndent::LMarg << "SocketListener::dump - ("
00156                              << (void *)this << ")" << endl ;
00157     BESIndent::Indent() ;
00158     if( _socket_list.size() )
00159     {
00160         strm << BESIndent::LMarg << "registered sockets:" << endl ;
00161         Socket_citer i = _socket_list.begin() ;
00162         Socket_citer ie = _socket_list.end() ;
00163         for( ; i != ie; i++ )
00164         {
00165             strm << BESIndent::LMarg << "socket: " << (*i).first ;
00166             Socket *s_ptr = (*i).second ;
00167             s_ptr->dump( strm ) ;
00168         }
00169     }
00170     else
00171     {
00172         strm << BESIndent::LMarg << "registered sockets: none" << endl ;
00173     }
00174     BESIndent::UnIndent() ;
00175 }
00176 

Generated on Wed Aug 29 02:59:02 2007 for OPeNDAP Back End Server (BES) by  doxygen 1.5.2