SoundBand/sync/node.h

106 lines
2.5 KiB
C
Raw Normal View History

2017-11-06 01:33:16 +03:00
#ifndef NODE_H
#define NODE_H
#include <QTcpServer>
2017-11-18 01:29:14 +03:00
#include "ETcpSocket.h"
2017-11-27 02:01:47 +03:00
#include "song.h"
2017-11-27 19:43:11 +03:00
#include "config.h"
2017-11-27 02:01:47 +03:00
2017-11-06 01:33:16 +03:00
namespace syncLib {
2017-11-24 21:17:41 +03:00
typedef unsigned char Type;
2017-11-20 00:37:12 +03:00
/**
* @brief The TypePackage enum
2017-11-26 19:19:43 +03:00
* t_void = this package empty and not valid.
* t_play = play curent audio file.
* t_song_h = the header of playing audio file.
* t_song = the package with this type is necessary for translite media data on network.
* t_sync = the infomation about sync playning media file on network.
* t_close = the information about close channel.
* t_stop = the package with type 'stop' necessary for stoping playning media files.
* t_what = request for information about the node
* t_brodcaster = information about the node
*/
2017-11-20 00:37:12 +03:00
enum TypePackage{
2017-11-22 20:05:53 +03:00
t_void = 0x00,
t_play = 0x01,
2017-11-24 21:17:41 +03:00
t_song_h = 0x02,
t_song = 0x04,
t_sync = 0x08,
2017-11-22 20:05:53 +03:00
t_close = 0x10,
t_stop = 0x20,
t_what = 0x40,
t_brodcaster = 0x80
2017-11-20 00:37:12 +03:00
};
2017-11-11 14:03:14 +03:00
/**
* @brief The package class. Package for translite media data on network
*
* parse map:
* 1 byle - type
* data
*/
2017-11-06 01:33:16 +03:00
class package
{
2017-11-20 00:37:12 +03:00
2017-11-06 01:33:16 +03:00
private:
2017-11-24 21:17:41 +03:00
Type type;
2017-11-06 01:33:16 +03:00
Song source;
2017-11-20 00:37:12 +03:00
SongHeader header;
2017-11-11 14:03:14 +03:00
Syncer playdata;
2017-11-06 01:33:16 +03:00
public:
package();
2017-11-27 02:01:47 +03:00
package(QByteArray &array);
2017-11-06 01:33:16 +03:00
~package();
2017-11-24 21:17:41 +03:00
/**
* @brief getHeader
* @return Header of the song
*/
const SongHeader& getHeader() const;
2017-11-11 14:03:14 +03:00
/**
* @brief getSong
* @return Song
*/
2017-11-06 01:33:16 +03:00
const Song& getSong() const;
2017-11-11 14:03:14 +03:00
/**
* @brief getPlayTime
* @return time of playning media data
*/
2017-11-24 21:17:41 +03:00
const Syncer &getPlayData() const;
const Type& getType() const;
2017-11-06 01:33:16 +03:00
bool isValid() const;
2017-11-20 00:37:12 +03:00
void clear();
2017-11-13 23:05:59 +03:00
QByteArray parseTo();
2017-11-27 02:01:47 +03:00
bool parseFrom(QByteArray& array);
2017-11-20 00:37:12 +03:00
friend class Sync;
2017-11-06 01:33:16 +03:00
};
class Node:public QTcpServer{
Q_OBJECT
protected:
2017-11-18 01:29:14 +03:00
QList<ETcpSocket*> clients;
2017-11-06 01:33:16 +03:00
private slots:
2017-11-18 01:29:14 +03:00
void acceptError_(ETcpSocket*);
2017-11-06 01:33:16 +03:00
void newConnection_();
2017-11-18 01:29:14 +03:00
void readData(ETcpSocket*_client);
2017-11-06 01:33:16 +03:00
public:
Node(const QString &addres = DEFAULT_ADRESS, int port = DEFAULT_PORT);
2017-11-06 01:33:16 +03:00
void WriteAll(const QByteArray&);
2017-11-18 01:29:14 +03:00
void disconnectClient(ETcpSocket*);
QList<ETcpSocket*>* getClients();
2017-11-27 19:43:11 +03:00
bool addNode(const QString &node, int port);
2017-11-18 01:29:14 +03:00
bool addNode(ETcpSocket* node);
2017-11-11 20:35:30 +03:00
~Node();
2017-11-06 01:33:16 +03:00
signals:
void Error(QString);
2017-11-22 20:05:53 +03:00
void Message(ETcpSocket*);
2017-11-18 01:29:14 +03:00
void ClientDisconnected(ETcpSocket*);
void ClientConnected(ETcpSocket*);
2017-11-06 01:33:16 +03:00
};
}
#endif // NODE_H