annotate Framework/Oracle/ThreadedOracle.cpp @ 1326:55166e57a77c broker

doc
author Benjamin Golinvaux <bgo@osimis.io>
date Tue, 24 Mar 2020 21:32:35 +0100
parents 257f2c9a02ac
children 297e57d3508c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
1270
2d8ab34c8c91 upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 979
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "ThreadedOracle.h"
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "SleepOracleCommand.h"
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
1077
d7a18a3cd6f9 IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1075
diff changeset
26 #include <Core/Logging.h>
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <Core/OrthancException.h>
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 namespace OrthancStone
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 class ThreadedOracle::Item : public Orthanc::IDynamicObject
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 private:
1075
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 979
diff changeset
34 boost::weak_ptr<IObserver> receiver_;
1298
8a0a62189f46 replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1270
diff changeset
35 std::unique_ptr<IOracleCommand> command_;
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 public:
1075
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 979
diff changeset
38 Item(boost::weak_ptr<IObserver> receiver,
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 IOracleCommand* command) :
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 receiver_(receiver),
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 command_(command)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 if (command == NULL)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
1098
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
49 boost::weak_ptr<IObserver> GetReceiver()
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 return receiver_;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 IOracleCommand& GetCommand()
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 assert(command_.get() != NULL);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 return *command_;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 };
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 class ThreadedOracle::SleepingCommands : public boost::noncopyable
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 private:
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 class Item
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 private:
1075
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 979
diff changeset
68 boost::weak_ptr<IObserver> receiver_;
1298
8a0a62189f46 replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1270
diff changeset
69 std::unique_ptr<SleepOracleCommand> command_;
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 boost::posix_time::ptime expiration_;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 public:
1098
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
73 Item(boost::weak_ptr<IObserver> receiver,
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 SleepOracleCommand* command) :
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 receiver_(receiver),
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 command_(command)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 if (command == NULL)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
760
1181e1ad98ec progressive loading working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 748
diff changeset
83 expiration_ = (boost::posix_time::microsec_clock::local_time() +
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 boost::posix_time::milliseconds(command_->GetDelay()));
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 const boost::posix_time::ptime& GetExpirationTime() const
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 return expiration_;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 void Awake(IMessageEmitter& emitter)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 assert(command_.get() != NULL);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 SleepOracleCommand::TimeoutMessage message(*command_);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 emitter.EmitMessage(receiver_, message);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 };
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 typedef std::list<Item*> Content;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 boost::mutex mutex_;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 Content content_;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 public:
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 ~SleepingCommands()
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 for (Content::iterator it = content_.begin(); it != content_.end(); ++it)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 if (*it != NULL)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 delete *it;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
1098
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
118 void Add(boost::weak_ptr<IObserver> receiver,
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 SleepOracleCommand* command) // Takes ownership
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 boost::mutex::scoped_lock lock(mutex_);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 content_.push_back(new Item(receiver, command));
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 void AwakeExpired(IMessageEmitter& emitter)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 boost::mutex::scoped_lock lock(mutex_);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129
760
1181e1ad98ec progressive loading working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 748
diff changeset
130 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 Content stillSleeping;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 for (Content::iterator it = content_.begin(); it != content_.end(); ++it)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 if (*it != NULL &&
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 (*it)->GetExpirationTime() <= now)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 (*it)->Awake(emitter);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 delete *it;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 *it = NULL;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 else
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 stillSleeping.push_back(*it);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 // Compact the still-sleeping commands
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 content_ = stillSleeping;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 };
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 void ThreadedOracle::Step()
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 {
1298
8a0a62189f46 replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1270
diff changeset
157 std::unique_ptr<Orthanc::IDynamicObject> object(queue_.Dequeue(100));
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 if (object.get() != NULL)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 Item& item = dynamic_cast<Item&>(*object);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162
1077
d7a18a3cd6f9 IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1075
diff changeset
163 if (item.GetCommand().GetType() == IOracleCommand::Type_Sleep)
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 {
1077
d7a18a3cd6f9 IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1075
diff changeset
165 SleepOracleCommand& command = dynamic_cast<SleepOracleCommand&>(item.GetCommand());
d7a18a3cd6f9 IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1075
diff changeset
166
1299
c38c89684d83 replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1279
diff changeset
167 std::unique_ptr<SleepOracleCommand> copy(new SleepOracleCommand(command.GetDelay()));
1077
d7a18a3cd6f9 IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1075
diff changeset
168
d7a18a3cd6f9 IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1075
diff changeset
169 if (command.HasPayload())
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 {
1128
8e3763d1736a removing CustomOracleCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1126
diff changeset
171 copy->AcquirePayload(command.ReleasePayload());
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 }
1077
d7a18a3cd6f9 IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1075
diff changeset
173
d7a18a3cd6f9 IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1075
diff changeset
174 sleepingCommands_->Add(item.GetReceiver(), copy.release());
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 }
1077
d7a18a3cd6f9 IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1075
diff changeset
176 else
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 {
1104
98cdfe5768a4 ReadFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1099
diff changeset
178 GenericOracleRunner runner;
979
18d53a8b41b7 HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 956
diff changeset
179
1109
79b1b541fe15 ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1104
diff changeset
180 {
79b1b541fe15 ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1104
diff changeset
181 boost::mutex::scoped_lock lock(mutex_);
79b1b541fe15 ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1104
diff changeset
182 runner.SetOrthanc(orthanc_);
79b1b541fe15 ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1104
diff changeset
183 runner.SetRootDirectory(rootDirectory_);
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184
1126
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1124
diff changeset
185 #if ORTHANC_ENABLE_DCMTK == 1
1124
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
186 if (dicomCache_)
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
187 {
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
188 runner.SetDicomCache(dicomCache_);
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
189 }
1126
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1124
diff changeset
190 #endif
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 }
1134
87fbeb823375 allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1128
diff changeset
192
87fbeb823375 allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1128
diff changeset
193 runner.Run(item.GetReceiver(), emitter_, item.GetCommand());
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 void ThreadedOracle::Worker(ThreadedOracle* that)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 assert(that != NULL);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 for (;;)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 boost::mutex::scoped_lock lock(that->mutex_);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 if (that->state_ != State_Running)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 return;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 that->Step();
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 void ThreadedOracle::SleepingWorker(ThreadedOracle* that)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 assert(that != NULL);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 for (;;)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 boost::mutex::scoped_lock lock(that->mutex_);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 if (that->state_ != State_Running)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 return;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 that->sleepingCommands_->AwakeExpired(that->emitter_);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 boost::this_thread::sleep(boost::posix_time::milliseconds(that->sleepingTimeResolution_));
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 void ThreadedOracle::StopInternal()
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 boost::mutex::scoped_lock lock(mutex_);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 if (state_ == State_Setup ||
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 state_ == State_Stopped)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 return;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 else
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 state_ = State_Stopped;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 if (sleepingWorker_.joinable())
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 sleepingWorker_.join();
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 for (size_t i = 0; i < workers_.size(); i++)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 if (workers_[i] != NULL)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 if (workers_[i]->joinable())
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 workers_[i]->join();
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 delete workers_[i];
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 ThreadedOracle::ThreadedOracle(IMessageEmitter& emitter) :
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 emitter_(emitter),
1109
79b1b541fe15 ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1104
diff changeset
277 rootDirectory_("."),
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 state_(State_Setup),
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 workers_(4),
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 sleepingCommands_(new SleepingCommands),
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 sleepingTimeResolution_(50) // By default, time resolution of 50ms
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285
765
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
286 ThreadedOracle::~ThreadedOracle()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
287 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
288 if (state_ == State_Running)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
289 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
290 LOG(ERROR) << "The threaded oracle is still running, explicit call to "
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
291 << "Stop() is mandatory to avoid crashes";
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
292 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
293
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
294 try
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
295 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
296 StopInternal();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
297 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
298 catch (Orthanc::OrthancException& e)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
299 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
300 LOG(ERROR) << "Exception while stopping the threaded oracle: " << e.What();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
301 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
302 catch (...)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
303 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
304 LOG(ERROR) << "Native exception while stopping the threaded oracle";
1097
4383382db01d deprecating LockingEmitter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1077
diff changeset
305 }
765
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
306 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
307
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
308
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 void ThreadedOracle::SetOrthancParameters(const Orthanc::WebServiceParameters& orthanc)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 boost::mutex::scoped_lock lock(mutex_);
1109
79b1b541fe15 ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1104
diff changeset
312 orthanc_ = orthanc;
79b1b541fe15 ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1104
diff changeset
313 }
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314
1109
79b1b541fe15 ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1104
diff changeset
315
79b1b541fe15 ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1104
diff changeset
316 void ThreadedOracle::SetRootDirectory(const std::string& rootDirectory)
79b1b541fe15 ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1104
diff changeset
317 {
79b1b541fe15 ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1104
diff changeset
318 boost::mutex::scoped_lock lock(mutex_);
79b1b541fe15 ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1104
diff changeset
319 rootDirectory_ = rootDirectory;
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322
760
1181e1ad98ec progressive loading working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 748
diff changeset
323 void ThreadedOracle::SetThreadsCount(unsigned int count)
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 boost::mutex::scoped_lock lock(mutex_);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 if (count <= 0)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331 else if (state_ != State_Setup)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 {
956
a7351ad54960 Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents: 819
diff changeset
333 LOG(ERROR) << "ThreadedOracle::SetThreadsCount(): (state_ != State_Setup)";
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336 else
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
338 workers_.resize(count);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343 void ThreadedOracle::SetSleepingTimeResolution(unsigned int milliseconds)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
345 boost::mutex::scoped_lock lock(mutex_);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
346
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
347 if (milliseconds <= 0)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 else if (state_ != State_Setup)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 {
956
a7351ad54960 Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents: 819
diff changeset
353 LOG(ERROR) << "ThreadedOracle::SetSleepingTimeResolution(): (state_ != State_Setup)";
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 else
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358 sleepingTimeResolution_ = milliseconds;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
361
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
362
1124
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
363 void ThreadedOracle::SetDicomCacheSize(size_t size)
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
364 {
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
365 #if ORTHANC_ENABLE_DCMTK == 1
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
366 boost::mutex::scoped_lock lock(mutex_);
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
367
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
368 if (state_ != State_Setup)
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
369 {
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
370 LOG(ERROR) << "ThreadedOracle::SetDicomCacheSize(): (state_ != State_Setup)";
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
371 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
372 }
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
373 else
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
374 {
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
375 if (size == 0)
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
376 {
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
377 dicomCache_.reset();
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
378 }
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
379 else
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
380 {
1150
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1140
diff changeset
381 dicomCache_.reset(new ParsedDicomCache(size));
1124
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
382 }
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
383 }
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
384 #endif
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
385 }
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
386
a8bf81756839 unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1109
diff changeset
387
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 void ThreadedOracle::Start()
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390 boost::mutex::scoped_lock lock(mutex_);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392 if (state_ != State_Setup)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 {
956
a7351ad54960 Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents: 819
diff changeset
394 LOG(ERROR) << "ThreadedOracle::Start(): (state_ != State_Setup)";
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
395 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
397 else
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398 {
1140
6333e6f7248e fix cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1134
diff changeset
399 LOG(WARNING) << "Starting oracle with " << workers_.size() << " worker threads";
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
400 state_ = State_Running;
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
402 for (unsigned int i = 0; i < workers_.size(); i++)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403 {
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
404 workers_[i] = new boost::thread(Worker, this);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
405 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
406
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407 sleepingWorker_ = boost::thread(SleepingWorker, this);
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411
1098
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
412 bool ThreadedOracle::Schedule(boost::shared_ptr<IObserver> receiver,
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
413 IOracleCommand* command)
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
414 {
1299
c38c89684d83 replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1279
diff changeset
415 std::unique_ptr<Item> item(new Item(receiver, command));
1098
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
416
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
417 {
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
418 boost::mutex::scoped_lock lock(mutex_);
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
419
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
420 if (state_ == State_Running)
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
421 {
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
422 //LOG(INFO) << "New oracle command queued";
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
423 queue_.Enqueue(item.release());
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
424 return true;
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
425 }
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
426 else
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
427 {
1140
6333e6f7248e fix cache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1134
diff changeset
428 LOG(TRACE) << "Command not enqueued, as the oracle has stopped";
1098
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
429 return false;
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
430 }
17660df24c36 simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1097
diff changeset
431 }
748
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
432 }
ab236bb5dbc7 ThreadedOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
433 }