summaryrefslogtreecommitdiffhomepage
path: root/whiteboard.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2023-01-28 20:31:24 +0100
committerRoland Reichwein <mail@reichwein.it>2023-01-28 20:31:24 +0100
commita69b1d0c580bc779740ef79a7d16b69229896785 (patch)
tree962ecac93c521c1d09f1dc1ca727b7121d1b5472 /whiteboard.cpp
parentdd358ac5cdfc2b449dcbe94a33a06a7540d8f966 (diff)
Client to Server: send diffs instead of whole file
Diffstat (limited to 'whiteboard.cpp')
-rw-r--r--whiteboard.cpp34
1 files changed, 21 insertions, 13 deletions
diff --git a/whiteboard.cpp b/whiteboard.cpp
index b84f7cc..059571d 100644
--- a/whiteboard.cpp
+++ b/whiteboard.cpp
@@ -39,6 +39,7 @@
#include "libreichwein/xml.h"
#include "config.h"
+#include "diff.h"
#include "qrcode.h"
#include "storage.h"
@@ -151,20 +152,27 @@ std::string Whiteboard::handle_request(Whiteboard::connection& c, const std::str
if (command == "modify") {
std::string id {xml.get<std::string>("request.id")};
- std::string data {xml.get<std::string>("request.data")};
- if (m_storage->getDocument(id) != data) {
- m_storage->setDocument(id, data);
- m_registry.setId(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)) }});
+
+ int baserev {xml.get<int>("request.baserev")};
+ if (baserev != m_storage->getRevision(id))
+ return make_xml({{"type", "error"}, {"message", "Bad base revision ("s + std::to_string(baserev) + "). Current: "s + std::to_string(m_storage->getRevision(id)) }});
+
+ pt::ptree ptree;
+ ptree.put_child("diff", xml.get_child("request.diff"));
+ Diff d{ptree};
+ std::string data {m_storage->getDocument(id)};
+ data = d.apply(data);
+
+ m_storage->setDocument(id, data);
+ m_registry.setId(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 {};
+ return make_xml({{"type", "modify"}, {"revision", std::to_string(m_storage->getRevision(id)) }});
} else if (command == "cursorpos") {
std::string id {xml.get<std::string>("request.id")};
int pos {xml.get<int>("request.pos")};