aboutsummaryrefslogtreecommitdiffstats
path: root/libdash/source/network/DownloadStateManager.cpp
blob: 5117c099825eff8fc279a84773e991acb99e62b2 (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
/*
 * DownloadStateManager.cpp
 *****************************************************************************
 * 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.
 *****************************************************************************/

#include "DownloadStateManager.h"

using namespace dash::network;

DownloadStateManager::DownloadStateManager  () :
                     state                  (NOT_STARTED)
{
    InitializeConditionVariable (&this->stateChanged);
    InitializeCriticalSection   (&this->stateLock);
}
DownloadStateManager::~DownloadStateManager ()
{
    DeleteConditionVariable (&this->stateChanged);
    DeleteCriticalSection   (&this->stateLock);
}

DownloadState   DownloadStateManager::State         () const
{
    EnterCriticalSection(&this->stateLock);

    DownloadState ret = this->state;

    LeaveCriticalSection(&this->stateLock);

    return ret;
}
void            DownloadStateManager::State         (DownloadState state)
{
    EnterCriticalSection(&this->stateLock);

    this->state = state;

    this->Notify();
    WakeAllConditionVariable(&this->stateChanged);
    LeaveCriticalSection(&this->stateLock);
}
void            DownloadStateManager::WaitState     (DownloadState state) const
{
    EnterCriticalSection(&this->stateLock);

    while(this->state != state)
        SleepConditionVariableCS(&this->stateChanged, &this->stateLock, INFINITE);

    LeaveCriticalSection(&this->stateLock);
}
void            DownloadStateManager::CheckAndWait  (DownloadState check, DownloadState wait) const
{
    EnterCriticalSection(&this->stateLock);

    if(this->state == check)
        while(this->state != wait)
            SleepConditionVariableCS(&this->stateChanged, &this->stateLock, INFINITE);

    LeaveCriticalSection(&this->stateLock);
}
void            DownloadStateManager::Attach        (IDownloadObserver *observer)
{
    EnterCriticalSection(&this->stateLock);
    this->observers.push_back(observer);
    LeaveCriticalSection(&this->stateLock);
}
void            DownloadStateManager::Detach        (IDownloadObserver *observer)
{
    EnterCriticalSection(&this->stateLock);

    uint32_t pos = -1;

    for(size_t i = 0; i < this->observers.size(); i++)
        if(this->observers.at(i) == observer)
            pos = i;

    if(pos != -1)
        this->observers.erase(this->observers.begin() + pos);

    LeaveCriticalSection(&this->stateLock);
}
void            DownloadStateManager::Notify        ()
{
    for(size_t i = 0; i < this->observers.size(); i++)
        this->observers.at(i)->OnDownloadStateChanged(this->state);
}
void            DownloadStateManager::CheckAndSet   (DownloadState check, DownloadState set)
{
    EnterCriticalSection(&this->stateLock);

    if(this->state == check)
        this->state = set;
    LeaveCriticalSection(&this->stateLock);
}