aboutsummaryrefslogtreecommitdiff
path: root/include/wanda/message.hpp
blob: 866408fa28e388fc8f841fa3c1adfb4d90f9e90c (plain)
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
/**
 * @file   message.hpp
 * @author Felix Morgner (felix.morgner@gmail.com)
 * @since  1.0.0
 */

#ifndef WANDA_MESSAGE_HPP
#define WANDA_MESSAGE_HPP

#include <cstddef>
#include <istream>
#include <optional>
#include <string>

namespace wanda
{
  inline namespace v1
  {
    /**
     * @brief The version argument for the hello message reflecting the current version
     */
    auto constexpr message_argument_hello = "1.0.0";
  }  // namespace v1

  /**
   * @brief A tag to mark messages originating from the controller
   */
  auto constexpr message_source_controller = "C";

  /**
   * @brief A tag to mark messages originating from the daemon
   */
  auto constexpr message_source_daemon = "D";

  /**
   * @brief The command of the hello message
   */
  auto constexpr message_command_hello = "HELLO";

  /**
   * @brief A control protocol message, consisting of a @p source, @p command, and @p arguments
   */
  struct message
  {
    /**
     * @brief Serialize this message into a string
     */
    explicit operator std::string() const;

    /**
     * @brief Get the size of the message as if it was serialized
     */
    std::size_t size() const;

    /**
     * @brief The source of the message
     */
    std::string source;

    /**
     * @brief The command of the message
     */
    std::string command;

    /**
     * @brief The arguments of the message command
     */
    std::optional<std::string> argument;
  };

  /**
   * @brief Deserialize a message from the given stream
   */
  std::istream & operator>>(std::istream & in, message & message);

  /**
   * @brief Serialize a message to the given stream
   */
  std::ostream & operator<<(std::ostream & out, message const & message);

}  // namespace wanda

#endif