public static DDS.TypedDataWriter <T> CreateDataWriter <T>(string topicName, string typeName) { DDS.DomainParticipant participant = Instance; DDS.Publisher publisher = participant.create_publisher( DDS.DomainParticipant.PUBLISHER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (publisher == null) { throw new ApplicationException("create_publisher 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"); } /* DDS.DataWriterQos dw_qos = new DDS.DataWriterQos(); * participant.get_default_datawriter_qos(dw_qos); * dw_qos.reliability.kind = DDS.ReliabilityQosPolicyKind.RELIABLE_RELIABILITY_QOS; * dw_qos.history.kind = DDS.HistoryQosPolicyKind.KEEP_ALL_HISTORY_QOS;*/ DDS.DataWriterQos dw_qos = new DDS.DataWriterQos(); participant.get_default_datawriter_qos(dw_qos); //Console.WriteLine("LIB CODE DW QOS: " + dw_qos.history.kind); //Console.WriteLine("LIB CODE DW QOS: " + dw_qos.reliability.kind); DDS.DataWriter writer = publisher.create_datawriter( topic, DDS.Publisher.DATAWRITER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (writer == null) { throw new ApplicationException("create_datawriter error"); } return((DDS.TypedDataWriter <T>)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 ande Delete Inconsistent Topic * --------------------------------------------------------------- * Here we create an inconsistent topic to trigger the subscriber * application's callback. * The inconsistent topic is created with the topic name used in * the Subscriber application, but with a different data type -- * the msg data type defined in partitions.idl. * Once it is created, we sleep to ensure the applications discover * each other and delete the Data Writer and Topic. */ /* First we register the msg type -- we name it * inconsistent_topic_type_name to avoid confusion. */ Console.WriteLine("Creating Inconsistent Topic..."); System.String inconsistent_type_name = msgTypeSupport.get_type_name(); try { msgTypeSupport.register_type(participant, inconsistent_type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } DDS.Topic inconsistent_topic = participant.create_topic( "Example listeners", inconsistent_type_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (inconsistent_topic == null) { shutdown(participant); throw new ApplicationException("create_topic error"); } /* We have to associate a writer to the topic, as Topic information is not * actually propagated until the creation of an associated writer. */ DDS.DataWriter inconsistent_writer = publisher.create_datawriter( inconsistent_topic, DDS.Publisher.DATAWRITER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (inconsistent_writer == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } msgDataWriter msg_writer = (msgDataWriter)inconsistent_writer; // Sleep to leave time for applications to discover each other const System.Int32 sleep_period = 2000; //millseconds System.Threading.Thread.Sleep(sleep_period); try { publisher.delete_datawriter(ref inconsistent_writer); } catch (DDS.Exception e) { Console.WriteLine("delete_datawriter error {0}", e); shutdown(participant); return; } try { participant.delete_topic(ref inconsistent_topic); } catch (DDS.Exception e) { Console.WriteLine("delete_topic error {0}", e); shutdown(participant); return; } Console.WriteLine("... Deleted Inconsistent Topic\n"); /* Create Consistent Topic * ----------------------------------------------------------------- * Once we have created the inconsistent topic with the wrong type, * we create a topic with the right type name -- listeners -- that we * will use to publish data. */ /* Register type before creating topic */ System.String type_name = listenersTypeSupport.get_type_name(); try { listenersTypeSupport.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 listeners", 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"); } /* We will use the Data Writer Listener defined above to print * a message when some of events are triggered in the DataWriter. * To do that, first we have to pass the writer_listener and then * we have to enable all status in the status mask. */ DataWriterListener writer_listener = new DataWriterListener(); DDS.DataWriter writer = publisher.create_datawriter( topic, DDS.Publisher.DATAWRITER_QOS_DEFAULT, writer_listener /* listener */, DDS.StatusMask.STATUS_MASK_ALL /* get all statuses */); if (writer == null) { shutdown(participant); writer_listener = null; throw new ApplicationException("create_datawriter error"); } listenersDataWriter listeners_writer = (listenersDataWriter)writer; // --- Write --- // /* Create data sample for writing */ listeners instance = listenersTypeSupport.create_data(); if (instance == null) { shutdown(participant); writer_listener = null; throw new ApplicationException( "listenersTypeSupport.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 = listeners_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 listeners, count {0}", count); /* Modify the data to be sent here */ instance.x = (short)count; try { listeners_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * listeners_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { listenersTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "listenersTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); writer_listener = null; }
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); }
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 = 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 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 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"); } pollDataWriter poll_writer = (pollDataWriter)writer; // --- Write --- // /* Create data sample for writing */ poll instance = pollTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "pollTypeSupport.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 = poll_writer.register_instance(instance); */ Random random = new Random(); /* Main loop */ const System.Int32 send_period = 1000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine("Writing poll, count {0}", count); /* Modify the data to be sent here */ instance.x = (short)random.Next(); try { poll_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * poll_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { pollTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "pollTypeSupport.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 = deadline_contentfilterTypeSupport.get_type_name(); try { deadline_contentfilterTypeSupport.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 deadline_contentfilter", 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 --- // //// Start changes for Deadline // Create listener deadlineListener writer_listener = new deadlineListener(); if (writer_listener == null) { shutdown(participant); throw new ApplicationException("listener instantiation error\n"); } /* To customize data writer QoS, use * the configuration file NDDS_QOS_PROFILES.xml */ DDS.DataWriter writer = publisher.create_datawriter( topic, DDS.Publisher.DATAWRITER_QOS_DEFAULT, writer_listener, /* listener */ DDS.StatusMask.STATUS_MASK_ALL); 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 deadline period to 2 seconds to trigger * a deadline if the DataWriter does not update often enough. */ /* * * 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\n", e); * } * * // Set deadline QoS to 1.5 sec * datawriter_qos.deadline.period.sec = 1; * datawriter_qos.deadline.period.nanosec = 500000000; // 500ms * * // --- Create writer --- // * * DDS.DataWriter writer = publisher.create_datawriter( * topic, * datawriter_qos, //DDS.Publisher.DATAWRITER_QOS_DEFAULT, * writer_listener, * DDS.StatusMask.STATUS_MASK_ALL); * if (writer == null) { * shutdown(participant); * throw new ApplicationException("create_datawriter error"); * } */ deadline_contentfilterDataWriter deadline_contentfilter_writer = (deadline_contentfilterDataWriter)writer; /* Create data sample for writing */ deadline_contentfilter instance0 = deadline_contentfilterTypeSupport.create_data(); if (instance0 == null) { shutdown(participant); throw new ApplicationException( "deadline_contentfilterTypeSupport.create_data error"); } deadline_contentfilter instance1 = deadline_contentfilterTypeSupport.create_data(); if (instance1 == null) { shutdown(participant); throw new ApplicationException( "deadline_contentfilterTypeSupport.create_data error"); } // Set keys -- we specify 'code' as the key field in the .idl instance0.code = 0; instance1.code = 1; /* 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_handle0 = DDS.InstanceHandle_t.HANDLE_NIL; DDS.InstanceHandle_t instance_handle1 = DDS.InstanceHandle_t.HANDLE_NIL; instance_handle0 = deadline_contentfilter_writer.register_instance(instance0); instance_handle1 = deadline_contentfilter_writer.register_instance(instance1); const System.Int32 send_period = 1000; // milliseconds instance0.x = instance0.y = instance1.x = instance1.y = 0; int writeTimes = 15; /* Main loop */ for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { //Console.WriteLine("Writing deadline, count {0}", count); /* Modify the data to be sent here */ instance0.x++; instance0.y++; instance1.x++; instance1.y++; Console.WriteLine("Writing instance0, x = {0}, y = {1}\n", instance0.x, instance0.y); try { deadline_contentfilter_writer.write(instance0, ref instance_handle0); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } if (count < writeTimes) { Console.WriteLine("Writing instance1, x = {0}, y = {1}\n", instance1.x, instance1.y); try { deadline_contentfilter_writer.write(instance1, ref instance_handle1); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } } else if (count == writeTimes) { Console.WriteLine("Stopping writes to instance1\n"); } System.Threading.Thread.Sleep(send_period); } // Unregister the instances try { deadline_contentfilter_writer.unregister_instance(instance0, ref instance_handle0); } catch (DDS.Exception e) { Console.WriteLine("unregister instance error: {0}", e); } try { deadline_contentfilter_writer.unregister_instance(instance1, ref instance_handle1); } catch (DDS.Exception e) { Console.WriteLine("unregister instance error: {0}", e); } // --- Shutdown --- // /* Delete data sample */ try { deadline_contentfilterTypeSupport.delete_data(instance0); } catch (DDS.Exception e) { Console.WriteLine("deadlineTypeSupport.delete_data error: {0}", e); } try { deadline_contentfilterTypeSupport.delete_data(instance1); } catch (DDS.Exception e) { Console.WriteLine("deadlineTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }
static void publish(int domain_id, int sample_count, int initial_value, int dwh, int sleep) { // --- 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 = hello_worldTypeSupport.get_type_name(); try { hello_worldTypeSupport.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 hello_world", 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 --- // /* If you use Durable Writer History, you need to set several * properties. These properties are set in the USER_QOS_PROFILE.xml * file, "durable_writer_history_Profile" profile. See that file for * further details. */ DDS.DataWriter writer = null; if (dwh == 1) { writer = publisher.create_datawriter_with_profile(topic, "persistence_example_Library", "durable_writer_history_Profile", null, DDS.StatusMask.STATUS_MASK_ALL); } else { writer = publisher.create_datawriter_with_profile(topic, "persistence_example_Library", "persistence_service_Profile", null, DDS.StatusMask.STATUS_MASK_ALL); } if (writer == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } hello_worldDataWriter hello_world_writer = (hello_worldDataWriter)writer; // --- Write --- // /* Create data sample for writing */ hello_world instance = hello_worldTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "hello_worldTypeSupport.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 = hello_world_writer.register_instance(instance); */ /* Main loop */ const System.Int32 send_period = 1000; // milliseconds const System.Int32 one_sec = 1000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine("Writing hello_world, count {0}", initial_value); /* Modify the data to be sent here */ instance.data = initial_value; initial_value++; try { hello_world_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } while (sleep != 0) { System.Threading.Thread.Sleep(one_sec); sleep--; } /* * try { * hello_world_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { hello_worldTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "hello_worldTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }
static void publish(int domain_id, String file_name) { // --- 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, 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 = SensorDataTypeSupport.get_type_name(); try { SensorDataTypeSupport.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( "Raw SensorData", 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"); } SensorDataDataWriter SensorData_writer = (SensorDataDataWriter)writer; // --- Write --- // /* Create data sample for writing */ SensorData instance = SensorDataTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "SensorDataTypeSupport.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; //wait for all data-readers to join before publishing: while (!wait_for_readers(writer, no_dr)) { } System.Threading.Thread.Sleep(1000); Console.WriteLine("datareaders have connected. starting to publish"); bool myparse = true; /* Main loop */ using (StreamReader sReader = new StreamReader(file_name)) { String line; publication_sw.Start(); start_ticks = publication_sw.ElapsedTicks; prev_ticks = start_ticks; int[] ballSensors = { 4, 8, 10, 12 }; while ((line = sReader.ReadLine()) != null && count < max_samples) { try { linesRead++; if (myparse) { int pos = 0; instance.sensor_id = myParseInt32(line, ref pos); //if (ballSensors.Contains(instance.sensor_id)) // continue; instance.ts = myParseLong(line, ref pos); instance.pos_x = myParseInt32(line, ref pos); instance.pos_y = myParseInt32(line, ref pos); instance.pos_z = myParseInt32(line, ref pos); instance.vel = myParseInt32(line, ref pos); instance.accel = myParseInt32(line, ref pos); instance.vel_x = myParseInt32(line, ref pos); instance.vel_y = myParseInt32(line, ref pos); instance.vel_z = myParseInt32(line, ref pos); instance.accel_x = myParseInt32(line, ref pos); instance.accel_y = myParseInt32(line, ref pos); instance.accel_z = myParseInt32(line, ref pos); /* * Console.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12}", * instance.sensor_id, * instance.ts, * instance.pos_x, * instance.pos_y, * instance.pos_z, * instance.vel, * instance.accel, * instance.vel_x, * instance.vel_y, * instance.vel_z, * instance.accel_x, * instance.accel_y, * instance.accel_z); */ } else { String[] fields = line.Split(','); instance.sensor_id = Int32.Parse(fields[0]); if (ballSensors.Contains(instance.sensor_id)) { continue; } instance.ts = Int64.Parse(fields[1]); instance.pos_x = Int32.Parse(fields[2]); instance.pos_y = Int32.Parse(fields[3]); instance.pos_z = Int32.Parse(fields[4]); instance.vel = Int32.Parse(fields[5]); instance.accel = Int32.Parse(fields[6]); instance.vel_x = Int32.Parse(fields[7]); instance.vel_y = Int32.Parse(fields[8]); instance.vel_z = Int32.Parse(fields[9]); instance.accel_x = Int32.Parse(fields[10]); instance.accel_y = Int32.Parse(fields[11]); instance.accel_z = Int32.Parse(fields[12]); } count++; SensorData_writer.write(instance, ref instance_handle); if (count % calculation_point == 0) { long curr_ticks = publication_sw.ElapsedTicks; ncalcs++; long lapse_ticks = curr_ticks - prev_ticks; double lapse_msec = (double)lapse_ticks * ns_per_tick / (1000.0 * 1000.0); sum_lapse_msec += lapse_msec; if (lapse_msec < min_lapse) { min_lapse = lapse_msec; } if (lapse_msec > max_lapse) { max_lapse = lapse_msec; } prev_ticks = curr_ticks; Console.WriteLine("Took {0} millisec to send next {1} samples: {2}", lapse_msec, calculation_point, count); double rate = (double)(calculation_point * 1000L * 1000L * 1000L) / (double)(lapse_ticks * ns_per_tick); if (rate < min_rate) { min_rate = rate; } if (rate > max_rate) { max_rate = rate; } Console.WriteLine("publishing rate:{0} samples/sec", rate); } } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } if ((target_rate != 0) && (count % 1000 == 0)) { //double calib = 0.7; //if(calibrationDict.ContainsKey(target_rate)) // calib = calibrationDict[target_rate]; System.Threading.Thread.Sleep(1000 * 1000 / target_rate * 30 / 100); } } end_ticks = publication_sw.ElapsedTicks; publication_sw.Stop(); } Console.WriteLine("Playback finished.. " + count); // --- Shutdown --- // double total_time_ms = (end_ticks - start_ticks) * ns_per_tick / 1000 / 1000; double rate_pub = (double)count / total_time_ms; Console.WriteLine("Target rate = {0} samples/sec.", target_rate); Console.WriteLine("Overall Publication Rate Observed: {0} per sec", rate_pub * 1000); Console.WriteLine("total time for playback in seconds: {0}", total_time_ms / 1000); Console.WriteLine("lines read = {0}, samples written = {1}", linesRead, count); /* Delete data sample */ try { SensorData_writer.dispose(instance, ref instance_handle); System.Threading.Thread.Sleep(10000); SensorDataTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "SensorDataTypeSupport.delete_data error: {0}", e); } Console.WriteLine("Press enter to exit"); Console.ReadLine(); using (StreamWriter file = new StreamWriter("publisher_output.txt")) { file.WriteLine("Publisher summary statistics:\n"); file.WriteLine("Sent {0} samples in {1} seconds", count, total_time_ms / 1000); file.WriteLine("\nAverage rate of publishing: {0} samples/sec", count / total_time_ms / 1000); file.WriteLine("Max publishing rate:{0} samples/sec.", max_rate); file.WriteLine("Min publishing rate:{0} samples/sec.", min_rate); file.WriteLine("\nAverage time taken to publish 10000 samples: {0} sec", (sum_lapse_msec) / (double)ncalcs); file.WriteLine("Max time to publish 10000 samples:{0} sec.", max_lapse); file.WriteLine("Min time to publish 10000 samples:{0} sec.", min_lapse); } /* 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) { /* Modify the data to be sent here */ /* Our purpose is to increment x every time we send a sample and to * reset the x counter to 0 every time we send 10 samples * (x=0,1,..,9). Using the value of count, we can get set x to the * appropriate value applying % 10 operation to it. */ instance.count = count; instance.x = count % 10; Console.WriteLine("Writing cft, count {0}\tx={1}", instance.count, instance.x); 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); }
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 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 = keysTypeSupport.get_type_name(); try { keysTypeSupport.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 keys", 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 you want to set the writer_data_lifecycle 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", e); * } * * datawriter_qos.writer_data_lifecycle.autodispose_unregistered_instances = false; * * 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"); } keysDataWriter keys_writer = (keysDataWriter)writer; // --- Write --- // /* 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; /* Creates three instances */ keys[] instance = new keys[3] { null, null, null }; /* Create data samples for writing */ instance[0] = keysTypeSupport.create_data(); instance[1] = keysTypeSupport.create_data(); instance[2] = keysTypeSupport.create_data(); if (instance[0] == null || instance[1] == null || instance[2] == null) { shutdown(participant); throw new ApplicationException( "keysTypeSupport::create_data error\n"); } /* RTI Connext could examine the key fields each time it needs to determine * which data-instance is being modified. * However, for performance and semantic reasons, it is better * for your application to declare all the data-instances it intends to * modify prior to actually writing any samples. This is known as registration. */ /* In order to register the instances, we must set their associated keys first */ instance[0].code = 0; instance[1].code = 1; instance[2].code = 2; /* Creates three handles for managing the registrations */ DDS.InstanceHandle_t[] handle = new DDS.InstanceHandle_t[] { DDS.InstanceHandle_t.HANDLE_NIL, DDS.InstanceHandle_t.HANDLE_NIL, DDS.InstanceHandle_t.HANDLE_NIL }; /* The keys must have been set before making this call */ Console.WriteLine("Registering instance {0}", instance[0].code); handle[0] = keys_writer.register_instance(instance[0]); /* Modify the data to be sent here */ instance[0].x = 1000; instance[1].x = 2000; instance[2].x = 3000; /* We only will send data over the instances marked as active */ int[] active = new int[] { 1, 0, 0 }; // Only send active tracks. /* Main loop */ const System.Int32 send_period = 1000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { //Console.WriteLine("Writing keys, count {0}", count); switch (count) { case 5: { /* Start sending the second and third instances */ Console.WriteLine("----Registering instance {0}", instance[1].code); Console.WriteLine("----Registering instance {0}", instance[2].code); handle[1] = keys_writer.register_instance(instance[1]); handle[2] = keys_writer.register_instance(instance[2]); active[1] = 1; active[2] = 1; } break; case 10: { /* Unregister the second instance */ Console.WriteLine("----Unregistering instance {0}", instance[1].code); try { keys_writer.unregister_instance(instance[1], ref handle[1]); } catch (DDS.Exception e) { Console.WriteLine("unregister instance error {0}", e); } active[1] = 0; } break; case 15: { /* Dispose the third instance */ Console.WriteLine("----Disposing instance {0}", instance[2].code); try { keys_writer.dispose(instance[2], ref handle[2]); } catch (DDS.Exception e) { Console.WriteLine("dispose instance error {0}", e); } active[2] = 0; } break; } /* Modify the data to be sent here */ instance[0].y = count; instance[1].y = count; instance[2].y = count; for (int i = 0; i < 3; ++i) { if (active[i] == 1) { Console.WriteLine("Writing instance {0}, x: {1}, y: {2}", instance[i].code, instance[i].x, instance[i].y); try { keys_writer.write(instance[i], ref handle[i]); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } } } System.Threading.Thread.Sleep(send_period); } // --- Shutdown --- // /* Delete data samples */ try { keysTypeSupport.delete_data(instance[0]); } catch (DDS.Exception e) { Console.WriteLine("keysTypeSupport.delete_data error: {0}", e); } try { keysTypeSupport.delete_data(instance[1]); } catch (DDS.Exception e) { Console.WriteLine("keysTypeSupport.delete_data error: {0}", e); } try { keysTypeSupport.delete_data(instance[2]); } catch (DDS.Exception e) { Console.WriteLine("keysTypeSupport.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 = sequencesTypeSupport.get_type_name(); try { sequencesTypeSupport.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 sequences", 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"); } sequencesDataWriter sequences_writer = (sequencesDataWriter)writer; /* Here we define two instances: owner_instance and borrower_instance.*/ /* owner_instance.data uses its own memory, as by default, a sequence * you create owns its memory unless you explicitly loan memory of your * own to it. */ sequences owner_instance = sequencesTypeSupport.create_data(); sequences borrower_instance = sequencesTypeSupport.create_data(); DDS.InstanceHandle_t owner_instance_handle = DDS.InstanceHandle_t.HANDLE_NIL; DDS.InstanceHandle_t borrower_instance_handle = DDS.InstanceHandle_t.HANDLE_NIL; /* If we want borrower_instance.data to loan a buffer of shorts, * first we have to allocate the buffer. Here we allocate a buffer * of MAX_SEQUENCE_LEN. */ short[] short_buffer = new short[MAX_SEQUENCE_LEN.VALUE]; /* Before calling loan(), we need to set sequence maximum to 0, * i.e., the sequence won't have memory allocated to it. */ borrower_instance.data.maximum = 0; /* Now that the sequence doesn't have memory allocated to it, we can * call loan() to loan shortBuffer to borrowerInstance. * We set the allocated number of elements to MAX_SEQUENCE_LEN, the * size of the buffer and the maximum size of the sequence as we * declared in the IDL. */ borrower_instance.data.loan( short_buffer, //Buffer 0); //Initial length /* Before starting to publish samples, set the instance id of each * instance*/ owner_instance.id = "owner_instance"; borrower_instance.id = "browser_instance"; /* * instance_handle = sequences_writer.register_instance(instance); */ /* We use Random to generate random values for the sequences */ Random random = new Random(); /* Main loop */ /* To illustrate the use of the sequences, in the main loop we set a * new sequence length every iteration to the sequences contained in * both instances (instance.data). The sequence length value cycles * between 0 and MAX_SEQUENCE_LEN. We assign a random number between * 0 and 100 to each sequence's elements. */ const System.Int32 send_period = 1000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { /* We set a different sequenceLength for both instances every * iteration. sequenceLength is based on the value of count * and its value cycles between the values of 1 and * MAX_SEQUENCE_LEN. */ int sequence_length = (count % MAX_SEQUENCE_LEN.VALUE) + 1; Console.WriteLine("Writing sequences, count {0}...", count); owner_instance.count = (short)count; borrower_instance.count = (short)count; /* Here we set the new length of each sequence */ owner_instance.data.length = sequence_length; borrower_instance.data.length = sequence_length; /* Now that the sequences have a new length, we assign a * random number between 0 and 100 to each element of * owner_instance->data and borrower_instance->data. */ for (int i = 0; i < sequence_length; ++i) { owner_instance.data.set_at(i, (short)random.Next(0, 100)); borrower_instance.data.set_at(i, (short)random.Next(0, 100)); } /* Both sequences have the same length, so we only print the length * of one of them. */ Console.WriteLine("Instances length = {0}", owner_instance.data.length); /* Write for each instance */ try { sequences_writer.write(owner_instance, ref owner_instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } try { sequences_writer.write(borrower_instance, ref borrower_instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* Once we are done with the sequence, we call unloan() */ borrower_instance.data.unloan(); /* * try { * sequences_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { sequencesTypeSupport.delete_data(owner_instance); } catch (DDS.Exception e) { Console.WriteLine( "sequencesTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }
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 --- // /* * This example uses a built-in QoS profile to enable monitoring on the * DomainParticipant. The code below loads the XML QoS from the * USER_QOS_PROFILES.xml file, which is inheriting from the * "BuiltinQoSLib::Generic.Monitoring.Common" profile to enable monitoring. * * !!!! NOTE: This example will only work when dynamically linking !!! * In Visual Studio, change the target to "Debug DLL" or "Release DLL" * to build. */ /* 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, DDS.StatusMask.STATUS_MASK_NONE); /* If you want to change the DomainParticipant'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_participant call above. * * This example uses a built-in QoS profile to enable * monitoring on the DomainParticipant. */ /* * DDS.DomainParticipant participant = DDS.DomainParticipantFactory.get_instance(). * create_participant_with_profile(domain_id, "BuiltinQosLib", * "Generic.Monitoring.Common", null, * 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 = profilesTypeSupport.get_type_name(); try { profilesTypeSupport.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 profiles", 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, DDS.StatusMask.STATUS_MASK_NONE); /* If you want to change the DataWriter's QoS programatically 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. * * This example uses a built-in QoS profile to tune the QoS for * reliable streaming data. */ /*DDS.DataWriter writer = publisher.create_datawriter_with_profile( * topic, DDS.BuiltinQosProfiles.BUILTIN_QOS_LIB_EXP, * DDS.BuiltinQosProfiles.PROFILE_PATTERN_RELIABLE_STREAMING, * null, DDS.StatusMask.STATUS_MASK_NONE); */ if (writer == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } profilesDataWriter profiles_writer = (profilesDataWriter)writer; // --- Write --- // /* Create data sample for writing */ profiles instance = profilesTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "profilesTypeSupport.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 = profiles_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 profiles, count {0}", count); /* Modify the data to be sent here */ try { profiles_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * profiles_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { profilesTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "profilesTypeSupport.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 = 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"); } /* Start changes for custom_flowcontroller */ /* If you want to change the Participant'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_participant call above. */ /* Get default participant QoS to customize */ /* DDS.DomainParticipantQos participant_qos = * new DDS.DomainParticipantQos(); * * try { * DDS.DomainParticipantFactory.get_instance(). * get_default_participant_qos(participant_qos); * } catch (DDS.Exception e) { * Console.WriteLine("get_default_participant_qos error {0}", e); * throw e; * } * * // By default, data will be sent via shared memory _and_ UDPv4. Because * // the flowcontroller limits writes across all interfaces, this halves * // the effective send rate. To avoid this, we enable only the UDPv4 * // transport * * participant_qos.transport_builtin.mask = * (int) DDS.TransportBuiltinKind.TRANSPORTBUILTIN_UDPv4; * * // To create participant with default QoS, * // use DDS_PARTICIPANT_QOS_DEFAULT instead of participant_qos * DDS.DomainParticipant participant = * DDS.DomainParticipantFactory.get_instance().create_participant( * domain_id, * participant_qos, * null, * DDS.StatusMask.STATUS_MASK_NONE); * if (participant == null) { * shutdown(participant); * throw new ApplicationException("create_participant error"); * } */ /* End changes for custom_flow_controller */ // --- 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 = cfcTypeSupport.get_type_name(); try { cfcTypeSupport.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 cfc", 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"); } /* Start changes for custom_flowcontroller */ /* 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 create the flowcontroller and the neccesary QoS * for the datawriter. */ /* * DDS.FlowControllerProperty_t custom_fcp = * new DDS.FlowControllerProperty_t(); * try { * participant.get_default_flowcontroller_property(custom_fcp); * } catch (DDS.Exception e) { * Console.WriteLine("get_default_flowcontroller_property error {0}", * e); * shutdown(participant); * throw e; * } * * // Don't allow too many tokens to accumulate * custom_fcp.token_bucket.max_tokens = * custom_fcp.token_bucket.tokens_added_per_period = 2; * * custom_fcp.token_bucket.tokens_leaked_per_period = * DDS.ResourceLimitsQosPolicy.LENGTH_UNLIMITED; * * //100 ms * custom_fcp.token_bucket.period.sec = 0; * custom_fcp.token_bucket.period.nanosec = 100000000; * * // The sample size is 1000, but the minimum bytes_per_token is 1024. * // Furthermore, we want to allow some overhead. * custom_fcp.token_bucket.bytes_per_token = 1024; * * // So, in summary, each token can be used to send about one message, * // and we get 2 tokens every 100ms, so this limits transmissions to * // about 20 messages per second. * * // Create flowcontroller and set properties * String cfc_name = "custom_flowcontroller"; * DDS.FlowController cfc = participant.create_flowcontroller( * cfc_name, custom_fcp); * if (cfc == null) { * shutdown(participant); * throw new ApplicationException("create_flowcontroller error"); * } * * //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; * } * * // As an alternative to increasing h istory depth, we can just * // set the qos to keep all samples * datawriter_qos.history.kind = * DDS.HistoryQosPolicyKind.KEEP_ALL_HISTORY_QOS; * * // Set flowcontroller for datawriter * datawriter_qos.publish_mode.kind = * DDS.PublishModeQosPolicyKind.ASYNCHRONOUS_PUBLISH_MODE_QOS; * datawriter_qos.publish_mode.flow_controller_name = cfc_name; * * // To create a datawriter with default QoS, use * // DDS.Publisher.DATAWRITER_QOS_DEFAULT instead of datawriter_qos * 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"); * } * */ // End changes for custom_flowcontroller cfcDataWriter cfc_writer = (cfcDataWriter)writer; // --- Write --- // /* Create data sample for writing */ cfc instance = cfcTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "cfcTypeSupport.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 = cfc_writer.register_instance(instance); */ char[] str = new char[1000]; for (int i = 0; i < 1000; ++i) { str[i] = 'a'; } String data = new String(str); /* Main loop */ System.Int32 send_period = 1000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { // Changes for custom_flowcontroller // Simulate bursty writer System.Threading.Thread.Sleep(send_period); for (int i = 0; i < 10; ++i) { int sample = count * 10 + i; Console.WriteLine("Writing cfc, sample {0}", sample); instance.x = sample; instance.str = data; try { cfc_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } } } //Console.WriteLine("{0}", instance.str); send_period = 4000; System.Threading.Thread.Sleep(send_period); /* * try { * cfc_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { cfcTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "cfcTypeSupport.delete_data error: {0}", e); } /* 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 --- // 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); }
static void publish(int domain_id, int sample_count) { /* There are several different approaches for loading QoS profiles from * XML files (see Configuring QoS with XML chapter in the RTI Connext * Core Libraries and Utilities User's Manual). In this example we * illustrate two of them: * * 1) Creating a file named USER_QOS_PROFILES.xml, which is loaded, * automatically by the DomainParticipantFactory. In this case, the file * defines a QoS profile named volatile_profile that configures reliable, * volatile DataWriters and DataReaders. * * 2) Adding XML documents to the DomainParticipantFactory using its * Profile QoSPolicy (DDS Extension). In this case, we add * my_custom_qos_profiles.xml to the url_profile sequence, which stores * the URLs of all the XML documents with QoS policies that are loaded * by the DomainParticipantFactory aside from the ones that are * automatically loaded. * my_custom_qos_profiles.xml defines a QoS profile named * transient_local_profile that configures reliable, transient local * DataWriters and DataReaders. */ /* To load my_custom_qos_profiles.xml, as explained above, we need to * modify the DDSTheParticipantFactory Profile QoSPolicy */ DDS.DomainParticipantFactoryQos factory_qos = new DDS.DomainParticipantFactoryQos(); DDS.DomainParticipantFactory.get_instance().get_qos(factory_qos); /* We are only going to add one XML file to the url_profile sequence, * so we ensure a length of 1,1. */ factory_qos.profile.url_profile.ensure_length(1, 1); /* The XML file will be loaded from the working directory. That means, * you need to run the example like this: * ./objs/<architecture>/profiles_publisher * (see README.txt for more information on how to run the example). * * Note that you can specify the absolute path of the XML QoS file to * avoid this problem. */ factory_qos.profile.url_profile.set_at(0, "file://my_custom_qos_profiles.xml"); DDS.DomainParticipantFactory.get_instance().set_qos(factory_qos); // --- Create participant --- // /* Our default Qos profile, volatile_profile, sets the participant name. * This is the only participant_qos policy that we change in our * example. As this is done in the default QoS profile, we don't need * to specify its name, so we can create the participant using the * create_participant() method rather than using * create_participant_with_profile(). */ 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 --- // /* We haven't changed the publisher_qos in any of QoS profiles we use in * this example, so we can just use the create_publisher() method. If * you want to load an specific profile in which you may have changed * the publisher_qos, use the create_publisher_with_profile() method. */ 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 = profilesTypeSupport.get_type_name(); try { profilesTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* We haven't changed the topic_qos in any of QoS profiles we use in * this example, so we can just use the create_topic() method. If you * want to load an specific profile in which you may have changed the * topic_qos, use the create_topic_with_profile() method. */ DDS.Topic topic = participant.create_topic( "Example profiles", 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 writers --- // /* Volatile writer -- As volatile_profile is the default qos profile * we don't need to specify the profile we are going to use, we can * just call create_datawriter passing DDS_DATAWRITER_QOS_DEFAULT. */ DDS.DataWriter writer_volatile = publisher.create_datawriter( topic, DDS.Publisher.DATAWRITER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (writer_volatile == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } /* Transient Local writer -- In this case we use * create_datawriter_with_profile, because we have to use a profile * other than the default one. This profile has been defined in * my_custom_qos_profiles.xml, but since we already loaded the XML file * we don't need to specify anything else. */ DDS.DataWriter writer_transient_local = publisher.create_datawriter( topic, DDS.Publisher.DATAWRITER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (writer_transient_local == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } profilesDataWriter profiles_writer_volatile = (profilesDataWriter)writer_volatile; profilesDataWriter profiles_writer_transient_local = (profilesDataWriter)writer_transient_local; // --- Write --- // /* Create data sample for writing */ profiles instance = profilesTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "profilesTypeSupport.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 = profiles_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 profiles, count {0}", count); /* Modify the data to be sent here */ instance.profile_name = "volatile_profile"; instance.x = count; Console.WriteLine("Writing profile_name = " + instance.profile_name + "\t x = " + instance.x); try { profiles_writer_volatile.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write volatile error {0}", e); } instance.profile_name = "transient_local_profile"; instance.x = count; Console.WriteLine("Writing profile_name = " + instance.profile_name + "\t x = " + instance.x + "\n"); try { profiles_writer_transient_local.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write transient local error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * profiles_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { profilesTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "profilesTypeSupport.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 = keysTypeSupport.get_type_name(); try { keysTypeSupport.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 keys", 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 --- // /* We are going to load different QoS profiles for the two DWs */ DDS.DataWriterQos writerQos = new DDS.DataWriterQos(); /* 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); DDS.DomainParticipantFactory.get_instance().get_datawriter_qos_from_profile(writerQos, "keys_Library", "keys_Profile_dw2"); /* If you want to set the writer_data_lifecycle 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 * and get_datawriter_qos_from_profile calls above. */ /* * publisher.get_default_datawriter_qos(writerQos); * * writerQos.writer_data_lifecycle.autodispose_unregistered_instances = false; * writerQos.ownership.kind = DDS.OwnershipQosPolicyKind.EXCLUSIVE_OWNERSHIP_QOS; * writerQos.ownership_strength.value = 10; * * DDS.DataWriter writer = publisher.create_datawriter( * topic, * writerQos, * null, * DDS.StatusMask.STATUS_MASK_NONE); * * writerQos.ownership_strength.value = 5; */ DDS.DataWriter writer2 = publisher.create_datawriter( topic, writerQos, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (writer == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } keysDataWriter keys_writer = (keysDataWriter)writer; if (writer2 == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } keysDataWriter keys_writer2 = (keysDataWriter)writer2; // --- Write --- // /* Creates three instances */ keys[] instance = new keys[3] { null, null, null }; /* Create data sample for writing */ instance[0] = keysTypeSupport.create_data(); instance[1] = keysTypeSupport.create_data(); instance[2] = keysTypeSupport.create_data(); if ((instance[0] == null) || (instance[1] == null) || (instance[2] == null) ) { shutdown(participant); throw new ApplicationException( "keysTypeSupport.create_data error"); } /* RTI Connext could examine the key fields each time it needs to determine * which data-instance is being modified. * However, for performance and semantic reasons, it is better * for your application to declare all the data-instances it intends to * modify prior to actually writing any samples. This is known as registration. */ /* In order to register the instances, we must set their associated keys first */ instance[0].code = 0; instance[1].code = 1; instance[2].code = 2; /* 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[] handle = new DDS.InstanceHandle_t[3]; Console.WriteLine("----DW1 registering instance handle", instance[0].code); handle[0] = keys_writer.register_instance(instance[0]); // Init coordinates instance[0].x = 1; instance[1].x = 1; instance[2].x = 1; int[] active = new int[] { 1, 0, 0 }; // Only send active keys. /* Make variables for the instance for the second datawriter to use. * Note that it actually refers to the same logical instance, but * because we're running both datawriters in the same thread, we * to create separate variables so they don't clobber each other. */ keys instance_dw2 = null; instance_dw2 = keysTypeSupport.create_data(); if (instance_dw2 == null) { shutdown(participant); throw new ApplicationException("keysTypeSupport.create_data error"); } /* instance_dw2 and instance[1] have the same key, and thus * will write to the same instance (ins1). */ instance_dw2.code = instance[1].code; instance_dw2.x = 2; DDS.InstanceHandle_t handle_dw2 = keys_writer2.register_instance(instance_dw2); int active_dw2 = 1; /* Main loop */ const System.Int32 send_period = 1000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { System.Threading.Thread.Sleep(send_period); /* Modify the data to be sent here */ instance[0].y = count; instance[1].y = count + 1000; instance[2].y = count + 2000; instance_dw2.y = -count - 1000; /* We control two datawriters via a state machine here rather than * introducing separate threads. */ /* Control first DataWriter */ switch (count) { case 4: { /* Start sending the second (ins1) and third instances (ins2) */ Console.WriteLine("----DW1 registering instance {0}", instance[1].code); Console.WriteLine("----DW1 registering instance {0}", instance[2].code); handle[1] = keys_writer.register_instance(instance[1]); handle[2] = keys_writer.register_instance(instance[2]); active[1] = 1; active[2] = 1; } break; case 8: { /* Dispose the second instance (ins1) */ Console.WriteLine("----DW1 disposing instance {0}", instance[1].code); try { keys_writer.dispose(instance[1], ref handle[1]); } catch (DDS.Exception e) { Console.WriteLine("dispose instance error: {0}", e); return; } active[1] = 0; } break; case 10: { /* Unregister the second instance (ins1) */ Console.WriteLine("----DW1 unregistering instance {0}", instance[1].code); try { keys_writer.unregister_instance(instance[1], ref handle[1]); } catch (DDS.Exception e) { Console.WriteLine("unregister instance error: {0}", e); return; } active[1] = 0; } break; case 12: { /* Unregister the third instance (ins2) */ Console.WriteLine("----DW1 unregistering instance {0}", instance[2].code); try { keys_writer.unregister_instance(instance[2], ref handle[2]); } catch (DDS.Exception e) { Console.WriteLine("unregister instance error: {0}", e); return; } active[2] = 0; /* Re-register the second instance (ins1) */ Console.WriteLine("----DW1 re-registering instance {0}", instance[1].code); handle[1] = keys_writer.register_instance(instance[1]); active[1] = 1; } break; case 16: { /* Re-register the third instance (ins2) */ Console.WriteLine("----DW1 re-registering instance {0}", instance[2].code); handle[2] = keys_writer.register_instance(instance[2]); active[2] = 1; } break; } for (int i = 0; i < 3; ++i) { if (active[i] == 1) { Console.WriteLine("DW1 write; code: {0}, x: {1}, y: {2}", instance[i].code, instance[i].x, instance[i].y); try { keys_writer.write(instance[i], ref handle[i]); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); return; } } } /* Control second datawriter */ switch (count) { case 16: { /* Dispose the instance (ins1). * Since it has lower ownership strength, this does nothing */ Console.WriteLine("----DW2 disposing instance {0}", instance_dw2.code); try { keys_writer2.dispose(instance_dw2, ref handle_dw2); } catch (DDS.Exception e) { Console.WriteLine("dispose error {0}", e); return; } active_dw2 = 0; } break; } if (active_dw2 == 1) { Console.WriteLine("DW2 write; code: {0}, x: {1}, y: {2}", instance_dw2.code, instance_dw2.x, instance_dw2.y); try { keys_writer2.write(instance_dw2, ref handle_dw2); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); return; } } } // --- Shutdown --- // /* Delete data sample */ for (int i = 0; i < 3; i++) { try { keysTypeSupport.delete_data(instance[i]); } catch (DDS.Exception e) { Console.WriteLine( "keysTypeSupport.delete_data error: {0}", e); } } try { keysTypeSupport.delete_data(instance_dw2); } catch (DDS.Exception e) { Console.WriteLine( "keysTypeSupport.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 = ordered_groupTypeSupport.get_type_name(); try { ordered_groupTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* Start changes for Ordered Presentation Group example */ /* TOPICS */ /* To customize topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic1 = participant.create_topic( "Topic1", type_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic1 == null) { shutdown(participant); throw new ApplicationException("create_topic error"); } /* To customize topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic2 = participant.create_topic( "Topic2", type_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic2 == null) { shutdown(participant); throw new ApplicationException("create_topic error"); } /* To customize topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic3 = participant.create_topic( "Topic3", type_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic3 == null) { shutdown(participant); throw new ApplicationException("create_topic error"); } /* DATAWRITERS */ /* To customize data writer QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DataWriter writer1 = publisher.create_datawriter( topic1, DDS.Publisher.DATAWRITER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (writer1 == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } ordered_groupDataWriter ordered_group_writer1 = (ordered_groupDataWriter)writer1; DDS.DataWriter writer2 = publisher.create_datawriter( topic2, DDS.Publisher.DATAWRITER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (writer2 == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } ordered_groupDataWriter ordered_group_writer2 = (ordered_groupDataWriter)writer2; DDS.DataWriter writer3 = publisher.create_datawriter( topic3, DDS.Publisher.DATAWRITER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (writer3 == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } ordered_groupDataWriter ordered_group_writer3 = (ordered_groupDataWriter)writer3; // --- Write --- // /* Instances */ ordered_group instance1 = ordered_groupTypeSupport.create_data(); if (instance1 == null) { shutdown(participant); throw new ApplicationException( "ordered_groupTypeSupport.create_data error"); } ordered_group instance2 = ordered_groupTypeSupport.create_data(); if (instance2 == null) { shutdown(participant); throw new ApplicationException( "ordered_groupTypeSupport.create_data error"); } ordered_group instance3 = ordered_groupTypeSupport.create_data(); if (instance3 == null) { shutdown(participant); throw new ApplicationException( "ordered_groupTypeSupport.create_data error"); } /* End changes for Ordered Presentation Example */ /* 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 = ordered_group_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 ordered_group, count {0}", count); /* Start changes for Ordered Presentation Example */ /* Modify the data to be sent here */ /* Instance 1 */ instance1.message = "First sample, Topic 1 by DataWriter number 1"; try { ordered_group_writer1.write(instance1, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } instance1.message = "Second sample, Topic 1 by DataWriter number 1"; try { ordered_group_writer1.write(instance1, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } /* Instance 2 */ instance2.message = "First sample, Topic 2 by DataWriter number 2"; try { ordered_group_writer2.write(instance2, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } instance2.message = "Second sample, Topic 2 by DataWriter number 2"; try { ordered_group_writer2.write(instance2, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } /* Instance 3 */ instance3.message = "First sample, Topic 3 by DataWriter number 3"; try { ordered_group_writer3.write(instance3, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } instance3.message = "Second sample, Topic 3 by DataWriter number 3"; try { ordered_group_writer3.write(instance3, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * ordered_group_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { ordered_groupTypeSupport.delete_data(instance1); ordered_groupTypeSupport.delete_data(instance2); ordered_groupTypeSupport.delete_data(instance3); } catch (DDS.Exception e) { Console.WriteLine( "ordered_groupTypeSupport.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"); } /* If you want to change the type_code_max_serialized_length * programmatically (e.g., to 3070) rather than using the XML file, you * will need to add the following lines to your code and comment out the * create_participant call above. */ /* * DDS.DomainParticipantQos participant_qos = * new DDS.DomainParticipantQos(); * try { * DDS.DomainParticipantFactory.get_instance(). * get_default_participant_qos(participant_qos); * * } catch (DDS.Exception) { * Console.WriteLine("error in participant_qos"); * } * * participant_qos.resource_limits.type_code_max_serialized_length = 3070; * * try { * DDS.DomainParticipantFactory.get_instance().delete_participant( * ref participant); * } * catch (DDS.Exception) { * Console.WriteLine("error deleting participant"); * } * * participant = * DDS.DomainParticipantFactory.get_instance().create_participant( * domain_id, participant_qos, null, DDS.StatusMask.STATUS_MASK_NONE); */ // --- 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 = 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 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"); } msgDataWriter msg_writer = (msgDataWriter)writer; // --- Write --- // /* Create data sample for writing */ msg instance = msgTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "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 System.Int32 send_period = 4000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine("Writing msg, count {0}", count); /* Modify the data to be sent here */ instance.count = count; try { msg_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * 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"); } /* 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_publisher call above. */ // Start changes for coherent_presentation /* DDS.PublisherQos publisher_qos = new DDS.PublisherQos(); * participant.get_default_publisher_qos(publisher_qos); * * // Topic access scope means that writes from a particular datawriter to * // multiple instances will be viewed coherently. * publisher_qos.presentation.access_scope = * DDS.PresentationQosPolicyAccessScopeKind.TOPIC_PRESENTATION_QOS; * publisher_qos.presentation.coherent_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"); * } */ // End changes for Coherent_Presentation // --- Create topic --- // /* Register type before creating topic */ System.String type_name = coherentTypeSupport.get_type_name(); try { coherentTypeSupport.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 coherent", 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 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. */ // Start changes for coherent_presentation /* Get default datawriter QoS to customize */ /* DDS.DataWriterQos datawriter_qos = new DDS.DataWriterQos(); * publisher.get_default_datawriter_qos(datawriter_qos); * * datawriter_qos.reliability.kind = * DDS.ReliabilityQosPolicyKind.RELIABLE_RELIABILITY_QOS; * datawriter_qos.history.depth = 10; * datawriter_qos.protocol.rtps_reliable_writer. * heartbeats_per_max_samples = 0; * * 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"); * } * */ // End changes for coherent_presentation coherentDataWriter coherent_writer = (coherentDataWriter)writer; // --- Write --- // /* Create data sample for writing */ coherent instance = coherentTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "coherentTypeSupport.create_data error"); } // Start changes for coherent_presentation /* 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.id = 0; instance_handle = coherent_writer.register_instance(instance); Random random = new Random(); publisher.begin_coherent_changes(); Console.WriteLine("Begin Coherent Changes"); /* Main loop */ const System.Int32 send_period = 1000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { System.Threading.Thread.Sleep(send_period); /* Modify the data to be sent here */ int mod = count % 7; if (mod == 6) { Console.WriteLine("Begin Coherent Changes"); try { publisher.begin_coherent_changes(); } catch (DDS.Exception e) { Console.WriteLine("begin_coherent_chages {0}", e); } continue; } //Choose a random value for each field instance.field = (char)('a' + mod); instance.value = (int)(random.Next(9)); Console.WriteLine(" Updating instance, {0}->{1}", instance.field, instance.value); try { coherent_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } // Begin a new change group after sending 6 samples if (mod == 5) { Console.WriteLine("End Coherent Changes\n"); try { publisher.end_coherent_changes(); } catch (DDS.Exception e) { Console.WriteLine("end_coherent_changes error {0}", e); throw new ApplicationException( "coherentTypeSupport.end_coherent_changes error"); } } } try { coherent_writer.unregister_instance( instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("unregister instance error: {0}", e); } // --- Shutdown --- // /* Delete data sample */ try { coherentTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "coherentTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }
static void publish(int domain_id, int sample_count, Route routeIn, Route.Bus busIn) { Random random = new Random(); int randomNumber = random.Next(0, 100); // --- 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 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 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 writer --- // /* To customize data writer QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DataWriter awriter = publisher.create_datawriter( atopic, DDS.Publisher.DATAWRITER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); DDS.DataWriter pwriter = publisher.create_datawriter( ptopic, DDS.Publisher.DATAWRITER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (awriter == null || pwriter == null) { shutdown(participant); throw new ApplicationException("create_datawriter error"); } AccidentDataWriter Accident_writer = (AccidentDataWriter)awriter; PositionDataWriter Position_writer = (PositionDataWriter)pwriter; // --- Write --- // /* Create data sample for writing */ Accident ainstance = AccidentTypeSupport.create_data(); Position pinstance = PositionTypeSupport.create_data(); if (ainstance == null || pinstance == null) { shutdown(participant); throw new ApplicationException( "(Accident|Position)TypeSupport.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 = Accident_writer.register_instance(instance); */ int numPasses = 0; /* Main loop */ int send_period = 4000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { if (count % routeIn.numStops == 0) { numPasses++; } if (numPasses > 3) { break; } Console.WriteLine("Writing Accident|Position, count {0}", count); /* Modify the data to be sent here */ int traffic = random.Next(0, 100); if (traffic <= 25) // light 25% { pinstance.trafficConditions = "light"; pinstance.timeBetweenStops = (int)(Math.Ceiling(routeIn.timeBetweenStops * .75)); } else if (traffic <= 50) // heavy 25% { pinstance.trafficConditions = "heavy"; pinstance.timeBetweenStops = (int)(routeIn.timeBetweenStops + Math.Ceiling(routeIn.timeBetweenStops * .25)); } else // normal 50% { pinstance.trafficConditions = "normal"; pinstance.timeBetweenStops = routeIn.timeBetweenStops; } ainstance.timestamp = DateTime.Now.ToString(); ainstance.route = routeIn.name; ainstance.vehicle = busIn.id; ainstance.stopNumber = busIn.stop; pinstance.timestamp = DateTime.Now.ToString(); pinstance.route = routeIn.name; pinstance.vehicle = busIn.id; pinstance.stopNumber = busIn.stop; pinstance.numStops = routeIn.numStops; pinstance.fillInRatio = random.Next(0, 100); int aOccurs = random.Next(0, 100); if (aOccurs <= 10) { try { Accident_writer.write(ainstance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } pinstance.timeBetweenStops += 10; } send_period = pinstance.timeBetweenStops * 1000; // threads use milliseconds busIn.stop = (busIn.stop % routeIn.numStops) + 1; try { Position_writer.write(pinstance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * Accident_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { AccidentTypeSupport.delete_data(ainstance); PositionTypeSupport.delete_data(pinstance); } catch (DDS.Exception e) { Console.WriteLine( "(Accident|Position)TypeSupport.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 = waitsetsTypeSupport.get_type_name(); try { waitsetsTypeSupport.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 waitsets", 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 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 reduce the liveliness timeout period to trigger the * StatusCondition DDS.StatusKind.LIVELINESS_CHANGED_STATUS */ /* * 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.liveliness.lease_duration.sec = 1; * datawriter_qos.liveliness.lease_duration.nanosec = 0; * * writer = publisher.create_datawriter(topic, datawriter_qos, * null, DDS.StatusMask.STATUS_MASK_NONE); * if (writer == null) { * shutdown(participant); * throw new ApplicationException("create_datawriter_qos error"); * } */ waitsetsDataWriter waitsets_writer = (waitsetsDataWriter)writer; // --- Write --- // /* Create data sample for writing */ waitsets instance = waitsetsTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "waitsetsTypeSupport.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 = waitsets_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 waitsets, count {0}", count); /* Modify the data to be sent here */ instance.x = (short)count; try { waitsets_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } System.Threading.Thread.Sleep(send_period); } /* * try { * waitsets_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { waitsetsTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "waitsetsTypeSupport.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 you want to change the DomainParticipant'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_participant call above. * * In this case, we set the transport settings in the XML by default, but * in the createParticipant call, we set up the transport * properties either using the Properties QoS in code. */ /* DDS.DomainParticipant participant = createParticipant(domain_id); */ if (participant == null) { shutdown(participant); throw new ApplicationException("create_participant error"); } if (checkParticipant(participant) == true) { Console.WriteLine("Ok, recv_socket_buffer_size....modified"); Console.WriteLine("Ok, send_socket_buffer_size....modified"); } // --- 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 = numbersTypeSupport.get_type_name(); try { numbersTypeSupport.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 numbers", 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"); } numbersDataWriter numbers_writer = (numbersDataWriter)writer; // --- Write --- // /* Create data sample for writing */ numbers instance = numbersTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new ApplicationException( "numbersTypeSupport.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 = numbers_writer.register_instance(instance); */ instance.number = 1000; instance.halfNumber = (float)(instance.number) / 2; /* Main loop */ const System.Int32 send_period = 4000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine("Writing numbers, count {0}", count); /* Modify the data to be sent here */ try { numbers_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } instance.number = (int)instance.halfNumber; instance.halfNumber = (float)(instance.number) / 2; System.Threading.Thread.Sleep(send_period); } /* * try { * numbers_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { numbersTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine( "numbersTypeSupport.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); }