annotate Framework/Messages/ICallable.h @ 973:38409549db43 toa2019082903

Log with addresses + added fingerprint mechanism to avoid calling zombie objects where: - a message is sent with a receiver - the receiver dies - another receiver with the SAME address is created - the message reply is executed --> execution on the wrong object! (since their "identity" is their address. The fix is to identify them with an UUID stored at creation time)
author Benjamin Golinvaux <bgo@osimis.io>
date Thu, 29 Aug 2019 18:07:55 +0200
parents f0008c55e5f7
children e75fd08d6c75
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
1 /**
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
2 * Stone of Orthanc
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
439
b70e9be013e4 preparing for 2019
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 299
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
6 *
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
10 * the License, or (at your option) any later version.
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
11 *
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
15 * Affero General Public License for more details.
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
16 *
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
19 **/
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
20
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
21
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
22 #pragma once
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
23
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
24 #include "IMessage.h"
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
25
973
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
26 #include <Core/Logging.h>
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
27
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
28 #include <boost/noncopyable.hpp>
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
29
973
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
30 #include <string>
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
31
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
32 namespace OrthancStone {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
33
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
34 class IObserver;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
35
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
36 // This is referencing an object and member function that can be notified
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
37 // by an IObservable. The object must derive from IO
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
38 // The member functions must be of type "void Function(const IMessage& message)" or reference a derived class of IMessage
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
39 class ICallable : public boost::noncopyable
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
40 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
41 public:
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
42 virtual ~ICallable()
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
43 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
44 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
45
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
46 virtual void Apply(const IMessage& message) = 0;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
47
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
48 virtual const MessageIdentifier& GetMessageIdentifier() = 0;
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
49
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
50 virtual IObserver* GetObserver() const = 0;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
51 };
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
52
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
53 template <typename TMessage>
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
54 class MessageHandler: public ICallable
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
55 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
56 };
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
57
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
58
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
59 template <typename TObserver,
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
60 typename TMessage>
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
61 class Callable : public MessageHandler<TMessage>
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
62 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
63 private:
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
64 typedef void (TObserver::* MemberFunction) (const TMessage&);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
65
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
66 TObserver& observer_;
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
67 MemberFunction function_;
973
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
68 std::string observerFingerprint_;
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
69
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
70 public:
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
71 Callable(TObserver& observer,
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
72 MemberFunction function) :
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
73 observer_(observer),
973
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
74 observerFingerprint_(observer.GetFingerprint()),
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
75 function_(function)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
76 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
77 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
78
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
79 void ApplyInternal(const TMessage& message)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
80 {
973
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
81 #if 0
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
82 (observer_.*function_) (message);
973
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
83 #else
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
84 if (observerFingerprint_ != observer_.GetFingerprint())
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
85 {
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
86 LOG(WARNING) << "The observer at address " << std::hex << &observer_ << std::dec << ") has a different fingerprint than the one recorded at callback registration time. Callback will NOT be sent!";
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
87 LOG(WARNING) << " recorded fingerprint = " << observerFingerprint_ << " current fingerprint = " << observer_.GetFingerprint();
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
88 }
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
89 else
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
90 {
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
91 (observer_.*function_) (message);
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
92 }
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
93 #endif
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
94 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
95
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
96 virtual void Apply(const IMessage& message)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
97 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
98 ApplyInternal(dynamic_cast<const TMessage&>(message));
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
99 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
100
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
101 virtual const MessageIdentifier& GetMessageIdentifier()
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
102 {
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
103 return TMessage::GetStaticIdentifier();
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
104 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
105
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
106 virtual IObserver* GetObserver() const
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
107 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
108 return &observer_;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
109 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
110 };
456
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
111
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
112 #if 0 /* __cplusplus >= 201103L*/
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
113
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
114 #include <functional>
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
115
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
116 template <typename TMessage>
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
117 class LambdaCallable : public MessageHandler<TMessage>
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
118 {
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
119 private:
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
120
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
121 IObserver& observer_;
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
122 std::function<void (const TMessage&)> lambda_;
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
123
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
124 public:
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
125 LambdaCallable(IObserver& observer,
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
126 std::function<void (const TMessage&)> lambdaFunction) :
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
127 observer_(observer),
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
128 lambda_(lambdaFunction)
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
129 {
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
130 }
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
131
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
132 virtual void Apply(const IMessage& message)
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
133 {
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
134 lambda_(dynamic_cast<const TMessage&>(message));
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
135 }
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
136
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
137 virtual IObserver* GetObserver() const
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
138 {
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
139 return &observer_;
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
140 }
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
141 };
b70fcc134ba4 moved LambdaCallable (currently still disable because it does not work in WASM)
Alain Mazy <alain@mazy.be>
parents: 439
diff changeset
142 #endif //__cplusplus >= 201103L
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
143 }