Subversion Repositories Projects

Rev

Rev 715 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/***************************************************************************
 *   Copyright (C) 2009 by Manuel Schrape                                  *
 *   manuel.schrape@gmx.de                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License.        *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "Input_TCP.h"

void Input_TCP::Init()
{
    b_Open = false;
    Timer = new QTimer();
    s_Buffer = "";
    connect(Timer, SIGNAL(timeout()), this, SLOT(slot_Timer()));

    for (int z = 0; z < MAX_Confirm; z++)
    {
        Confirm[z].ID = false;
    }
}

bool Input_TCP::Open(set_Input s_Input)
{
    TCP_Socket = new(QTcpSocket);
    TCP_Socket->connectToHost (s_Input.Main, s_Input.Sub.toInt());

    connect(TCP_Socket, SIGNAL(connected()), this, SLOT(slot_TCP_Connected()) );
    connect(TCP_Socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_TCP_Error(QAbstractSocket::SocketError)));

    return true;
}

bool Input_TCP::IsOpen()
{
    return b_Open;
}

bool Input_TCP::Close()
{
    TCP_Socket->disconnectFromHost();
    disconnect(TCP_Socket, SIGNAL(connected()), 0, 0);
    disconnect(TCP_Socket, SIGNAL(readyRead()), 0, 0);
    disconnect(TCP_Socket, SIGNAL(disconnected()), 0, 0);

    b_Open = false;
    Timer->stop();

    return true;
}

void Input_TCP::send_Data(QString t_Data, int ID)
{
    if ((ID > 0) && (ID < MAX_Confirm))
    {
        Timer->start(2500);
        Confirm[ID].ID = true;
        Confirm[ID].Data = t_Data;
    }

    if (b_Open)
    {
        usleep(50000);

        if (t_Data[t_Data.length() - 1] != '\r')
        {
            t_Data = t_Data + "\r";
        }

        QByteArray Temp;
        Temp = QByteArray(QString(t_Data + "\n").toAscii());

        TCP_Socket->write(Temp);
        TCP_Socket->flush();

//        qDebug(t_Data.toLatin1().data());
    }
}

void Input_TCP::send_RawData(char *t_Data)
{
    t_Data = t_Data;
}

void Input_TCP::stop_Resend(int ID)
{
    if ((ID > 0) && (ID < MAX_Confirm))
    {
        Confirm[ID].ID = false;
        Confirm[ID].Data = "";
    }
}

void Input_TCP::slot_Timer()
{
    bool Active = false;

    for (int x = 0; x < MAX_Confirm; x++)
    {
        if (Confirm[x].ID)
        {
            Active = true;
            send_Data(Confirm[x].Data);
        }
    }

    if (Active == false)
    {
        Timer->stop();
    }
}

void Input_TCP::slot_TCP_Connected()
{
    b_Open = true;
    connect(TCP_Socket, SIGNAL(readyRead()), SLOT(slot_TCP_ReadLine()));
    connect(TCP_Socket, SIGNAL(disconnected()),TCP_Socket, SLOT(deleteLater()));
    connect(TCP_Socket, SIGNAL(disconnected()),this, SLOT(slot_TCP_Disconnect()));

    emit sig_Connected();
}

void Input_TCP::slot_TCP_Disconnect()
{
    disconnect(TCP_Socket, SIGNAL(disconnected()), 0, 0);
    disconnect(TCP_Socket, SIGNAL(readyRead()), 0, 0);
//    emit sig_Disconnected(1);
}

void Input_TCP::slot_TCP_ReadLine()
{
//    QString t_Data = QString(TCP_Socket->readLine(TCP_Socket->bytesAvailable())).remove(QChar('\n'));
    QString t_Data = s_Buffer + QString(TCP_Socket->readAll()).remove(QChar('\n'));

    s_Buffer = "";

    t_Data = t_Data.replace('\r', "\r\n");

    QStringList l_Data;
    l_Data = t_Data.split('\n');

    for (int z = 0; z < l_Data.count(); z++)
    {

        if ((l_Data[z][l_Data[z].length() - 1] == '\r'))
        {
            emit(sig_NewData(l_Data[z]));
//                qDebug(QString("DATA: _" + l_Data[z] + "_ < END").toLatin1().data());
        }
        else
        {
            s_Buffer = s_Buffer + l_Data[z];
        }
    }
}

void Input_TCP::slot_TCP_Error(QAbstractSocket::SocketError Error)
{
    b_Open = false;

    disconnect(TCP_Socket, SIGNAL(disconnected()), 0, 0);
    disconnect(TCP_Socket, SIGNAL(error(QAbstractSocket::SocketError)), 0, 0);
    disconnect(TCP_Socket, SIGNAL(connected()), 0, 0);
    disconnect(TCP_Socket, SIGNAL(readyRead()), 0, 0);

//    qDebug("Error");
    switch (Error)
    {
        case QAbstractSocket::ConnectionRefusedError:
            emit sig_Disconnected(REFUSED);
        break;
        case QAbstractSocket::RemoteHostClosedError:
            emit sig_Disconnected(REMOTECLOSED);
        break;
        default:
            emit sig_Disconnected(CLOSED);
        break;
    }
//    emit sig_Disconnected(CLOSED);
}

eMode Input_TCP::Mode()
{
    return TCP;
}