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/.
|
|
|
|
*/
|
|
|
|
|
2020-04-18 03:39:50 -05:00
|
|
|
#pragma once
|
2016-12-10 21:33:45 -06:00
|
|
|
|
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
|
|
|
|
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:
|
|
|
|
|
2017-04-05 12:06:58 -05:00
|
|
|
SenderQueue()
|
2016-12-13 18:20:05 -06:00
|
|
|
{
|
|
|
|
}
|
2016-12-10 21:33:45 -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
|
|
|
|
2019-08-08 02:10:59 -05:00
|
|
|
if (!SigUtil::getTerminationFlag() && deduplicate(item))
|
2017-03-15 07:07:17 -05:00
|
|
|
_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);
|
|
|
|
|
2019-08-08 02:10:59 -05:00
|
|
|
if (!_queue.empty() && !SigUtil::getTerminationFlag())
|
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
|
|
|
|
{
|
2019-08-08 02:10:59 -05:00
|
|
|
if (SigUtil::getTerminationFlag())
|
2018-10-15 09:37:21 -05:00
|
|
|
LOG_DBG("SenderQueue: TerminationFlag is set");
|
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();
|
|
|
|
}
|
|
|
|
|
2017-04-05 11:59:29 -05:00
|
|
|
void dumpState(std::ostream& os)
|
|
|
|
{
|
2020-05-24 08:10:18 -05:00
|
|
|
os << "\n\t\tqueue size " << _queue.size() << '\n';
|
2017-04-05 11:59:29 -05:00
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
for (const Item &item : _queue)
|
|
|
|
{
|
2020-05-24 08:10:18 -05:00
|
|
|
os << "\t\t\ttype: " << (item->isBinary() ? "binary\n" : "text\n");
|
|
|
|
os << "\t\t\t" << item->abbr() << '\n';
|
2017-04-05 11:59:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:" ||
|
2017-04-16 19:50:15 -05:00
|
|
|
command == "invalidatecursor:" ||
|
|
|
|
command == "setpart:")
|
2016-12-18 16:48:44 -06:00
|
|
|
{
|
2020-04-26 15:55:16 -05:00
|
|
|
// Remove previous identical entries of this command,
|
2016-12-18 16:48:44 -06:00
|
|
|
// 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;
|
2018-02-07 03:17:59 -06:00
|
|
|
const Poco::Dynamic::Var newResult = newParser.parse(newMsg);
|
2016-12-18 16:28:35 -06:00
|
|
|
const auto& newJson = newResult.extract<Poco::JSON::Object::Ptr>();
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string viewId = newJson->get("viewId").toString();
|
2016-12-18 16:28:35 -06:00
|
|
|
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;
|
2018-02-07 03:17:59 -06:00
|
|
|
const Poco::Dynamic::Var result = parser.parse(msg);
|
2016-12-18 16:28:35 -06:00
|
|
|
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-10 21:33:45 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|