Main: move global variables to struct Instance
More preparations for multi-player support.
This commit is contained in:
parent
08dfd263ba
commit
b2d3d15e97
@ -186,6 +186,7 @@ src_mpd_SOURCES = \
|
||||
src/io_error.h \
|
||||
src/IOThread.cxx src/IOThread.hxx \
|
||||
src/Main.cxx src/Main.hxx \
|
||||
src/Instance.cxx src/Instance.hxx \
|
||||
src/Win32Main.cxx \
|
||||
src/GlobalEvents.cxx src/GlobalEvents.hxx \
|
||||
src/daemon.c \
|
||||
|
@ -21,7 +21,7 @@
|
||||
#include "ClientInternal.hxx"
|
||||
#include "ClientList.hxx"
|
||||
#include "Partition.hxx"
|
||||
#include "Main.hxx"
|
||||
#include "Instance.hxx"
|
||||
#include "fd_util.h"
|
||||
extern "C" {
|
||||
#include "resolver.h"
|
||||
@ -95,7 +95,8 @@ client_new(EventLoop &loop, Partition &partition,
|
||||
}
|
||||
#endif /* HAVE_WRAP */
|
||||
|
||||
if (client_list->IsFull()) {
|
||||
ClientList &client_list = *partition.instance.client_list;
|
||||
if (client_list.IsFull()) {
|
||||
g_warning("Max Connections Reached!");
|
||||
close_socket(fd);
|
||||
return;
|
||||
@ -106,7 +107,7 @@ client_new(EventLoop &loop, Partition &partition,
|
||||
|
||||
(void)send(fd, GREETING, sizeof(GREETING) - 1, 0);
|
||||
|
||||
client_list->Add(*client);
|
||||
client_list.Add(*client);
|
||||
|
||||
remote = sockaddr_to_string(sa, sa_length, NULL);
|
||||
g_log(G_LOG_DOMAIN, LOG_LEVEL_SECURE,
|
||||
@ -117,7 +118,7 @@ client_new(EventLoop &loop, Partition &partition,
|
||||
void
|
||||
Client::Close()
|
||||
{
|
||||
client_list->Remove(*this);
|
||||
partition.instance.client_list->Remove(*this);
|
||||
|
||||
SetExpired();
|
||||
|
||||
|
48
src/Instance.cxx
Normal file
48
src/Instance.cxx
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
||||
* 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"
|
||||
#include "Idle.hxx"
|
||||
|
||||
void
|
||||
Instance::DeleteSong(const song &song)
|
||||
{
|
||||
partition->DeleteSong(song);
|
||||
}
|
||||
|
||||
void
|
||||
Instance::DatabaseModified()
|
||||
{
|
||||
partition->playlist.FullIncrementVersions();
|
||||
idle_add(IDLE_DATABASE);
|
||||
}
|
||||
|
||||
void
|
||||
Instance::TagModified()
|
||||
{
|
||||
partition->playlist.TagChanged();
|
||||
}
|
||||
|
||||
void
|
||||
Instance::SyncWithPlayer()
|
||||
{
|
||||
partition->playlist.SyncWithPlayer(partition->pc);
|
||||
}
|
54
src/Instance.hxx
Normal file
54
src/Instance.hxx
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
||||
* 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
|
||||
|
||||
#include "check.h"
|
||||
|
||||
class ClientList;
|
||||
struct Partition;
|
||||
struct song;
|
||||
|
||||
struct Instance {
|
||||
ClientList *client_list;
|
||||
|
||||
Partition *partition;
|
||||
|
||||
void DeleteSong(const song &song);
|
||||
|
||||
/**
|
||||
* The database has been modified. Propagate the change to
|
||||
* all subsystems.
|
||||
*/
|
||||
void DatabaseModified();
|
||||
|
||||
/**
|
||||
* A tag in the play queue has been modified. Propagate the
|
||||
* change to all subsystems.
|
||||
*/
|
||||
void TagModified();
|
||||
|
||||
/**
|
||||
* Synchronize the player with the play queue.
|
||||
*/
|
||||
void SyncWithPlayer();
|
||||
};
|
||||
|
||||
#endif
|
@ -20,6 +20,7 @@
|
||||
#include "config.h"
|
||||
#include "Listen.hxx"
|
||||
#include "Main.hxx"
|
||||
#include "Instance.hxx"
|
||||
#include "Client.hxx"
|
||||
#include "conf.h"
|
||||
#include "event/ServerSocket.hxx"
|
||||
@ -43,7 +44,7 @@ public:
|
||||
private:
|
||||
virtual void OnAccept(int fd, const sockaddr &address,
|
||||
size_t address_length, int uid) {
|
||||
client_new(*main_loop, *global_partition,
|
||||
client_new(*main_loop, *instance->partition,
|
||||
fd, &address, address_length, uid);
|
||||
}
|
||||
};
|
||||
|
35
src/Main.cxx
35
src/Main.cxx
@ -19,6 +19,7 @@
|
||||
|
||||
#include "config.h"
|
||||
#include "Main.hxx"
|
||||
#include "Instance.hxx"
|
||||
#include "CommandLine.hxx"
|
||||
#include "PlaylistFile.hxx"
|
||||
#include "PlaylistGlobal.hxx"
|
||||
@ -97,9 +98,7 @@ enum {
|
||||
GThread *main_task;
|
||||
EventLoop *main_loop;
|
||||
|
||||
ClientList *client_list;
|
||||
|
||||
Partition *global_partition;
|
||||
Instance *instance;
|
||||
|
||||
static StateFile *state_file;
|
||||
|
||||
@ -251,7 +250,7 @@ glue_state_file_init(GError **error_r)
|
||||
}
|
||||
|
||||
state_file = new StateFile(std::move(path_fs), path,
|
||||
*global_partition, *main_loop);
|
||||
*instance->partition, *main_loop);
|
||||
g_free(path);
|
||||
|
||||
state_file->Read();
|
||||
@ -331,9 +330,10 @@ initialize_decoder_and_player(void)
|
||||
config_get_positive(CONF_MAX_PLAYLIST_LENGTH,
|
||||
DEFAULT_PLAYLIST_MAX_LENGTH);
|
||||
|
||||
global_partition = new Partition(max_length,
|
||||
buffered_chunks,
|
||||
buffered_before_play);
|
||||
instance->partition = new Partition(*instance,
|
||||
max_length,
|
||||
buffered_chunks,
|
||||
buffered_before_play);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -346,7 +346,7 @@ idle_event_emitted(void)
|
||||
clients */
|
||||
unsigned flags = idle_get();
|
||||
if (flags != 0)
|
||||
client_list->IdleAdd(flags);
|
||||
instance->client_list->IdleAdd(flags);
|
||||
|
||||
if (flags & (IDLE_PLAYLIST|IDLE_PLAYER|IDLE_MIXER|IDLE_OUTPUT))
|
||||
state_file->CheckModified();
|
||||
@ -421,8 +421,10 @@ int mpd_main(int argc, char *argv[])
|
||||
main_task = g_thread_self();
|
||||
main_loop = new EventLoop(EventLoop::Default());
|
||||
|
||||
instance = new Instance();
|
||||
|
||||
const unsigned max_clients = config_get_positive(CONF_MAX_CONN, 10);
|
||||
client_list = new ClientList(max_clients);
|
||||
instance->client_list = new ClientList(max_clients);
|
||||
|
||||
success = listen_global_init(&error);
|
||||
if (!success) {
|
||||
@ -469,7 +471,7 @@ int mpd_main(int argc, char *argv[])
|
||||
initialize_decoder_and_player();
|
||||
volume_init();
|
||||
initAudioConfig();
|
||||
audio_output_all_init(&global_partition->pc);
|
||||
audio_output_all_init(&instance->partition->pc);
|
||||
client_manager_init();
|
||||
replay_gain_global_init();
|
||||
|
||||
@ -495,7 +497,7 @@ int mpd_main(int argc, char *argv[])
|
||||
|
||||
ZeroconfInit(*main_loop);
|
||||
|
||||
player_create(&global_partition->pc);
|
||||
player_create(&instance->partition->pc);
|
||||
|
||||
if (create_db) {
|
||||
/* the database failed to load: recreate the
|
||||
@ -511,7 +513,7 @@ int mpd_main(int argc, char *argv[])
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
audio_output_all_set_replay_gain_mode(replay_gain_get_real_mode(global_partition->playlist.queue.random));
|
||||
audio_output_all_set_replay_gain_mode(replay_gain_get_real_mode(instance->partition->playlist.queue.random));
|
||||
|
||||
success = config_get_bool(CONF_AUTO_UPDATE, false);
|
||||
#ifdef ENABLE_INOTIFY
|
||||
@ -527,7 +529,7 @@ int mpd_main(int argc, char *argv[])
|
||||
|
||||
/* enable all audio outputs (if not already done by
|
||||
playlist_state_restore() */
|
||||
global_partition->pc.UpdateAudio();
|
||||
instance->partition->pc.UpdateAudio();
|
||||
|
||||
#ifdef WIN32
|
||||
win32_app_started();
|
||||
@ -551,10 +553,10 @@ int mpd_main(int argc, char *argv[])
|
||||
delete state_file;
|
||||
}
|
||||
|
||||
global_partition->pc.Kill();
|
||||
instance->partition->pc.Kill();
|
||||
ZeroconfDeinit();
|
||||
listen_global_finish();
|
||||
delete client_list;
|
||||
delete instance->client_list;
|
||||
|
||||
start = clock();
|
||||
DatabaseGlobalDeinit();
|
||||
@ -572,7 +574,7 @@ int mpd_main(int argc, char *argv[])
|
||||
audio_output_all_finish();
|
||||
volume_finish();
|
||||
mapper_finish();
|
||||
delete global_partition;
|
||||
delete instance->partition;
|
||||
command_finish();
|
||||
update_global_finish();
|
||||
decoder_plugin_deinit_all();
|
||||
@ -582,6 +584,7 @@ int mpd_main(int argc, char *argv[])
|
||||
config_global_finish();
|
||||
stats_global_finish();
|
||||
io_thread_deinit();
|
||||
delete instance;
|
||||
delete main_loop;
|
||||
daemonize_finish();
|
||||
#ifdef WIN32
|
||||
|
@ -23,14 +23,13 @@
|
||||
#include <glib.h>
|
||||
|
||||
class EventLoop;
|
||||
struct Instance;
|
||||
|
||||
extern GThread *main_task;
|
||||
|
||||
extern EventLoop *main_loop;
|
||||
|
||||
extern class ClientList *client_list;
|
||||
|
||||
extern struct Partition *global_partition;
|
||||
extern Instance *instance;
|
||||
|
||||
/**
|
||||
* A entry point for application.
|
||||
|
@ -22,6 +22,7 @@
|
||||
#include "ClientSubscribe.hxx"
|
||||
#include "ClientInternal.hxx"
|
||||
#include "ClientList.hxx"
|
||||
#include "Instance.hxx"
|
||||
#include "Main.hxx"
|
||||
#include "protocol/Result.hxx"
|
||||
#include "protocol/ArgParser.hxx"
|
||||
@ -81,7 +82,7 @@ handle_channels(Client *client,
|
||||
assert(argc == 1);
|
||||
|
||||
std::set<std::string> channels;
|
||||
for (const auto &c : *client_list)
|
||||
for (const auto &c : *instance->client_list)
|
||||
channels.insert(c->subscriptions.begin(),
|
||||
c->subscriptions.end());
|
||||
|
||||
@ -122,7 +123,7 @@ handle_send_message(Client *client,
|
||||
|
||||
bool sent = false;
|
||||
const ClientMessage msg(argv[1], argv[2]);
|
||||
for (const auto &c : *client_list)
|
||||
for (const auto &c : *instance->client_list)
|
||||
if (client_push_message(c, msg))
|
||||
sent = true;
|
||||
|
||||
|
@ -23,19 +23,24 @@
|
||||
#include "Playlist.hxx"
|
||||
#include "PlayerControl.hxx"
|
||||
|
||||
struct Instance;
|
||||
|
||||
/**
|
||||
* A partition of the Music Player Daemon. It is a separate unit with
|
||||
* a playlist, a player, outputs etc.
|
||||
*/
|
||||
struct Partition {
|
||||
Instance &instance;
|
||||
|
||||
struct playlist playlist;
|
||||
|
||||
player_control pc;
|
||||
|
||||
Partition(unsigned max_length,
|
||||
Partition(Instance &_instance,
|
||||
unsigned max_length,
|
||||
unsigned buffer_chunks,
|
||||
unsigned buffered_before_play)
|
||||
:playlist(max_length),
|
||||
:instance(_instance), playlist(max_length),
|
||||
pc(buffer_chunks, buffered_before_play) {
|
||||
}
|
||||
|
||||
|
@ -26,19 +26,19 @@
|
||||
#include "PlaylistGlobal.hxx"
|
||||
#include "Playlist.hxx"
|
||||
#include "Main.hxx"
|
||||
#include "Partition.hxx"
|
||||
#include "Instance.hxx"
|
||||
#include "GlobalEvents.hxx"
|
||||
|
||||
static void
|
||||
playlist_tag_event(void)
|
||||
{
|
||||
global_partition->playlist.TagChanged();
|
||||
instance->TagModified();
|
||||
}
|
||||
|
||||
static void
|
||||
playlist_event(void)
|
||||
{
|
||||
global_partition->playlist.SyncWithPlayer(global_partition->pc);
|
||||
instance->SyncWithPlayer();
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -32,7 +32,7 @@ extern "C" {
|
||||
}
|
||||
|
||||
#include "Main.hxx"
|
||||
#include "Partition.hxx"
|
||||
#include "Instance.hxx"
|
||||
#include "mpd_error.h"
|
||||
|
||||
#include <glib.h>
|
||||
@ -156,11 +156,9 @@ static void update_finished_event(void)
|
||||
|
||||
idle_add(IDLE_UPDATE);
|
||||
|
||||
if (modified) {
|
||||
if (modified)
|
||||
/* send "idle" events */
|
||||
global_partition->playlist.FullIncrementVersions();
|
||||
idle_add(IDLE_DATABASE);
|
||||
}
|
||||
instance->DatabaseModified();
|
||||
|
||||
path = update_queue_shift(&discard);
|
||||
if (path != NULL) {
|
||||
|
@ -20,13 +20,13 @@
|
||||
#include "config.h" /* must be first for large file support */
|
||||
#include "UpdateRemove.hxx"
|
||||
#include "Playlist.hxx"
|
||||
#include "Partition.hxx"
|
||||
#include "GlobalEvents.hxx"
|
||||
#include "thread/Mutex.hxx"
|
||||
#include "thread/Cond.hxx"
|
||||
|
||||
#include "song.h"
|
||||
#include "Main.hxx"
|
||||
#include "Instance.hxx"
|
||||
|
||||
#ifdef ENABLE_SQLITE
|
||||
#include "StickerDatabase.hxx"
|
||||
@ -63,7 +63,7 @@ song_remove_event(void)
|
||||
sticker_song_delete(removed_song);
|
||||
#endif
|
||||
|
||||
global_partition->DeleteSong(*removed_song);
|
||||
instance->DeleteSong(*removed_song);
|
||||
|
||||
/* clear "removed_song" and send signal to update thread */
|
||||
remove_mutex.lock();
|
||||
|
Loading…
Reference in New Issue
Block a user