diff --git a/vim_research/test2.cpp b/vim_research/test2.cpp index 1bb4dd2..b74b33a 100644 --- a/vim_research/test2.cpp +++ b/vim_research/test2.cpp @@ -7,11 +7,14 @@ extern "C" { #include } +#include #include +#include #include #include #include #include +#include #include #include @@ -31,6 +34,24 @@ extern "C" { //}; using ToxPubKey = std::array; +template<> +struct std::hash { + std::size_t operator()(ToxPubKey const& s) const noexcept { + static_assert(sizeof(size_t) == 8); + // TODO: maybe shuffle the indices a bit + return + (static_cast(s[0]) << 8*0) | + (static_cast(s[1]) << 8*1) | + (static_cast(s[2]) << 8*2) | + (static_cast(s[3]) << 8*3) | + (static_cast(s[4]) << 8*4) | + (static_cast(s[5]) << 8*5) | + (static_cast(s[6]) << 8*6) | + (static_cast(s[7]) << 8*7) + ; + } +}; + // single letter agent, for testing only //using Agent = char; //using Agent = uint16_t; // tmp local port @@ -38,6 +59,80 @@ using Agent = ToxPubKey; using Doc = GreenCRDT::TextDocument; using ListType = Doc::ListType; +struct Command { + Agent actor; + uint64_t seq {0}; // independed of the ops inside, theoretically + //... + std::vector ops; +}; + +namespace std { + template + static void to_json(nlohmann::json& nlohmann_json_j, const std::optional& nlohmann_json_t) { + if (nlohmann_json_t.has_value()) { + nlohmann_json_j = nlohmann_json_t.value(); + } else { + nlohmann_json_j = nullptr; + } + } + + template + static void from_json(const nlohmann::json& nlohmann_json_j, std::optional& nlohmann_json_t) { + if (nlohmann_json_j != nullptr) { + nlohmann_json_t = static_cast(nlohmann_json_j); + } else { + nlohmann_json_t = std::nullopt; + } + } +} // namespace std + +namespace GreenCRDT { + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ListType::ListID, + id, + seq +) + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ListType::OpAdd, + id, + parent_left, + parent_right, + value +) + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ListType::OpDel, + id +) + +} // namespace GreenCRDT + +// bc variant <.< +namespace std { + static void to_json(nlohmann::json& nlohmann_json_j, const Doc::Op& nlohmann_json_t) { + if (std::holds_alternative(nlohmann_json_t)) { + nlohmann_json_j["t"] = "add"; + nlohmann_json_j["d"] = std::get(nlohmann_json_t); + } else if (std::holds_alternative(nlohmann_json_t)) { + nlohmann_json_j["t"] = "del"; + nlohmann_json_j["d"] = std::get(nlohmann_json_t); + } + } + + static void from_json(const nlohmann::json& nlohmann_json_j, Doc::Op& nlohmann_json_t) { + if (nlohmann_json_j.at("t") == "add") { + nlohmann_json_j.at("d").get_to(std::get(nlohmann_json_t)); + } else if (nlohmann_json_j.at("t") == "del") { + nlohmann_json_j.at("d").get_to(std::get(nlohmann_json_t)); + } + } +} // namespace std + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Command, + actor, + seq, + ops +) + namespace vim { static bool sendResponse(zed_net_socket_t* remote_socket, const int64_t id, const nlohmann::json& j) { @@ -187,6 +282,9 @@ struct SharedContext { ToxPubKey agent; std::promise agent_set; + // TODO: this is inefficent + std::mutex command_lists_mutex; + std::unordered_map> command_lists; // remote op queue for receive // local op list for remote lookups @@ -332,6 +430,7 @@ void toxThread(SharedContext* ctx) { } // namespace tox std::ostream& operator<<(std::ostream& out, const ToxPubKey& id) { + out << std::hex << static_cast(id.front()); return out; } @@ -621,12 +720,14 @@ static void self_connection_status_cb(Tox*, TOX_CONNECTION connection_status, vo std::cout << "self_connection_status_cb " << connection_status << "\n"; } -static void group_custom_packet_cb(Tox* tox, uint32_t group_number, uint32_t peer_id, const uint8_t* data, size_t length, void* user_data) { +static void group_custom_packet_cb(Tox*, uint32_t group_number, uint32_t peer_id, const uint8_t* data, size_t length, void* user_data) { std::cout << "group_custom_packet_cb\n"; + SharedContext& ctx = *static_cast(user_data); } -static void group_custom_private_packet_cb(Tox* tox, uint32_t group_number, uint32_t peer_id, const uint8_t* data, size_t length, void* user_data) { +static void group_custom_private_packet_cb(Tox*, uint32_t group_number, uint32_t peer_id, const uint8_t* data, size_t length, void* user_data) { std::cout << "group_custom_private_packet_cb\n"; + SharedContext& ctx = *static_cast(user_data); } }