1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
/*
* Copyright 2003-2018 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 QOBUZ_CLIENT_HXX
#define QOBUZ_CLIENT_HXX
#include "check.h"
#include "QobuzSession.hxx"
#include "QobuzLoginRequest.hxx"
#include "lib/curl/Init.hxx"
#include "thread/Mutex.hxx"
#include "event/DeferEvent.hxx"
#include <boost/intrusive/list.hpp>
#include <memory>
#include <map>
#include <string>
class QobuzSessionHandler
: public boost::intrusive::list_base_hook<boost::intrusive::link_mode<boost::intrusive::safe_link>>
{
public:
virtual void OnQobuzSession() noexcept = 0;
};
class QobuzClient final : QobuzLoginHandler {
const char *const base_url;
const char *const app_id, *const app_secret;
const char *const device_manufacturer_id;
const char *const username, *const email, *const password;
CurlInit curl;
DeferEvent defer_invoke_handlers;
/**
* Protects #session, #error, #login_request, #handlers.
*/
mutable Mutex mutex;
QobuzSession session;
std::exception_ptr error;
typedef boost::intrusive::list<QobuzSessionHandler,
boost::intrusive::constant_time_size<false>> LoginHandlerList;
LoginHandlerList handlers;
std::unique_ptr<QobuzLoginRequest> login_request;
public:
QobuzClient(EventLoop &event_loop,
const char *_base_url,
const char *_app_id, const char *_app_secret,
const char *_device_manufacturer_id,
const char *_username, const char *_email,
const char *_password);
gcc_pure
CurlGlobal &GetCurl() noexcept;
void AddLoginHandler(QobuzSessionHandler &h) noexcept;
void RemoveLoginHandler(QobuzSessionHandler &h) noexcept {
const std::lock_guard<Mutex> protect(mutex);
if (h.is_linked())
handlers.erase(handlers.iterator_to(h));
}
/**
* Throws on error.
*/
QobuzSession GetSession() const;
std::string MakeSignedUrl(const char *object, const char *method,
const std::multimap<std::string, std::string> &query) const noexcept;
private:
void StartLogin() noexcept;
void InvokeHandlers() noexcept;
void ScheduleInvokeHandlers() noexcept {
defer_invoke_handlers.Schedule();
}
/* virtual methods from QobuzLoginHandler */
void OnQobuzLoginSuccess(QobuzSession &&session) noexcept override;
void OnQobuzLoginError(std::exception_ptr error) noexcept override;
};
#endif
|