/// <summary> /// Create DataWriter(s) /// </summary> public void createWriter() { status = publisher.GetDefaultDataWriterQos(ref WQosH); ErrorHandler.checkStatus(status, "Publisher.GetDefaultDataWriterQos"); status = publisher.CopyFromTopicQos(ref WQosH, topicQos); ErrorHandler.checkStatus(status, "Publisher.CopyFromTopicQos"); if (durabilityKind.Equals("transient")) { WQosH.Durability.Kind = DurabilityQosPolicyKind.TransientDurabilityQos; } else { WQosH.Durability.Kind = DurabilityQosPolicyKind.PersistentDurabilityQos; } WQosH.WriterDataLifecycle.AutodisposeUnregisteredInstances = autoDisposeFlag; writer = publisher.CreateDataWriter( topic, WQosH, null, StatusKind.Any); ErrorHandler.checkHandle(writer, "Publisher.CreateDataWriter"); if (exampleName.Equals("Lifecycle")) { writerStopper = publisher.CreateDataWriter( topic, WQosH, null, StatusKind.Any); ErrorHandler.checkHandle(writerStopper, "Publisher.CreateDataWriter"); } }
private void InitializeWriter(AdapterWriterQos desiredQos) { var writerQos = new DataWriterQos(); topicPublisher.GetDefaultDataWriterQos(ref writerQos); topicPublisher.CopyFromTopicQos(ref writerQos, currentTopic.TopicQos); if (desiredQos != null) { writerQos.WriterDataLifecycle.AutodisposeUnregisteredInstances = false; writerQos.Reliability.Kind = desiredQos.MessageReliabilityType.ConvertReliability(); writerQos.Durability.Kind = desiredQos.PersistenceType.ConvertPersistence(); } // Create a DataWritter for the current Topic dataWriter = topicPublisher.CreateDataWriter(currentTopic.TopicMessageTopic, writerQos) as DataWriter <T>; ErrorHandler.CheckHandle( DataWriter, "Publisher.CreateDatawriter (InitializeWriter)"); }
/*** * Operations ***/ public ITopic CreateSimulatedMultitopic( string name, string type_name, string subscription_expression, string[] expression_parameters) { /* Type-specific DDS entities */ ChatMessageDataReader chatMessageDR; NameServiceDataReader nameServiceDR; NamedMessageDataWriter namedMessageDW; /* Query related stuff */ IQueryCondition nameFinder; string[] nameFinderParams; /* QosPolicy holders */ TopicQos namedMessageQos = new TopicQos(); SubscriberQos subQos = new SubscriberQos(); PublisherQos pubQos = new PublisherQos(); /* Others */ IDataReader parentReader; IDataWriter parentWriter; string partitionName = "ChatRoom"; string nameFinderExpr; ReturnCode status; /* Lookup both components that constitute the multi-topic. */ chatMessageTopic = realParticipant.FindTopic( "Chat_ChatMessage", Duration.Infinite); ErrorHandler.checkHandle( chatMessageTopic, "DDS.DomainParticipant.FindTopic (Chat_ChatMessage)"); nameServiceTopic = realParticipant.FindTopic( "Chat_NameService", Duration.Infinite); ErrorHandler.checkHandle( nameServiceTopic, "DDS.DomainParticipant.FindTopic (Chat_NameService)"); /* Create a ContentFilteredTopic to filter out * our own ChatMessages. */ filteredMessageTopic = realParticipant.CreateContentFilteredTopic( "Chat_FilteredMessage", chatMessageTopic, "userID <> %0", expression_parameters); ErrorHandler.checkHandle( filteredMessageTopic, "DDS.DomainParticipant.CreateContentFilteredTopic"); /* Adapt the default SubscriberQos to read from the * "ChatRoom" Partition. */ status = realParticipant.GetDefaultSubscriberQos(ref subQos); ErrorHandler.checkStatus( status, "DDS.DomainParticipant.GetDefaultSubscriberQos"); subQos.Partition.Name = new string[1]; subQos.Partition.Name[0] = partitionName; /* Create a private Subscriber for the multitopic simulator. */ multiSub = realParticipant.CreateSubscriber(subQos); ErrorHandler.checkHandle( multiSub, "DDS.DomainParticipant.CreateSubscriber (for multitopic)"); /* Create a DataReader for the FilteredMessage Topic * (using the appropriate QoS). */ DataReaderQos drQos = new DataReaderQos(); TopicQos topicQos = new TopicQos(); multiSub.GetDefaultDataReaderQos(ref drQos); filteredMessageTopic.RelatedTopic.GetQos(ref topicQos); multiSub.CopyFromTopicQos(ref drQos, topicQos); parentReader = multiSub.CreateDataReader(filteredMessageTopic, drQos); ErrorHandler.checkHandle( parentReader, "DDS.Subscriber.create_datareader (ChatMessage)"); /* Narrow the abstract parent into its typed representative. */ chatMessageDR = parentReader as ChatMessageDataReader; /* Allocate the DataReaderListener Implementation. */ msgListener = new DataReaderListenerImpl(); /* Attach the DataReaderListener to the DataReader, * only enabling the data_available event. */ status = chatMessageDR.SetListener(msgListener, StatusKind.DataAvailable); ErrorHandler.checkStatus(status, "DDS.DataReader_set_listener"); /* Create a DataReader for the nameService Topic * (using the appropriate QoS). */ DataReaderQos nsDrQos = new DataReaderQos(); TopicQos nsQos = new TopicQos(); nameServiceTopic.GetQos(ref nsQos); multiSub.CopyFromTopicQos(ref nsDrQos, nsQos); parentReader = multiSub.CreateDataReader(nameServiceTopic, nsDrQos); ErrorHandler.checkHandle(parentReader, "DDS.Subscriber.CreateDatareader (NameService)"); /* Narrow the abstract parent into its typed representative. */ nameServiceDR = parentReader as NameServiceDataReader; /* Define the SQL expression (using a parameterized value). */ nameFinderExpr = "userID = %0"; /* Allocate and assign the query parameters. */ nameFinderParams = new string[1]; nameFinderParams[0] = expression_parameters[0]; /* Create a QueryCondition to only read corresponding * nameService information by key-value. */ nameFinder = nameServiceDR.CreateQueryCondition( SampleStateKind.Any, ViewStateKind.Any, InstanceStateKind.Any, nameFinderExpr, nameFinderParams); ErrorHandler.checkHandle( nameFinder, "DDS.DataReader.create_querycondition (nameFinder)"); /* Create the Topic that simulates the multi-topic * (use Qos from chatMessage).*/ status = chatMessageTopic.GetQos(ref namedMessageQos); ErrorHandler.checkStatus(status, "DDS.Topic.GetQos"); /* Create the NamedMessage Topic whose samples simulate * the MultiTopic */ namedMessageTopic = realParticipant.CreateTopic( "Chat_NamedMessage", type_name, namedMessageQos); ErrorHandler.checkHandle( namedMessageTopic, "DDS.DomainParticipant.CreateTopic (NamedMessage)"); /* Adapt the default PublisherQos to write into the * "ChatRoom" Partition. */ status = realParticipant.GetDefaultPublisherQos(ref pubQos); ErrorHandler.checkStatus( status, "DDS.DomainParticipant.get_default_publisher_qos"); pubQos.Partition.Name = new string[1]; pubQos.Partition.Name[0] = partitionName; /* Create a private Publisher for the multitopic simulator. */ multiPub = realParticipant.CreatePublisher(pubQos); ErrorHandler.checkHandle( multiPub, "DDS.DomainParticipant.create_publisher (for multitopic)"); DataWriterQos nmDwQos = new DataWriterQos(); TopicQos nmQos = new TopicQos(); multiPub.GetDefaultDataWriterQos(ref nmDwQos); namedMessageTopic.GetQos(ref nmQos); multiPub.CopyFromTopicQos(ref nmDwQos, nmQos); /* Create a DataWriter for the multitopic. */ parentWriter = multiPub.CreateDataWriter(namedMessageTopic, nmDwQos); ErrorHandler.checkHandle( parentWriter, "DDS.Publisher.CreateDatawriter (NamedMessage)"); /* Narrow the abstract parent into its typed representative. */ namedMessageDW = parentWriter as NamedMessageDataWriter; /* Store the relevant Entities in our Listener. */ msgListener.ChatMessageDR = chatMessageDR; msgListener.NameServiceDR = nameServiceDR; msgListener.NamedMessageDW = namedMessageDW; msgListener.NameFinder = nameFinder; msgListener.NameFinderParams = nameFinderParams; /* Return the simulated Multitopic. */ return(namedMessageTopic); }
/*** * Operations ***/ public ITopic CreateSimulatedMultitopic( string name, string type_name, string subscription_expression, string[] expression_parameters) { /* Type-specific DDS entities */ ChatMessageDataReader chatMessageDR; NameServiceDataReader nameServiceDR; NamedMessageDataWriter namedMessageDW; /* Query related stuff */ IQueryCondition nameFinder; string[] nameFinderParams; /* QosPolicy holders */ TopicQos namedMessageQos = new TopicQos(); SubscriberQos subQos = new SubscriberQos(); PublisherQos pubQos = new PublisherQos(); /* Others */ IDataReader parentReader; IDataWriter parentWriter; string partitionName = "ChatRoom"; string nameFinderExpr; ReturnCode status; /* Lookup both components that constitute the multi-topic. */ chatMessageTopic = realParticipant.FindTopic( "Chat_ChatMessage", Duration.Infinite); ErrorHandler.checkHandle( chatMessageTopic, "DDS.DomainParticipant.FindTopic (Chat_ChatMessage)"); nameServiceTopic = realParticipant.FindTopic( "Chat_NameService", Duration.Infinite); ErrorHandler.checkHandle( nameServiceTopic, "DDS.DomainParticipant.FindTopic (Chat_NameService)"); /* Create a ContentFilteredTopic to filter out our own ChatMessages. */ filteredMessageTopic = realParticipant.CreateContentFilteredTopic( "Chat_FilteredMessage", chatMessageTopic, "userID <> %0", expression_parameters); ErrorHandler.checkHandle( filteredMessageTopic, "DDS.DomainParticipant.CreateContentFilteredTopic"); /* Adapt the default SubscriberQos to read from the "ChatRoom" Partition. */ status = realParticipant.GetDefaultSubscriberQos (ref subQos); ErrorHandler.checkStatus( status, "DDS.DomainParticipant.GetDefaultSubscriberQos"); subQos.Partition.Name = new string[1]; subQos.Partition.Name[0] = partitionName; /* Create a private Subscriber for the multitopic simulator. */ multiSub = realParticipant.CreateSubscriber(subQos); ErrorHandler.checkHandle( multiSub, "DDS.DomainParticipant.CreateSubscriber (for multitopic)"); /* Create a DataReader for the FilteredMessage Topic (using the appropriate QoS). */ DataReaderQos drQos = new DataReaderQos(); TopicQos topicQos = new TopicQos(); multiSub.GetDefaultDataReaderQos(ref drQos); filteredMessageTopic.RelatedTopic.GetQos(ref topicQos); multiSub.CopyFromTopicQos(ref drQos, topicQos); parentReader = multiSub.CreateDataReader(filteredMessageTopic, drQos); ErrorHandler.checkHandle( parentReader, "DDS.Subscriber.create_datareader (ChatMessage)"); /* Narrow the abstract parent into its typed representative. */ chatMessageDR = parentReader as ChatMessageDataReader; /* Allocate the DataReaderListener Implementation. */ msgListener = new DataReaderListenerImpl(); /* Attach the DataReaderListener to the DataReader, only enabling the data_available event. */ status = chatMessageDR.SetListener(msgListener, StatusKind.DataAvailable); ErrorHandler.checkStatus(status, "DDS.DataReader_set_listener"); /* Create a DataReader for the nameService Topic (using the appropriate QoS). */ DataReaderQos nsDrQos = new DataReaderQos(); TopicQos nsQos = new TopicQos(); nameServiceTopic.GetQos(ref nsQos); multiSub.CopyFromTopicQos(ref nsDrQos, nsQos); parentReader = multiSub.CreateDataReader(nameServiceTopic, nsDrQos); ErrorHandler.checkHandle(parentReader, "DDS.Subscriber.CreateDatareader (NameService)"); /* Narrow the abstract parent into its typed representative. */ nameServiceDR = parentReader as NameServiceDataReader; /* Define the SQL expression (using a parameterized value). */ nameFinderExpr = "userID = %0"; /* Allocate and assign the query parameters. */ nameFinderParams = new string[1]; nameFinderParams[0] = expression_parameters[0]; /* Create a QueryCondition to only read corresponding nameService information by key-value. */ nameFinder = nameServiceDR.CreateQueryCondition( SampleStateKind.Any, ViewStateKind.Any, InstanceStateKind.Any, nameFinderExpr, nameFinderParams); ErrorHandler.checkHandle( nameFinder, "DDS.DataReader.create_querycondition (nameFinder)"); /* Create the Topic that simulates the multi-topic (use Qos from chatMessage).*/ status = chatMessageTopic.GetQos(ref namedMessageQos); ErrorHandler.checkStatus(status, "DDS.Topic.GetQos"); /* Create the NamedMessage Topic whose samples simulate the MultiTopic */ namedMessageTopic = realParticipant.CreateTopic( "Chat_NamedMessage", type_name, namedMessageQos); ErrorHandler.checkHandle( namedMessageTopic, "DDS.DomainParticipant.CreateTopic (NamedMessage)"); /* Adapt the default PublisherQos to write into the "ChatRoom" Partition. */ status = realParticipant.GetDefaultPublisherQos(ref pubQos); ErrorHandler.checkStatus( status, "DDS.DomainParticipant.get_default_publisher_qos"); pubQos.Partition.Name = new string[1]; pubQos.Partition.Name[0] = partitionName; /* Create a private Publisher for the multitopic simulator. */ multiPub = realParticipant.CreatePublisher(pubQos); ErrorHandler.checkHandle( multiPub, "DDS.DomainParticipant.create_publisher (for multitopic)"); DataWriterQos nmDwQos = new DataWriterQos(); TopicQos nmQos = new TopicQos(); multiPub.GetDefaultDataWriterQos(ref nmDwQos); namedMessageTopic.GetQos(ref nmQos); multiPub.CopyFromTopicQos(ref nmDwQos, nmQos); /* Create a DataWriter for the multitopic. */ parentWriter = multiPub.CreateDataWriter(namedMessageTopic, nmDwQos); ErrorHandler.checkHandle( parentWriter, "DDS.Publisher.CreateDatawriter (NamedMessage)"); /* Narrow the abstract parent into its typed representative. */ namedMessageDW = parentWriter as NamedMessageDataWriter; /* Store the relevant Entities in our Listener. */ msgListener.ChatMessageDR = chatMessageDR; msgListener.NameServiceDR = nameServiceDR; msgListener.NamedMessageDW = namedMessageDW; msgListener.NameFinder = nameFinder; msgListener.NameFinderParams = nameFinderParams; /* Return the simulated Multitopic. */ return namedMessageTopic; }
static void Main(string[] args) { int ownID = 1; string chatterName = null; int domain = DDS.DomainId.Default; string partitionName = "ChatRoom"; /* Options: Chatter [ownID [name]] */ if (args.Length > 0) { ownID = int.Parse(args[0]); if (args.Length > 1) { chatterName = args[1]; } } /* Create a DomainParticipantFactory and a DomainParticipant * (using Default QoS settings. */ DomainParticipantFactory dpf = DomainParticipantFactory.Instance; ErrorHandler.checkHandle(dpf, "DDS.DomainParticipantFactory.Instance"); IDomainParticipant participant = dpf.CreateParticipant(domain, null, StatusKind.Any); ErrorHandler.checkHandle(participant, "DDS.DomainParticipantFactory.CreateParticipant"); /* Register the required datatype for ChatMessage. */ ChatMessageTypeSupport chatMessageTS = new ChatMessageTypeSupport(); string chatMessageTypeName = chatMessageTS.TypeName; ReturnCode status = chatMessageTS.RegisterType( participant, chatMessageTypeName); ErrorHandler.checkStatus( status, "Chat.ChatMessageTypeSupport.RegisterType"); /* Register the required datatype for NameService. */ NameServiceTypeSupport nameServiceTS = new NameServiceTypeSupport(); string nameServiceTypeName = nameServiceTS.TypeName; status = nameServiceTS.RegisterType( participant, nameServiceTypeName); ErrorHandler.checkStatus( status, "Chat.NameServiceTypeSupport.RegisterType"); /* Initialise Qos variables */ TopicQos reliableTopicQos = new TopicQos(); TopicQos settingTopicQos = new TopicQos(); PublisherQos pubQos = new PublisherQos(); DataWriterQos dwQos = new DataWriterQos(); DataWriterQos nsDwQos = new DataWriterQos(); /* Set the ReliabilityQosPolicy to RELIABLE. */ status = participant.GetDefaultTopicQos(ref reliableTopicQos); ErrorHandler.checkStatus( status, "DDS.DomainParticipant.GetDefaultTopicQos"); reliableTopicQos.Reliability.Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos; /* Make the tailored QoS the new default. */ status = participant.SetDefaultTopicQos(reliableTopicQos); ErrorHandler.checkStatus( status, "DDS.DomainParticipant.SetDefaultTopicQos"); /* Use the changed policy when defining the ChatMessage topic */ ITopic chatMessageTopic = participant.CreateTopic( "Chat_ChatMessage", chatMessageTypeName, reliableTopicQos); ErrorHandler.checkHandle( chatMessageTopic, "DDS.DomainParticipant.CreateTopic (ChatMessage)"); /* Set the DurabilityQosPolicy to TRANSIENT. */ status = participant.GetDefaultTopicQos(ref settingTopicQos); ErrorHandler.checkStatus( status, "DDS.DomainParticipant.GetDefaultTopicQos"); settingTopicQos.Durability.Kind = DurabilityQosPolicyKind.TransientDurabilityQos; /* Create the NameService Topic. */ ITopic nameServiceTopic = participant.CreateTopic( "Chat_NameService", nameServiceTypeName, settingTopicQos); ErrorHandler.checkHandle( nameServiceTopic, "DDS.DomainParticipant.CreateTopic (NameService)"); /* Adapt the default PublisherQos to write into the * "ChatRoom" Partition. */ status = participant.GetDefaultPublisherQos(ref pubQos); ErrorHandler.checkStatus( status, "DDS.DomainParticipant.GetDefaultPublisherQos"); pubQos.Partition.Name = new string[1]; pubQos.Partition.Name[0] = partitionName; /* Create a Publisher for the chatter application. */ IPublisher chatPublisher = participant.CreatePublisher(pubQos); ErrorHandler.checkHandle( chatPublisher, "DDS.DomainParticipant.CreatePublisher"); /* Create a DataWriter for the ChatMessage Topic * (using the appropriate QoS). */ chatPublisher.GetDefaultDataWriterQos(ref dwQos); status = chatPublisher.CopyFromTopicQos(ref dwQos, reliableTopicQos); ErrorHandler.checkStatus(status, "DDS.Publisher.CopyFromTopicQos"); IDataWriter parentWriter = chatPublisher.CreateDataWriter(chatMessageTopic, dwQos); ErrorHandler.checkHandle( parentWriter, "DDS.Publisher.CreateDatawriter (chatMessage)"); /* Narrow the abstract parent into its typed representative. */ ChatMessageDataWriter talker = parentWriter as ChatMessageDataWriter; ErrorHandler.checkHandle( talker, "Chat.ChatMessageDataWriter"); /* Create a DataWriter for the NameService Topic * (using the appropriate QoS). */ status = chatPublisher.GetDefaultDataWriterQos(ref nsDwQos); ErrorHandler.checkStatus( status, "DDS.Publisher.GetDefaultDatawriterQos"); status = chatPublisher.CopyFromTopicQos(ref nsDwQos, settingTopicQos); ErrorHandler.checkStatus(status, "DDS.Publisher.CopyFromTopicQos"); WriterDataLifecycleQosPolicy writerDataLifecycle = nsDwQos.WriterDataLifecycle; writerDataLifecycle.AutodisposeUnregisteredInstances = false; IDataWriter nsParentWriter = chatPublisher.CreateDataWriter(nameServiceTopic, nsDwQos); ErrorHandler.checkHandle( nsParentWriter, "DDS.Publisher.CreateDatawriter (NameService)"); /* Narrow the abstract parent into its typed representative. */ NameServiceDataWriter nameServer = nsParentWriter as NameServiceDataWriter; ErrorHandler.checkHandle( nameServer, "Chat.NameServiceDataWriterHelper"); /* Initialize the NameServer attributes. */ NameService ns = new NameService(); ns.userID = ownID; if (chatterName != null) { ns.name = chatterName; } else { ns.name = "Chatter " + ownID; } /* Write the user-information into the system * (registering the instance implicitly). */ status = nameServer.Write(ns); ErrorHandler.checkStatus(status, "Chat.ChatMessageDataWriter.Write"); /* Initialize the chat messages. */ ChatMessage msg = new ChatMessage(); msg.userID = ownID; msg.index = 0; if (ownID == TERMINATION_MESSAGE) { msg.content = "Termination message."; } else { msg.content = "Hi there, I will send you " + NUM_MSG + " more messages."; } System.Console.WriteLine("Writing message: \"" + msg.content + "\""); /* Register a chat message for this user * (pre-allocating resources for it!!) */ InstanceHandle userHandle = talker.RegisterInstance(msg); /* Write a message using the pre-generated instance handle. */ status = talker.Write(msg, userHandle); ErrorHandler.checkStatus(status, "Chat.ChatMessageDataWriter.Write"); Thread.Sleep(1000); /* Write any number of messages . */ for (int i = 1; i <= NUM_MSG && ownID != TERMINATION_MESSAGE; i++) { msg.index = i; msg.content = "Message no. " + i; Console.WriteLine("Writing message: \"" + msg.content + "\""); status = talker.Write(msg, userHandle); ErrorHandler.checkStatus(status, "Chat.ChatMessageDataWriter.Write"); Thread.Sleep(1000); /* do not run so fast! */ } /* Leave the room by disposing and unregistering the message instance */ status = talker.Dispose(msg, userHandle); ErrorHandler.checkStatus( status, "Chat.ChatMessageDataWriter.Dispose"); status = talker.UnregisterInstance(msg, userHandle); ErrorHandler.checkStatus( status, "Chat.ChatMessageDataWriter.unregister_instance"); /* Also unregister our name. */ status = nameServer.UnregisterInstance(ns, InstanceHandle.Nil); ErrorHandler.checkStatus( status, "Chat.NameServiceDataWriter.unregister_instance"); /* Remove the DataWriters */ status = chatPublisher.DeleteDataWriter(talker); ErrorHandler.checkStatus( status, "DDS.Publisher.DeleteDatawriter (talker)"); status = chatPublisher.DeleteDataWriter(nameServer); ErrorHandler.checkStatus(status, "DDS.Publisher.DeleteDatawriter (nameServer)"); /* Remove the Publisher. */ status = participant.DeletePublisher(chatPublisher); ErrorHandler.checkStatus( status, "DDS.DomainParticipant.DeletePublisher"); /* Remove the Topics. */ status = participant.DeleteTopic(nameServiceTopic); ErrorHandler.checkStatus( status, "DDS.DomainParticipant.DeleteTopic (nameServiceTopic)"); status = participant.DeleteTopic(chatMessageTopic); ErrorHandler.checkStatus( status, "DDS.DomainParticipant.DeleteTopic (chatMessageTopic)"); /* Remove the DomainParticipant. */ status = dpf.DeleteParticipant(participant); ErrorHandler.checkStatus( status, "DDS.DomainParticipantFactory.DeleteParticipant"); }