summaryrefslogtreecommitdiffstats
path: root/common/MessageQueue.hpp
blob: 483f169fba1e893087092206d6d1b2ee646f413f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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/.
 */

#pragma once

#include <stdexcept>
#include <algorithm>
#include <functional>
#include <map>
#include <string>
#include <vector>

#include "Log.hpp"
#include "Protocol.hpp"

/// Thread-safe message queue (FIFO).
class MessageQueue
{
public:
    typedef std::vector<char> Payload;

    MessageQueue()
    {
    }

    virtual ~MessageQueue()
    {
        clear();
    }

    MessageQueue(const MessageQueue&) = delete;
    MessageQueue& operator=(const MessageQueue&) = delete;

    /// Thread safe insert the message.
    void put(const Payload& value)
    {
        if (value.empty())
        {
            throw std::runtime_error("Cannot queue empty item.");
        }

        put_impl(value);
    }

    void put(const std::string& value)
    {
        put(Payload(value.data(), value.data() + value.size()));
    }

    /// Thread safe obtaining of the message.
    /// timeoutMs can be 0 to signify infinity.
    /// Returns an empty payload on timeout.
    Payload get()
    {
        return get_impl();
    }

    /// Get a message without waiting
    Payload pop()
    {
        if (_queue.empty())
            return Payload();
        return get_impl();
    }

    /// Anything in the queue ?
    bool isEmpty()
    {
        return _queue.empty();
    }

    /// Thread safe removal of all the pending messages.
    void clear()
    {
        clear_impl();
    }

    /// Thread safe remove_if.
    void remove_if(const std::function<bool(const Payload&)>& pred)
    {
        std::remove_if(_queue.begin(), _queue.end(), pred);
    }

protected:
    virtual void put_impl(const Payload& value)
    {
        StringVector tokens = Util::tokenize(value.data(), value.size());
        if (tokens.equals(1, "textinput"))
        {
            const std::string newMsg = combineTextInput(tokens);
            if (!newMsg.empty())
            {
                _queue.push_back(Payload(newMsg.data(), newMsg.data() + newMsg.size()));
                return;
            }
        }
        else if (tokens.equals(1, "removetextcontext"))
        {
            const std::string newMsg = combineRemoveText(tokens);
            if (!newMsg.empty())
            {
                _queue.push_back(Payload(newMsg.data(), newMsg.data() + newMsg.size()));
                return;
            }
        }

        _queue.emplace_back(value);
    }

    virtual Payload get_impl()
    {
        Payload result = _queue.front();
        _queue.erase(_queue.begin());
        return result;
    }

    void clear_impl()
    {
        _queue.clear();
    }

    std::vector<Payload>& getQueue() { return _queue; }

    /// Search the queue for a previous textinput message and if found, remove it and combine its
    /// input with that in the current textinput message. We check that there aren't any interesting
    /// messages inbetween that would make it wrong to merge the textinput messages.
    ///
    /// @return New message to put into the queue. If empty, use what we got.
    std::string combineTextInput(const StringVector& tokens)
    {
        std::string id;
        std::string text;
        if (!COOLProtocol::getTokenString(tokens, "id", id) ||
            !COOLProtocol::getTokenString(tokens, "text", text))
            return std::string();

        int i = getQueue().size() - 1;
        while (i >= 0)
        {
            auto& it = getQueue()[i];

            const std::string queuedMessage(it.data(), it.size());
            StringVector queuedTokens = Util::tokenize(it.data(), it.size());

            // If any messages of these types are present before the current ("textinput") message,
            // no combination is possible.
            if (queuedTokens.size() == 1 ||
                (queuedTokens.equals(0, tokens, 0) &&
                 (queuedTokens.equals(1, "key") ||
                  queuedTokens.equals(1, "mouse") ||
                  queuedTokens.equals(1, "removetextcontext") ||
                  queuedTokens.equals(1, "windowkey"))))
                return std::string();

            std::string queuedId;
            std::string queuedText;
            if (queuedTokens.equals(0, tokens, 0) &&
                queuedTokens.equals(1, "textinput") &&
                COOLProtocol::getTokenString(queuedTokens, "id", queuedId) &&
                queuedId == id &&
                COOLProtocol::getTokenString(queuedTokens, "text", queuedText))
            {
                // Remove the queued textinput message and combine it with the current one
                getQueue().erase(getQueue().begin() + i);

                std::string newMsg = queuedTokens[0] + " textinput id=" + id + " text=" + queuedText + text;

                LOG_TRC("Combined [" << queuedMessage << "] with current message to [" << newMsg << "]");

                return newMsg;
            }

            --i;
        }

        return std::string();
    }

