Merge "Move blockUntilAvailableOrTimeout method to IoBridge"
diff --git a/luni/src/main/java/libcore/io/IoBridge.java b/luni/src/main/java/libcore/io/IoBridge.java
index f5177fd..e5d1930 100644
--- a/luni/src/main/java/libcore/io/IoBridge.java
+++ b/luni/src/main/java/libcore/io/IoBridge.java
@@ -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;
@@ -645,6 +644,29 @@
}
}
+ /**
+ * 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);
diff --git a/luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java b/luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java
index cf3af30..ae45c0d 100644
--- a/luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java
+++ b/luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java
@@ -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;
@@ -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;
@@ -40,6 +36,8 @@
import java.util.ArrayList;
import java.util.Enumeration;
+import libcore.io.IoBridge;
+
import static android.system.OsConstants.POLLIN;
/**
@@ -780,7 +778,7 @@
// 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()));
@@ -791,7 +789,10 @@
// 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);
@@ -802,7 +803,7 @@
// 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);
@@ -1245,14 +1246,5 @@
}
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) { }
- }
}