Search in sources :

Example 6 with Buffers

use of org.jgroups.nio.Buffers in project JGroups by belaban.

the class BuffersTest method testCopyWithPartialWrite.

public void testCopyWithPartialWrite() throws Exception {
    final String s1 = "hello", s2 = " world", s3 = " from ", s4 = "Bela";
    ByteBuffer a = ByteBuffer.wrap(s1.getBytes()), b = ByteBuffer.wrap(s2.getBytes()), c = ByteBuffer.wrap(s3.getBytes()), d = ByteBuffer.wrap(s4.getBytes());
    Buffers bufs = new Buffers(a, b, c, d);
    ByteBuffer recorder = ByteBuffer.allocate(100);
    MockSocketChannel ch = // a, b: OK, c: partial, d: fail
    new MockSocketChannel().bytesToWrite(13).recorder(// we're recording the writes so we can compare expected bytes to actually written bytes
    recorder);
    boolean success = bufs.write(ch);
    System.out.println("bufs = " + bufs);
    assert !success;
    assert bufs.position() == 2;
    assert bufs.limit() == 4;
    // copy the buffers which have not yet been written so that we can reuse buffers (not needed if buffers are already copies)
    // https://issues.jboss.org/browse/JGRP-1991
    bufs.copy();
    makeSpace(bufs);
    assert bufs.position() == 0;
    assert bufs.limit() == 2;
    assert bufs.nextToCopy() == 2;
    // now modify the original buffers
    for (ByteBuffer buf : Arrays.asList(a, b, c, d)) buf.putInt(0, 322649);
    // next write will be successful
    ch.bytesToWrite(100);
    success = bufs.write(ch);
    System.out.println("bufs = " + bufs);
    assert success;
    assert bufs.position() == 2;
    assert bufs.limit() == 2;
    // now compare the contents of the buffers
    recorder.flip();
    for (String s : Arrays.asList(s1, s2, s3, s4)) {
        byte[] expected = s.getBytes();
        byte[] actual = new byte[expected.length];
        recorder.get(actual);
        assert Arrays.equals(expected, actual) : String.format("expected %s, got %s\n", s, new String(actual));
    }
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers) ByteBuffer(java.nio.ByteBuffer)

Example 7 with Buffers

use of org.jgroups.nio.Buffers in project JGroups by belaban.

the class BuffersTest method testPartialRead.

public void testPartialRead() throws Exception {
    byte[] tmp = "hello world".getBytes();
    ByteBuffer data = ByteBuffer.allocate(Global.INT_SIZE + tmp.length).putInt(tmp.length).put(tmp);
    // read only the first 2 bytes of the length
    data.flip().limit(2);
    MockSocketChannel ch = new MockSocketChannel().bytesToRead(data);
    Buffers bufs = new Buffers(ByteBuffer.allocate(Global.INT_SIZE), null);
    ByteBuffer rc = bufs.readLengthAndData(ch);
    assert rc == null;
    // we can now read the remaining 2 bytes to complete the length, plus 4 bytes into the data
    data.limit(8);
    rc = bufs.readLengthAndData(ch);
    assert rc == null;
    // this will still not allow the read to complete
    data.limit(14);
    rc = bufs.readLengthAndData(ch);
    assert rc == null;
    // this will still not allow the read to complete
    data.limit(15);
    rc = bufs.readLengthAndData(ch);
    assert rc != null;
    System.out.println("rc = " + rc);
    assert Arrays.equals(tmp, rc.array());
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers) ByteBuffer(java.nio.ByteBuffer)

Example 8 with Buffers

use of org.jgroups.nio.Buffers in project JGroups by belaban.

the class BuffersTest method testRead3Buffers.

/**
 * Reads | version (short) | number (int) | cookie [4 bytes] |
 */
public void testRead3Buffers() throws Exception {
    byte[] cookie = { 'b', 'e', 'l', 'a' };
    int num = 322649;
    ByteBuffer input = (ByteBuffer) ByteBuffer.allocate(Global.SHORT_SIZE + cookie.length + Global.INT_SIZE).putShort(Version.version).putInt(num).put(cookie, 0, cookie.length).flip();
    MockSocketChannel ch = new MockSocketChannel().bytesToRead(input);
    Buffers bufs = new Buffers(ByteBuffer.allocate(2), ByteBuffer.allocate(4), ByteBuffer.allocate(4));
    boolean rc = bufs.read(ch);
    System.out.println("bufs = " + bufs);
    assert rc;
    assert bufs.position() == 3;
    assert bufs.limit() == 3;
    for (// reset position so data can be read
    ByteBuffer b : // reset position so data can be read
    bufs) b.clear();
    short version = bufs.get(0).getShort(0);
    assert version == Version.version;
    int num2 = bufs.get(1).getInt(0);
    assert num2 == num;
    byte[] tmp = new byte[4];
    bufs.get(2).get(tmp, 0, tmp.length);
    assert Arrays.equals(tmp, cookie);
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers) ByteBuffer(java.nio.ByteBuffer)

Example 9 with Buffers

use of org.jgroups.nio.Buffers in project JGroups by belaban.

the class BuffersTest method testRead2.

public void testRead2() throws Exception {
    byte[] cookie = { 'b', 'e', 'l', 'a' };
    IpAddress addr = new IpAddress(7500);
    ByteArrayDataOutputStream out = new ByteArrayDataOutputStream();
    addr.writeTo(out);
    MockSocketChannel ch = new MockSocketChannel().bytesToRead((ByteBuffer) ByteBuffer.allocate(cookie.length + Global.SHORT_SIZE + out.position()).put(cookie).putShort(Version.version).put(out.buffer(), 0, out.position()).flip());
    Buffers bufs = new Buffers(ByteBuffer.allocate(cookie.length), ByteBuffer.allocate(Global.SHORT_SIZE), ByteBuffer.allocate(out.position()));
    boolean rc = bufs.read(ch);
    assert rc;
    readCookieVersionAndAddress(bufs, cookie, addr);
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) IpAddress(org.jgroups.stack.IpAddress) Buffers(org.jgroups.nio.Buffers) ByteArrayDataOutputStream(org.jgroups.util.ByteArrayDataOutputStream)

Example 10 with Buffers

use of org.jgroups.nio.Buffers in project JGroups by belaban.

the class NioConnection method _receive.

protected boolean _receive(boolean update) throws Exception {
    ByteBuffer msg;
    Receiver receiver = server.receiver();
    if (peer_addr == null && server.usePeerConnections() && (peer_addr = readPeerAddress()) != null) {
        recv_buf = new Buffers(2).add(ByteBuffer.allocate(Global.INT_SIZE), null);
        server.addConnection(peer_addr, this);
        return true;
    }
    if ((msg = recv_buf.readLengthAndData(channel)) == null)
        return false;
    if (receiver != null)
        receiver.receive(peer_addr, msg);
    if (update)
        updateLastAccessed();
    return true;
}
Also used : Buffers(org.jgroups.nio.Buffers) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Buffers (org.jgroups.nio.Buffers)23 MockSocketChannel (org.jgroups.nio.MockSocketChannel)19 ByteBuffer (java.nio.ByteBuffer)14 IpAddress (org.jgroups.stack.IpAddress)2 ByteArrayDataOutputStream (org.jgroups.util.ByteArrayDataOutputStream)2 EOFException (java.io.EOFException)1