Search in sources :

Example 16 with BytesMessage

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

the class MessageDispatcherUnitTest method testNullMessageToAll.

public void testNullMessageToAll() throws Exception {
    da.setRequestHandler(new MyHandler(null));
    b = createChannel().name("B");
    long stop, start = System.currentTimeMillis();
    db = new MessageDispatcher(b, new MyHandler(null));
    stop = System.currentTimeMillis();
    b.connect("MessageDispatcherUnitTest");
    Assert.assertEquals(2, b.getView().size());
    System.out.println("view: " + b.getView());
    System.out.println("casting message");
    start = System.currentTimeMillis();
    RspList<byte[]> 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 2 == rsps.size();
    Rsp<byte[]> rsp = rsps.get(a.getAddress());
    assert rsp != null;
    Object ret = rsp.getValue();
    assert ret == null;
    rsp = rsps.get(b.getAddress());
    assert rsp != null;
    ret = rsp.getValue();
    assert ret == null;
}
Also used : MessageDispatcher(org.jgroups.blocks.MessageDispatcher) RequestOptions(org.jgroups.blocks.RequestOptions) BytesMessage(org.jgroups.BytesMessage)

Example 17 with BytesMessage

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

the class ByteBufferOutputStreamTest method testConstruction.

public void testConstruction() throws Exception {
    Address dest = Util.createRandomAddress("A");
    Message msg = new BytesMessage(dest, "hello world").setFlag(Message.Flag.DONT_BUNDLE, Message.Flag.OOB).putHeader((short) 22, NakAckHeader2.createMessageHeader(322649));
    int size = msg.size();
    ByteBuffer buf = ByteBuffer.allocate(size);
    ByteBufferOutputStream out = new ByteBufferOutputStream(buf);
    msg.writeTo(out);
    buf.flip();
    byte[] array = new byte[buf.limit()];
    System.arraycopy(buf.array(), buf.arrayOffset(), array, 0, buf.limit());
    ByteBufferInputStream in = new ByteBufferInputStream(ByteBuffer.wrap(array));
    Message copy = new BytesMessage();
    copy.readFrom(in);
    System.out.println("copy = " + copy);
    assert msg.getDest() != null && msg.getDest().equals(dest);
}
Also used : Address(org.jgroups.Address) BytesMessage(org.jgroups.BytesMessage) Message(org.jgroups.Message) ByteBufferOutputStream(org.jgroups.util.ByteBufferOutputStream) ByteBufferInputStream(org.jgroups.util.ByteBufferInputStream) BytesMessage(org.jgroups.BytesMessage) ByteBuffer(java.nio.ByteBuffer)

Example 18 with BytesMessage

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

the class STABLE_Test method testStableWithDontLoopback.

public void testStableWithDontLoopback() throws Exception {
    byte[] payload = new byte[5000];
    for (int i = 0; i < 10; i++) {
        Message msg = new BytesMessage(null, payload).setFlag(Message.TransientFlag.DONT_LOOPBACK);
        b.send(msg);
        // prevents batches, which trigger STABLE msgs in non-10000 increments
        Util.sleep(200);
    }
    Util.waitUntil(5000, 500, () -> Stream.of(r1, r3).allMatch(r -> r.size() == 10));
    assert r2.size() == 0;
    Util.waitUntilTrue(5000, 500, () -> Stream.of(a, b, c).map(c -> ((NAKACK2) c.getProtocolStack().findProtocol(NAKACK2.class)).getWindow(b.getAddress())).allMatch(t -> t.getHighestReceived() == 10 && t.getHighestDelivered() == 10 && t.getLow() == 10));
    for (JChannel ch : List.of(a, b, c)) {
        NAKACK2 n = ch.getProtocolStack().findProtocol(NAKACK2.class);
        Table<Message> t = n.getWindow(b.getAddress());
        assert t.getHighestReceived() == 10 && t.getHighestDelivered() == 10 && t.getLow() == 10 : String.format("table for %s is %s (low is probably 0)", ch.getName(), t);
    }
}
Also used : Table(org.jgroups.util.Table) Util(org.jgroups.util.Util) BeforeMethod(org.testng.annotations.BeforeMethod) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) BytesMessage(org.jgroups.BytesMessage) MyReceiver(org.jgroups.util.MyReceiver) List(java.util.List) Stream(java.util.stream.Stream) Message(org.jgroups.Message) NAKACK2(org.jgroups.protocols.pbcast.NAKACK2) Global(org.jgroups.Global) STABLE(org.jgroups.protocols.pbcast.STABLE) JChannel(org.jgroups.JChannel) NAKACK2(org.jgroups.protocols.pbcast.NAKACK2) JChannel(org.jgroups.JChannel) BytesMessage(org.jgroups.BytesMessage) Message(org.jgroups.Message) BytesMessage(org.jgroups.BytesMessage)

Example 19 with BytesMessage

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

the class ENCRYPTKeystoreTest method testEncryptEntireMessage.

public void testEncryptEntireMessage() throws Exception {
    SYM_ENCRYPT encrypt = create("defaultStore.keystore");
    Message msg = new BytesMessage(null, "hello world".getBytes()).putHeader((short) 1, new TpHeader("cluster"));
    MockProtocol mock = new MockProtocol();
    encrypt.setDownProtocol(mock);
    encrypt.down(msg);
    Message encrypted_msg = mock.getDownMessages().get("message0");
    encrypt.setDownProtocol(null);
    encrypt.setUpProtocol(mock);
    encrypt.up(encrypted_msg);
    Message decrypted_msg = mock.getUpMessages().get("message1");
    String temp = new String(decrypted_msg.getArray(), decrypted_msg.getOffset(), decrypted_msg.getLength());
    assert "hello world".equals(temp);
}
Also used : BytesMessage(org.jgroups.BytesMessage) Message(org.jgroups.Message) BytesMessage(org.jgroups.BytesMessage)

Example 20 with BytesMessage

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

the class ENCRYPTKeystoreTest method testMessageUpDecode.

public void testMessageUpDecode() throws Exception {
    SYM_ENCRYPT encrypt = create("defaultStore.keystore"), encrypt2 = create("defaultStore.keystore");
    MockProtocol observer = new MockProtocol();
    encrypt.setUpProtocol(observer);
    String messageText = "hello this is a test message";
    byte[] bytes = messageText.getBytes();
    byte[] iv = encrypt2.makeIv();
    byte[] encodedBytes = encrypt2.code(bytes, 0, bytes.length, iv, false);
    assert !new String(encodedBytes).equals(messageText);
    MessageDigest digest = MessageDigest.getInstance("MD5");
    digest.reset();
    digest.update(encrypt.secretKey().getEncoded());
    byte[] symVersion = digest.digest();
    Message msg = new BytesMessage(null, encodedBytes).putHeader(ENCRYPT_ID, new EncryptHeader((byte) 0, symVersion, iv));
    encrypt.up(msg);
    Message rcvdMsg = observer.getUpMessages().get("message0");
    String decText = new String(rcvdMsg.getArray(), rcvdMsg.getOffset(), rcvdMsg.getLength());
    assert decText.equals(messageText);
}
Also used : BytesMessage(org.jgroups.BytesMessage) Message(org.jgroups.Message) BytesMessage(org.jgroups.BytesMessage) MessageDigest(java.security.MessageDigest)

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