/*** * 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(); status = multiSub.GetDefaultDataReaderQos(ref nsDrQos); ErrorHandler.checkStatus(status, "DDS.Subscriber.GetDefaultDataReaderQos"); 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); }