PageRenderTime 101ms CodeModel.GetById 45ms app.highlight 4ms RepoModel.GetById 30ms app.codeStats 0ms

/src/libtomahawk/network/MsgProcessor.h

http://github.com/tomahawk-player/tomahawk
C Header | 76 lines | 35 code | 13 blank | 28 comment | 0 complexity | 35b287017d3c94b9c6470ab43fb0038e MD5 | raw file
 1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 2 *
 3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
 4 *
 5 *   Tomahawk is free software: you can redistribute it and/or modify
 6 *   it under the terms of the GNU General Public License as published by
 7 *   the Free Software Foundation, either version 3 of the License, or
 8 *   (at your option) any later version.
 9 *
10 *   Tomahawk is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/*
20    MsgProcessor is a FIFO queue of msg_ptr, you .add() a msg_ptr, and
21    it emits done(msg_ptr) for each msg, preserving the order.
22
23    It can be configured to auto-compress, or de-compress msgs for sending
24    or receiving.
25
26    It uses QtConcurrent, but preserves msg order.
27
28    NOT threadsafe.
29*/
30#ifndef MSGPROCESSOR_H
31#define MSGPROCESSOR_H
32
33#include "Typedefs.h"
34#include "Msg.h" // Needed because we have msg_ptr in a slot
35
36#include <QObject>
37
38class MsgProcessor : public QObject
39{
40Q_OBJECT
41public:
42    enum Mode
43    {
44        NOTHING = 0,
45        COMPRESS_IF_LARGE = 1,
46        UNCOMPRESS_ALL = 2,
47        PARSE_JSON = 4
48    };
49
50    explicit MsgProcessor( quint32 mode = NOTHING, quint32 t = 512 );
51
52    void setMode( quint32 m ) { m_mode = m ; }
53
54    static msg_ptr process( msg_ptr msg, quint32 mode, quint32 threshold );
55
56    int length() const { return m_msgs.length(); }
57
58signals:
59    void ready( msg_ptr );
60    void empty();
61
62public slots:
63    void append( msg_ptr msg );
64    void processed();
65
66private:
67    void handleProcessedMsg( msg_ptr msg );
68
69    quint32 m_mode;
70    quint32 m_threshold;
71    QList<msg_ptr> m_msgs;
72    QMap< Msg*, bool> m_msg_ready;
73    unsigned int m_totmsgsize;
74};
75
76#endif // MSGPROCESSOR_H