annotate OrthancStone/Sources/Oracle/ThreadedOracle.cpp @ 1830:fa38043b14b0

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