/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. MsgProcessor is a FIFO queue of msg_ptr, you .add() a msg_ptr, and
  20. it emits done(msg_ptr) for each msg, preserving the order.
  21. It can be configured to auto-compress, or de-compress msgs for sending
  22. or receiving.
  23. It uses QtConcurrent, but preserves msg order.
  24. NOT threadsafe.
  25. */
  26. #ifndef MSGPROCESSOR_H
  27. #define MSGPROCESSOR_H
  28. #include "Typedefs.h"
  29. #include "Msg.h" // Needed because we have msg_ptr in a slot
  30. #include <QObject>
  31. class MsgProcessor : public QObject
  32. {
  33. Q_OBJECT
  34. public:
  35. enum Mode
  36. {
  37. NOTHING = 0,
  38. COMPRESS_IF_LARGE = 1,
  39. UNCOMPRESS_ALL = 2,
  40. PARSE_JSON = 4
  41. };
  42. explicit MsgProcessor( quint32 mode = NOTHING, quint32 t = 512 );
  43. void setMode( quint32 m ) { m_mode = m ; }
  44. static msg_ptr process( msg_ptr msg, quint32 mode, quint32 threshold );
  45. int length() const { return m_msgs.length(); }
  46. signals:
  47. void ready( msg_ptr );
  48. void empty();
  49. public slots:
  50. void append( msg_ptr msg );
  51. void processed();
  52. private:
  53. void handleProcessedMsg( msg_ptr msg );
  54. quint32 m_mode;
  55. quint32 m_threshold;
  56. QList<msg_ptr> m_msgs;
  57. QMap< Msg*, bool> m_msg_ready;
  58. unsigned int m_totmsgsize;
  59. };
  60. #endif // MSGPROCESSOR_H