private static void write_data(DDS.DynamicData sample, DDS.TypeCodeFactory tcf) { /* Creating typecodes */ DDS.TypeCode sequence_tc = sequence_get_typecode(tcf); if (sequence_tc == null) { Console.WriteLine("Error to create sequence_get_typecode in " + "writing_data"); return; } DDS.TypeCode sequence_element_tc = sequence_element_get_typecode(tcf); if (sequence_element_tc == null) { Console.WriteLine("Error to create sequence_element_get_typecode " + "in writing_data"); return; } try { /* Creating DynamicData */ DDS.DynamicData seq_member = new DDS.DynamicData(sequence_tc, DDS.DynamicData.DYNAMIC_DATA_PROPERTY_DEFAULT); DDS.DynamicData seq_element = new DDS.DynamicData(sequence_element_tc, DDS.DynamicData.DYNAMIC_DATA_PROPERTY_DEFAULT); for (int i = 0; i < MAX_SEQ_LEN; ++i) { /* To access the elements of a sequence it is necessary * to use their id. This parameter allows accessing to every * element of the sequence using a 1-based index. * There are two ways of doing this: bind API and get API. * See the NestedStructExample for further details about the * differences between these two APIs. */ #if (USE_BIND_API) seq_member.bind_complex_member(seq_element, null, i + 1); seq_element.set_int("a_member", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, i); Console.WriteLine("Writing sequence elemente #{0} : ", i + 1); seq_element.print(1); seq_member.unbind_complex_member(seq_element); #else seq_element.set_int("a_member", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, i); Console.WriteLine("Writing sequence element #{0}", i + 1); seq_element.print(1); seq_member.set_complex_member(null, i + 1, seq_element); #endif } sample.set_complex_member(sequence_member_name, DDS.DynamicData.MEMBER_ID_UNSPECIFIED, seq_member); /* Delete the created TC */ if (sequence_element_tc != null) { tcf.delete_tc(sequence_element_tc); } if (sequence_tc != null) { tcf.delete_tc(sequence_tc); } return; } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); return; } }
static void publish(int domain_id, int sample_count) { // --- Create participant --- // DDS.DomainParticipant participant = DDS.DomainParticipantFactory.get_instance().create_participant( domain_id, DDS.DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (participant == null) { shutdown(participant); throw new ApplicationException("create_participant error"); } // --- Create publisher --- // DDS.Publisher publisher = participant.create_publisher( DDS.DomainParticipant.PUBLISHER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (publisher == null) { shutdown(participant); throw new ApplicationException("create_publisher error"); } /* Create DynamicData using TypeCode from Shapes.cpp * If you are NOT using a type generated with rtiddsgen, you * need to create this TypeCode from scratch. */ DDS.TypeCode type_code = ShapeType.get_typecode(); if (type_code == null) { shutdown(participant); throw new ApplicationException("create_typecode error"); } /* Create the Dynamic data type support object */ DDS.DynamicDataTypeSupport type_support = new DDS.DynamicDataTypeSupport(type_code, new DDS.DynamicDataTypeProperty_t()); if (type_support == null) { shutdown(participant); throw new ApplicationException("create_type_support error"); } // --- Create topic --- // /* Register type before creating topic */ System.String type_name = EXAMPLE_TYPE_NAME; try { type_support.register_type(participant, EXAMPLE_TYPE_NAME); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* Make sure both publisher and subscriber share the same topic * name. * In the Shapes example: we are publishing a Square, which is the * topic name. If you want to publish other shapes (Triangle or * Circle), you just need to update the topic name. */ DDS.Topic topic = participant.create_topic( "Square", type_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic == null) { shutdown(participant); throw new ApplicationException("create_topic error"); } // --- Create writer --- // /* First, we create a generic DataWriter for our topic */ DDS.DataWriter writer = publisher.create_datawriter( topic, DDS.Publisher.DATAWRITER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (writer == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } /* Then, to use DynamicData, we need to assign the generic * DataWriter to a DynamicDataWriter, using a casting. The follow * casting should never fail. */ DDS.DynamicDataWriter DynamicData_writer = (DDS.DynamicDataWriter)writer; // --- Write --- // /* Create data sample for writing */ DDS.DynamicData data = type_support.create_data(); if (data == null) { shutdown(participant); throw new ApplicationException( "ShapeTypeTypeSupport.create_data error"); } /* For a data type that has a key, if the same instance is going to be * written multiple times, initialize the key here * and register the keyed instance prior to writing */ DDS.InstanceHandle_t instance_handle = DDS.InstanceHandle_t.HANDLE_NIL; /* * instance_handle = ShapeType_writer.register_instance(instance); */ /*** Shape direction variables ***/ int direction = 1; /* 1 means left to right and -1, right to left */ int x_position = 50; /* 50 is the initial position */ /* Initialize the DynamicData object */ data.set_string("color", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, "BLUE"); data.set_int("x", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, x_position); data.set_int("y", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, 100); data.set_int("shapesize", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, 30); /* Main loop */ const System.Int32 send_period = 100; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine("Sending shapesize {0}", 30 + (count % 20)); Console.WriteLine("Sending x position {0}", x_position); /* Modify the shapesize from 30 to 50 */ try { data.set_int("shapesize", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, 30 + (count % 20)); } catch (DDS.Exception e) { Console.WriteLine("{0}\nError writing shapesize = {1}", e, 30 + (count % 20)); } /* Modify the position */ try { data.set_int("x", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, x_position); } catch (DDS.Exception e) { Console.WriteLine("{0}\nError writing x_position = {1}", e, x_position); } /* The x_position will be modified adding or substracting * 2 to the previous x_position depending on the direction. */ x_position += (direction * 2); /* The x_position will stay between 50 and 150 pixels. * When the position is greater than 150 'direction' will be * negative (moving to the left) and when it is lower than 50 * 'direction' will be possitive (moving to the right). */ if (x_position >= 150) { direction = -1; } if (x_position <= 50) { direction = 1; } try { DynamicData_writer.write(data, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * ShapeType_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { type_support.delete_data(data); } catch (DDS.Exception e) { Console.WriteLine( "ShapeTypeTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }