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 = ccfTypeSupport.get_type_name(); try { ccfTypeSupport.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( "Example ccf", 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"); } /* Start changes for Custom_Content_Filter */ /* Create and register custom filter */ custom_filter_type custom_filter = new custom_filter_type(); try { participant.register_contentfilter("Customfilter", custom_filter); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } /* End changes for Custom_Content_Filter */ // --- 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"); } ccfDataWriter ccf_writer = (ccfDataWriter)writer; // --- Write --- // /* Create data sample for writing */ ccf instance = ccfTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "ccfTypeSupport.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 = ccf_writer.register_instance(instance); */ /* Main loop */ const System.Int32 send_period = 1000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine("Writing ccf, count {0}", count); /* Modify the data to be sent here */ instance.x = count; try { ccf_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * ccf_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { ccfTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "ccfTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }
static void publish(int domain_id, int sample_count, int turbo_mode_on) { System.String profile_name = null; System.String library_name = "batching_Library"; /* We pick the profile name if the turbo_mode is selected or not. * If Turbo_mode is not selected, the batching profile will be used. */ if (turbo_mode_on == 1) { profile_name = "turbo_mode_profile"; Console.WriteLine("Turbo Mode enable"); } else { profile_name = "batch_profile"; Console.WriteLine("Manual batching enable"); } // --- Create participant --- // /* To customize participant QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DomainParticipant participant = DDS.DomainParticipantFactory.get_instance(). create_participant_with_profile( domain_id, library_name, profile_name, 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_with_profile( library_name, profile_name, 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 = batch_dataTypeSupport.get_type_name(); try { batch_dataTypeSupport.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( "Example batch_data", 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_with_profile( topic, library_name, profile_name, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (writer == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } batch_dataDataWriter batch_data_writer = (batch_dataDataWriter)writer; // --- Write --- // /* Create data sample for writing */ batch_data instance = batch_dataTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "batch_dataTypeSupport.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 = batch_data_writer.register_instance(instance); */ /* Main loop */ System.Int32 send_period = 1000; // milliseconds if (turbo_mode_on == 1) { send_period = 0; } for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine("Writing batch_data, count {0}", count); /* Modify the data to be sent here */ instance.x = count; try { batch_data_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * batch_data_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { batch_dataTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "batch_dataTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }
static void subscribe(int domain_id) { // --- Create participant --- // /* To customize the 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 subscriber --- // /* To customize the subscriber QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Subscriber subscriber = participant.create_subscriber( DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (subscriber == null) { shutdown(participant); throw new ApplicationException("create_subscriber error"); } // --- Create topic --- // /* Register the type before creating the topic */ System.String atype_name = AccidentTypeSupport.get_type_name(); System.String ptype_name = PositionTypeSupport.get_type_name(); try { AccidentTypeSupport.register_type( participant, atype_name); PositionTypeSupport.register_type( participant, ptype_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize the topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic atopic = participant.create_topic( "P3464_BHANDERSON: PT/ALR/ACC", atype_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); DDS.Topic ptopic = participant.create_topic( "P3464_BHANDERSON: PT/POS", ptype_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (atopic == null || ptopic == null) { shutdown(participant); throw new ApplicationException("create_topic error"); } // --- Create reader --- // /* Create a data reader listener */ AccidentListener reader_listener = new AccidentListener(); /* To customize the data reader QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DataReader areader = subscriber.create_datareader( atopic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, reader_listener, DDS.StatusMask.STATUS_MASK_ALL); DDS.DataReader preader = subscriber.create_datareader( ptopic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, reader_listener, DDS.StatusMask.STATUS_MASK_ALL); if (areader == null || preader == null) { shutdown(participant); reader_listener = null; throw new ApplicationException("create_datareader error"); } // --- Wait for data --- // /* Main loop */ const System.Int32 receive_period = 4000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine( "Subscriber sleeping for {0} sec...", receive_period / 1000); System.Threading.Thread.Sleep(receive_period); } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); reader_listener = null; }
static void subscribe(int domain_id, int sample_count, int sel_cft) { // --- Create participant --- // /* To customize the 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 subscriber --- // /* To customize the subscriber QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Subscriber subscriber = participant.create_subscriber( DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (subscriber == null) { shutdown(participant); throw new ApplicationException("create_subscriber error"); } // --- Create topic --- // /* Register the type before creating the topic */ System.String type_name = cftTypeSupport.get_type_name(); try { cftTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize the topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( "Example cft", 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"); } /* Sequence of parameters for the content filter expression */ DDS.StringSeq parameters = new DDS.StringSeq(2); /* The default parameter list that we will include in the * sequence of parameters will be "1","4" (i.e., 1 <= x <= 4). */ DDS.StringWrapper[] param_list = new DDS.StringWrapper[2] { "1", "4" }; parameters.from_array(param_list); /* Create the content filtered topic in case sel_cft * is true. * The Content Filter Expresion has two parameters: * - %0 -- x must be greater or equal than %0. * - %1 -- x must be less or equal than %1. */ DDS.ContentFilteredTopic cft = null; if (sel_cft != 0) { cft = participant.create_contentfilteredtopic( "ContentFilteredTopic", topic, "(x >= %0 and x <= %1)", parameters); if (cft == null) { shutdown(participant); throw new ApplicationException( "create_contentfilteredtopic error"); } } // --- Create reader --- // /* Create a data reader listener */ cftListener reader_listener = new cftListener(); /* Here we create the reader either using a Content Filtered Topic or * a normal topic */ DDS.DataReader reader = null; if (sel_cft != 0) { Console.WriteLine("Using ContentFiltered Topic"); reader = subscriber.create_datareader(cft, DDS.Subscriber.DATAREADER_QOS_DEFAULT, reader_listener, DDS.StatusMask.STATUS_MASK_ALL); } else { Console.WriteLine("Using Normal Topic"); reader = subscriber.create_datareader(topic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, reader_listener, DDS.StatusMask.STATUS_MASK_ALL); } if (reader == null) { shutdown(participant); reader_listener = null; throw new ApplicationException("create_datareader error"); } /* If you want to set the reliability and history QoS settings * programmatically rather than using the XML, you will need to add * the following lines to your code and comment out the * create_datareader calls above. */ /* * DDS.DataReaderQos datareader_qos = new DDS.DataReaderQos(); * try { * subscriber.get_default_datareader_qos(datareader_qos); * } catch (DDS.Exception e) { * Console.WriteLine("get_default_datareader_qos error {0}", e); * shutdown(participant); * throw e; * } * * datareader_qos.reliability.kind = * DDS.ReliabilityQosPolicyKind.RELIABLE_RELIABILITY_QOS; * datareader_qos.durability.kind = * DDS.DurabilityQosPolicyKind.TRANSIENT_LOCAL_DURABILITY_QOS; * datareader_qos.history.kind = * DDS.HistoryQosPolicyKind.KEEP_LAST_HISTORY_QOS; * datareader_qos.history.depth = 20; * * if (sel_cft != 0) { * Console.WriteLine("Using ContentFiltered Topic"); * reader = subscriber.create_datareader(cft, * datareader_qos, reader_listener, * DDS.StatusMask.STATUS_MASK_ALL); * } else { * Console.WriteLine("Using Normal Topic"); * reader = subscriber.create_datareader(topic, * datareader_qos, reader_listener, * DDS.StatusMask.STATUS_MASK_ALL); * } * * if (reader == null) { * shutdown(participant); * reader_listener = null; * throw new ApplicationException("create_datareader error"); * } * */ if (sel_cft != 0) { Console.WriteLine("\n=========================="); Console.WriteLine("Using CFT\nFilter: 1 <= x <= 4"); Console.WriteLine("=========================="); } // --- Wait for data --- // /* Main loop */ const System.Int32 receive_period = 1000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine( "cft subscriber sleeping for {0} sec...", receive_period / 1000); System.Threading.Thread.Sleep(receive_period); if (sel_cft == 0) { continue; } if (count == 10) { Console.WriteLine("\n=========================="); Console.WriteLine("Changing filter parameters"); Console.WriteLine("Filter: 5 <= x <= 9"); Console.WriteLine("==========================="); parameters.set_at(0, "5"); parameters.set_at(1, "9"); try { cft.set_expression_parameters(parameters); } catch (DDS.Exception e) { Console.WriteLine("set_expression_parameters error {0}", e); shutdown(participant); throw e; } } else if (count == 20) { Console.WriteLine("\n=========================="); Console.WriteLine("Changing filter parameters"); Console.WriteLine("Filter: 3 <= x <= 9"); Console.WriteLine("==========================="); DDS.StringSeq oldParameters = new DDS.StringSeq(); cft.get_expression_parameters(oldParameters); oldParameters.set_at(0, "3"); try { cft.set_expression_parameters(oldParameters); } catch (DDS.Exception e) { Console.WriteLine("set_expression_parameters error {0}", e); shutdown(participant); throw e; } } } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); reader_listener = null; }
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_with_profile( "ordered_Library", "ordered_Profile_subscriber_instance", null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (publisher == null) { shutdown(participant); throw new ApplicationException("create_publisher error"); } /* If you want to change the Publisher's QoS programmatically rather * than using the XML file, you will need to add the following lines to * your code and comment out the create_publisher call above. * * In this case, we set the presentation publish mode ordered in the * topic. */ /* Get default publisher QoS to customize */ /* DDS.PublisherQos publisher_qos = new DDS.PublisherQos(); * participant.get_default_publisher_qos(publisher_qos); * * publisher_qos.presentation.access_scope = * DDS.PresentationQosPolicyAccessScopeKind.TOPIC_PRESENTATION_QOS; * publisher_qos.presentation.ordered_access = true; * * // --- Create publisher --- // * * DDS.Publisher publisher = participant.create_publisher( * publisher_qos, null, 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 = orderedTypeSupport.get_type_name(); try { orderedTypeSupport.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( "Example ordered", 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"); } orderedDataWriter ordered_writer = (orderedDataWriter)writer; /* Start changes for Ordered_Presentation */ /* Create two instances */ ordered instance0 = null; ordered instance1 = null; DDS.InstanceHandle_t handle0 = DDS.InstanceHandle_t.HANDLE_NIL; DDS.InstanceHandle_t handle1 = DDS.InstanceHandle_t.HANDLE_NIL; // --- Write --- // /* Create data sample for writing */ instance0 = orderedTypeSupport.create_data(); if (instance0 == null) { shutdown(participant); throw new ApplicationException( "orderedTypeSupport.create_data error"); } instance1 = orderedTypeSupport.create_data(); if (instance1 == null) { shutdown(participant); throw new ApplicationException( "orderedTypeSupport.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 */ instance0.id = 0; instance1.id = 1; handle0 = ordered_writer.register_instance(instance0); handle1 = ordered_writer.register_instance(instance1); /* Main loop */ const System.Int32 send_period = 1000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { instance0.value = count; instance1.value = count; Console.WriteLine("writing instance0, value->{0}", instance0.value); try { ordered_writer.write(instance0, ref handle0); } catch (DDS.Exception e) { Console.WriteLine("write instance0 error {0}", e); } Console.WriteLine("writing instance1, value->{0}", instance1.value); try { ordered_writer.write(instance1, ref handle1); } catch (DDS.Exception e) { Console.WriteLine("write instance1 error {0}", e); } System.Threading.Thread.Sleep(send_period); } try { ordered_writer.unregister_instance( instance0, ref handle0); } catch (DDS.Exception e) { Console.WriteLine("unregister instance0 error: {0}", e); } try { ordered_writer.unregister_instance( instance1, ref handle1); } catch (DDS.Exception e) { Console.WriteLine("unregister instance1 error: {0}", e); } // --- Shutdown --- // /* Delete data sample */ try { orderedTypeSupport.delete_data(instance0); } catch (DDS.Exception e) { Console.WriteLine( "orderedTypeSupport.delete_data0 error: {0}", e); } try { orderedTypeSupport.delete_data(instance1); } catch (DDS.Exception e) { Console.WriteLine( "orderedTypeSupport.delete_data1 error: {0}", e); } /* Delete all entities */ shutdown(participant); }
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 = asyncTypeSupport.get_type_name(); try { asyncTypeSupport.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( "Example async", 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"); } /* 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"); } /* If you want to change the DataWriter's QoS programmatically rather than * using the XML file, you will need to add the following lines to your * code and comment out the create_datawriter call above. * * In this case, we set the publish mode qos to asynchronous publish mode, * which enables asynchronous publishing. We also set the flow controller * to be the fixed rate flow controller, and we increase the history depth. */ // /* Get default datawriter QoS to customize */ // DDS.DataWriterQos datawriter_qos = new DDS.DataWriterQos(); // try { // publisher.get_default_datawriter_qos(datawriter_qos); // } // catch (DDS.Exception e) // { // Console.WriteLine("get_default_datawriter_qos error {0}", e); // shutdown(participant); // throw e; // } ///* Since samples are only being sent once per second, datawriter will need // * to keep them on queue. History defaults to only keeping the last // * sample enqueued, so we increase that here. // */ // datawriter_qos.history.depth = 12; // /* Set flowcontroller for datawriter */ // datawriter_qos.publish_mode.kind = // DDS.PublishModeQosPolicyKind.ASYNCHRONOUS_PUBLISH_MODE_QOS; // datawriter_qos.publish_mode.flow_controller_name = // DDS.FlowController.FIXED_RATE_FLOW_CONTROLLER_NAME; // // --- Create writer --- // // /* To create datawriter with default QoS, use DDS_DATAWRITER_QOS_DEFAULT //instead of datawriter_qos */ // DDS.DataWriter writer = publisher.create_datawriter( // topic, // datawriter_qos, // null /* listener */, // DDS.StatusMask.STATUS_MASK_NONE); // if (writer == null) { // shutdown(participant); // throw new ApplicationException("create_datawriter error"); // } asyncDataWriter async_writer = (asyncDataWriter)writer; // --- Write --- // /* Create data sample for writing */ async instance = asyncTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "asyncTypeSupport.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 = async_writer.register_instance(instance); */ /* Main loop */ const System.Int32 send_period = 1000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine("Writing async, count {0}", count); /* Modify the data to be sent here */ instance.x = count; try { async_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * async_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { asyncTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "asyncTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }
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 = market_dataTypeSupport.get_type_name(); try { market_dataTypeSupport.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( "Example market_data", 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"); } /* If you want to change the DataWriter's QoS programmatically rather * than using the XML file, you will need to add the following lines to * your code and modify the datawriter creation fuction using writer_qos * * In this case, we set the publish as multichannel using the differents * channel to send differents symbol. Every channel have a IP to send * the data. */ /* Start changes for MultiChannel */ /* * DDS.DataWriterQos writer_qos = new DDS.DataWriterQos(); * try { * publisher.get_default_datawriter_qos(writer_qos); * } catch (DDS.Exception e) { * Console.WriteLine("get_default_datawriter_qos error {0}", e); * shutdown(participant); * throw e; * } */ /* Create 8 channels based on Symbol */ /* * writer_qos.multi_channel.channels.ensure_length(8, 8); * System.Int32 length = writer_qos.multi_channel.channels.length; * for (int i = 0; i < length; i++) { * DDS.ChannelSettings_t channelSetting = * new DDS.ChannelSettings_t(); * writer_qos.multi_channel.channels.set_at(i, channelSetting); * DDS.TransportMulticastSettingsSeq multiCastSettingSeq = * new DDS.TransportMulticastSettingsSeq(1); * writer_qos.multi_channel.channels.get_at(i). * multicast_settings = multiCastSettingSeq; * writer_qos.multi_channel.channels.get_at(i). * multicast_settings.ensure_length(1, 1); * DDS.TransportMulticastSettings_t multicastSetting = * new DDS.TransportMulticastSettings_t(); * writer_qos.multi_channel.channels.get_at(i). * multicast_settings.set_at(0, multicastSetting); * } * writer_qos.multi_channel.channels.get_at(0). * filter_expression = "Symbol MATCH '[A-C]*'"; * writer_qos.multi_channel.channels.get_at(0). * multicast_settings.ensure_length(1, 1); * writer_qos.multi_channel.channels.get_at(0). * multicast_settings.get_at(0).receive_address = "239.255.0.2"; * writer_qos.multi_channel.channels.get_at(1). * filter_expression = "Symbol MATCH '[D-F]*'"; * writer_qos.multi_channel.channels.get_at(1). * multicast_settings.ensure_length(1, 1); * writer_qos.multi_channel.channels.get_at(1). * multicast_settings.get_at(0).receive_address = "239.255.0.3"; * writer_qos.multi_channel.channels.get_at(2). * filter_expression = "Symbol MATCH '[G-I]*'"; * writer_qos.multi_channel.channels.get_at(2). * multicast_settings.ensure_length(1, 1); * writer_qos.multi_channel.channels.get_at(2). * multicast_settings.get_at(0).receive_address = "239.255.0.4"; * writer_qos.multi_channel.channels.get_at(3). * filter_expression = "Symbol MATCH '[J-L]*'"; * writer_qos.multi_channel.channels.get_at(3). * multicast_settings.ensure_length(1, 1); * writer_qos.multi_channel.channels.get_at(3). * multicast_settings.get_at(0).receive_address = "239.255.0.5"; * writer_qos.multi_channel.channels.get_at(4). * filter_expression = "Symbol MATCH '[M-O]*'"; * writer_qos.multi_channel.channels.get_at(4). * multicast_settings.ensure_length(1, 1); * writer_qos.multi_channel.channels.get_at(4). * multicast_settings.get_at(0).receive_address = "239.255.0.6"; * writer_qos.multi_channel.channels.get_at(5). * filter_expression = "Symbol MATCH '[P-S]*'"; * writer_qos.multi_channel.channels.get_at(5). * multicast_settings.ensure_length(1, 1); * writer_qos.multi_channel.channels.get_at(5). * multicast_settings.get_at(0).receive_address = "239.255.0.7"; * writer_qos.multi_channel.channels.get_at(6). * filter_expression = "Symbol MATCH '[T-V]*'"; * writer_qos.multi_channel.channels.get_at(6). * multicast_settings.ensure_length(1, 1); * writer_qos.multi_channel.channels.get_at(6). * multicast_settings.get_at(0).receive_address = "239.255.0.8"; * writer_qos.multi_channel.channels.get_at(7). * filter_expression = "Symbol MATCH '[W-Z]*'"; * writer_qos.multi_channel.channels.get_at(7). * multicast_settings.ensure_length(1, 1); * writer_qos.multi_channel.channels.get_at(7). * multicast_settings.get_at(0).receive_address = "239.255.0.9"; */ // --- Create writer --- // /* To customize data writer QoS, use * the configuration file USER_QOS_PROFILES.xml */ /* toggle between writer_qos and DDS_DATAWRITER_QOS_DEFAULT to alternate * between using code and using XML to specify the Qos */ DDS.DataWriter writer = publisher.create_datawriter( topic, /* writer_qos */ DDS.Publisher.DATAWRITER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (writer == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } market_dataDataWriter market_data_writer = (market_dataDataWriter)writer; /* End changes for MultiChannel */ // --- Write --- // /* Create data sample for writing */ market_data instance = market_dataTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "market_dataTypeSupport.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 = market_data_writer.register_instance(instance); */ /* Changes for MultiChannel */ const System.Int32 send_period = 100; // milliseconds /* Main loop */ for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { /* Console.WriteLine("Writing market_data, count {0}", count); */ /* Changes for MultiChannel */ /* Modify the data to be sent here */ char[] SymbolBuffer = { (char)('A' + (char)(count % 26)) }; String Symbol = new String(SymbolBuffer); instance.Symbol = Symbol; instance.Price = count; try { market_data_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * market_data_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { market_dataTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "market_dataTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }
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 --- // /* Start - modifying the generated example to showcase the usage of * the get_publishers API */ /* We create 2 publishers */ 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"); } /* We use the publisher.GetHashCode() to identify the publisher ID */ Console.WriteLine("The first publishers is " + publisher.GetHashCode()); DDS.Publisher publisher2 = participant.create_publisher( DDS.DomainParticipant.PUBLISHER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (publisher2 == null) { shutdown(participant); throw new ApplicationException("create_publisher2 error"); } Console.WriteLine("The second publishers is " + publisher2.GetHashCode()); Console.WriteLine("Let's call get_publisher() now and check if " + "I get all my publishers..."); DDS.PublisherSeq publisherSeq = new DDS.PublisherSeq(); participant.get_publishers(publisherSeq); for (int count = 0; count < publisherSeq.length; ++count) { DDS.Publisher tmp = publisherSeq.get_at(count); Console.WriteLine("The {0} publisher I found is: {1}", count, tmp.GetHashCode()); } /* * try { * Foo_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete all entities */ shutdown(participant); }
static void subscribe(int domain_id, int sample_count) { // --- Create participant --- // /* To customize the 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 subscriber --- // /* To customize the subscriber QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Subscriber subscriber = participant.create_subscriber( DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (subscriber == null) { shutdown(participant); throw new ApplicationException("create_subscriber error"); } // --- Create topic --- // /* Register the type before creating the topic */ System.String type_name = pollTypeSupport.get_type_name(); try { pollTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize the topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( "Example poll", 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 reader --- // /* To customize the data reader QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DataReader reader = subscriber.create_datareader( topic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, null, DDS.StatusMask.STATUS_MASK_NONE); if (reader == null) { shutdown(participant); throw new ApplicationException("create_datareader error"); } /* If you want to change datareader_qos.history.kind programmatically rather * than using the XML file, you will need to add the following lines to your * code and comment out the create_datareader call above. */ /*DDS.DataReaderQos datareader_qos = new DDS.DataReaderQos(); * * try * { * subscriber.get_default_datareader_qos(datareader_qos); * } * catch (DDS.Exception e) * { * Console.WriteLine("get_default_datareader_qos error {0}", e); * shutdown(participant); * throw e; * } * * datareader_qos.history.kind = * DDS.HistoryQosPolicyKind.KEEP_ALL_HISTORY_QOS; * * DDS.DataReader reader = subscriber.create_datareader( * topic, datareader_qos, null, * DDS.StatusMask.STATUS_MASK_ALL); * if (reader == null) { * shutdown(participant); * throw new ApplicationException("create_datareader error"); * } */ pollDataReader poll_reader = (pollDataReader)reader; // --- Wait for data --- // DDS.SampleInfoSeq info_seq = new DDS.SampleInfoSeq(); pollSeq data_seq = new pollSeq(); /* Main loop */ const System.Int32 receive_period = 5000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine( "poll subscriber sleeping for {0} sec...", receive_period / 1000); System.Threading.Thread.Sleep(receive_period); // --- Polling for data --- /// /* Check for new data calling the DataReader's take() method */ try { poll_reader.take( data_seq, info_seq, DDS.ResourceLimitsQosPolicy.LENGTH_UNLIMITED, DDS.SampleStateKind.ANY_SAMPLE_STATE, DDS.ViewStateKind.ANY_VIEW_STATE, DDS. InstanceStateKind.ANY_INSTANCE_STATE); } catch (DDS.Retcode_NoData) { // Not an error return; } catch (DDS.Exception e) { Console.WriteLine("take error {0}", e); return; } int len = 0; double sum = 0; /* Iterate through the samples read using the take() method, getting * the number of samples got and, adding the value of x on each of * them to calculate the average afterwards. */ for (int i = 0; i < data_seq.length; ++i) { if (!info_seq.get_at(i).valid_data) { continue; } len++; sum += data_seq.get_at(i).x; } if (len > 0) { Console.WriteLine("Got {0} samples. Avg = {1}", len, sum / len); } try { poll_reader.return_loan(data_seq, info_seq); } catch (DDS.Exception e) { Console.WriteLine("return loan error {0}", e); } } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); }
// This gets called when a participant has been discovered public override void on_data_available(DDS.DataReader reader) { DDS.ParticipantBuiltinTopicDataDataReader builtin_reader = (DDS.ParticipantBuiltinTopicDataDataReader)reader; string participant_data; DDS.ParticipantBuiltinTopicData cur_participant_builtin_topic_data; try { // We only process newly seen participants builtin_reader.take( data_seq, info_seq, DDS.ResourceLimitsQosPolicy.LENGTH_UNLIMITED, DDS.SampleStateKind.ANY_SAMPLE_STATE, DDS.ViewStateKind.NEW_VIEW_STATE, DDS.InstanceStateKind.ANY_INSTANCE_STATE); for (int i = 0; i < data_seq.length; ++i) { DDS.SampleInfo info = (DDS.SampleInfo)info_seq.get_at(i); if (info.valid_data) { participant_data = "nil"; bool is_auth = false; cur_participant_builtin_topic_data = (DDS.ParticipantBuiltinTopicData) data_seq.get_at(i); // see if there is any participant_data if (cur_participant_builtin_topic_data. user_data.value.length > 0) { participant_data = System.Text.Encoding.ASCII.GetString( cur_participant_builtin_topic_data. user_data.value.buffer, 0, cur_participant_builtin_topic_data. user_data.value.length); is_auth = (participant_data == auth); } Console.WriteLine( "Built-in Reader: found participant \n\tkey->'" + cur_participant_builtin_topic_data.key.GetHashCode() + "'\n\tuser_data->'" + participant_data + "'"); Console.WriteLine( "instance_handle: " + info.instance_handle); if (!is_auth) { Console.WriteLine( "Bad authorization, ignoring participant"); DDS.DomainParticipant participant = reader.get_subscriber().get_participant(); DDS.InstanceHandle_t temp = info.instance_handle; participant.ignore_participant(ref temp); info.instance_handle = temp; } } } } catch (DDS.Retcode_NoData) { // No data to process // This happens when we get announcements from participants we // already know about return; } finally { builtin_reader.return_loan(data_seq, info_seq); } }
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); }
static void publish(int domain_id, int sample_count) { // --- Create participant --- // /* Start changes for Builtin_Topics */ /* If you want to change the Factory's QoS programmatically rather * than using the XML file, you will need to add the following lines to * your code and comment out the participant call above. */ /* By default, the participant is enabled upon construction. * At that time our listeners for the builtin topics have not * been installed, so we disable the participant until we * set up the listeners. */ /* * DDS.DomainParticipantFactoryQos factory_qos = * new DDS.DomainParticipantFactoryQos(); * DDS.DomainParticipantFactory.get_instance().get_qos(factory_qos); * factory_qos.entity_factory.autoenable_created_entities = false; * DDS.DomainParticipantFactory.get_instance().set_qos(factory_qos); * */ // If you want to change the Participant's QoS programmatically // rather than using the XML file, you will need to uncomment the // following lines and replace in create_participant the argument // DDS.DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT with // participant_qos //DDS.DomainParticipantQos participant_qos = // new DDS.DomainParticipantQos(); //DDS.DomainParticipantFactory.get_instance(). // get_default_participant_qos(participant_qos); //participant_qos.resource_limits.participant_user_data_max_length = 1024; /* 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 Exception("create_participant error"); } // Installing listeners for the builting topics requires several steps // First get the builting subscriber DDS.Subscriber builtin_subscriber = participant.get_builtin_subscriber(); if (builtin_subscriber == null) { shutdown(participant); throw new Exception("create_builtin_subscriber error"); } /* Then get builtin subscriber's datareader for participants * The type name is a bit hairy, but can be read right to left: * DDS.ParticipantBuiltinTopicDataDataReader is a * DataReader for BuiltinTopicData concerning a discovered * Participant */ DDS.ParticipantBuiltinTopicDataDataReader builtin_participant_datareader = (DDS.ParticipantBuiltinTopicDataDataReader) builtin_subscriber.lookup_datareader( DDS.ParticipantBuiltinTopicDataTypeSupport. PARTICIPANT_TOPIC_NAME); // Install our listener BuiltinParticipantListener builtin_participant_listener = new BuiltinParticipantListener(); try { builtin_participant_datareader.set_listener( builtin_participant_listener, (DDS.StatusMask.STATUS_MASK_NONE | (DDS.StatusMask)DDS.StatusKind.DATA_AVAILABLE_STATUS)); } catch (DDS.Exception e) { shutdown(participant); Console.WriteLine("set_listener error: {0}", e); } // Get builtin subscriber's datareader for subscribers DDS.SubscriptionBuiltinTopicDataDataReader builtin_subscription_datareader = (DDS.SubscriptionBuiltinTopicDataDataReader) builtin_subscriber.lookup_datareader( DDS.SubscriptionBuiltinTopicDataTypeSupport. SUBSCRIPTION_TOPIC_NAME); if (builtin_participant_datareader == null) { shutdown(participant); throw new Exception("lookup_datareader error"); } // Install our listener BuiltinSubscriberListener builtin_subscriber_listener = new BuiltinSubscriberListener(); builtin_subscription_datareader.set_listener( builtin_subscriber_listener, (DDS.StatusMask.STATUS_MASK_NONE | (DDS.StatusMask)DDS.StatusKind.DATA_AVAILABLE_STATUS)); /* Done! All the listeners are installed, so we can enable the * participant now. */ participant.enable(); /* End changes for Builtin_Topics */ // --- 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 Exception("create_publisher error"); } // --- Create topic --- // /* Register type before creating topic */ string type_name = msgTypeSupport.get_type_name(); try { msgTypeSupport.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( "Example msg", type_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic == null) { shutdown(participant); throw new Exception("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, DDS.StatusMask.STATUS_MASK_NONE); if (writer == null) { shutdown(participant); throw new Exception("create_datawriter error"); } msgDataWriter msg_writer = (msgDataWriter)writer; // --- Write --- // /* Create data sample for writing */ msg instance = msgTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new Exception("msgTypeSupport.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 = msg_writer.register_instance(instance); */ /* Main loop */ const int send_period = 1000; // milliseconds /* Changes for Builtin_Topics */ for (short count = 0; (sample_count == 0) || (count < sample_count); ++count) { System.Threading.Thread.Sleep(send_period); Console.WriteLine("Writing msg, count {0}", count); /* Modify the data to be sent here */ /* Changes for Builtin_Topics */ instance.x = count; try { msg_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } } /* * try { * msg_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { msgTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine("msgTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }
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 = cftTypeSupport.get_type_name(); try { cftTypeSupport.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( "Example cft", 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"); } /* If you want to set the reliability and history QoS settings * programmatically rather than using the XML, you will need to add * the following lines to your code and comment out the * create_datawriter call above. */ /* * DDS.DataWriterQos datawriter_qos = new DDS.DataWriterQos(); * try { * publisher.get_default_datawriter_qos(datawriter_qos); * } catch (DDS.Exception e) { * Console.WriteLine("get_default_datawriter_qos error {0}", e); * shutdown(participant); * throw e; * } * * datawriter_qos.reliability.kind = * DDS.ReliabilityQosPolicyKind.RELIABLE_RELIABILITY_QOS; * datawriter_qos.durability.kind = * DDS.DurabilityQosPolicyKind.TRANSIENT_LOCAL_DURABILITY_QOS; * datawriter_qos.history.kind = * DDS.HistoryQosPolicyKind.KEEP_LAST_HISTORY_QOS; * datawriter_qos.history.depth = 20; * * DDS.DataWriter writer = publisher.create_datawriter( * topic, datawriter_qos, null, * DDS.StatusMask.STATUS_MASK_NONE); * if (writer == null) { * shutdown(participant); * throw new ApplicationException("create_datawriter error"); * } */ cftDataWriter cft_writer = (cftDataWriter)writer; // --- Write --- // /* Create data sample for writing */ cft instance = cftTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "cftTypeSupport.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 = cft_writer.register_instance(instance); */ /* Main loop */ const System.Int32 send_period = 1000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine("Writing cft, count {0}", count); /* Modify the data to be sent here */ if (count % 2 == 1) { instance.name = "ODD"; } else { instance.name = "EVEN"; } instance.count = count; try { cft_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * cft_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { cftTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "cftTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }
private void initializeDataReader(DDS.DomainParticipant participant) { DDS.Subscriber subscriber = participant.create_subscriber( DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (subscriber == null) { throw new ApplicationException("create_subscriber error"); } DDS.Topic topic = participant.create_topic( _topicName, _typeName, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic == null) { throw new ApplicationException("create_topic error"); } /* To customize the data reader QoS, use * the configuration file USER_QOS_PROFILES.xml */ _reader = subscriber.create_datareader( topic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, null, DDS.StatusMask.STATUS_MASK_ALL); if (_reader == null) { throw new ApplicationException("create_datareader error"); } _statusCondition = _reader.get_statuscondition(); try { var mask = (int)DDS.StatusKind.DATA_AVAILABLE_STATUS | (int)DDS.StatusKind.SUBSCRIPTION_MATCHED_STATUS | (int)DDS.StatusKind.LIVELINESS_CHANGED_STATUS | (int)DDS.StatusKind.SAMPLE_LOST_STATUS | (int)DDS.StatusKind.SAMPLE_REJECTED_STATUS; _statusCondition.set_enabled_statuses((DDS.StatusMask)mask); } catch (DDS.Exception e) { throw new ApplicationException($"set_enabled_statuses error {e}"); } _waitset = new DDS.WaitSet(); try { _waitset.attach_condition(_statusCondition); } catch (DDS.Exception e) { throw new ApplicationException("attach_condition error {0}", e); } }
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 --- // DDS.PublisherQos publisher_qos = new DDS.PublisherQos(); participant.get_default_publisher_qos(publisher_qos); /* If you want to change the Partition name programmatically rather than * using the XML, you will need to add the following lines to your code * and comment out the create_publisher() call bellow. */ /* * String[] partitions = { "ABC", "foo" }; * * publisher_qos.partition.name.ensure_length(2, 2); * publisher_qos.partition.name.from_array(partitions); * DDS.Publisher publisher = participant.create_publisher( * publisher_qos, * null, * DDS.StatusMask.STATUS_MASK_NONE); * */ DDS.Publisher publisher = participant.create_publisher( DDS.DomainParticipant.PUBLISHER_QOS_DEFAULT, null, DDS.StatusMask.STATUS_MASK_NONE); if (publisher == null) { shutdown(participant); throw new ApplicationException("create_publisher error"); } Console.WriteLine("Setting partition to '{0}', '{1}'...", publisher_qos.partition.name.get_at(0), publisher_qos.partition.name.get_at(1)); // --- Create topic --- // /* Register type before creating topic */ System.String type_name = partitionsTypeSupport.get_type_name(); try { partitionsTypeSupport.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( "Example partitions", 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 --- // /* In this example we set a Reliable datawriter, with Transient Local * durability. By default we set up these QoS settings via XML. If you * want to to it programmatically, use the following code, and comment out * the create_datawriter call bellow. */ /* * DDS.DataWriterQos writerQos = new DDS.DataWriterQos(); * publisher.get_default_datawriter_qos(writerQos); * * writerQos.reliability.kind = DDS.ReliabilityQosPolicyKind.RELIABLE_RELIABILITY_QOS; * writerQos.history.depth = 3; * writerQos.history.kind = DDS.HistoryQosPolicyKind.KEEP_LAST_HISTORY_QOS; * writerQos.durability.kind = DDS.DurabilityQosPolicyKind.TRANSIENT_LOCAL_DURABILITY_QOS; * * DDS.DataWriter writer = publisher.create_datawriter( * topic, * writerQos, * null, * DDS.StatusMask.STATUS_MASK_NONE); */ DDS.DataWriter writer = publisher.create_datawriter( topic, DDS.Publisher.DATAWRITER_QOS_DEFAULT, null, DDS.StatusMask.STATUS_MASK_NONE); if (writer == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } partitionsDataWriter partitions_writer = (partitionsDataWriter)writer; // --- Write --- // /* Create data sample for writing */ partitions instance = partitionsTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "partitionsTypeSupport.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 = partitions_writer.register_instance(instance); */ /* Main loop */ const System.Int32 send_period = 1000; // 1 second for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine("Writing partitions, count {0}", count); /* Modify the data to be sent here */ instance.x = count; try { partitions_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } if ((count + 1) % 25 == 0) { // Matches "ABC" -- name[1] here can match name[0] there, // as long as there is some overlapping name publisher_qos.partition.name.set_at(0, "zzz"); publisher_qos.partition.name.set_at(1, "A*C"); Console.WriteLine("Setting partition to '{0}', '{1}'...", publisher_qos.partition.name.get_at(0), publisher_qos.partition.name.get_at(1)); publisher.set_qos(publisher_qos); } else if ((count + 1) % 20 == 0) { // Strings that are regular expressions aren't tested for // literal matches, so this won't match "X*Z" publisher_qos.partition.name.set_at(0, "X*Z"); Console.WriteLine("Setting partition to '{0}', '{1}'...", publisher_qos.partition.name.get_at(0), publisher_qos.partition.name.get_at(1)); publisher.set_qos(publisher_qos); } else if ((count + 1) % 15 == 0) { // Matches "ABC" publisher_qos.partition.name.set_at(0, "A?C"); Console.WriteLine("Setting partition to '{0}', '{1}'...", publisher_qos.partition.name.get_at(0), publisher_qos.partition.name.get_at(1)); publisher.set_qos(publisher_qos); } else if ((count + 1) % 10 == 0) { // Matches "ABC" publisher_qos.partition.name.set_at(0, "A*"); Console.WriteLine("Setting partition to '{0}', '{1}'...", publisher_qos.partition.name.get_at(0), publisher_qos.partition.name.get_at(1)); publisher.set_qos(publisher_qos); } else if ((count + 1) % 5 == 0) { // No literal match for "bar" publisher_qos.partition.name.set_at(0, "bar"); Console.WriteLine("Setting partition to '{0}', '{1}'...", publisher_qos.partition.name.get_at(0), publisher_qos.partition.name.get_at(1)); publisher.set_qos(publisher_qos); } System.Threading.Thread.Sleep(send_period); } /* * try { * partitions_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { partitionsTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "partitionsTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }
private static void Publish(int domainId, int sampleCount) { // --- Create participant --- // DDS.DomainParticipant participant = DDS.DomainParticipantFactory.get_instance().create_participant( domainId, DDS.DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (participant == null) { Shutdown(participant); throw new Exception("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 Exception("create_publisher error"); } // --- Create topic --- // // Register type before creating topic. string typeName = FlightTypeSupport.get_type_name(); try { FlightTypeSupport.register_type(participant, typeName); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); Shutdown(participant); throw e; } DDS.Topic topic = participant.create_topic( "Example Flight", typeName, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic == null) { Shutdown(participant); throw new Exception("create_topic error"); } // --- Create writer --- // 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 Exception("create_datawriter error"); } FlightDataWriter flightWriter = (FlightDataWriter)writer; // --- Write --- // // Create data sample for writing. Flight[] flights_info = new Flight[4]; flights_info[0] = new Flight(); flights_info[0].trackId = 1111; flights_info[0].company = "CompanyA"; flights_info[0].altitude = 15000; flights_info[1] = new Flight(); flights_info[1].trackId = 2222; flights_info[1].company = "CompanyB"; flights_info[1].altitude = 20000; flights_info[2] = new Flight(); flights_info[2].trackId = 3333; flights_info[2].company = "CompanyA"; flights_info[2].altitude = 30000; flights_info[3] = new Flight(); flights_info[3].trackId = 4444; flights_info[3].company = "CompanyB"; flights_info[3].altitude = 25000; // 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; // Main loop const int sendPeriod = 1000; // Milliseconds for (int count = 0; (sampleCount == 0) || (count < sampleCount); count++) { // Update flight info latitude and write. Console.WriteLine("Updating and sending values"); for (int i = 0; i < flights_info.Length; i++) { // Set the plane altitude lineally (usually the max is // at 41,000ft). int altitude = flights_info[i].altitude + count * 100; flights_info[i].altitude = altitude >= 41000 ? 41000 : altitude; Console.WriteLine( "\t[trackId: {0}, company: {1}, altitude: {2}]", flights_info[i].trackId, flights_info[i].company, flights_info[i].altitude); try { flightWriter.write(flights_info[i], ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } } System.Threading.Thread.Sleep(sendPeriod); } // Delete all entities Shutdown(participant); }
private void Form1_Load(object sender, EventArgs e) { participant = DDS.DomainParticipantFactory.get_instance().create_participant( 0, 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 topics --- // /* OpenClose */ /* Register type before creating topic */ System.String type_name = cow.OpenCloseTypeSupport.get_type_name(); try { cow.OpenCloseTypeSupport.register_type( participant, type_name); } catch (DDS.Exception ex) { MessageBox.Show("register_type error " + ex.Message); shutdown(participant); throw ex; } /* Customize QoS settings */ DDS.TopicQos topicQos = new DDS.TopicQos(); participant.get_default_topic_qos(topicQos); topicQos.reliability.kind = DDS.ReliabilityQosPolicyKind.RELIABLE_RELIABILITY_QOS; topicQos.durability.kind = DDS.DurabilityQosPolicyKind.TRANSIENT_LOCAL_DURABILITY_QOS; DDS.Topic openCloseTopic = participant.create_topic( "OpenClose", type_name, topicQos, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (openCloseTopic == 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.DataWriterQos datawriterQos = new DDS.DataWriterQos(); publisher.get_default_datawriter_qos(datawriterQos); publisher.copy_from_topic_qos(datawriterQos, topicQos); DDS.DataWriter writer = publisher.create_datawriter( openCloseTopic, datawriterQos, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (writer == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } openCloseWriter = (cow.OpenCloseDataWriter)writer; /* SetWatts */ /* Register type before creating topic */ type_name = cow.SetWattsTypeSupport.get_type_name(); try { cow.SetWattsTypeSupport.register_type( participant, type_name); } catch (DDS.Exception ex) { MessageBox.Show("register_type error " + ex.Message); shutdown(participant); throw ex; } /* To customize topic QoS, use the configuration file USER_QOS_PROFILES.xml */ DDS.Topic setWattsTopic = participant.create_topic( "SetWatts", type_name, topicQos, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (setWattsTopic == null) { shutdown(participant); throw new ApplicationException("create_topic error"); } // --- Create writer --- // /* To customize data writer QoS, use the configuration file USER_QOS_PROFILES.xml */ writer = publisher.create_datawriter( setWattsTopic, datawriterQos, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (writer == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } setWattsWriter = (cow.SetWattsDataWriter)writer; }
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 = queryconditionTypeSupport.get_type_name(); try { queryconditionTypeSupport.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( "Example querycondition", 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"); } queryconditionDataWriter querycondition_writer = (queryconditionDataWriter)writer; // --- Write --- // /* Create data sample for writing */ querycondition instance = queryconditionTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "queryconditionTypeSupport.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 = querycondition_writer.register_instance(instance); */ /* Main loop */ string[] myIds = new string[3]; myIds[0] = "GUID1"; myIds[1] = "GUID2"; myIds[2] = "GUID3"; const System.Int32 send_period = 4000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine("Writing querycondition, count {0}", count); /* Modify the data to be sent here */ instance.id = myIds[count % 3]; Random rnd = new Random(); instance.value = rnd.Next(0, 10); try { querycondition_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * querycondition_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { queryconditionTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "queryconditionTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }