2011-01-29 09:26:22 +01:00
|
|
|
/*
|
2013-01-03 10:33:04 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2011-01-29 09:26:22 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-01-14 23:49:43 +01:00
|
|
|
#include "config.h"
|
2013-01-03 10:33:04 +01:00
|
|
|
#include "ClientInternal.hxx"
|
2013-01-09 08:36:52 +01:00
|
|
|
#include "Idle.hxx"
|
2011-01-29 09:26:22 +01:00
|
|
|
|
2013-01-03 17:58:22 +01:00
|
|
|
#include <assert.h>
|
2011-01-29 09:26:22 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-10-19 19:44:45 +02:00
|
|
|
bool Unsubscribe(const char *channel);
|
|
|
|
void UnsubscribeAll();
|
|
|
|
bool PushMessage(const ClientMessage &msg);
|
|
|
|
|
|
|
|
Client::SubscribeResult
|
|
|
|
Client::Subscribe(const char *channel)
|
2011-01-29 09:26:22 +01:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(channel != nullptr);
|
2011-01-29 09:26:22 +01:00
|
|
|
|
|
|
|
if (!client_message_valid_channel_name(channel))
|
2013-10-19 19:44:45 +02:00
|
|
|
return Client::SubscribeResult::INVALID;
|
2011-01-29 09:26:22 +01:00
|
|
|
|
2013-10-19 19:44:45 +02:00
|
|
|
if (num_subscriptions >= CLIENT_MAX_SUBSCRIPTIONS)
|
|
|
|
return Client::SubscribeResult::FULL;
|
2011-01-29 09:26:22 +01:00
|
|
|
|
2013-10-19 19:44:45 +02:00
|
|
|
auto r = subscriptions.insert(channel);
|
2013-01-03 11:31:32 +01:00
|
|
|
if (!r.second)
|
2013-10-19 19:44:45 +02:00
|
|
|
return Client::SubscribeResult::ALREADY;
|
2013-01-03 11:31:32 +01:00
|
|
|
|
2013-10-19 19:44:45 +02:00
|
|
|
++num_subscriptions;
|
2011-01-29 09:26:22 +01:00
|
|
|
|
|
|
|
idle_add(IDLE_SUBSCRIPTION);
|
|
|
|
|
2013-10-19 19:44:45 +02:00
|
|
|
return Client::SubscribeResult::OK;
|
2011-01-29 09:26:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-10-19 19:44:45 +02:00
|
|
|
Client::Unsubscribe(const char *channel)
|
2011-01-29 09:26:22 +01:00
|
|
|
{
|
2013-10-19 19:44:45 +02:00
|
|
|
const auto i = subscriptions.find(channel);
|
|
|
|
if (i == subscriptions.end())
|
2011-01-29 09:26:22 +01:00
|
|
|
return false;
|
|
|
|
|
2013-10-19 19:44:45 +02:00
|
|
|
assert(num_subscriptions > 0);
|
2011-01-29 09:26:22 +01:00
|
|
|
|
2013-10-19 19:44:45 +02:00
|
|
|
subscriptions.erase(i);
|
|
|
|
--num_subscriptions;
|
2011-01-29 09:26:22 +01:00
|
|
|
|
|
|
|
idle_add(IDLE_SUBSCRIPTION);
|
|
|
|
|
2013-10-19 19:44:45 +02:00
|
|
|
assert((num_subscriptions == 0) ==
|
|
|
|
subscriptions.empty());
|
2011-01-29 09:26:22 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-19 19:44:45 +02:00
|
|
|
Client::UnsubscribeAll()
|
2011-01-29 09:26:22 +01:00
|
|
|
{
|
2013-10-19 19:44:45 +02:00
|
|
|
subscriptions.clear();
|
|
|
|
num_subscriptions = 0;
|
2011-01-29 09:26:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-10-19 19:44:45 +02:00
|
|
|
Client::PushMessage(const ClientMessage &msg)
|
2011-01-29 09:26:22 +01:00
|
|
|
{
|
2013-10-19 19:44:45 +02:00
|
|
|
if (messages.size() >= CLIENT_MAX_MESSAGES ||
|
|
|
|
!IsSubscribed(msg.GetChannel()))
|
2011-01-29 09:26:22 +01:00
|
|
|
return false;
|
|
|
|
|
2013-10-19 19:44:45 +02:00
|
|
|
if (messages.empty())
|
|
|
|
IdleAdd(IDLE_MESSAGE);
|
2011-01-29 09:26:22 +01:00
|
|
|
|
2013-10-19 19:44:45 +02:00
|
|
|
messages.push_back(msg);
|
2011-01-29 09:26:22 +01:00
|
|
|
return true;
|
|
|
|
}
|