-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStompMessage.cpp
77 lines (68 loc) · 1.86 KB
/
StompMessage.cpp
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
#include "StompMessage.h"
using namespace std;
StompMessage::StompMessage(const char* rawMessage)
{
std::string strMessage(rawMessage);
vector<std::string> messageVector = messageToVector(strMessage, "\n");
m_message = messageVector.back(); // deep copies
messageVector.pop_back();
bool first = true;
for (const auto& header : messageVector)
{
size_t pos = header.find_first_of(':', 0);
std::string key = header.substr(0, pos);
if (first)
{
m_messageType = key;
first = false;
continue;
}
std::string value = header.substr(++pos, value.length() - pos);
m_headers[key] = value;
}
}
StompMessage::StompMessage(string messageType, map<string, string> headers, const char* messageBody)
{
m_messageType = std::string(messageType);
m_message = messageBody;
m_headers = headers;
}
std::string StompMessage::toString() const
{
std::string result = m_messageType + "\u000A";
for (const auto &header : m_headers)
{
result += header.first + ":" + header.second + "\u000A";
}
result += "\u000A" + m_message + "\u0000";
return result;
}
vector<string> StompMessage::messageToVector(const string& str, const string& delim)
{
vector<string> messageParts;
size_t prev = 0, pos = 0;
bool last = false;
do
{
if (last)
{
auto message = str.substr(prev, str.length() - prev);
messageParts.push_back(message);
break;
}
pos = str.find(delim, prev);
if (pos == string::npos) pos = str.length();
string token = str.substr(prev, pos - prev);
if (!token.empty())
{
messageParts.push_back(token);
}
else
{
// If the token is empty the headers finished =) Just the body left!
last = true;
}
prev = pos + delim.length();
} while (pos < str.length() && prev <= str.length()); // I know but we have to do it at least once, so...
return messageParts;
}