SoundBand/sync/node.cpp

184 lines
4.2 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(){
type = package::t_void;
2017-11-06 01:33:16 +03:00
source.clear();
2017-11-11 14:03:14 +03:00
playdata.run = 0;
playdata.seek = 0;
2017-11-06 13:20:52 +03:00
size = 0;
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-13 23:05:59 +03:00
package::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-06 01:33:16 +03:00
switch (type) {
case package::t_void:
return false;
case package::t_close:
return true;
case package::t_sync:
2017-11-13 23:05:59 +03:00
return playdata.run > 0 && playdata.seek > 0;
2017-11-06 01:33:16 +03:00
case package::t_song:
return source.size > 0;
case package::t_stop:
return true;
default:
return false;
}
}
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()){
switch (type) {
case package::t_void:
break;
case package::t_close:
2017-11-06 13:20:52 +03:00
stream << int();
2017-11-06 01:33:16 +03:00
stream << (unsigned char)(type);
2017-11-06 13:20:52 +03:00
stream.device()->seek(0);
stream << temp.size();
2017-11-06 01:33:16 +03:00
break;
case package::t_sync:
2017-11-06 13:20:52 +03:00
stream << int();
2017-11-06 01:33:16 +03:00
stream << (unsigned char)(type);
2017-11-11 14:03:14 +03:00
stream << playdata.run;
stream << playdata.seek;
2017-11-06 13:20:52 +03:00
stream.device()->seek(0);
stream << temp.size();
2017-11-06 01:33:16 +03:00
break;
case package::t_song:
2017-11-06 13:20:52 +03:00
stream << int();
2017-11-06 01:33:16 +03:00
stream << (unsigned char)(type);
stream << source;
2017-11-06 13:20:52 +03:00
stream.device()->seek(0);
stream << temp.size();
2017-11-06 01:33:16 +03:00
break;
case package::t_stop:
2017-11-06 13:20:52 +03:00
stream << int();
2017-11-06 01:33:16 +03:00
stream << (unsigned char)(type);
2017-11-06 13:20:52 +03:00
stream.device()->seek(0);
stream << temp.size();
2017-11-06 01:33:16 +03:00
break;
default:
break;
}
}
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-06 01:33:16 +03:00
type = t_void;
2017-11-13 23:05:59 +03:00
QDataStream stream(array);
2017-11-06 01:33:16 +03:00
switch (type) {
case package::t_void:
return false;
case package::t_close:
return true;
case package::t_sync:
2017-11-11 14:03:14 +03:00
stream >> playdata.run;
stream >> playdata.seek;
2017-11-06 01:33:16 +03:00
return isValid();
case package::t_song:
stream >> source;
return isValid();
case package::t_stop:
return true;
default:
return isValid();
}
}
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-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-18 01:29:14 +03:00
QList<ETcpSocket*>* Node::getClients(){
2017-11-06 01:33:16 +03:00
return &clients;
}
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-18 01:29:14 +03:00
void Node::readData(ETcpSocket *c){
package _package;
_package.parseFrom(*c->topStack());
emit Message(_package,c);
2017-11-06 01:33:16 +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-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
}