Retrieve instance key after unregister

6 posts / 0 new
Last post
Offline
Last seen: 9 years 6 months ago
Joined: 03/04/2014
Posts: 15
Retrieve instance key after unregister

Hello, please I'd appreciate any help with this:

How can a DataReader retrieve the key value of an instance that has been unregistered by a DataWriter?


Thanks

Gerardo Pardo's picture
Offline
Last seen: 1 week 1 day ago
Joined: 06/02/2010
Posts: 602

Hello,

Strictly speaking the DataReader is not notified of "unregistrations". It is notified that an Instance has "No Alive DataWriters". If the DataWriter that unregistered the instance was the only DataWriter that was writing that instance (a common case), then the "unregistration" will cause the instance to have no "alive datawriters" and you will get the notification. Note that other situations can also cause this. For example if the DataWriter simply dissapears even if it does not explicitly unregister the instance.

The DataReader receives this notification via a Sample (as if regular data had arrived) except that the DDS_SampleInfo specifies that there is no "data" (i.e. SampleInfo::valid_data is false), and also the SampleInfo::instance_state is set to DDS_NOT_ALIVE_NO_WRITERS_INSTANCE_STATE.  When this happens the SampleInfo::instance_handle holds a reference to the Instance and you can get the associated key calling get DataReader::get_key_value() operation on the DataReader and passing that instance_handle. 

Gerardo

 

 

Offline
Last seen: 9 years 6 months ago
Joined: 03/04/2014
Posts: 15

hello Gerardo, 

wow! thanks for your quick response, it has been of great help

Offline
Last seen: 9 years 6 months ago
Joined: 03/04/2014
Posts: 15

Hello again!

the 'get_key_value' API states that "If Foo has a key and handle represents an instance of another type or an instance of type Foo that has been unregistered, this method will fail with DDS_RETCODE_BAD_PARAMETER."

So, will 'get_key_value' work as expected?

Thanks

 

 

Gerardo Pardo's picture
Offline
Last seen: 1 week 1 day ago
Joined: 06/02/2010
Posts: 602

Hello Roderick,

The way the documentation is written is a litlle bit confusing. The get_key_value will still work even if the instance has been unregistered as long as the "unregistration" sample still lives in the DataReader cache, which it does until after " take" it. So if you are reacting to the reception of the notification when you read/take the sample you are holding a valid instance_handle to a sample still in the DataReader cache so it all works.

What would not work--which is what the documentation was trying to say--would be this: Imagine you have the instance_handle stored somewhere in your application code, and then after the instance has been unregistered and the application has called " take" on that sample and returned from the take operation. Then if at a future point in time you were to just call get_key_value() using that instance handle you kept in your application memory the get_key_value() operation would fail.

A side, but perhaps important, note: Depending how you set your DataWriter WRITER_DATA_LIFECYCLE Qos Policy you may get a DDS_NOT_ALIVE_DISPOSED_INSTANCE_STATE instead of a DDS_NOT_ALIVE_NO_WRITERS_INSTANCE_STATE. I have seen this happen if you set the autodispose_unregistered_instances to TRUE and I have also seen this be the default Out-of-the box setting in some version of the product. I consider this a bit quirky so we may change it in future versions but it is something to watch out in your code.

Gerardo

Offline
Last seen: 9 years 6 months ago
Joined: 03/04/2014
Posts: 15

Hello Gerardo,

again, thanks for your accurate and extremely fast response. I've been doing some tests and everything works as you've said;-)

Rodrigo