2017-11-06 01:33:16 +03:00
|
|
|
#include "node.h"
|
2017-11-18 01:29:14 +03:00
|
|
|
#include "exaptions.h"
|
2017-11-28 20:32:54 +03:00
|
|
|
#include "LocalScanner.h"
|
2017-11-27 02:01:47 +03:00
|
|
|
|
2017-11-13 23:05:59 +03:00
|
|
|
namespace syncLib{
|
2017-11-09 23:09:59 +03:00
|
|
|
|
2017-11-27 02:01:47 +03:00
|
|
|
package::package()
|
|
|
|
{
|
2017-11-20 00:37:12 +03:00
|
|
|
clear();
|
2017-11-06 01:33:16 +03:00
|
|
|
}
|
2017-11-27 02:01:47 +03:00
|
|
|
|
|
|
|
package::package( QByteArray &array):
|
2017-11-13 23:05:59 +03:00
|
|
|
package::package(){
|
2017-11-06 01:33:16 +03:00
|
|
|
parseFrom(array);
|
|
|
|
}
|
2017-11-24 21:17:41 +03:00
|
|
|
|
|
|
|
const SongHeader& package::getHeader() const{
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
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-24 21:17:41 +03:00
|
|
|
const Syncer& package::getPlayData() const{
|
2017-11-11 14:03:14 +03:00
|
|
|
return playdata;
|
2017-11-06 01:33:16 +03:00
|
|
|
}
|
|
|
|
|
2017-11-24 21:17:41 +03:00
|
|
|
const Type& 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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-02 13:22:20 +03:00
|
|
|
if(type & TypePackage::t_sync && type & t_brodcaster){
|
2017-11-21 16:44:55 +03:00
|
|
|
ret = ret && (playdata.run > 0 && playdata.seek > 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-02 13:22:20 +03:00
|
|
|
if(type & TypePackage::t_song_h && type & t_brodcaster){
|
2017-11-21 16:44:55 +03:00
|
|
|
ret = ret && header.size > 0;
|
|
|
|
|
2017-11-06 01:33:16 +03:00
|
|
|
}
|
2017-11-21 16:44:55 +03:00
|
|
|
|
2017-12-02 13:22:20 +03:00
|
|
|
if(type & TypePackage::t_song && type & t_brodcaster){
|
2017-11-21 16:44:55 +03:00
|
|
|
ret = ret && source.size > 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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-27 02:01:47 +03:00
|
|
|
QDataStream stream(&temp, QIODevice::WriteOnly);
|
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);
|
|
|
|
|
2017-12-02 13:22:20 +03:00
|
|
|
if(type & TypePackage::t_sync && type & t_brodcaster){
|
2017-11-11 14:03:14 +03:00
|
|
|
stream << playdata.run;
|
|
|
|
stream << playdata.seek;
|
2017-11-21 16:44:55 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-02 13:22:20 +03:00
|
|
|
if(type & TypePackage::t_song_h && type & t_brodcaster){
|
2017-11-20 00:37:12 +03:00
|
|
|
stream << header;
|
2017-11-21 16:44:55 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-02 13:22:20 +03:00
|
|
|
if(type & TypePackage::t_song && type & t_brodcaster){
|
2017-11-21 16:44:55 +03:00
|
|
|
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-27 02:01:47 +03:00
|
|
|
bool package::parseFrom(QByteArray &array){
|
2017-11-20 00:37:12 +03:00
|
|
|
type = TypePackage::t_void;
|
2017-11-27 02:01:47 +03:00
|
|
|
QDataStream stream(&array, QIODevice::ReadOnly);
|
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-27 02:01:47 +03:00
|
|
|
package::~package(){}
|
|
|
|
|
2017-11-28 20:32:54 +03:00
|
|
|
Node::Node(const QString &addres, int port):QTcpServer(){
|
|
|
|
QString address = addres;
|
|
|
|
if(address == DEFAULT_ADRESS){
|
|
|
|
address = LocalScanner::thisAdress().toString();
|
|
|
|
}
|
|
|
|
if(!listen(QHostAddress(address), port)){
|
2017-11-27 22:59:59 +03:00
|
|
|
#ifdef QT_DEBUG
|
|
|
|
qDebug() << errorString();
|
|
|
|
#endif
|
|
|
|
throw initNodeError();
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
#ifdef QT_DEBUG
|
|
|
|
qDebug() << "node started on:" << serverAddress().toString() << "port:" << serverPort();
|
|
|
|
#endif
|
2017-11-06 01:33:16 +03:00
|
|
|
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);
|
2017-12-02 13:22:20 +03:00
|
|
|
#ifdef QT_DEBUG
|
|
|
|
qDebug() << "node diskonected error:" <<c->getSource()->errorString() << " node:" << c->name();
|
|
|
|
#endif
|
2017-11-06 01:33:16 +03:00
|
|
|
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){
|
2017-11-27 22:59:59 +03:00
|
|
|
i->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);
|
2017-11-24 21:17:41 +03:00
|
|
|
}catch(AddNodeExaption &e){
|
2017-11-18 01:29:14 +03:00
|
|
|
#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-12-02 13:22:20 +03:00
|
|
|
connect(node,SIGNAL(Disconnected(ETcpSocket*)),this,SLOT(acceptError_(ETcpSocket*)));
|
|
|
|
connect(node,SIGNAL(Message(ETcpSocket*)),this,SLOT(readData(ETcpSocket*)));
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|