Skip to content

Commit b89cfb8

Browse files
author
Sam Raeburn
committed
CORE-13335: Fix examples that assume invalid data cannot be ALIVE
1 parent c1a6403 commit b89cfb8

File tree

5 files changed

+71
-72
lines changed

5 files changed

+71
-72
lines changed

examples/connext_dds/keyed_data/c++11/keys_subscriber.cxx

+14-15
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,23 @@ int process_data(dds::sub::DataReader<keys> reader)
3434
std::cout << "Instance " << sample.data().code()
3535
<< ", x: " << sample.data().x()
3636
<< ", y: " << sample.data().y() << std::endl;
37-
} else {
38-
// Since there is not valid data, it may include metadata.
39-
keys sample;
40-
reader.key_value(sample, info.instance_handle());
41-
42-
// Here we print a message if the instance state is
43-
// 'not_alive_no_writers' or 'not_alive_disposed'.
37+
} else {
38+
// A sample without valid data can be in any instance state, but the
39+
// key value is only available if it is ALIVE or NOT_ALIVE_DISPOSED
4440
const dds::sub::status::InstanceState &state =
4541
info.state().instance_state();
46-
if (state
47-
== dds::sub::status::InstanceState::not_alive_no_writers()) {
48-
std::cout << "Instance " << sample.code() << " has no writers"
49-
<< std::endl;
50-
} else if (
51-
state
52-
== dds::sub::status::InstanceState::not_alive_disposed()) {
53-
std::cout << "Instance " << sample.code() << " disposed"
42+
if (state == dds::sub::status::InstanceState::not_alive_no_writers()) {
43+
std::cout << "Instance is in NOT_ALIVE_NO_WRITERS instance state"
5444
<< std::endl;
45+
} else {
46+
// Since there is not valid data, it may include metadata.
47+
keys sample;
48+
reader.key_value(sample, info.instance_handle());
49+
std::cout << "Instance " << sample.code() << " is " <<
50+
((state == dds::sub::status::InstanceState::not_alive_disposed())
51+
? "disposed "
52+
: "alive ")
53+
<< std::endl;
5554
}
5655
}
5756

examples/connext_dds/keyed_data/c++98/keys_subscriber.cxx

+20-19
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,26 @@ unsigned int process_data(keysDataReader *typed_reader)
5555

5656
samples_read++;
5757
} else {
58-
/* Since there is not valid data, it may include metadata */
59-
keys dummy;
60-
retcode = typed_reader->get_key_value(
61-
dummy,
62-
info_seq[i].instance_handle);
63-
if (retcode != DDS_RETCODE_OK) {
64-
std::cout << "get_key_value error " << retcode << std::endl;
65-
continue;
66-
}
67-
68-
/* Here we print a message if the instance state is ALIVE_NO_WRITERS
69-
* or ALIVE_DISPOSED */
70-
if (info_seq[i].instance_state
71-
== DDS_NOT_ALIVE_NO_WRITERS_INSTANCE_STATE) {
72-
std::cout << "Instance " << dummy.code << " has no writers\n";
73-
} else if (
74-
info_seq[i].instance_state
75-
== DDS_NOT_ALIVE_DISPOSED_INSTANCE_STATE) {
76-
std::cout << "Instance " << dummy.code << " disposed\n";
58+
/*
59+
* An invalid sample can be in any instance state. The key value is
60+
* only available if it is ALIVE or NOT_ALIVE_DISPOSED.
61+
*/
62+
if (info_seq[i].instance_state == DDS_NOT_ALIVE_NO_WRITERS_INSTANCE_STATE) {
63+
std::cout << "Instance has no writers" << std::endl;
64+
} else {
65+
keys dummy;
66+
retcode = typed_reader->get_key_value(
67+
dummy,
68+
info_seq[i].instance_handle);
69+
if (retcode != DDS_RETCODE_OK) {
70+
std::cout << "get_key_value error " << retcode << std::endl;
71+
continue;
72+
}
73+
std::cout << "Instance " << dummy.code << " is "
74+
<< ((info_seq[i].instance_state == DDS_ALIVE_INSTANCE_STATE)
75+
? "alive"
76+
: "disposed")
77+
<< std::endl;
7778
}
7879
}
7980
/* End changes for Keyed_Data */

examples/connext_dds/keyed_data/c/keys_subscriber.c

+23-20
Original file line numberDiff line numberDiff line change
@@ -148,26 +148,29 @@ void keysListener_on_data_available(void *listener_data, DDS_DataReader *reader)
148148
printf("Instance %d: x: %d, y: %d\n", data->code, data->x, data->y);
149149

150150
} else {
151-
/* Since there is not valid data, it may include metadata */
152-
keys dummy;
153-
retcode = keysDataReader_get_key_value(
154-
keys_reader,
155-
&dummy,
156-
&info->instance_handle);
157-
if (retcode != DDS_RETCODE_OK) {
158-
printf("get_key_value error %d\n", retcode);
159-
continue;
160-
}
161-
162-
/* Here we print a message if the instance state is ALIVE_NO_WRITERS
163-
* or ALIVE_DISPOSED */
164-
if (info->instance_state
165-
== DDS_NOT_ALIVE_NO_WRITERS_INSTANCE_STATE) {
166-
printf("Instance %d has no writers\n", dummy.code);
167-
} else if (
168-
info->instance_state
169-
== DDS_NOT_ALIVE_DISPOSED_INSTANCE_STATE) {
170-
printf("Instance %d disposed\n", dummy.code);
151+
printf("No valid data for sample\n");
152+
/*
153+
* An invalid data sample can be in any instance state. The key
154+
* value may be available if the instance is either ALIVE or
155+
* NOT_ALIVE_DISPOSED.
156+
*/
157+
if (info->instance_state == DDS_NOT_ALIVE_NO_WRITERS_INSTANCE_STATE) {
158+
printf("Instance is in NOT_ALIVE_NO_WRITERS instance state\n");
159+
} else {
160+
keys key_value;
161+
retcode = keysDataReader_get_key_value(
162+
keys_reader,
163+
&key_value,
164+
&info->instance_handle);
165+
if (retcode != DDS_RETCODE_OK) {
166+
printf("get_key_value error %d\n", retcode);
167+
continue;
168+
}
169+
printf("Instance %d is in %s instance state\n",
170+
key_value.code,
171+
info->instance_state == DDS_NOT_ALIVE_DISPOSED_INSTANCE_STATE
172+
? "NOT_ALIVE_DISPOSED"
173+
: "ALIVE");
171174
}
172175
}
173176
/* End changes for Keyed_Data */

examples/connext_dds/keyed_data/java/keysPublisher.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private void runApplication()
105105
instance[2].code = 2;
106106

107107
// The keys must have been set before making this call
108-
System.out.println("Registering instance" + instance[0].code);
108+
System.out.println("Registering instance " + instance[0].code);
109109
handle[0] = writer.register_instance(instance[0]);
110110

111111
// Modify the data to be sent here

examples/connext_dds/keyed_data/java/keysSubscriber.java

+13-17
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,19 @@ private int processData()
6767
"Instance " + sample.code + ": x: " + sample.x
6868
+ ", y: " + sample.y + "\n");
6969
} else {
70-
// Since there is not valid data, it may include metadata
71-
keys dummy = new keys();
72-
reader.get_key_value(dummy, info.instance_handle);
73-
74-
// Here we print a message if the instance state is
75-
// ALIVE_NO_WRITERS or ALIVE_DISPOSED
76-
if (info.instance_state
77-
== InstanceStateKind
78-
.NOT_ALIVE_NO_WRITERS_INSTANCE_STATE) {
79-
System.out.print(
80-
"Instance " + dummy.code + " has no writers\n");
81-
} else if (
82-
info.instance_state
83-
== InstanceStateKind
84-
.NOT_ALIVE_DISPOSED_INSTANCE_STATE) {
85-
System.out.print(
86-
"Instance " + dummy.code + " disposed\n");
70+
if (info.instance_state ==
71+
InstanceStateKind.NOT_ALIVE_NO_WRITERS_INSTANCE_STATE) {
72+
System.out.print("Instance has no writers\n");
73+
} else {
74+
keys dummy = new keys();
75+
reader.get_key_value(dummy, info.instance_handle);
76+
if (info.instance_state ==
77+
InstanceStateKind.ALIVE_INSTANCE_STATE) {
78+
System.out.print("Instance " + dummy.code + " is ALIVE\n");
79+
} else if (info.instance_state ==
80+
InstanceStateKind.NOT_ALIVE_DISPOSED_INSTANCE_STATE) {
81+
System.out.print("Instance " + dummy.code + " is disposed\n");
82+
}
8783
}
8884
}
8985
samplesRead++;

0 commit comments

Comments
 (0)