Mercurial > hg > orthanc-stone
annotate Framework/Messages/LockingEmitter.h @ 1284:1bb54005e2bd bugs/2020-02-invisible-slice
merged default into branch
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Tue, 11 Feb 2020 14:38:36 +0100 |
parents | 2d8ab34c8c91 |
children |
rev | line source |
---|---|
843
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
1 /** |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
2 * Stone of Orthanc |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
1270
2d8ab34c8c91
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1071
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
843
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
6 * |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
7 * This program is free software: you can redistribute it and/or |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
8 * modify it under the terms of the GNU Affero General Public License |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
10 * the License, or (at your option) any later version. |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
11 * |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
12 * This program is distributed in the hope that it will be useful, but |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
15 * Affero General Public License for more details. |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
16 * |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
17 * You should have received a copy of the GNU Affero General Public License |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
19 **/ |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
20 |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
21 #pragma once |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
22 |
1071
6dd90b8d1589
Fixes related to refactorings in viewports.
Benjamin Golinvaux <bgo@osimis.io>
parents:
852
diff
changeset
|
23 #include <Core/Enumerations.h> |
6dd90b8d1589
Fixes related to refactorings in viewports.
Benjamin Golinvaux <bgo@osimis.io>
parents:
852
diff
changeset
|
24 #include <Core/OrthancException.h> |
6dd90b8d1589
Fixes related to refactorings in viewports.
Benjamin Golinvaux <bgo@osimis.io>
parents:
852
diff
changeset
|
25 |
843
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
26 #include "IMessageEmitter.h" |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
27 #include "IObservable.h" |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
28 |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
29 #include <boost/thread.hpp> |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
30 |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
31 namespace OrthancStone |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
32 { |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
33 /** |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
34 * This class is used when using the ThreadedOracle : since messages |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
35 * can be sent from multiple Oracle threads, this IMessageEmitter |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
36 * implementation serializes the callbacks. |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
37 * |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
38 * The internal mutex used in Oracle messaging can also be used to |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
39 * protect the application data. Thus, this class can be used as a single |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
40 * application-wide mutex. |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
41 */ |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
42 class LockingEmitter : public IMessageEmitter |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
43 { |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
44 private: |
1071
6dd90b8d1589
Fixes related to refactorings in viewports.
Benjamin Golinvaux <bgo@osimis.io>
parents:
852
diff
changeset
|
45 boost::shared_mutex mutex_; |
843
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
46 MessageBroker broker_; |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
47 IObservable oracleObservable_; |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
48 |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
49 public: |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
50 LockingEmitter() : |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
51 oracleObservable_(broker_) |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
52 { |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
53 } |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
54 |
852
6d15261f9c99
Added way to share the broker that is used inside the locking emitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
55 MessageBroker& GetBroker() |
6d15261f9c99
Added way to share the broker that is used inside the locking emitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
56 { |
6d15261f9c99
Added way to share the broker that is used inside the locking emitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
57 return broker_; |
6d15261f9c99
Added way to share the broker that is used inside the locking emitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
58 } |
6d15261f9c99
Added way to share the broker that is used inside the locking emitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
59 |
843
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
60 virtual void EmitMessage(const IObserver& observer, |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
61 const IMessage& message) ORTHANC_OVERRIDE |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
62 { |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
63 try |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
64 { |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
65 boost::unique_lock<boost::shared_mutex> lock(mutex_); |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
66 oracleObservable_.EmitMessage(observer, message); |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
67 } |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
68 catch (Orthanc::OrthancException& e) |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
69 { |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
70 LOG(ERROR) << "Exception while emitting a message: " << e.What(); |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
71 } |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
72 } |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
73 |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
74 |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
75 class ReaderLock : public boost::noncopyable |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
76 { |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
77 private: |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
78 LockingEmitter& that_; |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
79 boost::shared_lock<boost::shared_mutex> lock_; |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
80 |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
81 public: |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
82 ReaderLock(LockingEmitter& that) : |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
83 that_(that), |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
84 lock_(that.mutex_) |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
85 { |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
86 } |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
87 }; |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
88 |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
89 |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
90 class WriterLock : public boost::noncopyable |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
91 { |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
92 private: |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
93 LockingEmitter& that_; |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
94 boost::unique_lock<boost::shared_mutex> lock_; |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
95 |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
96 public: |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
97 WriterLock(LockingEmitter& that) : |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
98 that_(that), |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
99 lock_(that.mutex_) |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
100 { |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
101 } |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
102 |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
103 MessageBroker& GetBroker() |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
104 { |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
105 return that_.broker_; |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
106 } |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
107 |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
108 IObservable& GetOracleObservable() |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
109 { |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
110 return that_.oracleObservable_; |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
111 } |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
112 }; |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
113 }; |
67f9c27214c5
Removed extra logging + doc + added GuiAdapter and LockingEmitter
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
114 } |