annotate OrthancStone/Sources/Oracle/ThreadedOracle.cpp @ 1935:49c615745708

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