aboutsummaryrefslogtreecommitdiffstats
path: root/Input/ICNConnectionConsumerApi.cpp
blob: 9de829081d5462a2aee15e2641df9c1efa73b7c2 (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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ICNICPDOWNLOAD

#include "ICNConnectionConsumerApi.h"

#define DEFAULT_LIFETIME 250
#define RETRY_TIMEOUTS 5

using namespace dash;
using namespace dash::network;
using namespace dash::metrics;
using namespace icnet;

using std::bind;
using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;

using duration_in_seconds = std::chrono::duration<double, std::ratio<1, 1> >;


namespace libdash {
namespace framework {
namespace input {
ICNConnectionConsumerApi::ICNConnectionConsumerApi(double alpha, float beta, float drop) :
    m_recv_name(ccnx::Name()),
    m_first(1),
    m_isFinished(false),
    sizeDownloaded (0),
    cumulativeBytesReceived(0),
    icnAlpha(alpha),
    beta(beta),
    drop(drop)
{
    gamma = 1;
    this->speed = 0.0;
    this->dnltime = 0.0;
    this->deezData = NULL;
    this->deezDataSize = 0;
    this->datSize = 0;
    this->dataPos = 0;
    InitializeConditionVariable (&this->contentRetrieved);
    InitializeCriticalSection   (&this->monitorMutex);

    this->myConsumer = new ConsumerSocket(ccnx::Name(), TransportProtocolAlgorithms::RAAQM);
    this->myConsumer->setSocketOption(RaaqmTransportOptions::GAMMA_VALUE, (int)gamma);

    bool configFile = false;
    //CHECK if we are not going to override the configuration file. (if !autotune)
    if(FILE *fp = fopen("/usr/etc/consumer.conf", "r"))
    {
        fclose(fp);
        configFile = true;
    }
    if(!configFile)
    {
        qDebug("beta %f, drop %f", this->beta, this->drop);
        this->myConsumer->setSocketOption(RaaqmTransportOptions::BETA_VALUE, this->beta);
        this->myConsumer->setSocketOption(RaaqmTransportOptions::DROP_FACTOR, this->drop);
    }
    this->myConsumer->setSocketOption(RateEstimationOptions::RATE_ESTIMATION_OBSERVER, this);
    this->myConsumer->setSocketOption(ConsumerCallbacksOptions::CONTENT_RETRIEVED, (ConsumerContentCallback) bind(&ICNConnectionConsumerApi::processPayload, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
    this->myConsumer->setSocketOption(ConsumerCallbacksOptions::CONTENT_OBJECT_TO_VERIFY, (ConsumerContentObjectVerificationCallback)bind(&ICNConnectionConsumerApi::onPacket, this, std::placeholders::_1, std::placeholders::_2));
#ifdef NO_GUI
    if(this->icnAlpha != 20)
        this->icnRateBased = true;
#else
    this->icnRateBased = true;
#endif
    Debug("ICN class created\n");

}

ICNConnectionConsumerApi::~ICNConnectionConsumerApi() {
    delete this->myConsumer;
    if(this->deezData)
    {
        free(this->deezData);
        this->deezData = NULL;
    }
    DeleteConditionVariable (&this->contentRetrieved);
    DeleteCriticalSection   (&this->monitorMutex);
}

void ICNConnectionConsumerApi::Init(IChunk *chunk) {
    Debug("ICN Connection:	STARTING\n");
    m_first = 1;
    sizeDownloaded = 0;
    m_name = "ccnx:/" + chunk->Host() + chunk->Path();
    m_isFinished = false;

    res = false;
    dataPos = 0;
    datSize = 0;
    if(this->deezData)
    {
        memset(this->deezData, 0, this->deezDataSize);
    }

    qDebug("ICN_Connection:\tINTIATED_to_name %s\n", m_name.c_str());
    qDebug("ICN_Connection:\tSTARTING DOWNLOAD %s\n", m_name.c_str());
}

void ICNConnectionConsumerApi::InitForMPD(const std::string& url)
{
    m_first = 1;
    sizeDownloaded = 0;

    if(url.find("//") != std::string::npos)
    {
        int pos = url.find("//");
        char* myName = (char*)malloc(strlen(url.c_str()) - 1);
        strncpy(myName, url.c_str(), pos + 1);
        strncpy(myName + pos + 1, url.c_str() + pos + 2, strlen(url.c_str()) - pos - 2);
        m_name = std::string(myName);
        free(myName);
    }
    else
    {
        m_name = url;
    }
    m_isFinished = false;

    res = false;
    dataPos = 0;
    datSize = 0;
    Debug("ICN_Connection:\tINTIATED_for_mpd %s\n", m_name.c_str());
}

int	ICNConnectionConsumerApi::Read(uint8_t* data, size_t len, IChunk *chunk)
{
    return this->Read(data, len);
}

int	ICNConnectionConsumerApi::Read(uint8_t *data, size_t len)
{
    if(!res)
        m_start_time = std::chrono::system_clock::now();

    if(res)
    {
        if(this->dataPos == this->datSize)
        {
            this->dnltime = std::chrono::duration_cast<duration_in_seconds>(std::chrono::system_clock::now() - m_start_time).count();
            if(speed == 0 || !this->icnRateBased)
                speed = (double) (sizeDownloaded * 8 / this->dnltime);
            cumulativeBytesReceived += sizeDownloaded;
            Debug("ICN_Connection:\tFINISHED DOWNLOADING %s Average_DL: %f size: %lu cumulative: %lu Throughput: %f\n", m_name.c_str(), speed, sizeDownloaded, cumulativeBytesReceived, (double) (sizeDownloaded * 8 / this->dnltime));
            return 0;
        }
        if((this->datSize - this->dataPos) > (int)len)
        {
            memcpy(data, this->deezData + this->dataPos, len);
            this->dataPos += len;
            sizeDownloaded += len;
            return len;
        }
        else
        {
            assert(this->datSize - this->dataPos > 0);
            memcpy(data, this->deezData + this->dataPos, this->datSize - this->dataPos);
            int temp = this->datSize - this->dataPos;
            this->dataPos += this->datSize - this->dataPos;
            sizeDownloaded += temp;
            return temp;
        }
    }

    Debug("will consume: %s\n", m_name.c_str());
    this->myConsumer->consume(m_name);
    EnterCriticalSection(&this->monitorMutex);

    while(this->m_isFinished == false)
        SleepConditionVariableCS(&this->contentRetrieved, &this->monitorMutex, INFINITE);

    assert(this->datSize != 0);
    this->res = true;
    LeaveCriticalSection(&this->monitorMutex);
    if(this->datSize > (int)len)
    {
        memcpy(data, this->deezData, len);
        this->dataPos += len;
        sizeDownloaded += len;
        return len;
    }
    else
    {
        memcpy(data, this->deezData, this->datSize);
        this->dataPos += this->datSize;
        sizeDownloaded += this->datSize;
        return this->datSize;
    }
}

int             ICNConnectionConsumerApi::Peek(uint8_t *data, size_t len, IChunk *chunk) {
    return -1;
}

bool   ICNConnectionConsumerApi::onPacket(ConsumerSocket& c, const ContentObject& data)
{
    return true;
}

void   ICNConnectionConsumerApi::processPayload(ConsumerSocket& c, const uint8_t* buffer, size_t bufferSize)
{
    EnterCriticalSection(&this->monitorMutex);
    if(this->deezData == NULL)
    {
        this->deezData = (char *)malloc(bufferSize*sizeof(uint8_t));
        this->deezDataSize = bufferSize;
    }
    else
    {
        if(bufferSize > this->deezDataSize)
        {
            this->deezData = (char *)realloc(this->deezData, bufferSize * (sizeof(uint8_t)));
            this->deezDataSize = bufferSize;
        }
    }
    memcpy(this->deezData, buffer, bufferSize*sizeof(uint8_t));
    this->m_isFinished = true;
    this->datSize = (int) bufferSize;
    WakeAllConditionVariable(&this->contentRetrieved);
    LeaveCriticalSection(&this->monitorMutex);
}

double ICNConnectionConsumerApi::GetAverageDownloadingSpeed()
{
    Debug("ICNConnection:	DL speed is %f\n", this->speed);
    return this->speed;
}

double ICNConnectionConsumerApi::GetDownloadingTime()
{
    Debug("ICNConnection:	DL time is %f\n", this->dnltime);
    return this->dnltime;
}

const std::vector<ITCPConnection *> &ICNConnectionConsumerApi::GetTCPConnectionList() const {
    return tcpConnections;
}

const std::vector<IHTTPTransaction *> &ICNConnectionConsumerApi::GetHTTPTransactionList() const {
    return httpTransactions;
}

void ICNConnectionConsumerApi::notifyStats(double winSize)
{
    this->speed = (winSize); // * 1000000 * 1400 * 8;
    Debug("ICNConnection:\tNotificationICPDL\t%f\t%f\n", winSize, speed);
}

void ICNConnectionConsumerApi::SetBeta(float beta)
{
    this->beta = beta;
}


void ICNConnectionConsumerApi::SetDrop(float drop)
{
    this->drop = drop;
}

} /* namespace input */
} /* namespace framework */
} /* namespace libdash */


#endif //NDEF ICNICPDOWNLOAD