[Libreoffice-commits] online.git: loolwsd/ChildProcessSession.cpp loolwsd/ChildProcessSession.hpp loolwsd/LOOLKit.cpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Mon Jan 4 17:03:20 PST 2016


 loolwsd/ChildProcessSession.cpp |   16 ++++++++++---
 loolwsd/ChildProcessSession.hpp |    6 ++++
 loolwsd/LOOLKit.cpp             |   49 +++++++++++++++++++++++++++++++---------
 3 files changed, 57 insertions(+), 14 deletions(-)

New commits:
commit c818ece721a60c13692425c303c01dff024ba0e9
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Sun Jan 3 12:38:26 2016 -0500

    loolwsd: document loading/unloading notifications
    
    Change-Id: I435a4e7f89834bea854a492e487af2a686a6f650
    Reviewed-on: https://gerrit.libreoffice.org/21093
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/ChildProcessSession.cpp b/loolwsd/ChildProcessSession.cpp
index 7180776..8fa38fe 100644
--- a/loolwsd/ChildProcessSession.cpp
+++ b/loolwsd/ChildProcessSession.cpp
@@ -45,13 +45,17 @@ ChildProcessSession::ChildProcessSession(const std::string& id,
                                          std::shared_ptr<Poco::Net::WebSocket> ws,
                                          LibreOfficeKit *loKit,
                                          LibreOfficeKitDocument * loKitDocument,
-                                         const std::string& childId) :
+                                         const std::string& childId,
+                                         std::function<void(LibreOfficeKitDocument*, int)> onLoad,
+                                         std::function<void(int)> onUnload) :
     LOOLSession(id, Kind::ToMaster, ws),
     _loKitDocument(loKitDocument),
     _loKit(loKit),
     _childId(childId),
     _viewId(0),
-    _clientPart(0)
+    _clientPart(0),
+    _onLoad(onLoad),
+    _onUnload(onUnload)
 {
     Log::info("ChildProcessSession ctor [" + getName() + "].");
 }
@@ -62,12 +66,16 @@ ChildProcessSession::~ChildProcessSession()
 
     if (_loKitDocument != nullptr)
     {
+        _loKitDocument->pClass->registerCallback(_loKitDocument, nullptr, this);
+
         _loKitDocument->pClass->destroyView(_loKitDocument, _viewId);
         Log::debug("Destroy view [" + getName() + "]-> [" + std::to_string(_viewId) + "]");
     }
 
     if (LIBREOFFICEKIT_HAS(_loKit, registerCallback))
         _loKit->pClass->registerCallback(_loKit, nullptr, this);
+
+    _onUnload(_viewId);
 }
 
 bool ChildProcessSession::_handleInput(const char *buffer, int length)
@@ -234,7 +242,7 @@ bool ChildProcessSession::loadDocument(const char *buffer, int length, StringTok
 
     std::string timestamp;
     parseDocOptions(tokens, part, timestamp);
-
+    Log::info("docURL: [" + _docURL + "].");
     URI aUri;
     try
     {
@@ -307,6 +315,8 @@ bool ChildProcessSession::loadDocument(const char *buffer, int length, StringTok
     if (!getStatus(buffer, length))
         return false;
 
+    _onLoad(_loKitDocument, _viewId);
+
     return true;
 }
 
diff --git a/loolwsd/ChildProcessSession.hpp b/loolwsd/ChildProcessSession.hpp
index 7f5aa05..9bbb869 100644
--- a/loolwsd/ChildProcessSession.hpp
+++ b/loolwsd/ChildProcessSession.hpp
@@ -29,7 +29,9 @@ public:
                         std::shared_ptr<Poco::Net::WebSocket> ws,
                         LibreOfficeKit *loKit,
                         LibreOfficeKitDocument * loKitDocument,
-                        const std::string& childId);
+                        const std::string& childId,
+                        std::function<void(LibreOfficeKitDocument*, int)> onLoad,
+                        std::function<void(int)> onUnload);
     virtual ~ChildProcessSession();
 
     virtual bool getStatus(const char *buffer, int length);
@@ -80,6 +82,8 @@ private:
     /// View ID, returned by createView() or 0 by default.
     int _viewId;
     int _clientPart;
+    std::function<void(LibreOfficeKitDocument*, int)> _onLoad;
+    std::function<void(int)> _onUnload;
 };
 
 class CallBackNotification: public Poco::Notification
diff --git a/loolwsd/LOOLKit.cpp b/loolwsd/LOOLKit.cpp
index b17e188..142f935 100644
--- a/loolwsd/LOOLKit.cpp
+++ b/loolwsd/LOOLKit.cpp
@@ -259,7 +259,7 @@ public:
 
     void run()
     {
-        static const std::string thread_name = "lokit_callback_handler";
+        static const std::string thread_name = "kit_callback";
 #ifdef __linux
         if (prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(thread_name.c_str()), 0, 0, 0) != 0)
             Log::error("Cannot set thread name to " + thread_name + ".");
@@ -329,7 +329,7 @@ public:
 
     void run() override
     {
-        static const std::string thread_name = "lokit_queue_handler";
+        static const std::string thread_name = "kit_queue_" + _session->getId();
 #ifdef __linux
         if (prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(thread_name.c_str()), 0, 0, 0) != 0)
             Log::error("Cannot set thread name to " + thread_name + ".");
@@ -370,12 +370,16 @@ class Connection: public Runnable
 {
 public:
     Connection(LibreOfficeKit *loKit, LibreOfficeKitDocument *loKitDocument,
-               const std::string& childId, const std::string& sessionId) :
+               const std::string& childId, const std::string& sessionId,
+               std::function<void(LibreOfficeKitDocument*, int)> onLoad,
+               std::function<void(int)> onUnload) :
         _loKit(loKit),
         _loKitDocument(loKitDocument),
         _childId(childId),
         _sessionId(sessionId),
-        _stop(false)
+        _stop(false),
+        _onLoad(onLoad),
+        _onUnload(onUnload)
     {
         Log::info("Connection ctor in child: " + childId + ", thread: " + _sessionId);
     }
@@ -415,7 +419,7 @@ public:
 
     void run() override
     {
-        static const std::string thread_name = "lokit_connection";
+        static const std::string thread_name = "kit_socket_" + _sessionId;
 #ifdef __linux
         if (prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(thread_name.c_str()), 0, 0, 0) != 0)
             Log::error("Cannot set thread name to " + thread_name + ".");
@@ -433,7 +437,7 @@ public:
             HTTPResponse response;
             auto ws = std::make_shared<WebSocket>(cs, request, response);
 
-            _session.reset(new ChildProcessSession(_sessionId, ws, _loKit, _loKitDocument, _childId));
+            _session.reset(new ChildProcessSession(_sessionId, ws, _loKit, _loKitDocument, _childId, _onLoad, _onUnload));
             ws->setReceiveTimeout(0);
 
             // child Jail TID PID
@@ -498,6 +502,8 @@ private:
     Thread _thread;
     std::shared_ptr<ChildProcessSession> _session;
     volatile bool _stop;
+    std::function<void(LibreOfficeKitDocument*, int)> _onLoad;
+    std::function<void(int)> _onUnload;
 };
 
 // A document container.
@@ -514,8 +520,10 @@ public:
              const std::string& url)
       : _loKit(loKit),
         _childId(childId),
-        _url(url)
+        _url(url),
+        _loKitDocument(nullptr)
     {
+        Log::info("Document ctor for url [" + url + "] on child [" + childId + "].");
     }
 
     ~Document()
@@ -559,7 +567,9 @@ public:
                 Log::warn("Thread [" + sessionId + "] is not running. Restoring.");
                 _connections.erase(sessionId);
 
-                auto thread = std::make_shared<Connection>(_loKit, aItem->second->getLOKitDocument(), _childId, sessionId);
+                auto thread = std::make_shared<Connection>(_loKit, aItem->second->getLOKitDocument(), _childId, sessionId,
+                                                           [this](LibreOfficeKitDocument *loKitDocument, const int viewId) { onLoad(loKitDocument, viewId); },
+                                                           [this](const int viewId) { onUnload(viewId); });
                 _connections.emplace(sessionId, thread);
                 thread->start();
             }
@@ -572,13 +582,17 @@ public:
             if ( _connections.empty() )
             {
                 Log::info("Creating main thread for child: " + _childId + ", thread: " + sessionId);
-                thread = std::make_shared<Connection>(_loKit, nullptr, _childId, sessionId);
+                thread = std::make_shared<Connection>(_loKit, nullptr, _childId, sessionId,
+                                                      [this](LibreOfficeKitDocument *loKitDocument, const int viewId) { onLoad(loKitDocument, viewId); },
+                                                      [this](const int viewId) { onUnload(viewId); });
             }
             else
             {
                 Log::info("Creating view thread for child: " + _childId + ", thread: " + sessionId);
                 auto aConnection = _connections.begin();
-                thread = std::make_shared<Connection>(_loKit, aConnection->second->getLOKitDocument(), _childId, sessionId);
+                thread = std::make_shared<Connection>(_loKit, aConnection->second->getLOKitDocument(), _childId, sessionId,
+                                                      [this](LibreOfficeKitDocument *loKitDocument, const int viewId) { onLoad(loKitDocument, viewId); },
+                                                      [this](const int viewId) { onUnload(viewId); });
             }
 
             auto aInserted = _connections.emplace(sessionId, thread);
@@ -594,6 +608,21 @@ public:
 
 private:
 
+    void onLoad(LibreOfficeKitDocument *loKitDocument, const int viewId)
+    {
+        Log::info("Document [" + _url + "] loaded as view #" + std::to_string(viewId) + ".");
+        if (_loKitDocument != nullptr)
+            assert(_loKitDocument == loKitDocument);
+        _loKitDocument = loKitDocument;
+    }
+
+    void onUnload(const int viewId)
+    {
+        Log::info("Document [" + _url + "] view #" + std::to_string(viewId)+ " unloaded.");
+    }
+
+private:
+
     LibreOfficeKit *_loKit;
     const std::string _childId;
     const std::string _url;


More information about the Libreoffice-commits mailing list