Search in sources :

Example 36 with InterfaceAddress

use of java.net.InterfaceAddress in project mmupnp by ohmae.

the class Service method getCallback.

// VisibleForTesting
@Nonnull
String getCallback() {
    final StringBuilder sb = new StringBuilder();
    sb.append("<http://");
    final SsdpMessage ssdp = mDevice.getSsdpMessage();
    final InterfaceAddress ifa = ssdp.getInterfaceAddress();
    final int port = mControlPoint.getEventPort();
    // noinspection ConstantConditions : 受信したメッセージの場合はnullではない
    sb.append(NetworkUtils.getAddressString(ifa.getAddress(), port));
    sb.append("/>");
    return sb.toString();
}
Also used : InterfaceAddress(java.net.InterfaceAddress) Nonnull(javax.annotation.Nonnull)

Example 37 with InterfaceAddress

use of java.net.InterfaceAddress in project mmupnp by ohmae.

the class SsdpSearchServerTest method search_ST_ALLでのサーチ.

@Test
public void search_ST_ALLでのサーチ() throws Exception {
    final NetworkInterface networkInterface = NetworkUtils.getAvailableInet4Interfaces().get(0);
    final SsdpServerDelegate delegate = spy(new SsdpServerDelegate(mock(Receiver.class), Address.IP_V4, networkInterface));
    final InterfaceAddress interfaceAddress = TestUtils.createInterfaceAddress("192.0.2.2", "255.255.255.0", 16);
    doReturn(interfaceAddress).when(delegate).getInterfaceAddress();
    final MockMulticastSocket socket = new MockMulticastSocket();
    doReturn(socket).when(delegate).createMulticastSocket(anyInt());
    final SsdpSearchServer server = spy(new SsdpSearchServer(delegate));
    server.open();
    server.start();
    server.search();
    server.stop();
    server.close();
    final DatagramPacket packet = socket.getSendPacket();
    final SsdpRequest message = new SsdpRequest(mock(InterfaceAddress.class), packet.getData(), packet.getLength());
    assertThat(message.getMethod(), is(SsdpMessage.M_SEARCH));
    assertThat(message.getHeader(Http.ST), is(SsdpSearchServer.ST_ALL));
    assertThat(message.getHeader(Http.MAN), is(SsdpMessage.SSDP_DISCOVER));
}
Also used : InterfaceAddress(java.net.InterfaceAddress) DatagramPacket(java.net.DatagramPacket) NetworkInterface(java.net.NetworkInterface) Test(org.junit.Test)

Example 38 with InterfaceAddress

use of java.net.InterfaceAddress in project mmupnp by ohmae.

the class SsdpServerDelegateTest method findInet6Address_見つからなければException1.

@Test(expected = IllegalArgumentException.class)
public void findInet6Address_見つからなければException1() throws Exception {
    final InterfaceAddress ipv4 = TestUtils.createInterfaceAddress("192.168.0.1", "255.255.255.0", 24);
    SsdpServerDelegate.findInet6Address(Arrays.asList(ipv4));
}
Also used : InterfaceAddress(java.net.InterfaceAddress) Test(org.junit.Test)

Example 39 with InterfaceAddress

use of java.net.InterfaceAddress in project mmupnp by ohmae.

the class SsdpServerDelegateTest method findInet4Address_見つからなければException1.

@Test(expected = IllegalArgumentException.class)
public void findInet4Address_見つからなければException1() throws Exception {
    final InterfaceAddress ipv6 = TestUtils.createInterfaceAddress("fe80::a831:801b:8dc6:421f", "255.255.0.0", 16);
    SsdpServerDelegate.findInet4Address(Arrays.asList(ipv6, ipv6));
}
Also used : InterfaceAddress(java.net.InterfaceAddress) Test(org.junit.Test)

Example 40 with InterfaceAddress

use of java.net.InterfaceAddress in project mmupnp by ohmae.

the class SsdpSearchServerTest method setResponseListener_受信メッセージが通知されること.

@Test
public void setResponseListener_受信メッセージが通知されること() throws Exception {
    final SsdpServerDelegate delegate = mock(SsdpServerDelegate.class);
    final InterfaceAddress interfaceAddress = TestUtils.createInterfaceAddress("192.0.2.2", "255.255.255.0", 16);
    doReturn(interfaceAddress).when(delegate).getInterfaceAddress();
    final SsdpSearchServer server = spy(new SsdpSearchServer(delegate));
    final byte[] data = TestUtils.getResourceAsByteArray("ssdp-search-response0.bin");
    final InetAddress address = InetAddress.getByName("192.0.2.2");
    final ArgumentCaptor<SsdpResponse> captor = ArgumentCaptor.forClass(SsdpResponse.class);
    final ResponseListener listener = mock(ResponseListener.class);
    doNothing().when(listener).onReceiveResponse(captor.capture());
    server.setResponseListener(listener);
    server.onReceive(address, data, data.length);
    SsdpResponse response = captor.getValue();
    assertThat(response.getStatus(), is(Http.Status.HTTP_OK));
    assertThat(response.getUuid(), is("uuid:01234567-89ab-cdef-0123-456789abcdef"));
}
Also used : InterfaceAddress(java.net.InterfaceAddress) ResponseListener(net.mm2d.upnp.SsdpSearchServer.ResponseListener) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

InterfaceAddress (java.net.InterfaceAddress)94 NetworkInterface (java.net.NetworkInterface)63 InetAddress (java.net.InetAddress)52 Inet4Address (java.net.Inet4Address)26 SocketException (java.net.SocketException)24 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)15 IOException (java.io.IOException)9 UnknownHostException (java.net.UnknownHostException)7 NotifyListener (net.mm2d.upnp.SsdpNotifyReceiver.NotifyListener)7 Command (com.android.server.NativeDaemonConnector.Command)6 Inet6Address (java.net.Inet6Address)6 LinkAddress (android.net.LinkAddress)5 DatagramPacket (java.net.DatagramPacket)5 InetSocketAddress (java.net.InetSocketAddress)4 List (java.util.List)4 SharedPreferences (android.content.SharedPreferences)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Enumeration (java.util.Enumeration)3 LinkedList (java.util.LinkedList)3