using System;
using System.Collections.Generic;
using System.Text;
public class HelloWorldPublisher {
    public static void Main(string[] args) {
        
        int domain_id = 0;
        if (args.Length >= 1) {
            domain_id = Int32.Parse(args[0]);
        }
        
        int sample_count = 0;
        if (args.Length >= 2) {
            sample_count = Int32.Parse(args[1]);
        }
        
    
        
        try {
            HelloWorldPublisher.publish(
                domain_id, sample_count);
        }
        catch(DDS.Exception)
        {
            Console.WriteLine("error in publisher");
        }
    }
    static void publish(int domain_id, int sample_count) {
        
        
        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");
        }
        
        
        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");
        }
        
        
        System.String type_name = HelloWorldTypeSupport.get_type_name();
        try {
            HelloWorldTypeSupport.register_type(
                participant, type_name);
        }
        catch(DDS.Exception e) {
            Console.WriteLine("register_type error {0}", e);
            shutdown(participant);
            throw e;
        }
        
        DDS.Topic topic = participant.create_topic(
            "Example HelloWorld",
            type_name,
            DDS.DomainParticipant.TOPIC_QOS_DEFAULT,
            null ,
            DDS.StatusMask.STATUS_MASK_NONE);
        if (topic == null) {
            shutdown(participant);
            throw new ApplicationException("create_topic error");
        }
        
        
        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");
        }
        HelloWorldDataWriter HelloWorld_writer =
            (HelloWorldDataWriter)writer;
        
        
        HelloWorld instance = HelloWorldTypeSupport.create_data();
        if (instance == null) {
            shutdown(participant);
            throw new ApplicationException(
                "HelloWorldTypeSupport.create_data error");
        }
        
        DDS.InstanceHandle_t instance_handle = DDS.InstanceHandle_t.HANDLE_NIL;
        
        
        const System.Int32 send_period = 4000; 
        for (int count=0;
             (sample_count == 0) || (count < sample_count);
             ++count) {
            Console.WriteLine("Writing HelloWorld, count {0}", count);
            
            instance.msg = "Hello World! (" + count + ")";
            try {
                HelloWorld_writer.write(instance, ref instance_handle);
            }
            catch(DDS.Exception e) {
                Console.WriteLine("write error {0}", e);
            }
            System.Threading.Thread.Sleep(send_period);
        }
        
        
        
        try {
            HelloWorldTypeSupport.delete_data(instance);
        } catch(DDS.Exception e) {
            Console.WriteLine(
                "HelloWorldTypeSupport.delete_data error: {0}", e);
        }
        
        shutdown(participant);
    }
    static void shutdown(
        DDS.DomainParticipant participant) {
        
        if (participant != null) {
            participant.delete_contained_entities();
            DDS.DomainParticipantFactory.get_instance().delete_participant(
                ref participant);
        }
        
        
    }
}