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