NetworkStream.cpp 3.37 KB
Newer Older
Thiago C. Santini's avatar
Thiago C. Santini committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#include "NetworkStream.h"

NetworkStream::NetworkStream()
{
    connected = false;
}

NetworkStream::~NetworkStream()
{
}

void NetworkStream::onThreadStarted()
{
    if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) {
        tcpServer = new QTcpServer();
        if (! listen() )
            this->startTimer(5000);
    } else if (settings.value("dataStream/transportProtocol").toString().compare("UDP", Qt::CaseInsensitive) == 0) {
        socket = new QUdpSocket(this);
        socket->bind(QHostAddress(settings.value("dataStream/ip").toString()), settings.value("dataStream/port").toInt());
        socket->connectToHost(QHostAddress(settings.value("dataStream/ip").toString()), settings.value("dataStream/port").toInt());
    }
}

void NetworkStream::onThreadFinished()
{
    if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) {
        tcpServer->close();
        tcpServer->deleteLater();
        if (connected)
            socket->deleteLater();
    } else if (settings.value("dataStream/transportProtocol").toString().compare("UDP", Qt::CaseInsensitive) == 0) {
        delete socket; socket;
    }
}

void NetworkStream::openSocket()
{
    if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) {
        if (connected) {
            tcpServer->nextPendingConnection()->close();
            return;
        }
        socket = tcpServer->nextPendingConnection();
        socket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
        connect(socket, SIGNAL(disconnected()), this, SLOT(closeSocket()));
        connected = true;
    }
}

void NetworkStream::closeSocket()
{
    if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) {
        if (!connected)
            return;
        connected = false;
        socket->close();
        socket->deleteLater();
    }
}

void NetworkStream::push(JournalEntry journalEntry)
{
    if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) {
        if (connected)
            socket->write(journalEntry.toQString().prepend("J").toStdString().c_str());
    } else if (settings.value("dataStream/transportProtocol").toString().compare("UDP", Qt::CaseInsensitive) == 0) {
        socket->write(journalEntry.toQString().prepend("J").toStdString().c_str());
    }
}

void NetworkStream::timerEvent(QTimerEvent *event)
{
    if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) {
        if (listen())
            this->killTimer(event->timerId());
    }
}

bool NetworkStream::listen()
{
    if (settings.value("dataStream/transportProtocol").toString().compare("TCP", Qt::CaseInsensitive) == 0) {
        if (! tcpServer->isListening()) {
            tcpServer->listen(QHostAddress(settings.value("dataStream/ip").toString()), settings.value("dataStream/port").toInt());
            if (tcpServer->isListening()) {
                qDebug() << "Listening on " << tcpServer->serverAddress() << " port " << tcpServer->serverPort();
                connect(tcpServer, SIGNAL(newConnection()), this, SLOT(openSocket()));
                return true;
            }
            else {
                qDebug() << "Not listening";
                return false;
            }
        }
    }
}