From 4eedb599d8de5559daa4678c7520bb36968e767e Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 28 Jan 2023 21:24:11 +0100 Subject: Server to client: diffs --- whiteboard.cpp | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'whiteboard.cpp') diff --git a/whiteboard.cpp b/whiteboard.cpp index 059571d..35ae30b 100644 --- a/whiteboard.cpp +++ b/whiteboard.cpp @@ -83,33 +83,39 @@ void Whiteboard::storage_cleanup() } } -std::string make_xml(const std::initializer_list>& key_values) +pt::ptree make_ptree(const std::initializer_list>& key_values) { - pt::ptree xml; + pt::ptree ptree; for (const auto& i: key_values) { - xml.put(fmt::format("serverinfo.{}", i.first), i.second); + ptree.put(fmt::format("serverinfo.{}", i.first), i.second); } - return Reichwein::XML::plain_xml(xml); + return ptree; +} + +std::string make_xml(const std::initializer_list>& key_values) +{ + pt::ptree ptree{make_ptree(key_values)}; + return Reichwein::XML::plain_xml(ptree); } -void Whiteboard::notify_other_connections_file(Whiteboard::connection& c, const std::string& id) +void Whiteboard::notify_other_connections_diff(Whiteboard::connection& c, const std::string& id, const Diff& diff) { std::for_each(m_registry.begin(id), m_registry.end(id), [&](const Whiteboard::connection& ci) { if (c != ci) { boost::beast::flat_buffer buffer; - boost::beast::ostream(buffer) << make_xml({ - {"type", "getfile"}, - {"data", m_storage->getDocument(id)}, - {"revision", std::to_string(m_storage->getRevision(id)) }, - {"pos", std::to_string(m_storage->getCursorPos(id)) } - }); + pt::ptree ptree {make_ptree({ + {"type", "getdiff"}, + {"revision", std::to_string(m_storage->getRevision(id)) } + })}; + ptree.put_child("serverinfo.diff", diff.get_structure().get_child("diff")); + boost::beast::ostream(buffer) << Reichwein::XML::plain_xml(ptree); std::lock_guard lock(m_websocket_mutex); try { ci->write(buffer.data()); } catch (const std::exception& ex) { - std::cerr << "Warning: Notify getfile write for " << ci << " not possible, id " << id << std::endl; + std::cerr << "Warning: Notify getdiff write for " << ci << " not possible, id " << id << std::endl; m_registry.dump(); } } @@ -165,7 +171,7 @@ std::string Whiteboard::handle_request(Whiteboard::connection& c, const std::str m_storage->setDocument(id, data); m_registry.setId(c, id); - notify_other_connections_file(c, id); + notify_other_connections_diff(c, id, d); int pos {xml.get("request.pos")}; if (m_storage->getCursorPos(id) != pos) { -- cgit v1.2.3