Mercurial > hg > orthanc-stone
comparison Framework/Loaders/LoaderStateMachine.cpp @ 1381:f4a06ad1580b
Branch broker is now the new default
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Wed, 22 Apr 2020 14:05:47 +0200 |
parents | 8a0a62189f46 556b4bc19118 |
children | 30deba7bc8e2 |
comparison
equal
deleted
inserted
replaced
1375:4431ffdcc2a4 | 1381:f4a06ad1580b |
---|---|
19 **/ | 19 **/ |
20 | 20 |
21 | 21 |
22 #include "LoaderStateMachine.h" | 22 #include "LoaderStateMachine.h" |
23 | 23 |
24 #include "../Loaders/ILoadersContext.h" | |
25 | |
24 #include <Core/OrthancException.h> | 26 #include <Core/OrthancException.h> |
25 | 27 |
26 namespace OrthancStone | 28 namespace OrthancStone |
27 { | 29 { |
28 void LoaderStateMachine::State::Handle(const OrthancRestApiCommand::SuccessMessage& message) | 30 void LoaderStateMachine::State::Handle(const OrthancStone::OrthancRestApiCommand::SuccessMessage& message) |
29 { | 31 { |
30 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | 32 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
31 } | 33 } |
32 | 34 |
33 | 35 |
34 void LoaderStateMachine::State::Handle(const GetOrthancImageCommand::SuccessMessage& message) | 36 void LoaderStateMachine::State::Handle(const OrthancStone::GetOrthancImageCommand::SuccessMessage& message) |
35 { | 37 { |
36 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | 38 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
37 } | 39 } |
38 | 40 |
39 | 41 |
40 void LoaderStateMachine::State::Handle(const GetOrthancWebViewerJpegCommand::SuccessMessage& message) | 42 void LoaderStateMachine::State::Handle(const OrthancStone::GetOrthancWebViewerJpegCommand::SuccessMessage& message) |
41 { | 43 { |
42 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | 44 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
43 } | 45 } |
44 | 46 |
45 | 47 |
46 void LoaderStateMachine::Schedule(OracleCommandWithPayload* command) | 48 void LoaderStateMachine::Schedule(OrthancStone::OracleCommandBase* command) |
47 { | 49 { |
48 LOG(TRACE) << "LoaderStateMachine(" << std::hex << this << std::dec << ")::Schedule()"; | 50 LOG(TRACE) << "LoaderStateMachine(" << std::hex << this << std::dec << ")::Schedule()"; |
49 | 51 |
50 std::unique_ptr<OracleCommandWithPayload> protection(command); | 52 std::unique_ptr<OrthancStone::OracleCommandBase> protection(command); |
51 | 53 |
52 if (command == NULL) | 54 if (command == NULL) |
53 { | 55 { |
54 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | 56 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); |
55 } | 57 } |
88 { | 90 { |
89 if (!pendingCommands_.empty() && | 91 if (!pendingCommands_.empty() && |
90 activeCommands_ < simultaneousDownloads_) | 92 activeCommands_ < simultaneousDownloads_) |
91 { | 93 { |
92 | 94 |
93 IOracleCommand* nextCommand = pendingCommands_.front(); | 95 OrthancStone::IOracleCommand* nextCommand = pendingCommands_.front(); |
94 | 96 |
95 LOG(TRACE) << " LoaderStateMachine(" << std::hex << this << std::dec << | 97 LOG(TRACE) << " LoaderStateMachine(" << std::hex << this << std::dec << |
96 ")::Step(): activeCommands_ (" << activeCommands_ << | 98 ")::Step(): activeCommands_ (" << activeCommands_ << |
97 ") < simultaneousDownloads_ (" << simultaneousDownloads_ << | 99 ") < simultaneousDownloads_ (" << simultaneousDownloads_ << |
98 ") --> will Schedule command addr " << std::hex << nextCommand << std::dec; | 100 ") --> will Schedule command addr " << std::hex << nextCommand << std::dec; |
99 | 101 |
100 oracle_.Schedule(*this, nextCommand); | 102 { |
103 std::unique_ptr<OrthancStone::ILoadersContext::ILock> lock(loadersContext_.Lock()); | |
104 boost::shared_ptr<IObserver> observer(GetSharedObserver()); | |
105 lock->Schedule(observer, 0, nextCommand); // TODO: priority! | |
106 } | |
101 pendingCommands_.pop_front(); | 107 pendingCommands_.pop_front(); |
102 | 108 |
103 activeCommands_++; | 109 activeCommands_++; |
104 } | 110 } |
105 else | 111 else |
121 delete *it; | 127 delete *it; |
122 } | 128 } |
123 | 129 |
124 pendingCommands_.clear(); | 130 pendingCommands_.clear(); |
125 } | 131 } |
126 | 132 |
127 | 133 |
128 void LoaderStateMachine::HandleExceptionMessage(const OracleCommandExceptionMessage& message) | 134 void LoaderStateMachine::HandleExceptionMessage(const OrthancStone::OracleCommandExceptionMessage& message) |
129 { | 135 { |
130 LOG(ERROR) << "LoaderStateMachine::HandleExceptionMessage: error in the state machine, stopping all processing"; | 136 LOG(ERROR) << "LoaderStateMachine::HandleExceptionMessage: error in the state machine, stopping all processing"; |
131 LOG(ERROR) << "Error: " << message.GetException().What() << " Details: " << | 137 LOG(ERROR) << "Error: " << message.GetException().What() << " Details: " << |
132 message.GetException().GetDetails(); | 138 message.GetException().GetDetails(); |
133 Clear(); | 139 Clear(); |
134 } | 140 } |
135 | 141 |
136 template <typename T> | 142 template <typename T> |
137 void LoaderStateMachine::HandleSuccessMessage(const T& message) | 143 void LoaderStateMachine::HandleSuccessMessage(const T& message) |
138 { | 144 { |
139 LOG(TRACE) << "LoaderStateMachine(" << std::hex << this << std::dec << ")::HandleSuccessMessage(). Receiver fingerprint = " << GetFingerprint(); | |
140 if (activeCommands_ <= 0) { | 145 if (activeCommands_ <= 0) { |
141 LOG(ERROR) << "LoaderStateMachine(" << std::hex << this << std::dec << ")::HandleSuccessMessage : activeCommands_ should be > 0 but is: " << activeCommands_; | 146 LOG(ERROR) << "LoaderStateMachine(" << std::hex << this << std::dec << ")::HandleSuccessMessage : activeCommands_ should be > 0 but is: " << activeCommands_; |
142 } | 147 } |
143 else { | 148 else { |
144 activeCommands_--; | 149 activeCommands_--; |
155 } | 160 } |
156 } | 161 } |
157 } | 162 } |
158 | 163 |
159 | 164 |
160 LoaderStateMachine::LoaderStateMachine(IOracle& oracle, | 165 LoaderStateMachine::LoaderStateMachine( |
161 IObservable& oracleObservable) : | 166 OrthancStone::ILoadersContext& loadersContext) |
162 IObserver(oracleObservable.GetBroker()), | 167 : loadersContext_(loadersContext) |
163 oracle_(oracle), | 168 , active_(false) |
164 oracleObservable_(oracleObservable), | 169 , simultaneousDownloads_(4) |
165 active_(false), | 170 , activeCommands_(0) |
166 simultaneousDownloads_(4), | 171 { |
167 activeCommands_(0) | 172 using OrthancStone::ILoadersContext; |
168 { | 173 |
169 LOG(TRACE) << "LoaderStateMachine(" << std::hex << this << std::dec << ")::LoaderStateMachine()"; | 174 LOG(TRACE) |
170 | 175 << "LoaderStateMachine(" << std::hex << this |
171 oracleObservable.RegisterObserverCallback( | 176 << std::dec << ")::LoaderStateMachine()"; |
172 new Callable<LoaderStateMachine, OrthancRestApiCommand::SuccessMessage> | 177 } |
173 (*this, &LoaderStateMachine::HandleSuccessMessage)); | 178 |
174 | 179 void LoaderStateMachine::PostConstructor() |
175 oracleObservable.RegisterObserverCallback( | 180 { |
176 new Callable<LoaderStateMachine, GetOrthancImageCommand::SuccessMessage> | 181 std::unique_ptr<OrthancStone::ILoadersContext::ILock> |
177 (*this, &LoaderStateMachine::HandleSuccessMessage)); | 182 lock(loadersContext_.Lock()); |
178 | 183 |
179 oracleObservable.RegisterObserverCallback( | 184 OrthancStone::IObservable& observable = lock->GetOracleObservable(); |
180 new Callable<LoaderStateMachine, GetOrthancWebViewerJpegCommand::SuccessMessage> | 185 |
181 (*this, &LoaderStateMachine::HandleSuccessMessage)); | 186 // TODO => Move this out of constructor |
182 | 187 Register<OrthancStone::OrthancRestApiCommand::SuccessMessage>( |
183 oracleObservable.RegisterObserverCallback( | 188 observable, &LoaderStateMachine::HandleSuccessMessage); |
184 new Callable<LoaderStateMachine, OracleCommandExceptionMessage> | 189 Register<OrthancStone::GetOrthancImageCommand::SuccessMessage>( |
185 (*this, &LoaderStateMachine::HandleExceptionMessage)); | 190 observable, &LoaderStateMachine::HandleSuccessMessage); |
191 Register<OrthancStone::GetOrthancWebViewerJpegCommand::SuccessMessage>( | |
192 observable, &LoaderStateMachine::HandleSuccessMessage); | |
193 Register<OrthancStone::OracleCommandExceptionMessage>( | |
194 observable, &LoaderStateMachine::HandleExceptionMessage); | |
186 } | 195 } |
187 | 196 |
188 LoaderStateMachine::~LoaderStateMachine() | 197 LoaderStateMachine::~LoaderStateMachine() |
189 { | 198 { |
190 oracleObservable_.Unregister(this); | |
191 LOG(TRACE) << "LoaderStateMachine(" << std::hex << this << std::dec << ")::~LoaderStateMachine()"; | 199 LOG(TRACE) << "LoaderStateMachine(" << std::hex << this << std::dec << ")::~LoaderStateMachine()"; |
192 Clear(); | 200 Clear(); |
193 } | 201 } |
194 | 202 |
195 void LoaderStateMachine::SetSimultaneousDownloads(unsigned int count) | 203 void LoaderStateMachine::SetSimultaneousDownloads(unsigned int count) |