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