Search in sources :

Example 6 with DatagramChannel

use of java.nio.channels.DatagramChannel in project robovm by robovm.

the class DatagramChannelTest method test_read_LByteBuffer_closed_nullBuf.

/**
     * @tests DatagramChannel#read(ByteBuffer)
     */
public void test_read_LByteBuffer_closed_nullBuf() throws Exception {
    // regression test for Harmony-754
    ByteBuffer c = null;
    DatagramChannel channel = DatagramChannel.open();
    channel.close();
    try {
        channel.read(c);
        fail("Should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel) ByteBuffer(java.nio.ByteBuffer)

Example 7 with DatagramChannel

use of java.nio.channels.DatagramChannel in project robovm by robovm.

the class DatagramChannelTest method test_write_LBuffer_positioned.

public void test_write_LBuffer_positioned() throws Exception {
    // Regression test for Harmony-683
    int position = 16;
    DatagramChannel dc = DatagramChannel.open();
    byte[] sourceArray = new byte[CAPACITY_NORMAL];
    dc.connect(localAddr1);
    // write
    ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray);
    sourceBuf.position(position);
    assertEquals(CAPACITY_NORMAL - position, dc.write(sourceBuf));
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel) ByteBuffer(java.nio.ByteBuffer)

Example 8 with DatagramChannel

use of java.nio.channels.DatagramChannel in project robovm by robovm.

the class DatagramChannelTest method test_read_LByteBuffer_readOnlyBuf.

/**
     * @tests DatagramChannel#read(ByteBuffer)
     */
public void test_read_LByteBuffer_readOnlyBuf() throws Exception {
    // regression test for Harmony-754
    ByteBuffer c = ByteBuffer.allocate(1);
    DatagramChannel channel = DatagramChannel.open();
    try {
        channel.read(c.asReadOnlyBuffer());
        fail("Should throw NotYetConnectedException");
    } catch (NotYetConnectedException e) {
    } catch (IllegalArgumentException e) {
    // expected
    }
    channel.connect(localAddr1);
    try {
        channel.read(c.asReadOnlyBuffer());
        fail("Should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // expected
    }
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel) NotYetConnectedException(java.nio.channels.NotYetConnectedException) ByteBuffer(java.nio.ByteBuffer)

Example 9 with DatagramChannel

use of java.nio.channels.DatagramChannel in project robovm by robovm.

the class OldDatagramSocketTest method test_sendLjava_net_DatagramPacket.

public void test_sendLjava_net_DatagramPacket() throws Exception {
    // Test for method void
    // java.net.DatagramSocket.send(java.net.DatagramPacket)
    int[] ports = Support_PortManager.getNextPortsForUDP(2);
    final int portNumber = ports[0];
    class TestDGSend implements Runnable {

        Thread pThread;

        public TestDGSend(Thread t) {
            pThread = t;
        }

        public void run() {
            try {
                byte[] rbuf = new byte[1000];
                sds = new DatagramSocket(portNumber);
                DatagramPacket sdp = new DatagramPacket(rbuf, rbuf.length);
                sds.setSoTimeout(6000);
                sds.receive(sdp);
                retval = new String(rbuf, 0, testString.length());
                pThread.interrupt();
            } catch (java.io.InterruptedIOException e) {
                System.out.println("Recv operation timed out");
                pThread.interrupt();
                ds.close();
            } catch (Exception e) {
                System.out.println("Failed to establish Dgram server: " + e);
            }
        }
    }
    try {
        new Thread(new TestDGSend(Thread.currentThread()), "DGServer").start();
        ds = new java.net.DatagramSocket(ports[1]);
        dp = new DatagramPacket(testString.getBytes(), testString.length(), InetAddress.getLocalHost(), portNumber);
        // Wait to allow send to occur
        try {
            Thread.sleep(500);
            ds.send(dp);
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            ds.close();
            assertTrue("Incorrect data sent: " + retval, retval.equals(testString));
        }
    } catch (Exception e) {
        fail("Exception during send test : " + e.getMessage());
    } finally {
        ds.close();
    }
    /*
        SecurityManager sm = new SecurityManager() {

            public void checkPermission(Permission perm) {
            }

            public void checkMulticast(InetAddress maddr) {
                throw new SecurityException();
            }

            public void checkConnect(String host,
                    int port) {
                throw new SecurityException();
            }
        };
        try {

            ds = new java.net.DatagramSocket(ports[1]);
            dp = new DatagramPacket(testString.getBytes(), testString.length(),
                    InetAddress.getLocalHost(), portNumber);

            SecurityManager oldSm = System.getSecurityManager();
            System.setSecurityManager(sm);
            try {
                ds.send(dp);
                fail("SecurityException should be thrown.");
            } catch (SecurityException e) {
                // expected
            } catch (SocketException e) {
                fail("SocketException was thrown.");
            } finally {
                System.setSecurityManager(oldSm);
            }
        } catch(Exception e) {
            fail("Unexpected exception was thrown: " + e.getMessage());
        }
        */
    DatagramSocket socket = null;
    try {
        byte[] rbuf = new byte[1000];
        DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length);
        SocketAddress address = new InetSocketAddress(portNumber);
        DatagramChannel channel = DatagramChannel.open();
        channel.configureBlocking(false);
        socket = channel.socket();
        socket.send(rdp);
        fail("IllegalBlockingModeException was not thrown.");
    } catch (IllegalBlockingModeException ibme) {
    //expected
    } catch (IOException ioe) {
        fail("IOException was thrown: " + ioe.getMessage());
    } finally {
        socket.close();
    }
    //Regression for HARMONY-1118
    class testDatagramSocket extends DatagramSocket {

        public testDatagramSocket(DatagramSocketImpl impl) {
            super(impl);
        }
    }
    class testDatagramSocketImpl extends DatagramSocketImpl {

        protected void create() throws SocketException {
        }

        protected void bind(int arg0, InetAddress arg1) throws SocketException {
        }

        protected void send(DatagramPacket arg0) throws IOException {
        }

        protected int peek(InetAddress arg0) throws IOException {
            return 0;
        }

        protected int peekData(DatagramPacket arg0) throws IOException {
            return 0;
        }

        protected void receive(DatagramPacket arg0) throws IOException {
        }

        protected void setTTL(byte arg0) throws IOException {
        }

        protected byte getTTL() throws IOException {
            return 0;
        }

        protected void setTimeToLive(int arg0) throws IOException {
        }

        protected int getTimeToLive() throws IOException {
            return 0;
        }

        protected void join(InetAddress arg0) throws IOException {
        }

        protected void leave(InetAddress arg0) throws IOException {
        }

        protected void joinGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {
        }

        protected void leaveGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {
        }

        protected void close() {
        }

        public void setOption(int arg0, Object arg1) throws SocketException {
        }

        public Object getOption(int arg0) throws SocketException {
            return null;
        }
    }
    InetSocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), 0);
    //no exception expected for next line
    new testDatagramSocket(new testDatagramSocketImpl()).send(new DatagramPacket(new byte[272], 3, sa));
    // Regression test for Harmony-2938
    InetAddress i = InetAddress.getByName("127.0.0.1");
    DatagramSocket d = new DatagramSocket(0, i);
    try {
        d.send(new DatagramPacket(new byte[] { 1 }, 1));
        fail("should throw NPE.");
    } catch (NullPointerException e) {
    // expected;
    } finally {
        d.close();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InterruptedIOException(java.io.InterruptedIOException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) BindException(java.net.BindException) InterruptedIOException(java.io.InterruptedIOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) SocketTimeoutException(java.net.SocketTimeoutException) PortUnreachableException(java.net.PortUnreachableException) DatagramSocketImpl(java.net.DatagramSocketImpl) DatagramSocket(java.net.DatagramSocket) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) InetAddress(java.net.InetAddress)

Example 10 with DatagramChannel

use of java.nio.channels.DatagramChannel in project robovm by robovm.

the class OldDatagramSocketTest method test_getChannel.

public void test_getChannel() throws Exception {
    assertNull(new DatagramSocket().getChannel());
    int portNumber = Support_PortManager.getNextPortForUDP();
    DatagramSocket ds = null;
    try {
        InetAddress ia = InetAddress.getByName(Support_Configuration.IPv6GlobalAddressJcl4);
        ds = new DatagramSocket();
        assertNull(ds.getChannel());
        ds.connect(ia, portNumber);
        assertNull(ds.getChannel());
    } catch (SocketException e) {
        fail("SocketException was thrown.");
    } finally {
        ds.disconnect();
        ds.close();
    }
    portNumber = Support_PortManager.getNextPortForUDP();
    SocketAddress address = new InetSocketAddress(portNumber);
    DatagramChannel channel = DatagramChannel.open();
    DatagramSocket socket = channel.socket();
    assertEquals(channel, socket.getChannel());
    socket.close();
}
Also used : SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Aggregations

DatagramChannel (java.nio.channels.DatagramChannel)214 InetSocketAddress (java.net.InetSocketAddress)92 ByteBuffer (java.nio.ByteBuffer)71 IOException (java.io.IOException)58 MembershipKey (java.nio.channels.MembershipKey)22 DatagramSocket (java.net.DatagramSocket)21 SocketAddress (java.net.SocketAddress)21 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 SocketChannel (java.nio.channels.SocketChannel)17 SelectionKey (java.nio.channels.SelectionKey)16 InetAddress (java.net.InetAddress)13 Selector (java.nio.channels.Selector)13 Test (org.junit.Test)11 SocketException (java.net.SocketException)9 ClosedChannelException (java.nio.channels.ClosedChannelException)9 Histogram (org.HdrHistogram.Histogram)8 CancelledKeyException (java.nio.channels.CancelledKeyException)7 DatagramPacket (java.net.DatagramPacket)5 NetworkInterface (java.net.NetworkInterface)5 ArrayList (java.util.ArrayList)5