You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classMessage_Broker// Message broker class implementation: -count -messages_vector
18
+
{
19
+
private:
20
+
int _count;
21
+
std::vector<Message> Messages;
22
+
23
+
voidremove_at_index(std::vector<Message>& vec, int index);
24
+
public:
25
+
Message_Broker();
26
+
27
+
voidadd(const Message&);
28
+
voidadd(TargetType m_target_type, long m_target_id, std::string m_text);
29
+
30
+
constintcount();
31
+
constintcount(long target_id);
32
+
33
+
Message& peek_message_for(long m_target_id);
34
+
Message& peek_message_for(std::vector<Message>& m_vec, long m_target_id) const; // Overloaded const function cannot operate non const declared vector, so we pass it as argument
//list.close(); // For safety measures file closing can be added in the end of constructor
22
+
}
23
+
24
+
boolMessage_Reader::has_next() // Bool functions check presence of target_id and whatever target_type, if line meets this requirements it considered valid
25
+
{
26
+
auto tmp_line = buffer.substr(0, buffer.find("\n"));
27
+
28
+
if (buffer.length() > 1)
29
+
{
30
+
auto id_check = tmp_line.substr(0, tmp_line.find(""));
31
+
auto long_id_check = stol(id_check);
32
+
33
+
if (long_id_check > 0)
34
+
{
35
+
if (tmp_line.find("temperature_sensor")
36
+
|| tmp_line.find("pressure_sensor")
37
+
|| tmp_line.find("humidity_sensor")
38
+
|| tmp_line.find("heater")
39
+
|| tmp_line.find("air_purifier"))
40
+
{
41
+
returntrue;
42
+
}
43
+
else
44
+
{
45
+
if (list.is_open()) // If line cannot be read the list considered empty and file is closed
46
+
list.close();
47
+
returnfalse;
48
+
}
49
+
}
50
+
else
51
+
{
52
+
if (list.is_open())
53
+
list.close();
54
+
returnfalse;
55
+
}
56
+
}
57
+
else
58
+
{
59
+
if (list.is_open())
60
+
list.close();
61
+
returnfalse;
62
+
}
63
+
}
64
+
65
+
Message Message_Reader::read_next() //PARSING ALGORYTHM DESCRIPTION (based on line deletion from buffer)
66
+
{
67
+
auto tmp_line = buffer.substr(0, buffer.find("\n")); // Temporary line for parsing a message
68
+
auto tmp_line_init_length = tmp_line.length(); // Saving size of it, for deleting this line from the buffer in the end
69
+
70
+
auto id_parsed = tmp_line.substr(0, tmp_line.find("")); // ID parse
71
+
long id_to_long = stol(id_parsed); //
72
+
tmp_line.erase(0, id_parsed.length() + 1); // Clearing ID from Temporary line
73
+
74
+
auto target_type_parsed = tmp_line.substr(0, tmp_line.find("")); // Target Type parse
75
+
TargetType tmp_type = TargetType::UNKNOWN; //
76
+
for (auto i = TargetType::UNKNOWN; i >= TargetType::dev_temperature_sensor; i--) //
77
+
{ //
78
+
if (type_to_str(i) == target_type_parsed) // Comparison is performed on full name, so no mismatches can be met
79
+
tmp_type = i; //
80
+
} //
81
+
tmp_line.erase(0, target_type_parsed.length() + 1); // Clearing Target type from Temporary line
82
+
83
+
auto text_parsed = tmp_line.substr(0, tmp_line.length()); // Message Text parse
84
+
buffer.erase(0, tmp_line_init_length + 1); // Clearing initial size of Temporary Line from buffer ('+1' = \n)
85
+
86
+
returnMessage(tmp_type, id_to_long, text_parsed); // EXAMPLE MESSAGE: 756997 humidity_sensor MEASURE
0 commit comments