aboutsummaryrefslogtreecommitdiffstats
path: root/src/libdash/source/network/AbstractChunk.h
blob: 794469fdf70d3bc480d292011d86f4b8e86a2a9b (plain)
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
/*
 * AbstractChunk.h
 *****************************************************************************
 * Copyright (C) 2012, bitmovin Softwareentwicklung OG, All Rights Reserved
 *
 * Email: libdash-dev@vicky.bitmovin.net
 *
 * This source code and its use and distribution, is subject to the terms
 * and conditions of the applicable license agreement.
 *****************************************************************************/

#ifndef ABSTRACTCHUNK_H_
#define ABSTRACTCHUNK_H_

#include "config.h"

#include "IDownloadableChunk.h"
#include "DownloadStateManager.h"
#include "../helpers/SyncedBlockStream.h"
#include "../portable/Networking.h"
#include <curl/curl.h>
#include "../metrics/HTTPTransaction.h"
#include "../metrics/TCPConnection.h"
#include "../metrics/ThroughputMeasurement.h"
#include "../helpers/Time.h"

#include <chrono>

namespace dash
{
    namespace network
    {
        class AbstractChunk : public virtual IDownloadableChunk
        {
            public:
                AbstractChunk          ();
                virtual ~AbstractChunk ();

                /*
                 * Pure virtual IChunk Interface
                 */
                virtual std::string&    AbsoluteURI     ()  = 0;
                virtual std::string&    Host            ()  = 0;
                virtual size_t          Port            ()  = 0;
                virtual std::string&    Path            ()  = 0;
                virtual std::string&    Range           ()  = 0;
                virtual size_t          StartByte       ()  = 0;
                virtual size_t          EndByte         ()  = 0;
                virtual bool            HasByteRange    ()  = 0;
                virtual dash::metrics::HTTPTransactionType  GetType() = 0;
                /*
                 * IDownloadableChunk Interface
                 */
                virtual bool    StartDownload           (IConnection *connection);
                virtual bool    StartDownload           ();
                virtual void    AbortDownload           ();
                virtual int     Read                    (uint8_t *data, size_t len);
                virtual int     Peek                    (uint8_t *data, size_t len);
                virtual int     Peek                    (uint8_t *data, size_t len, size_t offset);
                virtual void    AttachDownloadObserver  (IDownloadObserver *observer);
                virtual void    DetachDownloadObserver  (IDownloadObserver *observer);
                /*
                 * Observer Notification
                 */
                void NotifyDownloadRateChanged (double bitrate);
                void NotifyDownloadTimeChanged (double dnltime);
                /*
                 * IDASHMetrics
                 */
                const std::vector<dash::metrics::ITCPConnection *>&     GetTCPConnectionList    () const;
                const std::vector<dash::metrics::IHTTPTransaction *>&   GetHTTPTransactionList  () const;

            private:
                std::vector<IDownloadObserver *>    observers;
                THREAD_HANDLE                       dlThread;
                IConnection                         *connection;
                helpers::SyncedBlockStream          blockStream;
                CURL                                *curl;
                CURLM								*curlm;
                CURLcode                            response;
                uint64_t                            bytesDownloaded;
                DownloadStateManager                stateManager;

                std::vector<dash::metrics::TCPConnection *>     tcpConnections;
                std::vector<dash::metrics::HTTPTransaction *>   httpTransactions;

                static uint32_t BLOCKSIZE;

                static void*    DownloadExternalConnection  (void *chunk);
                static void*    DownloadInternalConnection  (void *chunk);
                static size_t   CurlResponseCallback        (void *contents, size_t size, size_t nmemb, void *userp);
                static size_t   CurlHeaderCallback          (void *headerData, size_t size, size_t nmemb, void *userdata);
                static size_t   CurlDebugCallback           (CURL *url, curl_infotype infoType, char * data, size_t length, void *userdata);
                void            HandleHeaderOutCallback     ();
                void            HandleHeaderInCallback      (std::string data);
        };
    }
}

#endif /* ABSTRACTCHUNK_H_ */