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 #if 0
41 #ifdef _ACCEPT_USES_SOCKLEN_T
42 #define SOCK_TYPE socklen_t
43 #else
44 #define SOCK_TYPE int
45 #endif
46 #endif
47 
48 #include "SocketListener.h"
49 #include "BESInternalError.h"
50 #include "Socket.h"
51 #include "SocketConfig.h"
52 
54  : _accepting( false )
55 {
56 }
57 
59 {
60 }
61 
62 void
64 {
65  if( _accepting )
66  {
67  string err = (string)"Already accepting connections, "
68  + "no more sockets can be added" ;
69  throw BESInternalError( err, __FILE__, __LINE__ ) ;
70  }
71 
72  if( s && !s->isConnected() && !s->isListening() )
73  {
74  s->listen() ;
75  _socket_list[s->getSocketDescriptor()] = s ;
76  }
77  else
78  {
79  if( !s )
80  {
81  throw BESInternalError( "null socket passed", __FILE__, __LINE__ ) ;
82  }
83  else if( s->isConnected() )
84  {
85  string err( "socket already connected, cannot listen" ) ;
86  throw BESInternalError( err, __FILE__, __LINE__ ) ;
87  }
88  else if( s->isListening() )
89  {
90  string err( "socket already listening" ) ;
91  throw BESInternalError( err, __FILE__, __LINE__ ) ;
92  }
93  }
94 }
95 
97 Socket *
99 {
100  int msgsock ;
101 
102  fd_set read_fd ;
103  struct timeval timeout ;
104 
105  int maxfd ;
106 
107  for(;;)
108  {
109  timeout.tv_sec = 120 ;
110  timeout.tv_usec = 0 ;
111 
112  FD_ZERO( &read_fd ) ;
113 
114  maxfd = 0 ;
115  Socket_citer iter = _socket_list.begin() ;
116  for( ; iter != _socket_list.end(); iter++ )
117  {
118  Socket *s_ptr = (*iter).second ;
119  int s = s_ptr->getSocketDescriptor() ;
120  if( s > maxfd ) maxfd = s ;
121  FD_SET( s, &read_fd ) ;
122  }
123 
124 #if 0
125  // jhrg 5/16/11
126  //if( select( maxfd+1, &read_fd,
127  // (fd_set*)NULL, (fd_set*)NULL, &timeout) < 0 )
128 #endif
129  while (select(maxfd + 1, &read_fd, (fd_set*) NULL, (fd_set*) NULL, &timeout) < 0) {
130  switch (errno) {
131  case EAGAIN: // rerun select on interrupted calls, ...
132  case EINTR:
133  break;
134  case EBADF: // or exit on error
135  case EINVAL:
136  default: {
137  string err("select: ");
138  const char *error_info = strerror(errno);
139  if (error_info)
140  err += (string) error_info;
141  throw BESInternalError(err, __FILE__, __LINE__);
142  }
143  }
144  }
145 
146  iter = _socket_list.begin() ;
147  for( ; iter != _socket_list.end(); iter++ )
148  {
149  Socket *s_ptr = (*iter).second ;
150  int s = s_ptr->getSocketDescriptor() ;
151  if ( FD_ISSET( s, &read_fd ) )
152  {
153  struct sockaddr from ;
154  socklen_t len_from = sizeof( from ) ;
155 
156  while ((msgsock = ::accept(s, &from, &len_from)) < 0) {
157  if (errno == EINTR) {
158  continue;
159  }
160  else {
161  string err("accept: ");
162  const char *error_info = strerror(errno);
163  if (error_info)
164  err += (string) error_info;
165  throw BESInternalError(err, __FILE__, __LINE__);
166  }
167  }
168 
169  return s_ptr->newSocket( msgsock, (struct sockaddr *)&from ) ;
170  }
171  }
172  }
173 
174  return 0 ;
175 }
176 
183 void
184 SocketListener::dump( ostream &strm ) const
185 {
186  strm << BESIndent::LMarg << "SocketListener::dump - ("
187  << (void *)this << ")" << endl ;
189  if( _socket_list.size() )
190  {
191  strm << BESIndent::LMarg << "registered sockets:" << endl ;
192  Socket_citer i = _socket_list.begin() ;
193  Socket_citer ie = _socket_list.end() ;
194  for( ; i != ie; i++ )
195  {
196  strm << BESIndent::LMarg << "socket: " << (*i).first ;
197  Socket *s_ptr = (*i).second ;
198  s_ptr->dump( strm ) ;
199  }
200  }
201  else
202  {
203  strm << BESIndent::LMarg << "registered sockets: none" << endl ;
204  }
206 }
207 
Definition: Socket.h:44
virtual bool isListening()
Definition: Socket.h:65
exception thrown if inernal error encountered
virtual bool isConnected()
Definition: Socket.h:63
virtual ~SocketListener()
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Socket.cc:142
static void Indent()
Definition: BESIndent.cc:38
virtual Socket * accept()
Use the select() system call to wait for an incoming connection.
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
virtual void listen()=0
virtual void dump(ostream &strm) const
dumps information about this object
virtual int getSocketDescriptor()
Definition: Socket.h:74
static void UnIndent()
Definition: BESIndent.cc:44
virtual void listen(Socket *s)
virtual Socket * newSocket(int socket, struct sockaddr *addr)=0