summaryrefslogtreecommitdiffstats
path: root/src/net/mcast/McastReceiver.cpp
blob: 60702087d7d4344071ee4630caaf18a5fdfa61b5 (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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
# 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/mcast/McastReceiver.h
#    - implement the receiver-side multicast file transfer protocol -- implementation
# -----------------------------------------------------------------------------
*/

#include <QDataStream>
#include <QtDebug>
#include <QtGlobal>

#include <pgm/pgm.h>
// OpenPGM #defines bool. This is bad in C++.
#undef bool

#include "McastConstants.h"
#include "McastReceiver.h"

McastReceiver::McastReceiver(QIODevice* iodev, McastConfiguration* config, QObject* parent) :
    QObject(parent),
    _config(config ? new McastConfiguration(*config) : new McastConfiguration()),
    _socket(0),
    _curoffs(0),
    _closed(false),
    _hash(QCryptographicHash::Md5),
    _iodev(iodev)
{
    _config->setParent(this);
}

McastReceiver::~McastReceiver()
{
    if (_config)
        delete _config;
}

void McastReceiver::start()
{
    _socket = new McastPGMSocket(this);
    connect(_socket, SIGNAL(receivedPacket(QByteArray)), this, SLOT(receivedPacket(QByteArray)));
    connect(_socket, SIGNAL(connectionReset()), this, SLOT(connectionReset()));
    // connect(_socket, SIGNAL(connectionFinished()), this, SLOT(connectionFinished()));
    _socket->open(_config, McastPGMSocket::PSOCK_READ);
}

void McastReceiver::receivedPacket(QByteArray const& bytes)
{
    if(_closed)
        return;

    quint16 checksum_should = qChecksum(bytes.constData(), bytes.size() - 2);

    QDataStream strm(bytes);
    strm.setByteOrder(QDataStream::BigEndian);

    // read the packet
    quint64 magic;
    quint64 offset;
    quint16 checksum;


    strm >> magic;
    if(magic != MCASTFT_MAGIC)
    {
        qCritical() << "Received packet whose magic number does not match. Ignoring.";
        return;
    }

    strm >> offset;
    qDebug() << " Received packet for offset" << offset;

    if (offset == UINT64_C(0xffffffffffffffff))
    {
        // this is the end of the data stream.
        QByteArray md5;
        strm >> md5;

        quint16 fchecksum;
        strm >> fchecksum;

        // compare the hash value
        if ((fchecksum != checksum_should) || (md5 != _hash.result()))
        {
            _close(RES_MD5_MISMATCH);
        }
        else
        {
            _close(RES_OK);
        }

        return;
    }
    else if (offset != _curoffs)
    {
        qCritical() << "Packet loss or double delivery. PGM should have prevented this. Bailing out.";
        _close(RES_OFFSET_MISMATCH);
        return;
    }

    QByteArray contents;
    strm >> contents;
    _curoffs += contents.size();

    strm >> checksum;
    if(checksum != checksum_should)
    {
        qCritical() << "Checksum does not match. Bailing out.";
        _close(RES_CHECKSUM_MISMATCH);
        return;
    }

    _hash.addData(contents);

    _iodev->write(contents);

    emit progress(_curoffs);
}

void McastReceiver::connectionReset()
{
    _close(RES_CONNECTION_RESET);
}

void McastReceiver::_close(Result result)
{
	_iodev->close();
	_socket->finish();

    _closed = true;
    emit finished(result);
}