public void ValidateUadpPubSubConnectionNetworkMessagePublishMulticast()
        {
            //Arrange
            var localhost = GetFirstNic();

            Assert.IsNotNull(localhost, "localhost is null");
            Assert.IsNotNull(localhost.Address, "localhost.Address is null");

            //create publisher configuration object with modified port
            PubSubConfigurationDataType publisherConfiguration = UaPubSubConfigurationHelper.LoadConfiguration(PublisherConfigurationFileName);

            Assert.IsNotNull(publisherConfiguration, "publisherConfiguration is null");

            IPAddress multicastIPAddress = new IPAddress(new byte[4] {
                239, 0, 0, 1
            });

            Assert.IsNotNull(multicastIPAddress, "multicastIPAddress is null");

            NetworkAddressUrlDataType publisherAddress = new NetworkAddressUrlDataType();

            publisherAddress.Url = "opc.udp://" + multicastIPAddress.ToString() + ":4840";
            publisherConfiguration.Connections[0].Address = new ExtensionObject(publisherAddress);

            //create publisher UaPubSubApplication with changed configuration settings
            UaPubSubApplication publisherApplication = UaPubSubApplication.Create(publisherConfiguration);

            Assert.IsNotNull(publisherApplication, "publisherApplication is null");

            UadpPubSubConnection publisherConnection = publisherApplication.PubSubConnections[0] as UadpPubSubConnection;

            Assert.IsNotNull(publisherConnection, "publisherConnection is null");

            // will signal that the uadp message was received from local ip
            m_shutdownEvent = new ManualResetEvent(false);

            //setup uadp client for receiving from multicast (simulate a subscriber multicast)
            UdpClient udpMulticastClient = new UdpClientMulticast(localhost.Address, multicastIPAddress, 4840);

            udpMulticastClient.BeginReceive(new AsyncCallback(OnReceive), udpMulticastClient);

            // prepare a network message
            WriterGroupDataType writerGroup0   = publisherConnection.PubSubConnectionConfiguration.WriterGroups[0];
            UaNetworkMessage    networkMessage = publisherConnection.CreateNetworkMessage(writerGroup0);

            //Act
            publisherConnection.Start();
            publisherConnection.PublishNetworkMessage(networkMessage);

            //Assert
            if (!m_shutdownEvent.WaitOne(EstimatedPublishingTime))
            {
                Assert.Fail("The UADP message was not received");
            }

            publisherConnection.Stop();
            udpMulticastClient.Close();
            udpMulticastClient.Dispose();
        }
コード例 #2
0
        public void ValidateUdpPubSubConnectionNetworkMessageDiscoveryPublish()
        {
            //Arrange
            var localhost = GetFirstNic();

            Assert.IsNotNull(localhost, "localhost is null");
            Assert.IsNotNull(localhost.Address, "localhost.Address is null");

            //create publisher configuration object with modified port
            string configurationFile = Utils.GetAbsoluteFilePath(PublisherConfigurationFileName, true, true, false);
            PubSubConfigurationDataType publisherConfiguration = UaPubSubConfigurationHelper.LoadConfiguration(configurationFile);

            Assert.IsNotNull(publisherConfiguration, "publisherConfiguration is null");
            Assert.Greater(publisherConfiguration.Connections.Count, 1, "publisherConfiguration.Connection should be > 0");

            //discovery IP adress 224.0.2.14
            IPAddress[] multicastIPAddresses = Dns.GetHostAddresses(UdpDiscoveryIp);
            IPAddress   multicastIPAddress   = multicastIPAddresses.First();

            Assert.IsNotNull(multicastIPAddress, "multicastIPAddress is null");

            NetworkAddressUrlDataType publisherAddress = new NetworkAddressUrlDataType();

            publisherAddress.Url = string.Format(UdpUrlFormat, Utils.UriSchemeOpcUdp, multicastIPAddress.ToString());
            publisherConfiguration.Connections[0].Address = new ExtensionObject(publisherAddress);

            //create publisher UaPubSubApplication with changed configuration settings
            UaPubSubApplication publisherApplication = UaPubSubApplication.Create(publisherConfiguration);

            Assert.IsNotNull(publisherApplication, "publisherApplication is null");

            UdpPubSubConnection publisherConnection = publisherApplication.PubSubConnections.First() as UdpPubSubConnection;

            Assert.IsNotNull(publisherConnection, "publisherConnection is null");

            // will signal that the uadp message was received from local ip
            m_shutdownEvent = new ManualResetEvent(false);

            //setup uadp client for receiving from multicast (simulate a subscriber multicast)
            UdpClient udpMulticastClient = new UdpClientMulticast(localhost.Address, multicastIPAddress, DiscoveryPortNo);

            udpMulticastClient.BeginReceive(new AsyncCallback(OnReceive), udpMulticastClient);

            // prepare a network message
            WriterGroupDataType writerGroup0     = publisherConnection.PubSubConnectionConfiguration.WriterGroups.First();
            List <UInt16>       dataSetWriterIds = new List <UInt16>();

            foreach (DataSetWriterDataType dataSetWriterDataType in writerGroup0.DataSetWriters)
            {
                dataSetWriterIds.Add(dataSetWriterDataType.DataSetWriterId);
            }
            IList <UaNetworkMessage> networkMessages = publisherConnection.CreateDataSetMetaDataNetworkMessages(dataSetWriterIds.ToArray());

            Assert.IsNotNull(networkMessages, "connection.CreateNetworkMessages shall not return null");

            //Act
            publisherConnection.Start();

            if (networkMessages != null)
            {
                foreach (UaNetworkMessage uaNetworkMessage in networkMessages)
                {
                    if (uaNetworkMessage != null)
                    {
                        publisherConnection.PublishNetworkMessage(uaNetworkMessage);
                    }
                }
            }

            //Assert
            bool noMessageReceived = false;

            if (!m_shutdownEvent.WaitOne(EstimatedPublishingTime))
            {
                noMessageReceived = true;
            }

            publisherConnection.Stop();
            udpMulticastClient.Close();
            udpMulticastClient.Dispose();

            if (noMessageReceived)
            {
                Assert.Fail("The UDP message was not received");
            }
        }