SoundBand/Sync/node.cpp

120 lines
2.6 KiB
C++
Raw Normal View History

2017-11-06 01:33:16 +03:00
#include "node.h"
2017-11-18 01:29:14 +03:00
#include "exaptions.h"
#include "LocalScanner.h"
2017-11-27 02:01:47 +03:00
Node::Node(const QString &addres, int port):QTcpServer(){
QString address = addres;
2018-08-16 09:13:18 +03:00
fBroadcaster = false;
if(address == DEFAULT_ADRESS){
2017-12-03 18:19:49 +03:00
address = LocalScanner::thisAddress().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
2018-08-16 09:13:18 +03:00
2017-11-06 01:33:16 +03:00
connect(this,SIGNAL(newConnection()),SLOT(newConnection_()));
2018-08-16 09:13:18 +03:00
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::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
2018-01-11 21:48:01 +03:00
qDebug() << "node diskonected error:" <<c->getSource()->errorString() << " node:" << c->peerName();
2017-12-02 13:22:20 +03:00
#endif
2017-11-06 01:33:16 +03:00
emit ClientDisconnected(c);
delete c;
}
2017-11-22 20:05:53 +03:00
2018-08-16 09:13:18 +03:00
bool Node::isBroadcaster()const{
return fBroadcaster;
}
void Node::setBroadcaster(bool newValue){
fBroadcaster = newValue;
}
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);
2018-08-16 09:13:18 +03:00
connect(newClient, SIGNAL(Disconnected(ETcpSocket*)),
this, SLOT(acceptError_(ETcpSocket*)));
connect(newClient, SIGNAL(Message(ETcpSocket*)), this, SLOT(readData(ETcpSocket*)));
connect(newClient, SIGNAL(synced()), this, SLOT(synced()));
2017-11-06 01:33:16 +03:00
emit ClientConnected(newClient);
}
2017-11-22 20:05:53 +03:00
2018-08-16 09:13:18 +03:00
void Node::synced(){
emit NodeSynced(static_cast<ETcpSocket*>(this->sender()));
}
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);
2018-01-11 21:48:01 +03:00
#ifdef QT_DEBUG
qDebug() << i->peerName();
#endif
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