~mateo-salta/nitroshare/nitroshare

« back to all changes in this revision

Viewing changes to src/widgets/CBroadcastDiscoveryWidget.cpp

  • Committer: Nathan Osman
  • Date: 2012-06-29 22:01:55 UTC
  • Revision ID: admin@quickmediasolutions.com-20120629220155-ogwrn9fxxve9wyw1
Moved a TON of the broadcast and discovery code around, creating separate classes for each and partially implementing their use in the first start wizard.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
   You should have received a copy of the GNU General Public License
15
15
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
16
 
 
17
#include <util/network.h>
17
18
#include <widgets/CBroadcastDiscoveryWidget.h>
18
19
#include "ui_CBroadcastDiscoveryWidget.h"
19
20
 
20
21
CBroadcastDiscoveryWidget::CBroadcastDiscoveryWidget(QWidget * parent)
21
 
    : QWidget(parent), ui(new Ui::CBroadcastDiscoveryWidget)
 
22
    : QWidget(parent), ui(new Ui::CBroadcastDiscoveryWidget), m_listener(NULL)
22
23
{
23
24
    ui->setupUi(this);
24
25
}
25
26
 
26
27
CBroadcastDiscoveryWidget::~CBroadcastDiscoveryWidget()
27
28
{
 
29
    /* Free everything. */
 
30
    StopBroadcasting();
 
31
 
28
32
    delete ui;
29
33
}
 
34
 
 
35
void CBroadcastDiscoveryWidget::StartBroadcasting()
 
36
{
 
37
    ui->DiscoveryLabel->setText(tr("Broadcast discovery in progress..."));
 
38
 
 
39
    /* For each of the usable network interfaces, create a broadcaster
 
40
       that immediately begins listening / broadcasting. */
 
41
    QList<QNetworkInterface> interfaces = Network::FindUsableInterfaces();
 
42
    foreach(QNetworkInterface interface, interfaces)
 
43
    {
 
44
        CBasicBroadcaster * broadcaster = new CBasicBroadcaster;
 
45
        broadcaster->Init(interface);
 
46
 
 
47
        m_broadcasters.append(broadcaster);
 
48
    }
 
49
 
 
50
    /* And we also listen for any incoming pings. */
 
51
    m_listener = new CBasicListener;
 
52
    connect(m_listener, SIGNAL(Ping(QHostAddress,QByteArray)), SLOT(OnPing(QHostAddress)));
 
53
 
 
54
    m_listener->Init();
 
55
}
 
56
 
 
57
void CBroadcastDiscoveryWidget::StopBroadcasting()
 
58
{
 
59
    /* Free all of the broadcasters and the listener. */
 
60
    qDeleteAll(m_broadcasters);
 
61
 
 
62
    if(m_listener)
 
63
    {
 
64
        delete m_listener;
 
65
        m_listener = NULL;
 
66
    }
 
67
}
 
68
 
 
69
void CBroadcastDiscoveryWidget::OnPing(QHostAddress address)
 
70
{
 
71
    /* Loop through each of the network interfaces we are broadcasting
 
72
       on and check to see if this address is in its subnet. */
 
73
    foreach(CBasicBroadcaster * broadcaster, m_broadcasters)
 
74
        if(broadcaster->HasAddress(address))
 
75
        {
 
76
            ui->DiscoveryLabel->setText(tr("Found interface broadcasting to us."));
 
77
        }
 
78
}