Example usage for org.springframework.integration.ip.util SocketTestUtils chooseANic

List of usage examples for org.springframework.integration.ip.util SocketTestUtils chooseANic

Introduction

In this page you can find the example usage for org.springframework.integration.ip.util SocketTestUtils chooseANic.

Prototype

public static String chooseANic(boolean multicast) throws Exception 

Source Link

Usage

From source file:org.springframework.integration.ip.udp.MulticastRule.java

private String checkMulticast() throws Exception {
    String nic = SocketTestUtils.chooseANic(true);
    if (nic == null) { // no multicast support
        this.skip = true;
        return null;
    }// w  ww  . ja  v a  2 s  .  c  om
    try {
        MulticastSocket socket = new MulticastSocket();
        socket.joinGroup(InetAddress.getByName(this.group));
        socket.close();
    } catch (Exception e) {
        this.skip = true;
        // Ignore. Assume no Multicast - skip the test.
    }
    return nic;
}

From source file:org.springframework.integration.ip.udp.UdpChannelAdapterTests.java

@SuppressWarnings("unchecked")
@Test//from ww  w.j  a v  a  2 s  .c om
@Ignore
public void testMulticastReceiver() throws Exception {
    QueueChannel channel = new QueueChannel(2);
    int port = SocketUtils.findAvailableUdpSocket();
    MulticastReceivingChannelAdapter adapter = new MulticastReceivingChannelAdapter("225.6.7.8", port);
    adapter.setOutputChannel(channel);
    String nic = SocketTestUtils.chooseANic(true);
    if (nic == null) { // no multicast support
        LogFactory.getLog(this.getClass()).error("No Multicast support");
        return;
    }
    adapter.setLocalAddress(nic);
    adapter.start();
    SocketTestUtils.waitListening(adapter);

    Message<byte[]> message = MessageBuilder.withPayload("ABCD".getBytes()).build();
    DatagramPacketMessageMapper mapper = new DatagramPacketMessageMapper();
    DatagramPacket packet = mapper.fromMessage(message);
    packet.setSocketAddress(new InetSocketAddress("225.6.7.8", port));
    new DatagramSocket(0, Inet4Address.getByName(nic)).send(packet);

    Message<byte[]> receivedMessage = (Message<byte[]>) channel.receive(2000);
    assertNotNull(receivedMessage);
    assertEquals(new String(message.getPayload()), new String(receivedMessage.getPayload()));
}

From source file:org.springframework.integration.ip.udp.UdpChannelAdapterTests.java

@SuppressWarnings("unchecked")
@Test//from   w ww .ja  v  a  2 s .co  m
@Ignore
public void testMulticastSender() throws Exception {
    QueueChannel channel = new QueueChannel(2);
    int port = SocketUtils.findAvailableUdpSocket();
    UnicastReceivingChannelAdapter adapter = new MulticastReceivingChannelAdapter("225.6.7.9", port);
    adapter.setOutputChannel(channel);
    String nic = SocketTestUtils.chooseANic(true);
    if (nic == null) { // no multicast support
        LogFactory.getLog(this.getClass()).error("No Multicast support");
        return;
    }
    adapter.setLocalAddress(nic);
    adapter.start();
    SocketTestUtils.waitListening(adapter);

    MulticastSendingMessageHandler handler = new MulticastSendingMessageHandler("225.6.7.9", port);
    handler.setLocalAddress(nic);
    Message<byte[]> message = MessageBuilder.withPayload("ABCD".getBytes()).build();
    handler.handleMessage(message);

    Message<byte[]> receivedMessage = (Message<byte[]>) channel.receive(2000);
    assertNotNull(receivedMessage);
    assertEquals(new String(message.getPayload()), new String(receivedMessage.getPayload()));
}