SoundBand/sync/node.cpp

206 lines
3.7 KiB
C++
Raw Normal View History

2017-11-06 01:33:16 +03:00
#include "node.h"
#include <QTcpSocket>
#include "song.h"
2017-11-06 13:20:52 +03:00
#include <QDataStream>
2017-11-18 01:29:14 +03:00
#include "exaptions.h"
2017-11-13 23:05:59 +03:00
namespace syncLib{
2017-11-09 23:09:59 +03:00
2017-11-13 23:05:59 +03:00
package::package(){
2017-11-20 00:37:12 +03:00
clear();
2017-11-06 01:33:16 +03:00
}
2017-11-13 23:05:59 +03:00
package::package(const QByteArray &array):
package::package(){
2017-11-06 01:33:16 +03:00
parseFrom(array);
}
2017-11-13 23:05:59 +03:00
const Song& package::getSong() const{
2017-11-06 01:33:16 +03:00
return source;
}
2017-11-13 23:05:59 +03:00
Syncer package::getPlayData() const{
2017-11-11 14:03:14 +03:00
return playdata;
2017-11-06 01:33:16 +03:00
}
2017-11-20 00:37:12 +03:00
TypePackage package::getType() const{
2017-11-06 01:33:16 +03:00
return type;
}
2017-11-13 23:05:59 +03:00
bool package::isValid() const{
2017-11-21 16:44:55 +03:00
bool ret = true;
if(type == TypePackage::t_void){
2017-11-06 01:33:16 +03:00
return false;
2017-11-21 16:44:55 +03:00
}
if(type & TypePackage::t_play){
ret = ret && true;
}
if(type & TypePackage::t_sync){
ret = ret && (playdata.run > 0 && playdata.seek > 0);
}
if(type & TypePackage::t_song_h){
ret = ret && header.size > 0;
2017-11-06 01:33:16 +03:00
}
2017-11-21 16:44:55 +03:00
if(type & TypePackage::t_song){
ret = ret && source.size > 0;
}
if(type & TypePackage::t_close){
ret = ret && true;
}
if(type & TypePackage::t_stop){
ret = ret && true;
}
return ret;
2017-11-06 01:33:16 +03:00
}
2017-11-20 00:37:12 +03:00
void package::clear(){
type = TypePackage::t_void;
source.clear();
playdata.run = 0;
playdata.seek = 0;
}
2017-11-13 23:05:59 +03:00
QByteArray package::parseTo(){
2017-11-06 01:33:16 +03:00
QByteArray temp;
2017-11-13 23:05:59 +03:00
QDataStream stream(temp);
2017-11-06 01:33:16 +03:00
temp.clear();
if(isValid()){
2017-11-21 16:44:55 +03:00
stream << static_cast<unsigned char>(type);
if(type & TypePackage::t_sync){
2017-11-11 14:03:14 +03:00
stream << playdata.run;
stream << playdata.seek;
2017-11-21 16:44:55 +03:00
}
if(type & TypePackage::t_song_h){
2017-11-20 00:37:12 +03:00
stream << header;
2017-11-21 16:44:55 +03:00
}
if(type & TypePackage::t_song){
stream << source;
2017-11-06 01:33:16 +03:00
}
2017-11-21 16:44:55 +03:00
2017-11-06 01:33:16 +03:00
}
2017-11-13 23:05:59 +03:00
return temp;
2017-11-06 01:33:16 +03:00
}
2017-11-13 23:05:59 +03:00
bool package::parseFrom(const QByteArray &array){
2017-11-20 00:37:12 +03:00
type = TypePackage::t_void;
2017-11-13 23:05:59 +03:00
QDataStream stream(array);
2017-11-21 16:44:55 +03:00
unsigned char temp_type;
stream >> temp_type;
type = static_cast<TypePackage> (temp_type);
if(type & TypePackage::t_sync){
2017-11-11 14:03:14 +03:00
stream >> playdata.run;
stream >> playdata.seek;
2017-11-21 16:44:55 +03:00
}
if(type & TypePackage::t_song_h){
2017-11-20 00:37:12 +03:00
stream >> header;
2017-11-21 16:44:55 +03:00
2017-11-06 01:33:16 +03:00
}
2017-11-21 16:44:55 +03:00
if(type & TypePackage::t_song){
stream >> source;
}
return isValid();
2017-11-06 01:33:16 +03:00
}
2017-11-13 23:05:59 +03:00
Node::Node():QTcpServer(){
2017-11-06 01:33:16 +03:00
connect(this,SIGNAL(acceptError(QAbstractSocket::SocketError)),SLOT(acceptError_(QAbstractSocket::SocketError)));
connect(this,SIGNAL(newConnection()),SLOT(newConnection_()));
}
2017-11-22 20:05:53 +03:00
2017-11-18 01:29:14 +03:00
void Node::acceptError_(ETcpSocket*c){
c->getSource()->close();
2017-11-06 01:33:16 +03:00
clients.removeOne(c);
emit ClientDisconnected(c);
delete c;
}
2017-11-22 20:05:53 +03:00
2017-11-18 01:29:14 +03:00
QList<ETcpSocket*>* Node::getClients(){
2017-11-06 01:33:16 +03:00
return &clients;
}
2017-11-22 20:05:53 +03:00
2017-11-13 23:05:59 +03:00
void Node::newConnection_(){
2017-11-18 01:29:14 +03:00
ETcpSocket *newClient=new ETcpSocket(nextPendingConnection());
2017-11-06 01:33:16 +03:00
clients.push_back(newClient);
2017-11-18 01:29:14 +03:00
connect(newClient,SIGNAL(Disconnected(ETcpSocket*)),this,SLOT(acceptError_(ETcpSocket*)));
connect(newClient,SIGNAL(Message(ETcpSocket*)),this,SLOT(readData(ETcpSocket*)));
2017-11-06 01:33:16 +03:00
emit ClientConnected(newClient);
}
2017-11-22 20:05:53 +03:00
2017-11-18 01:29:14 +03:00
void Node::readData(ETcpSocket *c){
2017-11-22 20:05:53 +03:00
emit Message(c);
2017-11-06 01:33:16 +03:00
}
2017-11-22 20:05:53 +03:00
2017-11-13 23:05:59 +03:00
void Node::WriteAll(const QByteArray &data){
2017-11-18 01:29:14 +03:00
for(ETcpSocket*i:clients){
i->getSource()->write(data);
2017-11-06 01:33:16 +03:00
}
}
2017-11-22 20:05:53 +03:00
2017-11-18 01:29:14 +03:00
void Node::disconnectClient(ETcpSocket *c){
c->getSource()->close();
2017-11-06 01:33:16 +03:00
clients.removeOne(c);
delete c;
}
2017-11-13 23:05:59 +03:00
bool Node::addNode(const QString &node,int port){
2017-11-18 01:29:14 +03:00
ETcpSocket *temp;
2017-11-09 23:09:59 +03:00
2017-11-18 01:29:14 +03:00
try{
temp = new ETcpSocket(node,port);
}catch(addNodeExaption &e){
#ifdef QT_DEBUG
qDebug() << e.what();
#endif
return false;
2017-11-09 23:09:59 +03:00
}
2017-11-18 01:29:14 +03:00
clients.push_back(temp);
return true;
2017-11-09 23:09:59 +03:00
}
2017-11-18 01:29:14 +03:00
bool Node::addNode(ETcpSocket *node){
if(node->getSource()->isOpen()){
2017-11-09 23:09:59 +03:00
clients.append(node);
return true;
}
return false;
}
2017-11-13 23:05:59 +03:00
Node::~Node(){
2017-11-18 01:29:14 +03:00
for(ETcpSocket *i:clients){
i->getSource()->close();
2017-11-06 01:33:16 +03:00
delete i;
}
this->close();
}
2017-11-13 23:05:59 +03:00
}