-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.h
More file actions
149 lines (125 loc) · 3.78 KB
/
Copy pathmessage.h
File metadata and controls
149 lines (125 loc) · 3.78 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Header for the Message class.
* Represents a single message (request or response) in the network protocol.
*
* CSF Assignment 5
*
* Ifrah Attar - iattar1@jh.edu
* Morgan Huberty - mhubert3@jh.edu
*
*/
#ifndef MESSAGE_H
#define MESSAGE_H
#include <vector>
#include <string>
/**
* @enum MessageType
* @brief Enumerates the different types of messages in the protocol.
*/
enum class MessageType {
// Used only for uninitialized Message objects
NONE,
// Requests
LOGIN, CREATE, PUSH, POP, TOP, SET, GET, ADD, SUB, MUL, DIV, BEGIN, COMMIT, BYE,
// Responses
OK, FAILED, ERROR, DATA,
};
/**
* @class Message
* @brief Represents a single protocol message.
*
* This class encapsulates the type and arguments of a message and provides
* methods to access its components and validate its structure according
* to the key/value store protocol.
*/
class Message {
private:
MessageType m_message_type;
std::vector<std::string> m_args;
public:
// Maximum encoded message length (including terminator newline character)
static const unsigned MAX_ENCODED_LEN = 1024;
/**
* @brief Default constructor. Creates a message of type NONE.
*/
Message();
/**
* @brief Constructs a message with a specific type and arguments.
* @param message_type The type of the message.
* @param args An initializer list of string arguments for the message.
*/
Message( MessageType message_type, std::initializer_list<std::string> args = std::initializer_list<std::string>() );
/**
* @brief Copy constructor.
* @param other The Message object to copy.
*/
Message( const Message &other );
/**
* @brief Destructor.
*/
~Message();
/**
* @brief Assignment operator.
* @param rhs The Message object to assign from.
* @return A reference to this Message object.
*/
Message &operator=( const Message &rhs );
/**
* @brief Gets the type of the message.
* @return The MessageType of this message.
*/
MessageType get_message_type() const;
/**
* @brief Sets the type of the message.
* @param message_type The new MessageType for this message.
*/
void set_message_type( MessageType message_type );
/**
* @brief Gets the username from a LOGIN message.
* @return The username string.
*/
std::string get_username() const;
/**
* @brief Gets the table name from a CREATE, SET, or GET message.
* @return The table name string.
*/
std::string get_table() const;
/**
* @brief Gets the key from a SET or GET message.
* @return The key string.
*/
std::string get_key() const;
/**
* @brief Gets the value from a PUSH or DATA message.
* @return The value string.
*/
std::string get_value() const;
/**
* @brief Gets the quoted text from a FAILED or ERROR message.
* @return The text content of the response.
*/
std::string get_quoted_text() const;
/**
* @brief Adds an argument to the message's argument list.
* @param arg The string argument to add.
*/
void push_arg( const std::string &arg );
/**
* @brief Validates the message against the protocol specification.
* @return True if the message has the correct number and type of arguments
* for its MessageType, false otherwise.
*/
bool is_valid() const;
/**
* @brief Gets the number of arguments.
* @return The number of arguments in the message.
*/
unsigned get_num_args() const { return m_args.size(); }
/**
* @brief Gets a specific argument by index.
* @param i The index of the argument to retrieve.
* @return The argument string at the specified index.
*/
std::string get_arg( unsigned i ) const { return m_args.at( i ); }
};
#endif // MESSAGE_H