NetworkStream.cpp 3.46 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
#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());
    }
}

Thiago C. Santini's avatar
Thiago C. Santini committed
25
void NetworkStream::onThreadFinishing()
Thiago C. Santini's avatar
Thiago C. Santini committed
26 27 28 29 30 31 32
{
    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) {
33
        delete socket;
Thiago C. Santini's avatar
Thiago C. Santini committed
34
    }
Thiago C. Santini's avatar
Thiago C. Santini committed
35
    qDebug() << QThread::currentThread()->objectName() << " done.";
Thiago C. Santini's avatar
Thiago C. Santini committed
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
}

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)
{
65 66 67
    if (!socket)
        return;

Thiago C. Santini's avatar
Thiago C. Santini committed
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 97 98 99 100
    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;
            }
        }
    }
}