URY playd
C++ minimalist audio player
response.hpp
Go to the documentation of this file.
1 // This file is part of playd.
2 // playd is licensed under the MIT licence: see LICENSE.txt.
3 
10 #ifndef PLAYD_IO_RESPONSE_HPP
11 #define PLAYD_IO_RESPONSE_HPP
12 
13 #include <cstdint>
14 #include <map>
15 #include <memory>
16 #include <ostream>
17 #include <string>
18 #include <vector>
19 
20 #include "errors.hpp"
21 
23 class Response
24 {
25 public:
27  static const std::string NOREQUEST;
28 
35  enum class Code : std::uint8_t {
36  OHAI,
37  IAMA,
38  FLOAD,
39  EJECT,
40  POS,
41  END,
42  PLAY,
43  STOP,
44  ACK
45  };
46 
48  static constexpr std::uint8_t CODE_COUNT = 9;
49 
55  Response(const std::string &tag, Response::Code code);
56 
62  Response &AddArg(const std::string &arg);
63 
69  std::string Pack() const;
70 
76  static Response Success(const std::string &tag);
77 
84  static Response Invalid(const std::string &tag, const std::string &msg);
85 
92  static Response Failure(const std::string &tag, const std::string &msg);
93 
94 private:
95 
100  static const std::array<std::string, CODE_COUNT> STRINGS;
101 
107  static std::string EscapeArg(const std::string &arg);
108 
111  std::string string;
112 };
113 
118 {
119 public:
121  virtual ~ResponseSink() = default;
122 
130  virtual void Respond(size_t id, const Response &response) const;
131 };
132 
133 #endif // PLAYD_IO_RESPONSE_HPP
Declarations of the playd Error exception set.
A response.
Definition: response.hpp:23
std::string Pack() const
Packs the Response, converting it to a BAPS3 protocol message.
Definition: response.cpp:44
std::string string
The current packed form of the response.
Definition: response.hpp:111
static const std::array< std::string, CODE_COUNT > STRINGS
A map from Response::Code codes to their string equivalents.
Definition: response.hpp:100
static constexpr std::uint8_t CODE_COUNT
The number of codes, which should agree with Response::Code.
Definition: response.hpp:48
Command result.
static const std::string NOREQUEST
The tag for unsolicited messages (not from responses).
Definition: response.hpp:27
static Response Success(const std::string &tag)
Shortcut for constructing a final response to a successful request.
Definition: response.cpp:49
Abstract class for anything that can be sent a response.
Definition: response.hpp:117
Response(const std::string &tag, Response::Code code)
Constructs a Response with no arguments.
Definition: response.cpp:32
The loaded file just changed.
The loaded file is playing.
The loaded file just ended.
The loaded file just ejected.
static Response Failure(const std::string &tag, const std::string &msg)
Shortcut for constructing a final response to a failed request.
Definition: response.cpp:62
static std::string EscapeArg(const std::string &arg)
Escapes a single response argument.
Definition: response.cpp:68
Server sending its role.
Response & AddArg(const std::string &arg)
Adds an argument to this Response.
Definition: response.cpp:38
Code
Enumeration of all possible response codes.
Definition: response.hpp:35
The loaded file has stopped.
Server starting up.
Server sending current song time.
static Response Invalid(const std::string &tag, const std::string &msg)
Shortcut for constructing a final response to a invalid request.
Definition: response.cpp:56