Search in sources :

Example 6 with BytesMessage

use of org.jgroups.BytesMessage in project JGroups by belaban.

the class MessageBeforeConnectedTestHelper method sendUnicast.

/**
 * Sends a unicast message up UNICAST2. Do *NOT* make this method static, or Byteman will not find it!
 */
public void sendUnicast(JChannel ch) throws Exception {
    final Message msg = new BytesMessage(ch.getAddress(), "hello-1").setSrc(ch.getAddress());
    // Add a UNICAST2 header
    final UNICAST3 unicast = ch.getProtocolStack().findProtocol(UNICAST3.class);
    UnicastHeader3 hdr = UnicastHeader3.createDataHeader(1, (short) 1, true);
    msg.putHeader(unicast.getId(), hdr);
    new Thread(() -> unicast.down(msg)).start();
}
Also used : BytesMessage(org.jgroups.BytesMessage) Message(org.jgroups.Message) BytesMessage(org.jgroups.BytesMessage) UNICAST3(org.jgroups.protocols.UNICAST3) UnicastHeader3(org.jgroups.protocols.UnicastHeader3)

Example 7 with BytesMessage

use of org.jgroups.BytesMessage in project JGroups by belaban.

the class BPING method run.

public void run() {
    final byte[] receive_buf = new byte[65535];
    DatagramPacket packet = new DatagramPacket(receive_buf, receive_buf.length);
    DataInput inp;
    while (sock != null && Thread.currentThread().equals(receiver)) {
        packet.setData(receive_buf, 0, receive_buf.length);
        try {
            sock.receive(packet);
            inp = new ByteArrayDataInputStream(packet.getData(), packet.getOffset(), packet.getLength());
            Message msg = new BytesMessage();
            msg.readFrom(inp);
            up(msg);
        } catch (SocketException socketEx) {
            break;
        } catch (Throwable ex) {
            log.error(Util.getMessage("FailedReceivingPacketFrom"), packet.getSocketAddress(), ex);
        }
    }
    if (log.isTraceEnabled())
        log.trace("receiver thread terminated");
}
Also used : DataInput(java.io.DataInput) SocketException(java.net.SocketException) BytesMessage(org.jgroups.BytesMessage) Message(org.jgroups.Message) DatagramPacket(java.net.DatagramPacket) BytesMessage(org.jgroups.BytesMessage) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream)

Example 8 with BytesMessage

use of org.jgroups.BytesMessage in project JGroups by belaban.

the class COMPRESS_Test method testSimpleCompression.

public void testSimpleCompression() throws Exception {
    a = create("A").connect(COMPRESS_Test.class.getSimpleName());
    b = create("B").connect(COMPRESS_Test.class.getSimpleName());
    Util.waitUntilAllChannelsHaveSameView(10000, 500, a, b);
    a.setReceiver(r1);
    b.setReceiver(r2);
    byte[] array = Util.generateArray(100);
    a.send(new BytesMessage(b.getAddress(), array));
    Util.waitUntil(10000, 500, () -> r2.size() > 0);
    Message msg = r2.list().get(0);
    assert msg.hasPayload() && msg.getLength() == array.length;
    Util.verifyArray(msg.getArray());
}
Also used : BytesMessage(org.jgroups.BytesMessage) Message(org.jgroups.Message) BytesMessage(org.jgroups.BytesMessage)

Example 9 with BytesMessage

use of org.jgroups.BytesMessage in project JGroups by belaban.

the class MessageDispatcherUnitTest method sendMessageToBothChannels.

