URY playd
C++ minimalist audio player
Response Class Reference

A response. More...

#include <response.hpp>

+ Collaboration diagram for Response:

Public Types

enum  Code : std::uint8_t {
  Code::OHAI, Code::IAMA, Code::FLOAD, Code::EJECT,
  Code::POS, Code::END, Code::PLAY, Code::STOP,
  Code::ACK
}
 Enumeration of all possible response codes. More...
 

Public Member Functions

 Response (const std::string &tag, Response::Code code)
 Constructs a Response with no arguments. More...
 
ResponseAddArg (const std::string &arg)
 Adds an argument to this Response. More...
 
std::string Pack () const
 Packs the Response, converting it to a BAPS3 protocol message. More...
 

Static Public Member Functions

static Response Success (const std::string &tag)
 Shortcut for constructing a final response to a successful request. More...
 
static Response Invalid (const std::string &tag, const std::string &msg)
 Shortcut for constructing a final response to a invalid request. More...
 
static Response Failure (const std::string &tag, const std::string &msg)
 Shortcut for constructing a final response to a failed request. More...
 

Static Public Attributes

static const std::string NOREQUEST = "!"
 The tag for unsolicited messages (not from responses).
 
static constexpr std::uint8_t CODE_COUNT = 9
 The number of codes, which should agree with Response::Code.
 

Static Private Member Functions

static std::string EscapeArg (const std::string &arg)
 Escapes a single response argument. More...
 

Private Attributes

std::string string
 The current packed form of the response. More...
 

Static Private Attributes

static const std::array< std::string, CODE_COUNTSTRINGS
 A map from Response::Code codes to their string equivalents. More...
 

Detailed Description

A response.

Definition at line 23 of file response.hpp.

Member Enumeration Documentation

§ Code

enum Response::Code : std::uint8_t
strong

Enumeration of all possible response codes.

Note
If you're adding new responses here, update Response::STRINGS.
See also
Response::STRINGS
Enumerator
OHAI 

Server starting up.

IAMA 

Server sending its role.

FLOAD 

The loaded file just changed.

EJECT 

The loaded file just ejected.

POS 

Server sending current song time.

END 

The loaded file just ended.

PLAY 

The loaded file is playing.

STOP 

The loaded file has stopped.

ACK 

Command result.

Definition at line 35 of file response.hpp.

35  : std::uint8_t {
36  OHAI,
37  IAMA,
38  FLOAD,
39  EJECT,
40  POS,
41  END,
42  PLAY,
43  STOP,
44  ACK
45  };

Constructor & Destructor Documentation

§ Response()

Response::Response ( const std::string &  tag,
Response::Code  code 
)

Constructs a Response with no arguments.

Parameters
tagThe tag of the response.
codeThe Response::Code representing the response command.

Definition at line 32 of file response.cpp.

References EscapeArg(), and STRINGS.

Referenced by Failure(), Invalid(), and Success().

33 {
34  this->string = Response::EscapeArg(tag) + " " +
35  STRINGS[static_cast<uint8_t>(code)];
36 }
static const std::array< std::string, CODE_COUNT > STRINGS
A map from Response::Code codes to their string equivalents.
Definition: response.hpp:100
static std::string EscapeArg(const std::string &arg)
Escapes a single response argument.
Definition: response.cpp:68

Member Function Documentation

§ AddArg()

Response & Response::AddArg ( const std::string &  arg)

Adds an argument to this Response.

Parameters
argThe argument to add. The argument must not be escaped.
Returns
A reference to this Response, for chaining.

Definition at line 38 of file response.cpp.

References EscapeArg().

39 {
40  this->string += " " + Response::EscapeArg(arg);
41  return *this;
42 }
static std::string EscapeArg(const std::string &arg)
Escapes a single response argument.
Definition: response.cpp:68

§ EscapeArg()

std::string Response::EscapeArg ( const std::string &  arg)
staticprivate

Escapes a single response argument.

Parameters
argThe argument to escape.
Returns
The escaped argument.

Definition at line 68 of file response.cpp.

Referenced by AddArg(), and Response().

