Mercurial > hg > orthanc-stone
comparison Framework/Loaders/OracleScheduler.h @ 1228:c471a0aa137b broker
adding the next generation of loaders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 09 Dec 2019 13:58:37 +0100 |
parents | |
children | 0ca50d275b9a |
comparison
equal
deleted
inserted
replaced
1227:a1c0c9c9f9af | 1228:c471a0aa137b |
---|---|
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 #if !defined(ORTHANC_ENABLE_DCMTK) | |
25 # error The macro ORTHANC_ENABLE_DCMTK must be defined | |
26 #endif | |
27 | |
28 #include "../Messages/IMessageEmitter.h" | |
29 #include "../Messages/ObserverBase.h" | |
30 #include "../Oracle/GetOrthancImageCommand.h" | |
31 #include "../Oracle/GetOrthancWebViewerJpegCommand.h" | |
32 #include "../Oracle/HttpCommand.h" | |
33 #include "../Oracle/IOracle.h" | |
34 #include "../Oracle/OracleCommandExceptionMessage.h" | |
35 #include "../Oracle/OrthancRestApiCommand.h" | |
36 #include "../Oracle/ReadFileCommand.h" | |
37 | |
38 #if ORTHANC_ENABLE_DCMTK == 1 | |
39 # include "../Oracle/ParseDicomSuccessMessage.h" | |
40 #endif | |
41 | |
42 namespace OrthancStone | |
43 { | |
44 class OracleScheduler : public ObserverBase<OracleScheduler> | |
45 { | |
46 public: | |
47 static const int PRIORITY_HIGH = -1; | |
48 static const int PRIORITY_LOW = 100; | |
49 | |
50 private: | |
51 enum Priority | |
52 { | |
53 Priority_Low, | |
54 Priority_Standard, | |
55 Priority_High | |
56 }; | |
57 | |
58 class ReceiverPayload; | |
59 class ScheduledCommand; | |
60 | |
61 typedef std::multimap<int, ScheduledCommand*> Queue; | |
62 | |
63 IOracle& oracle_; | |
64 IMessageEmitter& emitter_; | |
65 Queue standardPriorityQueue_; | |
66 Queue highPriorityQueue_; | |
67 Queue lowPriorityQueue_; | |
68 unsigned int maxHighPriorityCommands_; // Used if priority <= PRIORITY_HIGH | |
69 unsigned int maxStandardPriorityCommands_; | |
70 unsigned int maxLowPriorityCommands_; // Used if priority >= PRIORITY_LOW | |
71 unsigned int activeHighPriorityCommands_; | |
72 unsigned int activeStandardPriorityCommands_; | |
73 unsigned int activeLowPriorityCommands_; | |
74 uint64_t totalScheduled_; | |
75 uint64_t totalProcessed_; | |
76 | |
77 void ClearQueue(Queue& queue); | |
78 | |
79 void RemoveReceiverFromQueue(Queue& queue, | |
80 boost::shared_ptr<IObserver> receiver); | |
81 | |
82 void CheckInvariants() const; | |
83 | |
84 void SpawnFromQueue(Queue& queue, | |
85 Priority priority); | |
86 | |
87 void SpawnCommands(); | |
88 | |
89 void RemoveActiveCommand(const ReceiverPayload& payload); | |
90 | |
91 void Handle(const GetOrthancImageCommand::SuccessMessage& message); | |
92 | |
93 void Handle(const GetOrthancWebViewerJpegCommand::SuccessMessage& message); | |
94 | |
95 void Handle(const HttpCommand::SuccessMessage& message); | |
96 | |
97 void Handle(const OrthancRestApiCommand::SuccessMessage& message); | |
98 | |
99 #if ORTHANC_ENABLE_DCMTK == 1 | |
100 void Handle(const ParseDicomSuccessMessage& message); | |
101 #endif | |
102 | |
103 void Handle(const ReadFileCommand::SuccessMessage& message); | |
104 | |
105 void Handle(const OracleCommandExceptionMessage& message); | |
106 | |
107 OracleScheduler(IOracle& oracle, | |
108 IMessageEmitter& emitter, | |
109 unsigned int maxHighPriority, | |
110 unsigned int maxStandardPriority, | |
111 unsigned int maxLowPriority); | |
112 | |
113 public: | |
114 static boost::shared_ptr<OracleScheduler> Create(IOracle& oracle, | |
115 IObservable& oracleObservable, | |
116 IMessageEmitter& emitter) | |
117 { | |
118 return Create(oracle, oracleObservable, emitter, 1, 4, 1); | |
119 } | |
120 | |
121 static boost::shared_ptr<OracleScheduler> Create(IOracle& oracle, | |
122 IObservable& oracleObservable, | |
123 IMessageEmitter& emitter, | |
124 unsigned int maxHighPriority, | |
125 unsigned int maxStandardPriority, | |
126 unsigned int maxLowPriority); | |
127 | |
128 ~OracleScheduler(); | |
129 | |
130 unsigned int GetMaxHighPriorityCommands() const | |
131 { | |
132 return maxHighPriorityCommands_; | |
133 } | |
134 | |
135 unsigned int GetMaxStandardPriorityCommands() const | |
136 { | |
137 return maxStandardPriorityCommands_; | |
138 } | |
139 | |
140 unsigned int GetMaxLowPriorityCommands() const | |
141 { | |
142 return maxLowPriorityCommands_; | |
143 } | |
144 | |
145 uint64_t GetTotalScheduled() const | |
146 { | |
147 return totalScheduled_; | |
148 } | |
149 | |
150 uint64_t GetTotalProcessed() const | |
151 { | |
152 return totalProcessed_; | |
153 } | |
154 | |
155 // Cancel the HTTP requests that are still pending in the queues, | |
156 // and that are associated with the given receiver. Note that the | |
157 // receiver might still receive answers to HTTP requests that were | |
158 // already submitted to the oracle. | |
159 void CancelRequests(boost::shared_ptr<IObserver> receiver); | |
160 | |
161 void CancelAllRequests(); | |
162 | |
163 void Schedule(boost::shared_ptr<IObserver> receiver, | |
164 int priority, | |
165 IOracleCommand* command /* Takes ownership */); | |
166 }; | |
167 } |