Mercurial > hg > orthanc-stone
annotate Framework/Messages/MessageForwarder.h @ 309:14ef1227120f am-callable-and-promise
web services: better handling of failures
author | am@osimis.io |
---|---|
date | Fri, 28 Sep 2018 15:02:43 +0200 |
parents | 4a79193ffb58 |
children | b66d13708f40 |
rev | line source |
---|---|
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
1 /** |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
2 * Stone of Orthanc |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
6 * |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
7 * This program is free software: you can redistribute it and/or |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
8 * modify it under the terms of the GNU Affero General Public License |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
10 * the License, or (at your option) any later version. |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
11 * |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
12 * This program is distributed in the hope that it will be useful, but |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
15 * Affero General Public License for more details. |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
16 * |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
17 * You should have received a copy of the GNU Affero General Public License |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
19 **/ |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
20 |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
21 |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
22 #pragma once |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
23 |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
24 #include "ICallable.h" |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
25 #include "IObserver.h" |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
26 |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
27 #include <boost/noncopyable.hpp> |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
28 |
302
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
29 namespace OrthancStone |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
30 { |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
31 |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
32 class IObservable; |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
33 |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
34 class IMessageForwarder : public boost::noncopyable |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
35 { |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
36 IObservable& emitter_; |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
37 public: |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
38 IMessageForwarder(IObservable& emitter) |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
39 : emitter_(emitter) |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
40 {} |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
41 virtual ~IMessageForwarder() {} |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
42 |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
43 protected: |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
44 void ForwardMessageInternal(const IMessage& message); |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
45 void RegisterForwarderInEmitter(); |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
46 |
302
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
47 }; |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
48 |
302
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
49 /* When an Observer (B) simply needs to re-emit a message it has received, instead of implementing |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
50 * a specific member function to forward the message, it can create a MessageForwarder. |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
51 * The MessageForwarder will re-emit the message "in the name of (B)" |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
52 * |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
53 * Consider the chain where |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
54 * A is an observable |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
55 * | |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
56 * B is an observer of A and observable |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
57 * | |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
58 * C is an observer of B and knows that B is re-emitting many messages from A |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
59 * |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
60 * instead of implementing a callback, B will create a MessageForwarder that will emit the messages in his name: |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
61 * A.RegisterObserverCallback(new MessageForwarder<A::MessageType>(broker, *this) // where this is B |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
62 * |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
63 * in C: |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
64 * B.RegisterObserverCallback(new Callable<C, A:MessageTyper>(*this, &B::MyCallback)) // where this is C |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
65 */ |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
66 template<typename TMessage> |
302
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
67 class MessageForwarder : public IMessageForwarder, public IObserver, public Callable<MessageForwarder<TMessage>, TMessage> |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
68 { |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
69 public: |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
70 MessageForwarder(MessageBroker& broker, |
302
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
71 IObservable& emitter // the object that will emit the messages to forward |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
72 ) |
302
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
73 : IMessageForwarder(emitter), |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
74 IObserver(broker), |
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
75 Callable<MessageForwarder<TMessage>, TMessage>(*this, &MessageForwarder::ForwardMessage) |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
76 { |
302
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
77 RegisterForwarderInEmitter(); |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
78 } |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
79 |
302
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
80 protected: |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
81 void ForwardMessage(const TMessage& message) |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
82 { |
302
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
83 ForwardMessageInternal(message); |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
84 } |
302
4a79193ffb58
support for custom messages + no leaks in unit-tests
am@osimis.io
parents:
300
diff
changeset
|
85 |
300
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
86 }; |
b4abaeb783b1
messaging refactoring almost complete: works fine in native
am@osimis.io
parents:
diff
changeset
|
87 } |