#include "tcpequipment.h"

TcpEquipment::TcpEquipment()
{

}

TcpEquipment::TcpEquipment(QString IP)
{
    this->listenIp=IP;
    this->serverFlag=false;
}

void TcpEquipment::setIP(QString IP)
{
    this->listenIp=IP;
}

void TcpEquipment::startServer()
{
    this->server=new QTcpServer(this);
    if(listenIp.isEmpty())
    {
        this->server->listen(QHostAddress::Any,TCP_SERVER_PORT);
    }
    else
    {
        this->server->listen(QHostAddress(this->listenIp),TCP_SERVER_PORT);
    }
    QObject::connect(this->server,&QTcpServer::newConnection,this,&TcpEquipment::onClientConnected);
    QObject::connect(this->server,&QTcpServer::acceptError,this,&TcpEquipment::onServerAcceptError);
    this->serverFlag=true;
}

void TcpEquipment::startServer(QString IP)
{
    setIP(IP);
    startServer();
}

void TcpEquipment::stopServer()
{
    QList<QString> keyList=this->clientList.keys();
    for(int i=0;i<keyList.count();i++)
    {
        this->clientList[keyList[i]]->close();
    }
    this->clientList.clear();
    this->server->close();
    this->serverFlag=false;
}

void TcpEquipment::send2All(QString strMsg)
{
    if(!this->serverFlag){
        return;
    }
    QList<QString> mKeys=clientList.keys();
    for(int i=0;i<mKeys.count();i++){
        QTcpSocket *temp= (QTcpSocket*)(clientList.value(mKeys[i]));
        temp->write(strMsg.toUtf8());
    }
}

void TcpEquipment::send2All(QByteArray buffer)
{
    if(!this->serverFlag){
        return;
    }
    QList<QString> mKeys=clientList.keys();
    for(int i=0;i<mKeys.count();i++){
        QTcpSocket *temp= (QTcpSocket*)(clientList.value(mKeys[i]));
        temp->write(buffer);
    }
}

void TcpEquipment::send2One(QString strMsg, QString ip)
{
    if(!this->serverFlag){
        return;
    }

    QTcpSocket *temp= (QTcpSocket*)(clientList.value(ip));
    temp->write(strMsg.toUtf8());

}

void TcpEquipment::send2One(QByteArray buffer, QString ip)
{
    if(!this->serverFlag){
        return;
    }
    QTcpSocket *temp= (QTcpSocket*)(clientList.value(ip));
    temp->write(buffer);
}

void TcpEquipment::onServerAcceptError()
{

}

void TcpEquipment::onClientConnected()
{
    QTcpSocket* tcp =this->server->nextPendingConnection();
    QString strIP=tcp->peerAddress().toString();
    int iPort=tcp->peerPort();
    qDebug()<<"Connected!!IP:"+strIP+":"+QString::number(iPort);
    emit stateChanged("Connected!!IP:"+strIP+":"+QString::number(iPort));
    QObject::connect(tcp,&QTcpSocket::readyRead,this,&TcpEquipment::onClientDataReceived);
    QObject::connect(tcp,&QTcpSocket::disconnected,this,&TcpEquipment::onClinetDisconnected);
    this->clientList.insert(strIP,tcp);
}

void TcpEquipment::onClientDataReceived()
{
    QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(sender());
    QString strIP=tcp->peerAddress().toString();
    int iPort=tcp->peerPort();
    QByteArray tempBuffer=tcp->readAll();
    QString strBuffer=QString(tempBuffer);
    qDebug()<<"IP:"+strIP+":"+QString::number(iPort)+"--"+strBuffer;
    dealRev(strIP,iPort,tempBuffer);
    emit dateReceived(strIP,iPort,tempBuffer);
}

void TcpEquipment::onClinetDisconnected()
{
    QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(sender());
    QString strIP=tcp->peerAddress().toString();
    int iPort=tcp->peerPort();
    qDebug()<<"Disconnected!!IP:"+strIP+":"+QString::number(iPort);
    emit stateChanged("Disconnected!!IP:"+strIP+":"+QString::number(iPort));
    QObject::disconnect(tcp,&QTcpSocket::readyRead,this,&TcpEquipment::onClientDataReceived);
    QObject::disconnect(tcp,&QTcpSocket::disconnected,this,&TcpEquipment::onClinetDisconnected);
    //delete this->clientList.value(strIP);
    this->clientList.remove(strIP);
}

void TcpEquipment::dealRev(QString strIP, int iPort, QByteArray buffer)
{
    Q_UNUSED(strIP)
    Q_UNUSED(iPort)
    Q_UNUSED(buffer)
}