Instance.cxx 3.96 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.
 */

#include "config.h"
#include "Instance.hxx"
#include "Partition.hxx"
23
#include "IdleFlags.hxx"
24
#include "StateFile.hxx"
25
#include "Stats.hxx"
26
#include "client/List.hxx"
27
#include "input/cache/Manager.hxx"
28

29 30
#ifdef ENABLE_CURL
#include "RemoteTagCache.hxx"
Max Kellermann's avatar
Max Kellermann committed
31
#include "util/UriExtract.hxx"
32 33
#endif

34
#ifdef ENABLE_DATABASE
35
#include "db/DatabaseError.hxx"
36
#include "db/Interface.hxx"
37
#include "db/update/Service.hxx"
38
#include "storage/StorageInterface.hxx"
39

40 41 42 43
#ifdef ENABLE_NEIGHBOR_PLUGINS
#include "neighbor/Glue.hxx"
#endif

44
#ifdef ENABLE_SQLITE
45
#include "sticker/Database.hxx"
46 47
#include "sticker/SongSticker.hxx"
#endif
48 49
#endif

50
Instance::Instance()
51
	:rtio_thread(true),
52 53 54
#ifdef ENABLE_SYSTEMD_DAEMON
	 systemd_watchdog(event_loop),
#endif
55
	 idle_monitor(event_loop, BIND_THIS_METHOD(OnIdle))
56 57 58
{
}

59 60 61
Instance::~Instance() noexcept
{
#ifdef ENABLE_DATABASE
62 63
	delete update;

64 65
	if (database != nullptr) {
		database->Close();
66
		database.reset();
67 68 69 70 71
	}

	delete storage;
#endif
}
72

73 74 75 76 77 78 79
void
Instance::OnStateModified() noexcept
{
	if (state_file)
		state_file->CheckModified();
}

80
Partition *
81
Instance::FindPartition(const char *name) noexcept
82 83 84 85 86 87 88 89
{
	for (auto &partition : partitions)
		if (partition.name == name)
			return &partition;

	return nullptr;
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103
void
Instance::DeletePartition(Partition &partition) noexcept
{
	// TODO: use boost::intrusive::list to avoid this loop
	for (auto i = partitions.begin();; ++i) {
		assert(i != partitions.end());

		if (&*i == &partition) {
			partitions.erase(i);
			break;
		}
	}
}

104
#ifdef ENABLE_DATABASE
105

106 107 108 109 110 111 112 113 114 115
const Database &
Instance::GetDatabaseOrThrow() const
{
	if (database == nullptr)
		throw DatabaseError(DatabaseErrorCode::DISABLED,
				    "No database");

	return *database;
}

116
void
117
Instance::OnDatabaseModified() noexcept
118
{
119 120 121 122 123
	assert(database != nullptr);

	/* propagate the change to all subsystems */

	stats_invalidate();
124 125 126

	for (auto &partition : partitions)
		partition.DatabaseModified(*database);
127
}
128

129
void
130
Instance::OnDatabaseSongRemoved(const char *uri) noexcept
131 132 133 134 135
{
	assert(database != nullptr);

#ifdef ENABLE_SQLITE
	/* if the song has a sticker, remove it */
136
	if (HasStickerDatabase()) {
137
		try {
138
			sticker_song_delete(*sticker_database, uri);
139
		} catch (...) {
140 141
		}
	}
142 143
#endif

144 145
	for (auto &partition : partitions)
		partition.StaleSong(uri);
146 147
}

148 149
#endif

150 151 152
#ifdef ENABLE_NEIGHBOR_PLUGINS

void
Rosen Penev's avatar
Rosen Penev committed
153
Instance::FoundNeighbor([[maybe_unused]] const NeighborInfo &info) noexcept
154
{
155
	EmitIdle(IDLE_NEIGHBOR);
156 157 158
}

void
Rosen Penev's avatar
Rosen Penev committed
159
Instance::LostNeighbor([[maybe_unused]] const NeighborInfo &info) noexcept
160
{
161
	EmitIdle(IDLE_NEIGHBOR);
162 163 164
}

#endif
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

#ifdef ENABLE_CURL

void
Instance::LookupRemoteTag(const char *uri) noexcept
{
	if (!uri_has_scheme(uri))
		return;

	if (!remote_tag_cache)
		remote_tag_cache = std::make_unique<RemoteTagCache>(event_loop,
								    *this);

	remote_tag_cache->Lookup(uri);
}

void
Instance::OnRemoteTag(const char *uri, const Tag &tag) noexcept
{
	if (!tag.IsDefined())
		/* boring */
		return;

	for (auto &partition : partitions)
		partition.TagModified(uri, tag);
}

#endif
193 194 195 196

void
Instance::OnIdle(unsigned flags) noexcept
{
197 198 199
	/* broadcast to all partitions */
	for (auto &partition : partitions)
		partition.EmitIdle(flags);
200
}
201 202 203 204 205 206 207

void
Instance::FlushCaches() noexcept
{
	if (input_cache)
		input_cache->Flush();
}