ClientSubscribe.cxx 2.04 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "ClientInternal.hxx"
22
#include "Partition.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "Idle.hxx"
24

25
#include <assert.h>
26 27 28

Client::SubscribeResult
Client::Subscribe(const char *channel)
29
{
30
	assert(channel != nullptr);
31 32

	if (!client_message_valid_channel_name(channel))
33
		return Client::SubscribeResult::INVALID;
34

35 36
	if (num_subscriptions >= CLIENT_MAX_SUBSCRIPTIONS)
		return Client::SubscribeResult::FULL;
37

38
	auto r = subscriptions.insert(channel);
39
	if (!r.second)
40
		return Client::SubscribeResult::ALREADY;
41

42
	++num_subscriptions;
43

44
	partition.EmitIdle(IDLE_SUBSCRIPTION);
45

46
	return Client::SubscribeResult::OK;
47 48 49
}

bool
50
Client::Unsubscribe(const char *channel)
51
{
52 53
	const auto i = subscriptions.find(channel);
	if (i == subscriptions.end())
54 55
		return false;

56
	assert(num_subscriptions > 0);
57

58 59
	subscriptions.erase(i);
	--num_subscriptions;
60

61
	partition.EmitIdle(IDLE_SUBSCRIPTION);
62

63 64
	assert((num_subscriptions == 0) ==
	       subscriptions.empty());
65 66 67 68 69

	return true;
}

void
70
Client::UnsubscribeAll()
71
{
72 73
	subscriptions.clear();
	num_subscriptions = 0;
74 75 76
}

bool
77
Client::PushMessage(const ClientMessage &msg)
78
{
79 80
	if (messages.size() >= CLIENT_MAX_MESSAGES ||
	    !IsSubscribed(msg.GetChannel()))
81 82
		return false;

83 84
	if (messages.empty())
		IdleAdd(IDLE_MESSAGE);
85

86
	messages.push_back(msg);
87 88
	return true;
}