summaryrefslogtreecommitdiffstats
path: root/common/messagequeue.h
blob: 8ea8d8b9845c1d0da63112f77aaa11e1e817b6d5 (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
#pragma once

#include <QObject>
#include <string>
#include <functional>
#include <QString>
#include <Async/Async>
#include "storage.h"

/**
 * A persistent FIFO message queue.
 */
class MessageQueue : public QObject
{
    Q_OBJECT
public:
    class Error
    {
    public:
        Error(const QByteArray &s, int c, const QByteArray &m)
            : store(s), message(m), code(c) {}
        QByteArray store;
        QByteArray message;
        int code;
    };

    MessageQueue(const QString &storageRoot, const QString &name);

    void enqueue(void const *msg, size_t size);
    void enqueue(const QByteArray &value);
    //Dequeue a message. This will return a new message everytime called.
    //Call the result handler with a success response to remove the message from the store.
    //TODO track processing progress to avoid processing the same message with the same preprocessor twice?
    void dequeue(const std::function<void(void *ptr, int size, std::function<void(bool success)>)> & resultHandler,
              const std::function<void(const Error &error)> &errorHandler);
    KAsync::Job<void> dequeueBatch(int maxBatchSize, const std::function<KAsync::Job<void>(const QByteArray &)> &resultHandler);
    bool isEmpty();
signals:
    void messageReady();
    void drained();

private:
    Q_DISABLE_COPY(MessageQueue);
    Akonadi2::Storage mStorage;
};