OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
SocketListener.cc
Go to the documentation of this file.
1 // SocketListener.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <ctype.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 
37 #include <cstring>
38 #include <cerrno>
39 
40 #include "SocketListener.h"
41 #include "BESInternalError.h"
42 #include "Socket.h"
43 #include "SocketConfig.h"
44 
46  : _accepting( false )
47 {
48 }
49 
51 {
52 }
53 
54 void
56 {
57  if( _accepting )
58  {
59  string err = (string)"Already accepting connections, "
60  + "no more sockets can be added" ;
61  throw BESInternalError( err, __FILE__, __LINE__ ) ;
62  }
63 
64  if( s && !s->isConnected() && !s->isListening() )
65  {
66  s->listen() ;
67  _socket_list[s->getSocketDescriptor()] = s ;
68  }
69  else
70  {
71  if( !s )
72  {
73  throw BESInternalError( "null socket passed", __FILE__, __LINE__ ) ;
74  }
75  else if( s->isConnected() )
76  {
77  string err( "socket already connected, cannot listen" ) ;
78  throw BESInternalError( err, __FILE__, __LINE__ ) ;
79  }
80  else if( s->isListening() )
81  {
82  string err( "socket already listening" ) ;
83  throw BESInternalError( err, __FILE__, __LINE__ ) ;
84  }
85  }
86 }
87 
89 Socket *
91 {
92  int msgsock ;
93 
94  fd_set read_fd ;
95  struct timeval timeout ;
96 
97  int maxfd ;
98 
99  for(;;)
100  {
101  timeout.tv_sec = 120 ;
102  timeout.tv_usec = 0 ;
103 
104  FD_ZERO( &read_fd ) ;
105 
106  maxfd = 0 ;
107  Socket_citer iter = _socket_list.begin() ;
108  for( ; iter != _socket_list.end(); iter++ )
109  {
110  Socket *s_ptr = (*iter).second ;
111  int s = s_ptr->getSocketDescriptor() ;
112  if( s > maxfd ) maxfd = s ;
113  FD_SET( s, &read_fd ) ;
114  }
115 
116  if( select( maxfd+1, &read_fd,
117  (fd_set*)NULL, (fd_set*)NULL, &timeout) < 0 )
118  {
119  string err( "selecting sockets " ) ;
120  const char *error_info = strerror( errno ) ;
121  if( error_info )
122  err += " " + (string)error_info ;
123  throw BESInternalError( err, __FILE__, __LINE__ ) ;
124  }
125 
126  iter = _socket_list.begin() ;
127  for( ; iter != _socket_list.end(); iter++ )
128  {
129  Socket *s_ptr = (*iter).second ;
130  int s = s_ptr->getSocketDescriptor() ;
131  if ( FD_ISSET( s, &read_fd ) )
132  {
133  struct sockaddr from ;
134  int len_from = sizeof( from ) ;
135 #ifdef _ACCEPT_USES_SOCKLEN_T
136  msgsock = ::accept( s, (struct sockaddr *)&from,
137  (socklen_t *)&len_from ) ;
138 #else
139  msgsock = ::accept( s, (struct sockaddr *)&from,
140  &len_from ) ;
141 #endif
142  return s_ptr->newSocket( msgsock, (struct sockaddr *)&from );
143  }
144  }
145  }
146  return 0 ;
147 }
148 
155 void
156 SocketListener::dump( ostream &strm ) const
157 {
158  strm << BESIndent::LMarg << "SocketListener::dump - ("
159  << (void *)this << ")" << endl ;
161  if( _socket_list.size() )
162  {
163  strm << BESIndent::LMarg << "registered sockets:" << endl ;
164  Socket_citer i = _socket_list.begin() ;
165  Socket_citer ie = _socket_list.end() ;
166  for( ; i != ie; i++ )
167  {
168  strm << BESIndent::LMarg << "socket: " << (*i).first ;
169  Socket *s_ptr = (*i).second ;
170  s_ptr->dump( strm ) ;
171  }
172  }
173  else
174  {
175  strm << BESIndent::LMarg << "registered sockets: none" << endl ;
176  }
178 }
179