public void ValidateUadpPubSubConnectionNetworkMessagePublishUnicast()
        {
            //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 unicastIPAddress = localhost.Address;

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

            NetworkAddressUrlDataType publisherAddress = new NetworkAddressUrlDataType();

            publisherAddress.Url = "opc.udp://" + unicastIPAddress.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 unicast)
            UdpClient udpUnicastClient = new UdpClientUnicast(localhost.Address, 4840);

            Assert.IsNotNull(udpUnicastClient, "udpUnicastClient is null");
            udpUnicastClient.BeginReceive(new AsyncCallback(OnReceive), udpUnicastClient);

            // 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();
            udpUnicastClient.Close();
            udpUnicastClient.Dispose();
        }
        public void ValidateUadpPubSubConnectionNetworkMessageReceiveFromUnicast()
        {
            // Arrange
            var localhost = GetFirstNic();

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

            PubSubConfigurationDataType subscriberConfiguration = UaPubSubConfigurationHelper.LoadConfiguration(SubscriberConfigurationFileName);

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

            NetworkAddressUrlDataType subscriberAddress = new NetworkAddressUrlDataType();

            subscriberAddress.Url = "opc.udp://" + localhost.Address.ToString() + ":4840";
            subscriberConfiguration.Connections[0].Address = new ExtensionObject(subscriberAddress);
            UaPubSubApplication subscriberApplication = UaPubSubApplication.Create(subscriberConfiguration);

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

            UadpPubSubConnection subscriberConnection = subscriberApplication.PubSubConnections[0] as UadpPubSubConnection;

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

            subscriberConnection.UadpMessageReceived += DataReceived;

            PubSubConfigurationDataType publisherConfiguration = UaPubSubConfigurationHelper.LoadConfiguration(PublisherConfigurationFileName);

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

            NetworkAddressUrlDataType publisherAddress = new NetworkAddressUrlDataType();

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

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

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

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

            //Act
            subscriberConnection.Start();
            m_shutdownEvent = new ManualResetEvent(false);

            // physical network ip is mandatory on UdpClientUnicast as parameter
            UdpClient udpUnicastClient = new UdpClientUnicast(localhost.Address, 4840);

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

            // first physical network ip = unicast address ip
            IPEndPoint remoteEndPoint = new IPEndPoint(localhost.Address, 4840);

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

            m_sentBytes = PrepareData(publisherConnection);
            int sentBytesLen = udpUnicastClient.Send(m_sentBytes, m_sentBytes.Length, remoteEndPoint);

            Assert.AreEqual(sentBytesLen, m_sentBytes.Length, "Sent bytes size not equal to published bytes size!");

            Thread.Sleep(EstimatedPublishingTime);

            // Assert
            if (!m_shutdownEvent.WaitOne(EstimatedPublishingTime))
            {
                Assert.Fail("Subscriber unicast error ... published data not received");
            }

            subscriberConnection.Stop();
        }
コード例 #3
0
        public void ValidateUdpPubSubConnectionNetworkMessagePublishUnicast()
        {
            //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");

            IPAddress unicastIPAddress = localhost.Address;

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

            NetworkAddressUrlDataType publisherAddress = new NetworkAddressUrlDataType();

            publisherAddress.Url = string.Format(UdpUrlFormat, Utils.UriSchemeOpcUdp, unicastIPAddress.ToString());
            publisherConfiguration.Connections.First().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 unicast)
            UdpClient udpUnicastClient = new UdpClientUnicast(localhost.Address, DiscoveryPortNo);

            Assert.IsNotNull(udpUnicastClient, "udpUnicastClient is null");
            udpUnicastClient.BeginReceive(new AsyncCallback(OnReceive), udpUnicastClient);

            // prepare a network message
            WriterGroupDataType writerGroup0 = publisherConnection.PubSubConnectionConfiguration.WriterGroups.First();

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

            System.Collections.Generic.IList <UaNetworkMessage> networkMessages = publisherConnection.CreateNetworkMessages(writerGroup0, new WriterGroupPublishState());
            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();
            udpUnicastClient.Close();
            udpUnicastClient.Dispose();

            if (noMessageReceived == true)
            {
                Assert.Fail("The UDP message was not received");
            }
        }
コード例 #4
0
        public void ValidateUdpPubSubConnectionNetworkMessageReceiveFromUnicast()
        {
            // Arrange
            var localhost = GetFirstNic();

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

            string configurationFile = Utils.GetAbsoluteFilePath(m_subscriberConfigurationFileName, true, true, false);
            PubSubConfigurationDataType subscriberConfiguration = UaPubSubConfigurationHelper.LoadConfiguration(configurationFile);

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

            NetworkAddressUrlDataType subscriberAddress = new NetworkAddressUrlDataType();

            subscriberAddress.Url = string.Format(kUdpUrlFormat, Utils.UriSchemeOpcUdp, localhost.Address.ToString());
            subscriberConfiguration.Connections.First().Address = new ExtensionObject(subscriberAddress);
            UaPubSubApplication subscriberApplication           = UaPubSubApplication.Create(subscriberConfiguration);

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

            UdpPubSubConnection subscriberConnection = subscriberApplication.PubSubConnections[0] as UdpPubSubConnection;

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

            subscriberApplication.RawDataReceived += RawDataReceived;

            configurationFile = Utils.GetAbsoluteFilePath(m_publisherConfigurationFileName, true, true, false);
            PubSubConfigurationDataType publisherConfiguration = UaPubSubConfigurationHelper.LoadConfiguration(configurationFile);

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

            NetworkAddressUrlDataType publisherAddress = new NetworkAddressUrlDataType();

            publisherAddress.Url = string.Format(kUdpUrlFormat, Utils.UriSchemeOpcUdp, localhost.Address.ToString());
            publisherConfiguration.Connections.First().Address = new ExtensionObject(publisherAddress);
            UaPubSubApplication publisherApplication           = UaPubSubApplication.Create(publisherConfiguration);

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

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

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

            //Act
            subscriberConnection.Start();
            m_shutdownEvent = new ManualResetEvent(false);

            // physical network ip is mandatory on UdpClientUnicast as parameter
            UdpClient udpUnicastClient = new UdpClientUnicast(localhost.Address, kDiscoveryPortNo);

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

            // first physical network ip = unicast address ip
            IPEndPoint remoteEndPoint = new IPEndPoint(localhost.Address, kDiscoveryPortNo);

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

            m_sentBytes = PrepareData(publisherConnection);
            int sentBytesLen = udpUnicastClient.Send(m_sentBytes, m_sentBytes.Length, remoteEndPoint);

            Assert.AreEqual(sentBytesLen, m_sentBytes.Length, "Sent bytes size not equal to published bytes size!");

            Thread.Sleep(kEstimatedPublishingTime);

            // Assert
            if (!m_shutdownEvent.WaitOne(kEstimatedPublishingTime))
            {
                Assert.Fail("Subscriber unicast error ... published data not received");
            }

            subscriberConnection.Stop();
        }