2016-12-10 21:33:45 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_SENDERQUEUE_HPP
|
|
|
|
#define INCLUDED_SENDERQUEUE_HPP
|
|
|
|
|
2016-12-13 18:20:05 -06:00
|
|
|
#include <condition_variable>
|
2016-12-11 10:59:46 -06:00
|
|
|
#include <deque>
|
|
|
|
#include <memory>
|
2016-12-13 18:20:05 -06:00
|
|
|
#include <mutex>
|
2017-03-06 16:03:11 -06:00
|
|
|
#include <thread>
|
2016-12-11 10:59:46 -06:00
|
|
|
#include <vector>
|
2016-12-10 21:33:45 -06:00
|
|
|
|
2016-12-18 16:28:35 -06:00
|
|
|
#include <Poco/Dynamic/Var.h>
|
|
|
|
#include <Poco/JSON/JSON.h>
|
|
|
|
#include <Poco/JSON/Object.h>
|
|
|
|
#include <Poco/JSON/Parser.h>
|
|
|
|
|
2016-12-11 10:59:46 -06:00
|
|
|
#include "common/SigUtil.hpp"
|
2016-12-10 21:33:45 -06:00
|
|
|
#include "Log.hpp"
|
2016-12-18 11:04:18 -06:00
|
|
|
#include "TileDesc.hpp"
|
2016-12-10 21:33:45 -06:00
|
|
|
|
2017-03-06 10:26:52 -06:00
|
|
|
#include "Socket.hpp" // FIXME: hack for wakeup-world ...
|
|
|
|
|
2016-12-10 21:33:45 -06:00
|
|
|
struct SendItem
|
|
|
|
{
|
2017-01-21 18:51:02 -06:00
|
|
|
std::shared_ptr<Message> Data;
|
2016-12-13 18:20:05 -06:00
|
|
|
std::string Meta;
|
2016-12-10 21:33:45 -06:00
|
|
|
std::chrono::steady_clock::time_point BirthTime;
|
|
|
|
};
|
|
|
|
|
2016-12-13 18:20:05 -06:00
|
|
|
/// A queue of data to send to certain Session's WS.
|
|
|
|
template <typename Item>
|
2016-12-11 17:07:26 -06:00
|
|
|
class SenderQueue final
|
2016-12-10 21:33:45 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-12-13 18:20:05 -06:00
|
|
|
SenderQueue() :
|
|
|
|
_stop(false)
|
|
|
|
{
|
|
|
|
}
|
2016-12-10 21:33:45 -06:00
|
|
|
|
2016-12-11 10:59:46 -06:00
|
|
|
bool stopping() const { return _stop || TerminationFlag; }
|
|
|
|
void stop()
|
|
|
|
{
|
2016-12-13 18:20:05 -06:00
|
|
|
_stop = true;
|
2016-12-11 10:59:46 -06:00
|
|
|
}
|
|
|
|
|
2016-12-13 18:20:05 -06:00
|
|
|
size_t enqueue(const Item& item)
|
2016-12-10 21:33:45 -06:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
2016-12-13 18:20:05 -06:00
|
|
|
|
2017-03-15 07:07:17 -05:00
|
|
|
if (!stopping() && deduplicate(item))
|
|
|
|
_queue.push_back(item);
|
2016-12-10 21:33:45 -06:00
|
|
|
|
2017-03-15 07:07:17 -05:00
|
|
|
return _queue.size();
|
2016-12-10 21:33:45 -06:00
|
|
|
}
|
|
|
|
|
2017-03-15 05:20:43 -05:00
|
|
|
/// Dequeue an item if we have one - @returns true if we do, else false.
|
|
|
|
bool dequeue(Item& item)
|
2016-12-10 21:33:45 -06:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2017-03-15 05:20:43 -05:00
|
|
|
if (!_queue.empty() && !stopping())
|
2016-12-10 21:33:45 -06:00
|
|
|
{
|
2017-03-15 05:20:43 -05:00
|
|
|
item = _queue.front();
|
|
|
|
_queue.pop_front();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (stopping())
|
|
|
|
LOG_DBG("SenderQueue: stopping");
|
2016-12-11 10:59:46 -06:00
|
|
|
return false;
|
2016-12-10 21:33:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
return _queue.size();
|
|
|
|
}
|
|
|
|
|
2016-12-18 11:04:18 -06:00
|
|
|
private:
|
|
|
|
/// Deduplicate messages based on the new one.
|
|
|
|
/// Returns true if the new message should be
|
|
|
|
/// enqueued, otherwise false.
|
|
|
|
bool deduplicate(const Item& item)
|
|
|
|
{
|
2016-12-18 13:11:36 -06:00
|
|
|
// Deduplicate messages based on the incoming one.
|
2016-12-18 12:29:45 -06:00
|
|
|
const std::string command = item->firstToken();
|
2016-12-18 11:04:18 -06:00
|
|
|
if (command == "tile:")
|
|
|
|
{
|
2016-12-18 13:11:36 -06:00
|
|
|
// Remove previous identical tile, if any, and use most recent (incoming).
|
|
|
|
const TileDesc newTile = TileDesc::parse(item->firstLine());
|
|
|
|
const auto& pos = std::find_if(_queue.begin(), _queue.end(),
|
|
|
|
[&newTile](const queue_item_t& cur)
|
2016-12-18 11:04:18 -06:00
|
|
|
{
|
2017-03-30 04:15:28 -05:00
|
|
|
return cur->firstToken() == "tile:" &&
|
|
|
|
newTile == TileDesc::parse(cur->firstLine());
|
2016-12-18 13:11:36 -06:00
|
|
|
});
|
2016-12-18 11:04:18 -06:00
|
|
|
|
2016-12-18 13:11:36 -06:00
|
|
|
if (pos != _queue.end())
|
2016-12-18 16:28:35 -06:00
|
|
|
{
|
|
|
|
_queue.erase(pos);
|
|
|
|
}
|
|
|
|
}
|
2016-12-18 16:48:44 -06:00
|
|
|
else if (command == "statusindicatorsetvalue:" ||
|
|
|
|
command == "invalidatecursor:")
|
|
|
|
{
|
|
|
|
// Remove previous identical enties of this command,
|
|
|
|
// if any, and use most recent (incoming).
|
|
|
|
const auto& pos = std::find_if(_queue.begin(), _queue.end(),
|
|
|
|
[&command](const queue_item_t& cur)
|
|
|
|
{
|
2017-03-30 04:15:28 -05:00
|
|
|
return cur->firstToken() == command;
|
2016-12-18 16:48:44 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
if (pos != _queue.end())
|
|
|
|
{
|
|
|
|
_queue.erase(pos);
|
|
|
|
}
|
|
|
|
}
|
2016-12-18 16:28:35 -06:00
|
|
|
else if (command == "invalidateviewcursor:")
|
|
|
|
{
|
|
|
|
// Remove previous cursor invalidation for same view,
|
|
|
|
// if any, and use most recent (incoming).
|
|
|
|
const std::string newMsg = item->jsonString();
|
|
|
|
Poco::JSON::Parser newParser;
|
|
|
|
const auto newResult = newParser.parse(newMsg);
|
|
|
|
const auto& newJson = newResult.extract<Poco::JSON::Object::Ptr>();
|
|
|
|
const auto viewId = newJson->get("viewId").toString();
|
|
|
|
const auto& pos = std::find_if(_queue.begin(), _queue.end(),
|
|
|
|
[command, viewId](const queue_item_t& cur)
|
|
|
|
{
|
|
|
|
if (cur->firstToken() == command)
|
|
|
|
{
|
|
|
|
const std::string msg = cur->jsonString();
|
|
|
|
Poco::JSON::Parser parser;
|
|
|
|
const auto result = parser.parse(msg);
|
|
|
|
const auto& json = result.extract<Poco::JSON::Object::Ptr>();
|
2017-03-30 04:15:28 -05:00
|
|
|
return viewId == json->get("viewId").toString();
|
2016-12-18 16:28:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (pos != _queue.end())
|
2016-12-18 13:11:36 -06:00
|
|
|
_queue.erase(pos);
|
2016-12-18 11:04:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-10 21:33:45 -06:00
|
|
|
private:
|
|
|
|
mutable std::mutex _mutex;
|
2016-12-13 18:20:05 -06:00
|
|
|
std::deque<Item> _queue;
|
2016-12-18 11:04:18 -06:00
|
|
|
typedef typename std::deque<Item>::value_type queue_item_t;
|
2016-12-11 10:59:46 -06:00
|
|
|
std::atomic<bool> _stop;
|
2016-12-10 21:33:45 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|