private void sendMessageToBothChannels(int size) throws Exception {
    long start, stop;
    da.setRequestHandler(new MyHandler(new byte[size]));
    b = createChannel();
    b.setName("B");
    db = new MessageDispatcher(b, new MyHandler(new byte[size]));
    b.connect("MessageDispatcherUnitTest");
    assert 2 == b.getView().size();
    System.out.println("casting message");
    start = System.currentTimeMillis();
    // RspList<Object> rsps=d1.castMessage(null, buf, new RequestOptions(ResponseMode.GET_ALL, 0));
    RspList<Object> rsps = da.castMessage(null, new BytesMessage(null, buf), new RequestOptions(ResponseMode.GET_ALL, 0));
    stop = System.currentTimeMillis();
    System.out.println("rsps:\n" + rsps);
    System.out.println("call took " + (stop - start) + " ms");
    assert rsps != null;
    Assert.assertEquals(2, rsps.size());
    Rsp rsp = rsps.get(a.getAddress());
    assert rsp != null;
    byte[] ret = (byte[]) rsp.getValue();
    Assert.assertEquals(size, ret.length);
    rsp = rsps.get(b.getAddress());
    assert rsp != null;
    ret = (byte[]) rsp.getValue();
    Assert.assertEquals(size, ret.length);
    Util.close(b);
}
Also used : MessageDispatcher(org.jgroups.blocks.MessageDispatcher) RequestOptions(org.jgroups.blocks.RequestOptions) BytesMessage(org.jgroups.BytesMessage) Rsp(org.jgroups.util.Rsp)

Example 10 with BytesMessage

use of org.jgroups.BytesMessage in project JGroups by belaban.

the class MessageDispatcherUnitTest method testBlockingSecondMember.

/**
 * In this scenario we block the second member so to verify the sender actually waits;
 * this won't trigger a timeout but will produce a response having "received=false".
 * It's hard to otherwise make sure casting isn't being done asynchronously.
 */
public void testBlockingSecondMember() throws Exception {
    RequestOptions requestOptions = RequestOptions.SYNC().exclusionList(// redundant - simplifies debugging
    a.getAddress()).setMode(// redundant - implied by SYNC()
    ResponseMode.GET_ALL).setTransientFlags(// redundant - self is excluded
    Message.TransientFlag.DONT_LOOPBACK).setTimeout(// Speed up the test execution
    1000);
    b = createChannel().name("B");
    BlockableRequestHandler blockableHandler = new BlockableRequestHandler();
    db = new MessageDispatcher(b).setRequestHandler(blockableHandler);
    b.connect("MessageDispatcherUnitTest");
    assert 2 == b.getView().size();
    blockableHandler.installThreadTrap();
    try {
        RspList<Object> rsps = da.castMessage(null, new BytesMessage(null, buf), requestOptions);
        System.out.printf("responses: %s\n", rsps);
        assert 1 == rsps.size();
        Rsp rsp = rsps.get(b.getAddress());
        assert rsp != null;
        assert !rsp.wasReceived();
        assert !rsp.wasSuspected();
    } catch (Exception e) {
        Assert.fail("exception returned by castMessage", e);
    } finally {
        blockableHandler.releaseBlockedThreads();
    }
    assert blockableHandler.receivedAnything();
}
Also used : MessageDispatcher(org.jgroups.blocks.MessageDispatcher) RequestOptions(org.jgroups.blocks.RequestOptions) BytesMessage(org.jgroups.BytesMessage) Rsp(org.jgroups.util.Rsp)

Aggregations

BytesMessage (org.jgroups.BytesMessage)24 Message (org.jgroups.Message)19 RequestOptions (org.jgroups.blocks.RequestOptions)5 Address (org.jgroups.Address)3 MessageDispatcher (org.jgroups.blocks.MessageDispatcher)3 DataInput (java.io.DataInput)2 ByteBuffer (java.nio.ByteBuffer)2 MessageDigest (java.security.MessageDigest)2 Stream (java.util.stream.Stream)2 Rsp (org.jgroups.util.Rsp)2 Util (org.jgroups.util.Util)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 Test (org.testng.annotations.Test)2 DatagramPacket (java.net.DatagramPacket)1 SocketException (java.net.SocketException)1 Arrays (java.util.Arrays)1 List (java.util.List)1 TimeoutException (java.util.concurrent.TimeoutException)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Collectors (java.util.stream.Collectors)1