summaryrefslogtreecommitdiffhomepage
path: root/whiteboard.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2023-01-27 19:42:08 +0100
committerRoland Reichwein <mail@reichwein.it>2023-01-27 19:42:08 +0100
commitf44d36b05e43cabde31aeaba5d25fded140345a1 (patch)
tree1024a76cb1ae671c9445dcc379cb9eddd26922aa /whiteboard.cpp
parent789e5555ab4c44a1ae779eccf6ccf8340602cf22 (diff)
Added diff.cpp
Diffstat (limited to 'whiteboard.cpp')
-rw-r--r--whiteboard.cpp55
1 files changed, 48 insertions, 7 deletions
diff --git a/whiteboard.cpp b/whiteboard.cpp
index b15ebbe..df18242 100644
--- a/whiteboard.cpp
+++ b/whiteboard.cpp
@@ -34,6 +34,7 @@
#include <fmt/core.h>
+#include "libreichwein/base64.h"
#include "libreichwein/file.h"
#include "config.h"
@@ -89,11 +90,12 @@ std::string make_xml(const std::initializer_list<std::pair<std::string, std::str
std::ostringstream oss;
// write_xml_element instead of write_xml to omit <!xml...> header
+ //pt::xml_parser::write_xml(oss, xml);
pt::xml_parser::write_xml_element(oss, {}, xml, -1, boost::property_tree::xml_writer_settings<pt::ptree::key_type>{});
return oss.str();
}
-void Whiteboard::notify_other_connections(Whiteboard::connection& c, const std::string& id)
+void Whiteboard::notify_other_connections_file(Whiteboard::connection& c, const std::string& id)
{
std::for_each(m_registry.begin(id), m_registry.end(id), [&](const Whiteboard::connection& ci)
{
@@ -103,13 +105,34 @@ void Whiteboard::notify_other_connections(Whiteboard::connection& c, const std::
{"type", "getfile"},
{"data", m_storage->getDocument(id)},
{"revision", std::to_string(m_storage->getRevision(id)) },
- {"cursorpos", std::to_string(m_storage->getCursorPos(id)) }
+ {"pos", std::to_string(m_storage->getCursorPos(id)) }
});
std::lock_guard<std::mutex> lock(m_websocket_mutex);
try {
ci->write(buffer.data());
} catch (const std::exception& ex) {
- std::cerr << "Warning: Notify write for " << ci << " not possible, id " << id << std::endl;
+ std::cerr << "Warning: Notify getfile write for " << ci << " not possible, id " << id << std::endl;
+ m_registry.dump();
+ }
+ }
+ });
+}
+
+void Whiteboard::notify_other_connections_pos(Whiteboard::connection& c, const std::string& id)
+{
+ 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", "getpos"},
+ {"pos", std::to_string(m_storage->getCursorPos(id)) }
+ });
+ std::lock_guard<std::mutex> lock(m_websocket_mutex);
+ try {
+ ci->write(buffer.data());
+ } catch (const std::exception& ex) {
+ std::cerr << "Warning: Notify getpos write for " << ci << " not possible, id " << id << std::endl;
m_registry.dump();
}
}
@@ -135,7 +158,13 @@ std::string Whiteboard::handle_request(Whiteboard::connection& c, const std::str
if (m_storage->getDocument(id) != data) {
m_storage->setDocument(id, data);
m_registry.setId(c, id);
- notify_other_connections(c, id);
+ notify_other_connections_file(c, id);
+
+ int pos {xml.get<int>("request.pos")};
+ if (m_storage->getCursorPos(id) != pos) {
+ m_storage->setCursorPos(id, pos);
+ notify_other_connections_pos(c, id);
+ }
return make_xml({{"type", "modify"}, {"revision", std::to_string(m_storage->getRevision(id)) }});
}
return {};
@@ -144,7 +173,7 @@ std::string Whiteboard::handle_request(Whiteboard::connection& c, const std::str
int pos {xml.get<int>("request.pos")};
if (m_storage->getCursorPos(id) != pos) {
m_storage->setCursorPos(id, pos);
- notify_other_connections(c, id);
+ notify_other_connections_pos(c, id);
}
return {};
} else if (command == "getfile") {
@@ -165,7 +194,14 @@ std::string Whiteboard::handle_request(Whiteboard::connection& c, const std::str
{"type", "getfile"},
{"data", filedata},
{"revision", std::to_string(m_storage->getRevision(id)) },
- {"cursorpos", std::to_string(m_storage->getCursorPos(id)) }
+ {"pos", std::to_string(m_storage->getCursorPos(id)) }
+ });
+ } else if (command == "getpos") {
+ std::string id {xml.get<std::string>("request.id")};
+
+ return make_xml({
+ {"type", "getpos"},
+ {"pos", std::to_string(m_storage->getCursorPos(id)) }
});
} else if (command == "newid") {
return make_xml({{"type", "newid"}, {"id", m_storage->generate_id()}});
@@ -177,7 +213,12 @@ std::string Whiteboard::handle_request(Whiteboard::connection& c, const std::str
std::string pngdata {QRCode::getQRCode(url)};
- return make_xml({{"type", "qrcode"}, {"png", pngdata}});
+ return make_xml({{"type", "qrcode"}, {"png", Reichwein::Base64::encode64(pngdata)}});
+ } else if (command == "getversion") {
+ return make_xml({
+ {"type", "version"},
+ {"version", WHITEBOARD_VERSION }
+ });
} else {
throw std::runtime_error("Bad command: "s + command);
}