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(); }
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(); }