comparison OrthancStone/Sources/Loaders/OracleScheduler.h @ 1512:244ad1e4e76a

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