MPD  0.20.6
Manager.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_NFS_MANAGER_HXX
21 #define MPD_NFS_MANAGER_HXX
22 
23 #include "check.h"
24 #include "Connection.hxx"
25 #include "Compiler.h"
26 #include "event/IdleMonitor.hxx"
27 
28 #include <boost/intrusive/set.hpp>
29 #include <boost/intrusive/slist.hpp>
30 
35 class NfsManager final : IdleMonitor {
36  struct LookupKey {
37  const char *server;
38  const char *export_name;
39  };
40 
41  class ManagedConnection final
42  : public NfsConnection,
43  public boost::intrusive::slist_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>>,
44  public boost::intrusive::set_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>> {
45  NfsManager &manager;
46 
47  public:
48  ManagedConnection(NfsManager &_manager, EventLoop &_loop,
49  const char *_server,
50  const char *_export_name)
51  :NfsConnection(_loop, _server, _export_name),
52  manager(_manager) {}
53 
54  protected:
55  /* virtual methods from NfsConnection */
56  void OnNfsConnectionError(std::exception_ptr &&e) override;
57  };
58 
59  struct Compare {
60  gcc_pure
61  bool operator()(const LookupKey a,
62  const ManagedConnection &b) const;
63 
64  gcc_pure
65  bool operator()(const ManagedConnection &a,
66  const LookupKey b) const;
67 
68  gcc_pure
69  bool operator()(const ManagedConnection &a,
70  const ManagedConnection &b) const;
71  };
72 
76  typedef boost::intrusive::set<ManagedConnection,
77  boost::intrusive::compare<Compare>,
78  boost::intrusive::constant_time_size<false>> Map;
79 
80  Map connections;
81 
82  typedef boost::intrusive::slist<ManagedConnection> List;
83 
89  List garbage;
90 
91 public:
93  :IdleMonitor(_loop) {}
94 
98  ~NfsManager();
99 
100  gcc_pure
101  NfsConnection &GetConnection(const char *server,
102  const char *export_name);
103 
104 private:
105  void ScheduleDelete(ManagedConnection &c) {
106  connections.erase(connections.iterator_to(c));
107  garbage.push_front(c);
109  }
110 
114  void CollectGarbage();
115 
116  /* virtual methods from IdleMonitor */
117  void OnIdle() override;
118 };
119 
120 #endif
~NfsManager()
Must be run from EventLoop's thread.
An event that runs when the EventLoop has become idle, before waiting for more events.
Definition: IdleMonitor.hxx:35
An event loop that polls for events on file/socket descriptors.
Definition: Loop.hxx:51
NfsManager(EventLoop &_loop)
Definition: Manager.hxx:92
A manager for NFS connections.
Definition: Manager.hxx:35
An asynchronous connection to a NFS server.
Definition: Connection.hxx:42
void Schedule()
int e
Definition: Log.hxx:115
#define gcc_pure
Definition: Compiler.h:116
gcc_pure NfsConnection & GetConnection(const char *server, const char *export_name)