SoundBand/sync/node.cpp

175 lines
4.0 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-09 23:09:59 +03:00
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-13 23:05:59 +03:00
void Node::acceptError_(QTcpSocket*c){
2017-11-06 01:33:16 +03:00
c->close();
clients.removeOne(c);
emit ClientDisconnected(c);
delete c;
}
2017-11-13 23:05:59 +03:00
QList<QTcpSocket*>* 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-06 01:33:16 +03:00
QTcpSocket *newClient=new QTcpSocket(nextPendingConnection());
clients.push_back(newClient);
connect(newClient,SIGNAL(Disconnected(ETcpSocket*)),this,SLOT(acceptError_(QTcpSocket*)));
connect(newClient,SIGNAL(Message(ETcpSocket*)),this,SLOT(readData(QTcpSocket*)));
emit ClientConnected(newClient);
}
2017-11-13 23:05:59 +03:00
void Node::readData(QTcpSocket *c){
2017-11-06 01:33:16 +03:00
emit Message(c);
}
2017-11-13 23:05:59 +03:00
void Node::WriteAll(const QByteArray &data){
2017-11-06 01:33:16 +03:00
for(QTcpSocket*i:clients){
2017-11-11 14:03:14 +03:00
i->write(data);
2017-11-06 01:33:16 +03:00
}
}
2017-11-13 23:05:59 +03:00
void Node::disconnectClient(QTcpSocket *c){
2017-11-11 14:03:14 +03:00
c->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-09 23:09:59 +03:00
QTcpSocket *temp = new QTcpSocket;
2017-11-13 23:05:59 +03:00
if(temp->bind(QHostAddress(node),port) && temp->open(QIODevice::ReadWrite)){
2017-11-09 23:09:59 +03:00
clients.append(temp);
return true;
}
return false;
}
2017-11-13 23:05:59 +03:00
bool Node::addNode(QTcpSocket *node){
2017-11-09 23:09:59 +03:00
if(node->isOpen()){
clients.append(node);
return true;
}
return false;
}
2017-11-13 23:05:59 +03:00
Node::~Node(){
2017-11-06 01:33:16 +03:00
for(QTcpSocket *i:clients){
2017-11-11 14:03:14 +03:00
i->abort();
2017-11-06 01:33:16 +03:00
delete i;
}
this->close();
}
2017-11-13 23:05:59 +03:00
}