[Libreoffice-commits] online.git: loolwsd/DocumentBroker.cpp loolwsd/DocumentBroker.hpp loolwsd/LOOLWSD.cpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Mon May 2 02:56:16 UTC 2016


 loolwsd/DocumentBroker.cpp |   30 ++++++++++++++++++++++++++++++
 loolwsd/DocumentBroker.hpp |   20 +++++++++++++++++++-
 loolwsd/LOOLWSD.cpp        |    1 +
 3 files changed, 50 insertions(+), 1 deletion(-)

New commits:
commit 10417c9447ec1d34a8a599daf28ac4339a37930a
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Sun May 1 20:39:36 2016 -0400

    loolwsd: establish communication with child from DocumentBroker
    
    The WebSocket that each child created with WSD is not used
    except to request the child to load the document a client
    requests. Beyond this point, it was not utilized for anything.
    
    In fact, there are no handlers in WSD for messages coming
    from the child; it is a one-way communication.
    
    That is until now. With the move to unify communication
    between WSD and each child, DocumentBroker can now
    receive and handle messages from its ChildProcess.
    
    Change-Id: Ie7f030a92db8303cd7087fff2325f136a49bc7fc
    Reviewed-on: https://gerrit.libreoffice.org/24581
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/DocumentBroker.cpp b/loolwsd/DocumentBroker.cpp
index 8f50e32..52e30f0 100644
--- a/loolwsd/DocumentBroker.cpp
+++ b/loolwsd/DocumentBroker.cpp
@@ -17,6 +17,27 @@
 #include "LOOLWSD.hpp"
 #include "Storage.hpp"
 #include "TileCache.hpp"
+#include "LOOLProtocol.hpp"
+
+using namespace LOOLProtocol;
+
+void ChildProcess::socketProcessor()
+{
+    IoUtil::SocketProcessor(_ws,
+        [this](const std::vector<char>& payload)
+        {
+            auto docBroker = this->_docBroker.lock();
+            if (docBroker)
+            {
+                return docBroker->handleInput(payload);
+            }
+
+            Log::warn("No DocumentBroker to handle child message: [" + LOOLProtocol::getAbbreviatedMessage(payload) + "].");
+            return true;
+        },
+        []() { },
+        [this]() { return !!this->_stop; });
+}
 
 namespace
 {
@@ -79,6 +100,7 @@ DocumentBroker::DocumentBroker(const Poco::URI& uriPublic,
 {
     assert(!_docKey.empty());
     assert(!_childRoot.empty());
+
     Log::info("DocumentBroker [" + _uriPublic.toString() + "] created. DocKey: [" + _docKey + "]");
 }
 
@@ -359,6 +381,14 @@ size_t DocumentBroker::removeSession(const std::string& id)
     return _sessions.size();
 }
 
+bool DocumentBroker::handleInput(const std::vector<char>& payload)
+{
+    Log::trace("DocumentBroker got child message: [" + LOOLProtocol::getAbbreviatedMessage(payload) + "].");
+
+    //TODO: Handle message.
+    return true;
+}
+
 bool DocumentBroker::canDestroy()
 {
     std::unique_lock<std::mutex> lock(_mutex);
diff --git a/loolwsd/DocumentBroker.hpp b/loolwsd/DocumentBroker.hpp
index 965ced3..efdedc3 100644
--- a/loolwsd/DocumentBroker.hpp
+++ b/loolwsd/DocumentBroker.hpp
@@ -17,6 +17,7 @@
 #include <memory>
 #include <mutex>
 #include <string>
+#include <thread>
 #include <map>
 
 #include <Poco/URI.h>
@@ -39,8 +40,10 @@ public:
     /// @param ws is the control WebSocket to the child.
     ChildProcess(const Poco::Process::PID pid, const std::shared_ptr<Poco::Net::WebSocket>& ws) :
         _pid(pid),
-        _ws(ws)
+        _ws(ws),
+        _stop(false)
     {
+        _thread = std::thread([this]() { this->socketProcessor(); });
         Log::info("ChildProcess ctor [" + std::to_string(_pid) + "].");
     }
 
@@ -57,8 +60,15 @@ public:
         }
     }
 
+    void setDocumentBroker(const std::shared_ptr<DocumentBroker>& docBroker)
+    {
+        _docBroker = docBroker;
+    }
+
     void close(const bool rude)
     {
+        _stop = true;
+        _thread.join();
         _ws.reset();
         if (_pid != -1)
         {
@@ -95,8 +105,14 @@ public:
     }
 
 private:
+    void socketProcessor();
+
+private:
     Poco::Process::PID _pid;
     std::shared_ptr<Poco::Net::WebSocket> _ws;
+    std::weak_ptr<DocumentBroker> _docBroker;
+    std::thread _thread;
+    std::atomic<bool> _stop;
 };
 
 /// DocumentBroker is responsible for setting up a document
@@ -184,6 +200,8 @@ public:
     bool canDestroy();
     bool isMarkedToDestroy() const { return _markToDestroy; }
 
+    bool handleInput(const std::vector<char>& payload);
+
 private:
 
     /// Sends the .uno:Save command to LoKit.
diff --git a/loolwsd/LOOLWSD.cpp b/loolwsd/LOOLWSD.cpp
index 5de9afc..1266417 100644
--- a/loolwsd/LOOLWSD.cpp
+++ b/loolwsd/LOOLWSD.cpp
@@ -533,6 +533,7 @@ private:
             // Set one we just created.
             Log::debug("New DocumentBroker for docKey [" + docKey + "].");
             docBroker = std::make_shared<DocumentBroker>(uriPublic, docKey, LOOLWSD::ChildRoot, child);
+            child->setDocumentBroker(docBroker);
         }
 
         // Validate the broker.


More information about the Libreoffice-commits mailing list