Skip to content

Commit

Permalink
Move blockUntilAvailableOrTimeout method to IoBridge
Browse files Browse the repository at this point in the history
... so that we can reuse this method in other classes.

Test: java.nio.channels.DatagramChannelMulticastTest
Change-Id: Ibf6c1a6bef3cc1d0f76387331159f0c9a383a919
  • Loading branch information
kongy committed Dec 6, 2016
1 parent 813c124 commit d880e69
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
24 changes: 23 additions & 1 deletion luni/src/main/java/libcore/io/IoBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.PortUnreachableException;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketOptions;
import java.net.SocketTimeoutException;
Expand Down Expand Up @@ -645,6 +644,29 @@ public static FileDescriptor socket(int domain, int type, int protocol) throws S
}
}

/**
* Wait for some event on a file descriptor, blocks until the event happened or timeout period
* passed. See poll(2) and @link{android.system.Os.Poll}.
*
* @throws SocketException if poll(2) fails.
* @throws SocketTimeoutException if the event has not happened before timeout period has passed.
*/
public static void poll(FileDescriptor fd, int events, int timeout)
throws SocketException, SocketTimeoutException {
StructPollfd[] pollFds = new StructPollfd[]{ new StructPollfd() };
pollFds[0].fd = fd;
pollFds[0].events = (short) events;

try {
int ret = android.system.Os.poll(pollFds, timeout);
if (ret == 0) {
throw new SocketTimeoutException("Poll timed out");
}
} catch (ErrnoException e) {
e.rethrowAsSocketException();
}
}

public static InetSocketAddress getLocalInetSocketAddress(FileDescriptor fd) throws SocketException {
try {
return (InetSocketAddress) Libcore.os.getsockname(fd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@

import junit.framework.AssertionFailedError;
import junit.framework.TestCase;

import android.system.ErrnoException;
import android.system.StructPollfd;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
Expand All @@ -32,6 +27,7 @@
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
Expand All @@ -40,6 +36,8 @@
import java.util.ArrayList;
import java.util.Enumeration;

import libcore.io.IoBridge;

import static android.system.OsConstants.POLLIN;

/**
Expand Down Expand Up @@ -780,7 +778,7 @@ private void test_block_filtersAsExpected(
// Send a message. It should be received.
String msg1 = "Hello1";
sendMessage(sendingChannel, msg1, groupSocketAddress);
blockUntilAvailableOrTimeout(receivingChannel.socket(), 1000);
IoBridge.poll(receivingChannel.socket().getFileDescriptor$(), POLLIN, 1000);
InetSocketAddress sourceAddress1 = (InetSocketAddress) receivingChannel.receive(receiveBuffer);
assertEquals(sourceAddress1, sendingAddress);
assertEquals(msg1, new String(receiveBuffer.array(), 0, receiveBuffer.position()));
Expand All @@ -791,7 +789,10 @@ private void test_block_filtersAsExpected(
// Send a message. It should be filtered.
String msg2 = "Hello2";
sendMessage(sendingChannel, msg2, groupSocketAddress);
blockUntilAvailableOrTimeout(receivingChannel.socket(), 1000);
try {
IoBridge.poll(receivingChannel.socket().getFileDescriptor$(), POLLIN, 1000);
fail();
} catch (SocketTimeoutException expected) { }
receiveBuffer.position(0);
InetSocketAddress sourceAddress2 = (InetSocketAddress) receivingChannel.receive(receiveBuffer);
assertNull(sourceAddress2);
Expand All @@ -802,7 +803,7 @@ private void test_block_filtersAsExpected(
// Send a message. It should be received.
String msg3 = "Hello3";
sendMessage(sendingChannel, msg3, groupSocketAddress);
blockUntilAvailableOrTimeout(receivingChannel.socket(), 1000);
IoBridge.poll(receivingChannel.socket().getFileDescriptor$(), POLLIN, 1000);
receiveBuffer.position(0);
InetSocketAddress sourceAddress3 = (InetSocketAddress) receivingChannel.receive(receiveBuffer);
assertEquals(sourceAddress3, sendingAddress);
Expand Down Expand Up @@ -1245,14 +1246,5 @@ private static InetAddress getLocalIpv6Address(NetworkInterface networkInterface
}
throw new AssertionFailedError("Unable to find local IPv6 address for " + networkInterface);
}

private static void blockUntilAvailableOrTimeout(DatagramSocket s, int timeout) {
try {
StructPollfd[] pollFds = new StructPollfd[]{ new StructPollfd() };
pollFds[0].fd = s.getFileDescriptor$();
pollFds[0].events = (short) POLLIN;
android.system.Os.poll(pollFds, timeout);
} catch (ErrnoException ignored) { }
}
}

0 comments on commit d880e69

Please sign in to comment.