aboutsummaryrefslogtreecommitdiffstats
path: root/src/libdash/source/network/DownloadStateManager.cpp
diff options
context:
space:
mode:
authorAngelo Mantellini (manangel) <angelo.mantellini@irt-systemx.fr>2017-03-29 18:00:06 +0200
committerAngelo Mantellini (manangel) <angelo.mantellini@irt-systemx.fr>2017-03-30 18:58:33 +0200
commit3137acdd5a45285dab9903f9d41560c63eca8523 (patch)
tree38bd8525a9e214d848a73fc40e81ddb182cf91b6 /src/libdash/source/network/DownloadStateManager.cpp
parent9b30fc10fb1cbebe651e5a107e8ca5b24de54675 (diff)
first commit
Change-Id: I8412b8e7d966c2fbc508b537fd9a9bbcfc628ca8 Signed-off-by: Angelo Mantellini (manangel) <angelo.mantellini@irt-systemx.fr>
Diffstat (limited to 'src/libdash/source/network/DownloadStateManager.cpp')
-rw-r--r--src/libdash/source/network/DownloadStateManager.cpp100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/libdash/source/network/DownloadStateManager.cpp b/src/libdash/source/network/DownloadStateManager.cpp
new file mode 100644
index 00000000..5117c099
--- /dev/null
+++ b/src/libdash/source/network/DownloadStateManager.cpp
@@ -0,0 +1,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);
+}