    /// Search the queue for a previous removetextcontext message (which actually means "remove text
    /// content", the word "context" is becaue of some misunderstanding lost in history) and if
    /// found, remove it and combine its input with that in the current removetextcontext message.
    /// We check that there aren't any interesting messages inbetween that would make it wrong to
    /// merge the removetextcontext messages.
    ///
    /// @return New message to put into the queue. If empty, use what we got.
    std::string combineRemoveText(const StringVector& tokens)
    {
        std::string id;
        int before = 0;
        int after = 0;
        if (!COOLProtocol::getTokenString(tokens, "id", id) ||
            !COOLProtocol::getTokenInteger(tokens, "before", before) ||
            !COOLProtocol::getTokenInteger(tokens, "after", after))
            return std::string();

        int i = getQueue().size() - 1;
        while (i >= 0)
        {
            auto& it = getQueue()[i];

            const std::string queuedMessage(it.data(), it.size());
            StringVector queuedTokens = Util::tokenize(it.data(), it.size());

            // If any messages of these types are present before the current (removetextcontext)
            // message, no combination is possible.
            if (queuedTokens.size() == 1 ||
                (queuedTokens.equals(0, tokens, 0) &&
                 (queuedTokens.equals(1, "key") ||
                  queuedTokens.equals(1, "mouse") ||
                  queuedTokens.equals(1, "textinput") ||
                  queuedTokens.equals(1, "windowkey"))))
                return std::string();

            std::string queuedId;
            int queuedBefore = 0;
            int queuedAfter = 0;
            if (queuedTokens.equals(0, tokens, 0) &&
                queuedTokens.equals(1, "removetextcontext") &&
                COOLProtocol::getTokenStringFromMessage(queuedMessage, "id", queuedId) &&
                queuedId == id &&
                COOLProtocol::getTokenIntegerFromMessage(queuedMessage, "before", queuedBefore) &&
                COOLProtocol::getTokenIntegerFromMessage(queuedMessage, "after", queuedAfter))
            {
                // Remove the queued removetextcontext message and combine it with the current one
                getQueue().erase(getQueue().begin() + i);

                std::string newMsg = queuedTokens[0] + " removetextcontext id=" + id +
                    " before=" + std::to_string(queuedBefore + before) +
                    " after=" + std::to_string(queuedAfter + after);

                LOG_TRC("Combined [" << queuedMessage << "] with current message to [" << newMsg << "]");

                return newMsg;
            }

            --i;
        }

        return std::string();
    }

private:
    std::vector<Payload> _queue;
};

/// MessageQueue specialized for priority handling of tiles.
class TileQueue : public MessageQueue
{
    friend class TileQueueTests;

private:
    class CursorPosition
    {
    public:
        CursorPosition() {}
        CursorPosition(int part, int x, int y, int width, int height)
            : _part(part)
            , _x(x)
            , _y(y)
            , _width(width)
            , _height(height)
        {
        }

        int getPart() const { return _part; }
        int getX() const { return _x; }
        int getY() const { return _y; }
        int getWidth() const { return _width; }
        int getHeight() const { return _height; }

    private:
        int _part = 0;
        int _x = 0;
        int _y = 0;
        int _width = 0;
        int _height = 0;
    };

public:
    void updateCursorPosition(int viewId, int part, int x, int y, int width, int height)
    {
        const TileQueue::CursorPosition cursorPosition = CursorPosition(part, x, y, width, height);

        auto it = _cursorPositions.lower_bound(viewId);
        if (it != _cursorPositions.end() && it->first == viewId)
        {
            it->second = cursorPosition;
        }
        else
        {
            _cursorPositions.insert(it, std::make_pair(viewId, cursorPosition));
        }

        // Move to front, so the current front view
        // becomes the second.
        const auto view = std::find(_viewOrder.begin(), _viewOrder.end(), viewId);
        if (view != _viewOrder.end())
        {
            _viewOrder.erase(view);
        }

        _viewOrder.push_back(viewId);
    }

    void removeCursorPosition(int viewId)
    {
        const auto view = std::find(_viewOrder.begin(), _viewOrder.end(), viewId);
        if (view != _viewOrder.end())
        {
            _viewOrder.erase(view);
        }

        _cursorPositions.erase(viewId);
    }

    void dumpState(std::ostream& oss);

protected:
    virtual void put_impl(const Payload& value) override;

    virtual Payload get_impl() override;

private:
    /// Search the queue for a duplicate tile and remove it (if present).
    void removeTileDuplicate(const std::string& tileMsg);

    /// Search the queue for a duplicate callback and remove it (if present).
    ///
    /// This removes also callbacks that are made invalid by the current
    /// message, like the new cursor position invalidates the old one etc.
    ///
    /// @return New message to put into the queue.  If empty, use what was in callbackMsg.
    std::string removeCallbackDuplicate(const std::string& callbackMsg);

    /// De-prioritize the previews (tiles with 'id') - move them to the end of
    /// the queue.
    void deprioritizePreviews();

    /// Priority of the given tile message.
    /// -1 means the lowest prio (the tile does not intersect any of the cursors),
    /// the higher the number, the bigger is priority [up to _viewOrder.size()-1].
    int priority(const std::string& tileMsg);

private:
    std::map<int, CursorPosition> _cursorPositions;

    /// Check the views in the order of how the editing (cursor movement) has
    /// been happening (0 == oldest, size() - 1 == newest).
    std::vector<int> _viewOrder;
};

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */