summaryrefslogtreecommitdiffstats
path: root/src/net/pvsServiceBroadcast.cpp
blob: a8614567a8993185fc8447bb24a38c7dcf489928 (plain) (blame)
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
/*
# Copyright (c) 2009 - OpenSLX Project, Computer Center University of Freiburg
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
# src/net/pvsServiceBroadcast.cpp
#    - broadcast sessionname
# -----------------------------------------------------------------------------
*/
/********** COMPILE-TIME SETTINGS ***************/

#include "pvsServiceBroadcast.h"
#include "src/setup.h"
#include <cassert>
#include "src/util/serviceDiscoveryUtil.h"

#include <QNetworkInterface>
#include <QNetworkAddressEntry>

PVSServiceBroadcast::PVSServiceBroadcast()
{
    _announce = NULL;
    _timer = 0;
    _broadcaster.bind(SD_PORT_CONSOLE);

    foreach (QNetworkInterface interface, QNetworkInterface::allInterfaces())
        foreach (QNetworkAddressEntry entry, interface.addressEntries())
            if (!entry.broadcast().isNull() && entry.broadcast().toString() != "127.255.255.255")
                _everyone.append(entry.broadcast());

    if (_everyone.isEmpty())
        qDebug("ERROR: No broadcast address found");
}

PVSServiceBroadcast::~PVSServiceBroadcast()
{
    if (_announce != NULL) delete _announce;
    if (_timer != 0) this->killTimer(_timer);
}

void PVSServiceBroadcast::setFingerprint(QByteArray sha1)
{
    if (_announce != NULL) _announce->clear();
    else _announce = new QByteArray();
    appendSdField(_announce, "hsh", QString(sha1.toBase64()));
    appendSdField(_announce, "prt", SERVER_PORT); // TODO: maybe this has to come from somewhere else if configurable
    appendSdField(_announce, "aut", "SHA1");
    if (_timer == 0) _timer = this->startTimer(SB_INTERVAL * 1000);
}

void PVSServiceBroadcast::timerEvent(QTimerEvent *event)
{
    if (_announce == NULL) return;
    foreach (QHostAddress bcast, _everyone)
    {
        qDebug("DEBUG: Broadcasting to %s:%i", qPrintable(bcast.toString()),SD_PORT_CLIENT);
        _broadcaster.writeDatagram(*_announce, bcast, SD_PORT_CLIENT);
    }
}