69 {
70  bool escaping = false;
71  std::string escaped;
72 
73  for (char c : arg) {
74  // These are the characters (including all whitespace, via
75  // isspace()) whose presence means we need to single-quote
76  // escape the argument.
77  bool is_escaper = c == '"' || c == '\'' || c == '\\';
78  if (isspace(c) || is_escaper) escaping = true;
79 
80  // Since we use single-quote escaping, the only thing we need
81  // to escape by itself is single quotes, which are replaced by
82  // the sequence '\'' (break out of single quotes, escape a
83  // single quote, then re-enter single quotes).
84  escaped += (c == '\'') ? R"('\'')" : std::string(1, c);
85  }
86 
87  // Only single-quote escape if necessary.
88  // Otherwise, it wastes two characters!
89  if (escaping) return "'" + escaped + "'";
90  return escaped;
91 }

§ Failure()

Response Response::Failure ( const std::string &  tag,
const std::string &  msg 
)
static

Shortcut for constructing a final response to a failed request.

Parameters
tagThe tag of the original request.
msgThe failure message.
Returns
A Response acknowledging a failed request.

Definition at line 62 of file response.cpp.

References ACK, and Response().

Referenced by Player::Dump(), Player::Eject(), Player::End(), Player::Load(), Player::Pos(), Player::Quit(), and Player::SetPlaying().

64 {
65  return Response(tag, Response::Code::ACK).AddArg("FAIL").AddArg(msg);
66 }
Command result.
Response(const std::string &tag, Response::Code code)
Constructs a Response with no arguments.
Definition: response.cpp:32

§ Invalid()

Response Response::Invalid ( const std::string &  tag,
const std::string &  msg 
)
static

Shortcut for constructing a final response to a invalid request.

Parameters
tagThe tag of the original request.
msgThe failure message.
Returns
A Response acknowledging an invalid request.

Definition at line 56 of file response.cpp.

References ACK, and Response().

Referenced by Player::Load(), Player::Pos(), Connection::RunCommand(), and Player::SetPlaying().

58 {
59  return Response(tag, Response::Code::ACK).AddArg("WHAT").AddArg(msg);
60 }
Command result.
Response(const std::string &tag, Response::Code code)
Constructs a Response with no arguments.
Definition: response.cpp:32

§ Pack()

std::string Response::Pack ( ) const

Packs the Response, converting it to a BAPS3 protocol message.

Pack()ing does not alter the Response, which may be Pack()ed again.

Returns
The BAPS3 message, sans newline, ready to send.

Definition at line 44 of file response.cpp.

References string.

Referenced by IoCore::Broadcast(), Connection::Respond(), and IoCore::Unicast().

45 {
46  return this->string;
47 }
std::string string
The current packed form of the response.
Definition: response.hpp:111

§ Success()

Response Response::Success ( const std::string &  tag)
static

Shortcut for constructing a final response to a successful request.

Parameters
tagThe tag of the original request.
Returns
A Response acknowledging a successful request.

Definition at line 49 of file response.cpp.

References ACK, and Response().

Referenced by IoCore::Accept(), Player::Dump(), Player::Eject(), Player::End(), Player::Load(), Player::Pos(), Player::Quit(), and Player::SetPlaying().

50 {
51  return Response(tag, Response::Code::ACK)
52  .AddArg("OK")
53  .AddArg("success");
54 }
Command result.
Response(const std::string &tag, Response::Code code)
Constructs a Response with no arguments.
Definition: response.cpp:32

Member Data Documentation

§ string

std::string Response::string
private

The current packed form of the response.

See also
Pack

Definition at line 111 of file response.hpp.

Referenced by Pack().

§ STRINGS

const std::array< std::string, Response::CODE_COUNT > Response::STRINGS
staticprivate
Initial value:
{{
"OHAI",
"IAMA",
"FLOAD",
"EJECT",
"POS",
"END",
"PLAY",
"STOP",
"ACK"
}}

A map from Response::Code codes to their string equivalents.

See also
Response::Code

Definition at line 100 of file response.hpp.

Referenced by Response().


The documentation for this class was generated from the following files: