Instance.hxx 4.87 KB
Newer Older
1
/*
2
 * Copyright 2003-2019 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
#include "util/Compiler.h"
28

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

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

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

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

49
#include <memory>
50 51
#include <list>

52 53
class ClientList;
struct Partition;
54
class StateFile;
55
class RemoteTagCache;
56
class StickerDatabase;
57
class InputCacheManager;
58

59 60 61 62 63 64 65 66
/**
 * 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;
};

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

90 91 92 93 94 95 96 97
	/**
	 * 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;

98 99 100 101
#ifdef ENABLE_SYSTEMD_DAEMON
	Systemd::Watchdog systemd_watchdog;
#endif

102 103
	std::unique_ptr<InputCacheManager> input_cache;

104
	MaskMonitor idle_monitor;
105

106
#ifdef ENABLE_NEIGHBOR_PLUGINS
107
	std::unique_ptr<NeighborGlue> neighbors;
108 109
#endif

110
#ifdef ENABLE_DATABASE
111
	DatabasePtr database;
112

113 114 115 116
	/**
	 * This is really a #CompositeStorage.  To avoid heavy include
	 * dependencies, we declare it as just #Storage.
	 */
117
	Storage *storage = nullptr;
118

119
	UpdateService *update = nullptr;
120
#endif
121

122 123 124 125
#ifdef ENABLE_CURL
	std::unique_ptr<RemoteTagCache> remote_tag_cache;
#endif

126
	std::unique_ptr<ClientList> client_list;
127

128
	std::list<Partition> partitions;
129

130
	StateFile *state_file = nullptr;
131

132 133 134 135
#ifdef ENABLE_SQLITE
	std::unique_ptr<StickerDatabase> sticker_database;
#endif

136
	Instance();
137
	~Instance() noexcept;
138

139
	/**
140
	 * Wrapper for EventLoop::Break().  Call to initiate shutdown.
141
	 */
142
	void Break() noexcept {
143 144
		event_loop.Break();
	}
145

146
	void EmitIdle(unsigned mask) noexcept {
147 148 149
		idle_monitor.OrMask(mask);
	}

150 151 152 153 154
	/**
	 * Find a #Partition with the given name.  Returns nullptr if
	 * no such partition was found.
	 */
	gcc_pure
155
	Partition *FindPartition(const char *name) noexcept;
156

157 158
	void BeginShutdownPartitions() noexcept;

159
#ifdef ENABLE_DATABASE
160 161 162 163 164
	/**
	 * Returns the global #Database instance.  May return nullptr
	 * if this MPD configuration has no database (no
	 * music_directory was configured).
	 */
165
	Database *GetDatabase() noexcept {
166
		return database.get();
167
	}
168 169 170 171 172 173 174

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

177 178 179 180 181 182
#ifdef ENABLE_SQLITE
	bool HasStickerDatabase() noexcept {
		return sticker_database != nullptr;
	}
#endif

183 184
	void BeginShutdownUpdate() noexcept;

185 186 187 188 189 190 191 192
#ifdef ENABLE_CURL
	void LookupRemoteTag(const char *uri) noexcept;
#else
	void LookupRemoteTag(const char *) noexcept {
		/* no-op */
	}
#endif

193
private:
194
#ifdef ENABLE_DATABASE
195 196 197
	/* virtual methods from class DatabaseListener */
	void OnDatabaseModified() noexcept override;
	void OnDatabaseSongRemoved(const char *uri) noexcept override;
198
#endif
199 200 201

#ifdef ENABLE_NEIGHBOR_PLUGINS
	/* virtual methods from class NeighborListener */
202 203
	void FoundNeighbor(const NeighborInfo &info) noexcept override;
	void LostNeighbor(const NeighborInfo &info) noexcept override;
204
#endif
205

206 207 208 209 210
#ifdef ENABLE_CURL
	/* virtual methods from class RemoteTagCacheHandler */
	void OnRemoteTag(const char *uri, const Tag &tag) noexcept override;
#endif

211
	/* callback for #idle_monitor */
212
	void OnIdle(unsigned mask) noexcept;
213 214 215
};

#endif