comparison Framework/Loaders/LoaderStateMachine.h @ 815:df442f1ba0c6

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 28 May 2019 21:59:20 +0200
parents
children 38409549db43
comparison
equal deleted inserted replaced
814:aead999345e0 815:df442f1ba0c6
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #pragma once
23
24 #include "../Messages/IObservable.h"
25 #include "../Messages/IObserver.h"
26 #include "../Oracle/GetOrthancImageCommand.h"
27 #include "../Oracle/GetOrthancWebViewerJpegCommand.h"
28 #include "../Oracle/IOracle.h"
29 #include "../Oracle/OracleCommandExceptionMessage.h"
30 #include "../Oracle/OrthancRestApiCommand.h"
31
32 #include <Core/IDynamicObject.h>
33
34 #include <list>
35
36 namespace OrthancStone
37 {
38 /**
39 This class is supplied with Oracle commands and will schedule up to
40 simultaneousDownloads_ of them at the same time, then will schedule the
41 rest once slots become available. It is used, a.o., by the
42 OrtancMultiframeVolumeLoader class.
43 */
44 class LoaderStateMachine : public IObserver
45 {
46 protected:
47 class State : public Orthanc::IDynamicObject
48 {
49 private:
50 LoaderStateMachine& that_;
51
52 public:
53 State(LoaderStateMachine& that) :
54 that_(that)
55 {
56 }
57
58 State(const State& currentState) :
59 that_(currentState.that_)
60 {
61 }
62
63 void Schedule(OracleCommandWithPayload* command) const
64 {
65 that_.Schedule(command);
66 }
67
68 template <typename T>
69 T& GetLoader() const
70 {
71 return dynamic_cast<T&>(that_);
72 }
73
74 virtual void Handle(const OrthancRestApiCommand::SuccessMessage& message);
75
76 virtual void Handle(const GetOrthancImageCommand::SuccessMessage& message);
77
78 virtual void Handle(const GetOrthancWebViewerJpegCommand::SuccessMessage& message);
79 };
80
81 void Schedule(OracleCommandWithPayload* command);
82
83 void Start();
84
85 private:
86 void Step();
87
88 void Clear();
89
90 void HandleExceptionMessage(const OracleCommandExceptionMessage& message);
91
92 template <typename T>
93 void HandleSuccessMessage(const T& message);
94
95 typedef std::list<IOracleCommand*> PendingCommands;
96
97 IOracle& oracle_;
98 bool active_;
99 unsigned int simultaneousDownloads_;
100 PendingCommands pendingCommands_;
101 unsigned int activeCommands_;
102
103 public:
104 LoaderStateMachine(IOracle& oracle,
105 IObservable& oracleObservable);
106
107 virtual ~LoaderStateMachine()
108 {
109 Clear();
110 }
111
112 bool IsActive() const
113 {
114 return active_;
115 }
116
117 void SetSimultaneousDownloads(unsigned int count);
118 };
119 }