OpenSDN source code
server_manager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #ifndef SRC_IO_SERVER_MANAGER_H_
7 #define SRC_IO_SERVER_MANAGER_H_
8 
9 #include <set>
10 #include <mutex>
11 
12 //
13 // ServerManager is the place holder for all the TcpServer and UdpServer
14 // objects instantiated in the life time of a process
15 //
16 // TcpServer and UdpServer objects are help in ServerSet until all the cleanup
17 // is complete and only then should they be deleted via DeleteServer() API
18 //
19 // Since TcpServer and UdpServer bjects are also held by boost::asio routines,
20 // they are protected using intrusive pointers
21 //
22 // This is similar to how TcpSession objects are managed via TcpSessionPtr
23 //
24 template <typename ServerType, typename ServerPtrType>
26 public:
27  //
28  // Add a server object to the data base, by creating an intrusive reference
29  //
30  static void AddServer(ServerType *server) {
31  std::scoped_lock lock(mutex_);
32  server_ref_.insert(ServerPtrType(server));
33  }
34  //
35  // Delete a server object from the data base, by removing the intrusive
36  // reference. If any other objects has a reference to this server such as
37  // boost::asio, the server object deletion is automatically deferred
38  //
39  static void DeleteServer(ServerType *server) {
40  std::scoped_lock lock(mutex_);
41  server_ref_.erase(ServerPtrType(server));
42  }
43  //
44  // Return number of Servers.
45  // Used in tests to ensure there are no leaks.
46  //
47  size_t GetServerCount() {
48  return server_ref_.size();
49  }
50 
51 private:
52  struct ServerPtrCmp {
53  bool operator()(const ServerPtrType &lhs,
54  const ServerPtrType &rhs) const {
55  return lhs.get() < rhs.get();
56  }
57  };
58  typedef std::set<ServerPtrType, ServerPtrCmp> ServerSet;
59 
60  static std::mutex mutex_;
62 };
63 
64 template <typename ServerType, typename ServerPtrType>
67 
68 template <typename ServerType, typename ServerPtrType>
70 
71 #endif // SRC_IO_SERVER_MANAGER_H_
static std::mutex mutex_
static void AddServer(ServerType *server)
size_t GetServerCount()
static ServerSet server_ref_
std::set< ServerPtrType, ServerPtrCmp > ServerSet
static void DeleteServer(ServerType *server)
bool operator()(const ServerPtrType &lhs, const ServerPtrType &rhs) const