MPD: lib/nfs/Connection.hxx Source File

MPD 0.20.15

Connection.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_CONNECTION_HXX
21 #define MPD_NFS_CONNECTION_HXX
22
23 #include "Cancellable.hxx"
24 #include "event/SocketMonitor.hxx"
25 #include "event/TimeoutMonitor.hxx"
26 #include "event/DeferredMonitor.hxx"
27
28 #include <string>
29 #include <list>
30 #include <forward_list>
31 #include <exception>
32
33 struct nfs_context;
34 struct nfsdir;
35 struct nfsdirent;
36 class NfsCallback;
37 class NfsLease;
38
42 class NfsConnection : SocketMonitor, TimeoutMonitor, DeferredMonitor {
43 class CancellableCallback : public CancellablePointer<NfsCallback> {
44 NfsConnection &connection;
45
52 const bool open;
53
58 struct nfsfh *close_fh;
59
60 public:
61 explicit CancellableCallback(NfsCallback &_callback,
62 NfsConnection &_connection,
63 bool _open)
64 :CancellablePointer<NfsCallback>(_callback),
65 connection(_connection),
66 open(_open), close_fh(nullptr) {}
67
68 void Stat(nfs_context *context, const char *path);
69 void OpenDirectory(nfs_context *context, const char *path);
70 void Open(nfs_context *context, const char *path, int flags);
71 void Stat(nfs_context *context, struct nfsfh *fh);
72 void Read(nfs_context *context, struct nfsfh *fh,
73 uint64_t offset, size_t size);
74
79 void CancelAndScheduleClose(struct nfsfh *fh);
80
86 void PrepareDestroyContext();
87
88 private:
89 static void Callback(int err, struct nfs_context *nfs,
90 void *data, void *private_data);
91 void Callback(int err, void *data);
92 };
93
94 std::string server, export_name;
95
96 nfs_context *context;
97
98 typedef std::list<NfsLease *> LeaseList;
99 LeaseList new_leases, active_leases;
100
101 typedef CancellableList<NfsCallback, CancellableCallback> CallbackList;
102 CallbackList callbacks;
103
111 std::forward_list<struct nfsfh *> deferred_close;
112
113 std::exception_ptr postponed_mount_error;
114
115 #ifndef NDEBUG
116
119 bool in_service;
120
125 bool in_event;
126
130 bool in_destroy;
131 #endif
132
133 bool mount_finished;
134
135 public:
136 gcc_nonnull_all
137 NfsConnection(EventLoop &_loop,
138 const char *_server, const char *_export_name) noexcept
139 :SocketMonitor(_loop), TimeoutMonitor(_loop),
140 DeferredMonitor(_loop),
141 server(_server), export_name(_export_name),
142 context(nullptr) {}
143
147 ~NfsConnection();
148
149 gcc_pure
150 const char *GetServer() const noexcept {
151 return server.c_str();
152 }
153
154 gcc_pure
155 const char *GetExportName() const noexcept {
156 return export_name.c_str();
157 }
158
159 EventLoop &GetEventLoop() noexcept {
160 return SocketMonitor::GetEventLoop();
161 }
162
170 void AddLease(NfsLease &lease);
171 void RemoveLease(NfsLease &lease);
172
173 void Stat(const char *path, NfsCallback &callback);
174
175 void OpenDirectory(const char *path, NfsCallback &callback);
176 const struct nfsdirent *ReadDirectory(struct nfsdir *dir);
177 void CloseDirectory(struct nfsdir *dir);
178
182 void Open(const char *path, int flags, NfsCallback &callback);
183
184 void Stat(struct nfsfh *fh, NfsCallback &callback);
185
189 void Read(struct nfsfh *fh, uint64_t offset, size_t size,
190 NfsCallback &callback);
191
192 void Cancel(NfsCallback &callback);
193
194 void Close(struct nfsfh *fh);
195 void CancelAndClose(struct nfsfh *fh, NfsCallback &callback);
196
197 protected:
198 virtual void OnNfsConnectionError(std::exception_ptr &&e) = 0;
199
200 private:
201 void DestroyContext();
202
206 void InternalClose(struct nfsfh *fh);
207
211 void DeferClose(struct nfsfh *fh);
212
213 void MountInternal();
214 void BroadcastMountSuccess();
215 void BroadcastMountError(std::exception_ptr &&e);
216 void BroadcastError(std::exception_ptr &&e);
217
218 static void MountCallback(int status, nfs_context *nfs, void *data,
219 void *private_data);
220 void MountCallback(int status, nfs_context *nfs, void *data);
221
222 void ScheduleSocket();
223
227 int Service(unsigned flags);
228
229 /* virtual methods from SocketMonitor */
230 virtual bool OnSocketReady(unsigned flags) override;
231
232 /* virtual methods from TimeoutMonitor */
233 void OnTimeout() final;
234
235 /* virtual methods from DeferredMonitor */
236 virtual void RunDeferred() override;
237 };
238
239 #endif
NfsLease
Definition: Lease.hxx:27
NfsConnection::GetExportName
gcc_pure const char * GetExportName() const noexcept
Definition: Connection.hxx:155
CancellableList< NfsCallback, CancellableCallback >
NfsConnection::GetServer
gcc_pure const char * GetServer() const noexcept
Definition: Connection.hxx:150
gcc_nonnull_all
#define gcc_nonnull_all
Definition: Compiler.h:122
NfsConnection::GetEventLoop
EventLoop & GetEventLoop() noexcept
Definition: Connection.hxx:159
NfsConnection::CloseDirectory
void CloseDirectory(struct nfsdir *dir)
CancellablePointer
Definition: Cancellable.hxx:32
SocketMonitor::SocketMonitor
SocketMonitor(EventLoop &_loop)
Definition: SocketMonitor.hxx:71
TimeoutMonitor
This class monitors a timeout.
Definition: TimeoutMonitor.hxx:37
NfsConnection::OpenDirectory
void OpenDirectory(const char *path, NfsCallback &callback)
Cancellable.hxx
NfsConnection::~NfsConnection
~NfsConnection()
Must be run from EventLoop&#39;s thread.
NfsConnection::ReadDirectory
const struct nfsdirent * ReadDirectory(struct nfsdir *dir)
EventLoop
An event loop that polls for events on file/socket descriptors.
Definition: Loop.hxx:51
DeferredMonitor
Defer execution of an event into an EventLoop.
Definition: DeferredMonitor.hxx:32
SocketMonitor.hxx
NfsConnection::RemoveLease
void RemoveLease(NfsLease &lease)
NfsConnection::NfsConnection
gcc_nonnull_all NfsConnection(EventLoop &_loop, const char *_server, const char *_export_name) noexcept
Definition: Connection.hxx:137
NfsConnection::Open
void Open(const char *path, int flags, NfsCallback &callback)
Throws std::runtime_error on error.
TimeoutMonitor.hxx
SocketMonitor
Monitor events on a socket.
Definition: SocketMonitor.hxx:54
NfsConnection
An asynchronous connection to a NFS server.
Definition: Connection.hxx:42
SocketMonitor::GetEventLoop
EventLoop & GetEventLoop()
Definition: SocketMonitor.hxx:79
DeferredMonitor::DeferredMonitor
DeferredMonitor(EventLoop &_loop)
Definition: DeferredMonitor.hxx:39
DeferredMonitor::Cancel
void Cancel()
NfsCallback
Callbacks for an asynchronous libnfs operation.
Definition: Callback.hxx:32
DeferredMonitor.hxx
NfsConnection::Read
void Read(struct nfsfh *fh, uint64_t offset, size_t size, NfsCallback &callback)
Throws std::runtime_error on error.
TimeoutMonitor::TimeoutMonitor
TimeoutMonitor(EventLoop &_loop)
Definition: TimeoutMonitor.hxx:45
NfsConnection::CancelAndClose
void CancelAndClose(struct nfsfh *fh, NfsCallback &callback)
SocketMonitor::Close
void Close()
e
int e
Definition: Log.hxx:115
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
NfsConnection::Stat
void Stat(const char *path, NfsCallback &callback)
NfsConnection::AddLease
void AddLease(NfsLease &lease)
Ensure that the connection is established.
NfsConnection::OnNfsConnectionError
virtual void OnNfsConnectionError(std::exception_ptr &&e)=0