Skip to content

Commit b67bf6e

Browse files
committed
fix messaging subscription scoping
1 parent f0524ce commit b67bf6e

File tree

2 files changed

+104
-32
lines changed

2 files changed

+104
-32
lines changed

src/architecture/messaging/messaging.h

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class ReadFunctor{
3838
messageType* payloadPointer; //!< -- pointer to the incoming msg data
3939
MsgHeader *headerPointer; //!< -- pointer to the incoming msg header
4040
bool initialized; //!< -- flag indicating if the input message is connect to another message
41+
void* sourceRef; // Generic pointer to store source reference
42+
void (*refIncCallback)(void*); // Function pointer for increment ref
43+
void (*refDecCallback)(void*); // Function pointer for decrement ref
4144

4245
public:
4346
//!< -- BSK Logging
@@ -46,10 +49,20 @@ class ReadFunctor{
4649

4750

4851
//! constructor
49-
ReadFunctor() : initialized(false) {};
52+
ReadFunctor() :
53+
initialized(false),
54+
sourceRef(nullptr),
55+
refIncCallback(nullptr),
56+
refDecCallback(nullptr) {}
5057

5158
//! constructor
52-
ReadFunctor(messageType* payloadPtr, MsgHeader *headerPtr) : payloadPointer(payloadPtr), headerPointer(headerPtr), initialized(true){};
59+
ReadFunctor(messageType* payloadPtr, MsgHeader *headerPtr) :
60+
payloadPointer(payloadPtr),
61+
headerPointer(headerPtr),
62+
initialized(true),
63+
sourceRef(nullptr),
64+
refIncCallback(nullptr),
65+
refDecCallback(nullptr) {}
5366

5467
//! constructor
5568
const messageType& operator()(){
@@ -174,6 +187,43 @@ class ReadFunctor{
174187

175188
//! Recorder method description
176189
Recorder<messageType> recorder(uint64_t timeDiff = 0){return Recorder<messageType>(this, timeDiff);}
190+
191+
/*! Set source reference and callback functions for reference counting
192+
* @param src Pointer to source object
193+
* @param incRef Function to increment reference count
194+
* @param decRef Function to decrement reference count
195+
*/
196+
void setSourceRef(void* src, void (*incRef)(void*), void (*decRef)(void*)) {
197+
if (sourceRef && refDecCallback) {
198+
refDecCallback(sourceRef);
199+
}
200+
sourceRef = src;
201+
refIncCallback = incRef;
202+
refDecCallback = decRef;
203+
if (sourceRef && refIncCallback) {
204+
refIncCallback(sourceRef);
205+
}
206+
}
207+
208+
/*! Check if this reader is subscribed to a specific message source
209+
* @param source Pointer to message source to check
210+
* @return 1 if subscribed, 0 if not
211+
*/
212+
uint8_t isSubscribedTo(const Message<messageType>* source) const {
213+
if (!source) return 0;
214+
return (this->payloadPointer == source->getPayloadPtr() &&
215+
this->headerPointer == source->getHeaderPtr());
216+
}
217+
218+
/*! Check if this reader is subscribed to a void pointer source
219+
* @param source Void pointer to message source to check
220+
* @return 1 if subscribed, 0 if not
221+
*/
222+
uint8_t isSubscribedTo(const void* source) const {
223+
const Message<messageType>* msgSource =
224+
static_cast<const Message<messageType>*>(source);
225+
return isSubscribedTo(msgSource);
226+
}
177227
};
178228

179229
/*! Write Functor */
@@ -232,6 +282,16 @@ class Message{
232282

233283
//! Return the memory size of the payload, be careful about dynamically sized things
234284
uint64_t getPayloadSize() {return sizeof(messageType);};
285+
286+
/*! Get pointer to message payload
287+
* @return Const pointer to payload data
288+
*/
289+
const messageType* getPayloadPtr() const { return &payload; }
290+
291+
/*! Get pointer to message header
292+
* @return Const pointer to message header
293+
*/
294+
const MsgHeader* getHeaderPtr() const { return &header; }
235295
};
236296

237297

src/architecture/messaging/newMessaging.ih

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,50 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3434

3535
%template(messageType ## Reader) ReadFunctor<messageTypePayload>;
3636
%extend ReadFunctor<messageTypePayload> {
37-
%pythoncode %{
38-
def subscribeTo(self, source):
39-
if type(source) == messageType:
40-
self.__subscribe_to(source)
37+
void setPySourceRef(PyObject* source) {
38+
if (source) {
39+
self->setSourceRef(source,
40+
[](void* ptr) { Py_INCREF((PyObject*)ptr); },
41+
[](void* ptr) { Py_DECREF((PyObject*)ptr); }
42+
);
43+
}
44+
}
45+
46+
%pythoncode %{
47+
def subscribeTo(self, source):
48+
if type(source) == messageType:
49+
self.__subscribe_to(source)
50+
self.setPySourceRef(source)
51+
return
52+
53+
try:
54+
from Basilisk.architecture.messaging.messageType ## Payload import messageType ## _C
55+
if type(source) == messageType ## _C:
56+
self.__subscribe_to_C(source)
57+
self.setPySourceRef(source)
4158
return
42-
43-
try:
44-
from Basilisk.architecture.messaging.messageType ## Payload import messageType ## _C
45-
if type(source) == messageType ## _C:
46-
self.__subscribe_to_C(source)
47-
return
48-
except ImportError:
49-
pass
50-
51-
raise Exception('tried to subscribe ReadFunctor<messageTypePayload> to output message type'
52-
+ str(type(source)))
53-
54-
55-
def isSubscribedTo(self, source):
56-
if type(source) == messageType:
57-
return self.__is_subscribed_to(source)
58-
59-
try:
60-
from Basilisk.architecture.messaging.messageType ## Payload import messageType ## _C
61-
except ImportError:
62-
return 0
63-
59+
except ImportError:
60+
pass
61+
62+
raise Exception('tried to subscribe ReadFunctor<messageTypePayload> to output message type'
63+
+ str(type(source)))
64+
%}
65+
66+
%pythoncode %{
67+
def isSubscribedTo(self, source):
68+
"""Check if this reader is subscribed to the given source"""
69+
if type(source) == messageType:
70+
return self.__is_subscribed_to(source)
71+
72+
try:
73+
from Basilisk.architecture.messaging.messageType ## Payload import messageType ## _C
6474
if type(source) == messageType ## _C:
6575
return self.__is_subscribed_to_C(source)
66-
else:
67-
return 0
68-
%}
76+
except ImportError:
77+
pass
78+
79+
return 0
80+
%}
6981
};
7082

7183
%template(messageType ## Writer) WriteFunctor<messageTypePayload>;
@@ -110,7 +122,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
110122
for el, k in zip(attr, range(len(attr))):
111123
self.explore_and_find_subattr(el, attr_name + "[" + str(k) + "]", content)
112124
else:
113-
# The attribute is a list of common types
125+
# The attribute is a list of common types
114126
content[attr_name] = attr
115127
elif "Basilisk" in str(type(attr)):
116128
# The attribute is a swigged BSK object

0 commit comments

Comments
 (0)