MongoDB  2.7.0
listen.h
1 // listen.h
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <set>
21 #include <string>
22 #include <vector>
23 
24 #include "mongo/platform/atomic_word.h"
25 #include "mongo/util/concurrency/ticketholder.h"
26 #include "mongo/util/log.h"
27 #include "mongo/util/net/sock.h"
28 
29 namespace mongo {
30 
31  const int DEFAULT_MAX_CONN = 1000000;
32 
33  class MessagingPort;
34 
35  class Listener : boost::noncopyable {
36  public:
37 
38  Listener(const std::string& name, const std::string &ip, int port, bool logConnect=true );
39 
40  virtual ~Listener();
41 
42  void initAndListen(); // never returns unless error (start a thread)
43 
44  /* spawn a thread, etc., then return */
45  virtual void accepted(boost::shared_ptr<Socket> psocket, long long connectionId );
46  virtual void acceptedMP(MessagingPort *mp);
47 
48  const int _port;
49 
61  long long getMyElapsedTimeMillis() const { return _elapsedTime; }
62 
67  void setupSockets();
68 
69  void setAsTimeTracker() {
70  _timeTracker = this;
71  }
72 
73  static const Listener* getTimeTracker() {
74  return _timeTracker;
75  }
76 
77  static long long getElapsedTimeMillis() {
78  if ( _timeTracker )
79  return _timeTracker->getMyElapsedTimeMillis();
80 
81  // should this assert or throw? seems like callers may not expect to get zero back, certainly not forever.
82  return 0;
83  }
84 
85  private:
86  std::vector<SockAddr> _mine;
87  std::vector<SOCKET> _socks;
88  std::string _name;
89  std::string _ip;
90  bool _setupSocketsSuccessful;
91  bool _logConnect;
92  long long _elapsedTime;
93 
94 #ifdef MONGO_SSL
95  SSLManagerInterface* _ssl;
96 #endif
97 
98  void _logListen( int port , bool ssl );
99 
100  static const Listener* _timeTracker;
101 
102  virtual bool useUnixSockets() const { return false; }
103 
104  public:
106  static AtomicInt64 globalConnectionNumber;
107 
110 
112  static void checkTicketNumbers();
113  };
114 
116  public:
118  : _mutex("ListeningSockets")
119  , _sockets( new std::set<int>() )
120  , _socketPaths( new std::set<std::string>() )
121  { }
122  void add( int sock ) {
123  scoped_lock lk( _mutex );
124  _sockets->insert( sock );
125  }
126  void addPath( const std::string& path ) {
127  scoped_lock lk( _mutex );
128  _socketPaths->insert( path );
129  }
130  void remove( int sock ) {
131  scoped_lock lk( _mutex );
132  _sockets->erase( sock );
133  }
134  void closeAll() {
135  std::set<int>* sockets;
136  std::set<std::string>* paths;
137 
138  {
139  scoped_lock lk( _mutex );
140  sockets = _sockets;
141  _sockets = new std::set<int>();
142  paths = _socketPaths;
143  _socketPaths = new std::set<std::string>();
144  }
145 
146  for ( std::set<int>::iterator i=sockets->begin(); i!=sockets->end(); i++ ) {
147  int sock = *i;
148  log() << "closing listening socket: " << sock << std::endl;
149  closesocket( sock );
150  }
151 
152  for ( std::set<std::string>::iterator i=paths->begin(); i!=paths->end(); i++ ) {
153  std::string path = *i;
154  log() << "removing socket file: " << path << std::endl;
155  ::remove( path.c_str() );
156  }
157  }
158  static ListeningSockets* get();
159  private:
160  mongo::mutex _mutex;
161  std::set<int>* _sockets;
162  std::set<std::string>* _socketPaths; // for unix domain sockets
163  static ListeningSockets* _instance;
164  };
165 
166 }
Definition: message_port.h:66
Definition: ticketholder.h:24
Definition: mutex.h:101
static AtomicInt64 globalConnectionNumber
the "next" connection number.
Definition: listen.h:106
static void checkTicketNumbers()
makes sure user input is sane
Definition: listen.cpp:555
static TicketHolder globalTicketHolder
keeps track of how many allowed connections there are and how many are being used ...
Definition: listen.h:109
Definition: listen.h:115
On pthread systems, it is an error to destroy a mutex while held (boost mutex may use pthread)...
Definition: mutex.h:74
LogstreamBuilder log()
Returns a LogstreamBuilder for logging a message with LogSeverity::Log().
Definition: log.h:69
Definition: listen.h:35
long long getMyElapsedTimeMillis() const
Definition: listen.h:61
void setupSockets()
Allocate sockets for the listener and set _setupSocketsSuccessful to true iff the process was success...
Definition: listen.cpp:114