-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
decouple writing from reading in websockets, implement a messaging sy…
…stem through JSON, update client page to test messaging
- Loading branch information
Showing
6 changed files
with
187 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "json_message.hpp" | ||
|
||
std::string json_message::type_to_str(const MessageType& type) { | ||
if (type == QueryState) return "query_state"; | ||
if (type == QueryStateResult) return "query_state_result"; | ||
if (type == ChangeState) return "change_state"; | ||
if (type == Availability) return "availability"; | ||
if (type == Text) return "text"; | ||
} | ||
Check warning on line 9 in src/json_message.cpp GitHub Actions / build_and_upload
|
||
|
||
json_message::MessageType json_message::str_to_type(const std::string_view str) { | ||
if (str == "query_state") return QueryState; | ||
if (str == "query_state_result") return QueryStateResult; | ||
if (str == "change_state") return ChangeState; | ||
if (str == "availability") return Availability; | ||
if (str == "text") return Text; | ||
} | ||
Check warning on line 17 in src/json_message.cpp GitHub Actions / build_and_upload
|
||
|
||
std::string json_message::create_message( | ||
const MessageType type, | ||
const nlohmann::json payload | ||
) { | ||
return nlohmann::json( | ||
{ | ||
{ "type", type_to_str(type) }, | ||
{ "payload", payload } | ||
} | ||
).dump(); | ||
} | ||
|
||
std::string json_message::create_message( | ||
const std::pair<const MessageType, const nlohmann::json> msg | ||
) { | ||
return create_message(msg.first, msg.second); | ||
} | ||
|
||
json_message json_message::parse_message( | ||
const std::string_view str | ||
) { | ||
nlohmann::json parsed_json = nlohmann::json::parse(str); | ||
|
||
if ( | ||
!parsed_json.is_object() || | ||
!parsed_json["type"].is_string() || | ||
(!parsed_json["payload"].is_primitive() && !parsed_json["payload"].is_structured()) | ||
) { | ||
throw json_message_parse_error("malformed JSON data"); | ||
} | ||
|
||
return json_message( | ||
json_message::str_to_type(parsed_json["type"].get<std::string>()), | ||
parsed_json["payload"] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#ifndef JSON_MESSAGE_HPP | ||
#define JSON_MESSAGE_HPP | ||
|
||
#include <nlohmann/json.hpp> | ||
#include <string> | ||
|
||
class json_message { | ||
public: | ||
enum MessageType { | ||
QueryState, | ||
QueryStateResult, | ||
ChangeState, | ||
Availability, | ||
Text | ||
}; | ||
|
||
class json_message_parse_error : std::runtime_error { | ||
public: | ||
json_message_parse_error(const char* what) : std::runtime_error(what) {} | ||
}; | ||
|
||
MessageType type; | ||
nlohmann::json::value_t payload; | ||
|
||
json_message( | ||
const MessageType type, | ||
nlohmann::json::value_t payload | ||
) : type(type), payload(payload) {} | ||
|
||
static std::string type_to_str(const MessageType& type); | ||
static MessageType str_to_type(const std::string_view str); | ||
|
||
static std::string create_message( | ||
const MessageType type, | ||
const nlohmann::json payload = nullptr | ||
); | ||
|
||
static std::string create_message( | ||
const std::pair<const MessageType, const nlohmann::json> msg | ||
); | ||
|
||
static json_message parse_message(const std::string_view str); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters