static void publish(int domain_id, int sample_count) { // --- Create participant --- // /* To customize participant QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 --- // /* To customize publisher QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 topic --- // /* Register type before creating topic */ System.String type_name = ChatObjectTypeSupport.get_type_name(); try { ChatObjectTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( My.CHAT_TOPIC_NAME.VALUE, /*>>><<<*/ 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 --- // /* To customize data writer QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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"); } ChatObjectDataWriter ChatObject_writer = (ChatObjectDataWriter)writer; // --- Write --- // /* Create data sample for writing */ ChatObject instance = ChatObjectTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "ChatObjectTypeSupport.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.user = "******"; // instance_handle = ChatObject_writer.register_instance(instance); /* <<< */ /* Main loop */ const System.Int32 send_period = 4000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine("Writing ChatObject, count {0}", count); /* Modify the data to be sent here */ /* >>> */ instance.msg = "Hello " + count; /* <<< */ try { ChatObject_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * ChatObject_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { ChatObjectTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "ChatObjectTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }
static void publish(int domain_id, int sample_count) { // --- Register userGenerated datatype --- DDS.DomainParticipantFactory.get_instance(). register_type_support( ChatObjectTypeSupport.get_instance(), "My::Type::Chat::Obj"); // --- Create participant --- // /* To customize the participant QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DomainParticipant participant = DDS.DomainParticipantFactory.get_instance(). create_participant_from_config( "MyParticipant_Library::MyPublicationParticipant"); if (participant == null) { shutdown(participant); throw new ApplicationException("create_participant error"); } // --- Lookup writer --- // /* To customize data writer QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DataWriter writer = participant.lookup_datawriter_by_name( "MyPublisher::ChatObjectWriter"); if (writer == null) { shutdown(participant); throw new ApplicationException("lookup_datawriter error"); } ChatObjectDataWriter ChatObject_writer = (ChatObjectDataWriter)writer; // --- Write --- // /* Create data sample for writing */ ChatObject instance = ChatObjectTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "ChatObjectTypeSupport.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.user = "******"; // instance_handle = ChatObject_writer.register_instance(instance); /* <<< */ /* Main loop */ const System.Int32 send_period = 4000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine("Writing ChatObject, count {0}", count); /* Modify the data to be sent here */ /* >>> */ instance.msg = "Hello " + count; /* <<< */ try { ChatObject_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * ChatObject_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { ChatObjectTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "ChatObjectTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }