[Libreoffice-commits] online.git: 2 commits - loleaflet/src loolwsd/Admin.cpp loolwsd/AdminModel.hpp loolwsd/Auth.hpp loolwsd/Storage.hpp
Pranav Kant
pranavk at collabora.com
Tue Mar 8 10:09:06 UTC 2016
loleaflet/src/admin/AdminSocketOverview.js | 1
loolwsd/Admin.cpp | 21 +++++++-
loolwsd/AdminModel.hpp | 70 ++++++++++++++++++++++++++---
loolwsd/Auth.hpp | 1
loolwsd/Storage.hpp | 1
5 files changed, 86 insertions(+), 8 deletions(-)
New commits:
commit 1d511b109ab5e4e16abc3b2824cef5dc4fb55db6
Author: Pranav Kant <pranavk at collabora.com>
Date: Sat Mar 5 20:53:01 2016 +0530
loleaflet: Subscribe to appropriate commands
Change-Id: I0f6571a453fb8850902003d1cc2fecafa5eb4474
diff --git a/loleaflet/src/admin/AdminSocketOverview.js b/loleaflet/src/admin/AdminSocketOverview.js
index 8d89a21..6371d8e 100644
--- a/loleaflet/src/admin/AdminSocketOverview.js
+++ b/loleaflet/src/admin/AdminSocketOverview.js
@@ -16,6 +16,7 @@ var AdminSocketOverview = AdminSocketBase.extend({
onSocketOpen: function() {
this.socket.send('documents');
+ this.socket.send('subscribe document addview rmview rmdoc');
this._getBasicStats();
var socketOverview = this;
commit afb7e7dcff4657b34ffdde60f5ab84b225b25f8a
Author: Pranav Kant <pranavk at collabora.com>
Date: Sat Mar 5 20:51:25 2016 +0530
loolwsd: Make clients subscribe to commands
... for which they want to be notified.
Needed to add some missing header files to Storage and Auth class
because of conflict raised when LOOLWSD.hpp is included in
Admin.cpp
Change-Id: Ia1c8ed82f8cd979eaf93267ae5dfa347acf895f4
diff --git a/loolwsd/Admin.cpp b/loolwsd/Admin.cpp
index c4a6d33..3649df6 100644
--- a/loolwsd/Admin.cpp
+++ b/loolwsd/Admin.cpp
@@ -23,6 +23,7 @@
#include "AdminModel.hpp"
#include "Common.hpp"
#include "LOOLProtocol.hpp"
+#include "LOOLWSD.hpp"
#include "Util.hpp"
using namespace LOOLProtocol;
@@ -52,7 +53,9 @@ public:
{
assert(request.serverAddress().port() == ADMIN_PORT_NUMBER);
- const std::string thread_name = "admin_ws";
+ // Different session id pool for admin sessions (?)
+ const auto nSessionId = Util::decodeId(LOOLWSD::GenSessionId());
+ const std::string thread_name = "admin_ws_" + std::to_string(nSessionId);
try
{
if (prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(thread_name.c_str()), 0, 0, 0) != 0)
@@ -64,7 +67,7 @@ public:
// Subscribe the websocket of any AdminModel updates
AdminModel& model = _admin->getModel();
- model.subscribe(ws);
+ model.subscribe(nSessionId, ws);
const Poco::Timespan waitTime(POLL_TIMEOUT_MS * 1000);
int flags = 0;
@@ -143,6 +146,20 @@ public:
ws->sendFrame(statsResponse.data(), statsResponse.size());
}
+ else if (tokens[0] == "subscribe" && tokens.count() > 1)
+ {
+ for (unsigned i = 0; i < tokens.count() - 1; i++)
+ {
+ model.subscribe(nSessionId, tokens[i + 1]);
+ }
+ }
+ else if (tokens[0] == "unsubscribe" && tokens.count() > 1)
+ {
+ for (unsigned i = 0; i < tokens.count() - 1; i++)
+ {
+ model.unsubscribe(nSessionId, tokens[i + 1]);
+ }
+ }
else if (tokens[0] == "documents")
{
diff --git a/loolwsd/AdminModel.hpp b/loolwsd/AdminModel.hpp
index 755a6aa..e94fbbf 100644
--- a/loolwsd/AdminModel.hpp
+++ b/loolwsd/AdminModel.hpp
@@ -11,6 +11,7 @@
#define INCLUDED_ADMIN_MODEL_HPP
#include <memory>
+#include <set>
#include <sstream>
#include <string>
@@ -126,8 +127,10 @@ private:
class Subscriber
{
public:
- Subscriber(std::shared_ptr<Poco::Net::WebSocket>& ws)
- : _ws(ws)
+ Subscriber(int nSessionId, std::shared_ptr<Poco::Net::WebSocket>& ws)
+ : _nSessionId(nSessionId),
+ _ws(ws),
+ _start(std::time(nullptr))
{
Log::info("Subscriber ctor.");
}
@@ -139,6 +142,11 @@ public:
bool notify(const std::string& message)
{
+ Poco::StringTokenizer tokens(message, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
+
+ if (_subscriptions.find(tokens[0]) == _subscriptions.end())
+ return true;
+
auto webSocket = _ws.lock();
if (webSocket)
{
@@ -151,10 +159,38 @@ public:
}
}
+ bool subscribe(const std::string& command)
+ {
+ auto ret = _subscriptions.insert(command);
+ return ret.second;
+ }
+
+ void unsubscribe(const std::string& command)
+ {
+ _subscriptions.erase(command);
+ }
+
+ void expire()
+ {
+ _end = std::time(nullptr);
+ }
+
+ bool isExpired() const
+ {
+ return _end != 0 && std::time(nullptr) >= _end;
+ }
+
private:
+ /// Admin session Id
+ int _nSessionId;
/// WebSocket to use to send messages to session
std::weak_ptr<Poco::Net::WebSocket> _ws;
+ std::set<std::string> _subscriptions;
+
+ std::time_t _start;
+ std::time_t _end = 0;
+
/// In case of huge number of documents,
/// client can tell us the specific page it is
/// interested in getting live notifications
@@ -249,9 +285,31 @@ public:
return totalMem;
}
- void subscribe(std::shared_ptr<Poco::Net::WebSocket>& ws)
+ void subscribe(int nSessionId, std::shared_ptr<Poco::Net::WebSocket>& ws)
{
- _subscribers.push_back(ws);
+ auto ret = _subscribers.insert(std::pair<int, Subscriber>(nSessionId, Subscriber(nSessionId, ws)));
+ if (!ret.second)
+ {
+ Log::warn() << "Subscriber already exists" << Log::end;
+ }
+ }
+
+ void subscribe(int nSessionId, const std::string& command)
+ {
+ auto subscriber = _subscribers.find(nSessionId);
+ if (subscriber == _subscribers.end() )
+ return;
+
+ subscriber->second.subscribe(command);
+ }
+
+ void unsubscribe(int nSessionId, const std::string& command)
+ {
+ auto subscriber = _subscribers.find(nSessionId);
+ if (subscriber == _subscribers.end())
+ return;
+
+ subscriber->second.unsubscribe(command);
}
private:
@@ -286,7 +344,7 @@ private:
auto it = std::begin(_subscribers);
while (it != std::end(_subscribers))
{
- if (!it->notify(message))
+ if (!it->second.notify(message))
{
it = _subscribers.erase(it);
}
@@ -334,7 +392,7 @@ private:
}
private:
- std::vector<Subscriber> _subscribers;
+ std::map<int, Subscriber> _subscribers;
std::map<Poco::Process::PID, Document> _documents;
/// Number of active documents
diff --git a/loolwsd/Auth.hpp b/loolwsd/Auth.hpp
index f118505..d1201a1 100644
--- a/loolwsd/Auth.hpp
+++ b/loolwsd/Auth.hpp
@@ -17,6 +17,7 @@
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
+#include <Poco/URI.h>
#include "Util.hpp"
diff --git a/loolwsd/Storage.hpp b/loolwsd/Storage.hpp
index f38c593..a9db691 100644
--- a/loolwsd/Storage.hpp
+++ b/loolwsd/Storage.hpp
@@ -12,6 +12,7 @@
#define INCLUDED_STORAGE_HPP
#include <string>
+#include <fstream>
#include <Poco/Net/HTTPResponse.h>
More information about the Libreoffice-commits
mailing list