Instance.hxx 5.47 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_INSTANCE_HXX
#define MPD_INSTANCE_HXX

23
#include "config.h"
24
#include "event/Loop.hxx"
25
#include "event/Thread.hxx"
26
#include "event/MaskMonitor.hxx"
27

28 29 30 31
#ifdef ENABLE_SYSTEMD_DAEMON
#include "lib/systemd/Watchdog.hxx"
#endif

32 33 34 35
#ifdef ENABLE_CURL
#include "RemoteTagCacheHandler.hxx"
#endif

36 37 38 39 40
#ifdef ENABLE_NEIGHBOR_PLUGINS
#include "neighbor/Listener.hxx"
class NeighborGlue;
#endif

41 42
#ifdef ENABLE_DATABASE
#include "db/DatabaseListener.hxx"
43
#include "db/Ptr.hxx"
44
class Storage;
45
class UpdateService;
46 47 48
#ifdef ENABLE_INOTIFY
class InotifyUpdate;
#endif
49 50
#endif

51
#include <memory>
52 53
#include <list>

54 55
class ClientList;
struct Partition;
56
class StateFile;
57
class RemoteTagCache;
58
class StickerDatabase;
59
class InputCacheManager;
60

61 62 63 64 65 66 67 68
/**
 * A utility class which, when used as the first base class, ensures
 * that the #EventLoop gets initialized before the other base classes.
 */
struct EventLoopHolder {
	EventLoop event_loop;
};

69
struct Instance final
70
	: EventLoopHolder
71
#if defined(ENABLE_DATABASE) || defined(ENABLE_NEIGHBOR_PLUGINS)
72
	,
73 74 75
#endif
#ifdef ENABLE_DATABASE
	public DatabaseListener
76
#ifdef ENABLE_NEIGHBOR_PLUGINS
77 78 79 80 81
	,
#endif
#endif
#ifdef ENABLE_NEIGHBOR_PLUGINS
	public NeighborListener
82
#endif
83 84 85
#ifdef ENABLE_CURL
	, public RemoteTagCacheHandler
#endif
86
{
87 88 89
	/**
	 * A thread running an #EventLoop for non-blocking (bulk) I/O.
	 */
90 91
	EventThread io_thread;

92 93 94 95 96 97 98 99
	/**
	 * Another thread running an #EventLoop for non-blocking
	 * (real-time) I/O.  This is used instead of #io_thread for
	 * events which require low latency, e.g. for filling hardware
	 * ring buffers.
	 */
	EventThread rtio_thread;

100 101 102 103
#ifdef ENABLE_SYSTEMD_DAEMON
	Systemd::Watchdog systemd_watchdog;
#endif

104 105
	std::unique_ptr<InputCacheManager> input_cache;

106 107 108 109
	/**
	 * Monitor for global idle events to be broadcasted to all
	 * partitions.
	 */
110
	MaskMonitor idle_monitor;
111

112
#ifdef ENABLE_NEIGHBOR_PLUGINS
113
	std::unique_ptr<NeighborGlue> neighbors;
114 115
#endif

116
#ifdef ENABLE_DATABASE
117
	DatabasePtr database;
118

119 120 121 122
	/**
	 * This is really a #CompositeStorage.  To avoid heavy include
	 * dependencies, we declare it as just #Storage.
	 */
123
	Storage *storage = nullptr;
124

125
	UpdateService *update = nullptr;
126 127 128 129

#ifdef ENABLE_INOTIFY
	std::unique_ptr<InotifyUpdate> inotify_update;
#endif
130
#endif
131

132 133 134 135
#ifdef ENABLE_CURL
	std::unique_ptr<RemoteTagCache> remote_tag_cache;
#endif

136
	std::unique_ptr<ClientList> client_list;
137

138
	std::list<Partition> partitions;
139

140
	std::unique_ptr<StateFile> state_file;
141

142 143 144 145
#ifdef ENABLE_SQLITE
	std::unique_ptr<StickerDatabase> sticker_database;
#endif

146
	Instance();
147
	~Instance() noexcept;
148

149
	/**
150
	 * Wrapper for EventLoop::Break().  Call to initiate shutdown.
151
	 */
152
	void Break() noexcept {
153 154
		event_loop.Break();
	}
155

156 157 158 159 160
	/**
	 * Emit an "idle" event to all clients of all partitions.
	 *
	 * This method can be called from any thread.
	 */
161
	void EmitIdle(unsigned mask) noexcept {
162 163 164
		idle_monitor.OrMask(mask);
	}

165 166 167 168 169 170 171 172
	/**
	 * Notify the #Instance that the state has been modified, and
	 * the #StateFile may need to be saved.
	 *
	 * This method must be called from the main thread.
	 */
	void OnStateModified() noexcept;

173 174 175 176
	/**
	 * Find a #Partition with the given name.  Returns nullptr if
	 * no such partition was found.
	 */
177
	[[gnu::pure]]
178
	Partition *FindPartition(const char *name) noexcept;
179

180 181
	void DeletePartition(Partition &partition) noexcept;

182 183
	void BeginShutdownPartitions() noexcept;

184
#ifdef ENABLE_DATABASE
185 186 187 188 189
	/**
	 * Returns the global #Database instance.  May return nullptr
	 * if this MPD configuration has no database (no
	 * music_directory was configured).
	 */
190
	Database *GetDatabase() noexcept {
191
		return database.get();
192
	}
193 194 195 196 197 198 199

	/**
	 * Returns the global #Database instance.  Throws
	 * DatabaseError if this MPD configuration has no database (no
	 * music_directory was configured).
	 */
	const Database &GetDatabaseOrThrow() const;
200 201
#endif

202
#ifdef ENABLE_SQLITE
203
	bool HasStickerDatabase() const noexcept {
204 205 206 207
		return sticker_database != nullptr;
	}
#endif

208 209
	void BeginShutdownUpdate() noexcept;

210 211 212 213 214 215 216 217
#ifdef ENABLE_CURL
	void LookupRemoteTag(const char *uri) noexcept;
#else
	void LookupRemoteTag(const char *) noexcept {
		/* no-op */
	}
#endif

218 219
	void FlushCaches() noexcept;

220
private:
221
#ifdef ENABLE_DATABASE
222 223 224
	/* virtual methods from class DatabaseListener */
	void OnDatabaseModified() noexcept override;
	void OnDatabaseSongRemoved(const char *uri) noexcept override;
225
#endif
226 227 228

#ifdef ENABLE_NEIGHBOR_PLUGINS
	/* virtual methods from class NeighborListener */
229 230
	void FoundNeighbor(const NeighborInfo &info) noexcept override;
	void LostNeighbor(const NeighborInfo &info) noexcept override;
231
#endif
232

233 234 235 236 237
#ifdef ENABLE_CURL
	/* virtual methods from class RemoteTagCacheHandler */
	void OnRemoteTag(const char *uri, const Tag &tag) noexcept override;
#endif

238
	/* callback for #idle_monitor */
239
	void OnIdle(unsigned mask) noexcept;
240 241 242
};

#endif