Search in sources :

Example 11 with ByteArray

use of org.jgroups.util.ByteArray in project JGroups by belaban.

the class DynamicDiscardTest method testLeaveDuringSend.

public void testLeaveDuringSend() throws Exception {
    final JChannel[] channels = new JChannel[NUM];
    final MessageDispatcher[] dispatchers = new MessageDispatcher[NUM];
    for (int i = 0; i < NUM; i++) {
        channels[i] = new JChannel(new SHARED_LOOPBACK(), new SHARED_LOOPBACK_PING(), new MERGE3(), new FD_ALL3().setTimeout(1000).setInterval(300), new NAKACK2(), new UNICAST3(), new STABLE(), new GMS(), new RSVP().ackOnDelivery(false).throwExceptionOnTimeout(false));
        channels[i].setName(Character.toString((char) ('A' + i)));
        channels[i].setDiscardOwnMessages(true);
        dispatchers[i] = new MessageDispatcher(channels[i], new MyRequestHandler());
        dispatchers[i].setReceiver(new MyMembershipListener(channels[i]));
        channels[i].connect("DynamicDiscardTest");
        System.out.print(i + 1 + " ");
    }
    Util.waitUntilAllChannelsHaveSameView(10000, 1000, channels);
    // discard all messages (except those to self)
    DISCARD discard = new DISCARD().discardAll(true);
    channels[0].getProtocolStack().insertProtocol(discard, ProtocolStack.Position.ABOVE, TP.class);
    // send a RSVP message
    byte[] data = "message2".getBytes();
    ByteArray buf = new ByteArray(data, 0, data.length);
    RspList<Object> rsps = dispatchers[0].castMessage(null, new BytesMessage(null, buf), RequestOptions.SYNC().timeout(5000).flags(Message.Flag.RSVP, Message.Flag.OOB));
    Rsp<Object> objectRsp = rsps.get(channels[1].getAddress());
    assertFalse(objectRsp.wasReceived());
    assertTrue(objectRsp.wasSuspected());
}
Also used : GMS(org.jgroups.protocols.pbcast.GMS) NAKACK2(org.jgroups.protocols.pbcast.NAKACK2) MessageDispatcher(org.jgroups.blocks.MessageDispatcher) ByteArray(org.jgroups.util.ByteArray) STABLE(org.jgroups.protocols.pbcast.STABLE)

Example 12 with ByteArray

use of org.jgroups.util.ByteArray in project JGroups by belaban.

the class ByteArrayTest method testGetBytes.

public void testGetBytes() {
    ByteArray ba = new ByteArray(HELLO);
    byte[] bytes = ba.getBytes();
    assert Arrays.equals(bytes, HELLO);
    // same memory location
    assert HELLO.hashCode() == bytes.hashCode();
    ba = new ByteArray(HELLO, 0, 5);
    bytes = ba.getBytes();
    assert HELLO.hashCode() != bytes.hashCode();
    assert new String(bytes).equals("hello");
    ba = new ByteArray(HELLO, 6, 5);
    bytes = ba.getBytes();
    assert HELLO.hashCode() != bytes.hashCode();
    assert new String(bytes).equals("world");
}
Also used : ByteArray(org.jgroups.util.ByteArray)

Example 13 with ByteArray

use of org.jgroups.util.ByteArray in project JGroups by belaban.

the class SERIALIZE method down.

public Object down(Message msg) {
    if (msg.getSrc() == null)
        msg.setSrc(local_addr);
    ByteArray serialized_msg = null;
    try {
        serialized_msg = Util.messageToBuffer(msg);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    // exclude existing headers, they will be seen again when we unmarshal the message at the receiver
    Message tmp = new BytesMessage(msg.dest(), serialized_msg).setFlag(msg.getFlags(), false);
    GMS.GmsHeader hdr = msg.getHeader(GMS_ID);
    if (hdr != null)
        tmp.putHeader(GMS_ID, hdr);
    return down_prot.down(tmp);
}
Also used : BytesMessage(org.jgroups.BytesMessage) Message(org.jgroups.Message) ByteArray(org.jgroups.util.ByteArray) BytesMessage(org.jgroups.BytesMessage) GMS(org.jgroups.protocols.pbcast.GMS)

Example 14 with ByteArray

use of org.jgroups.util.ByteArray in project JGroups by belaban.

the class MessageDispatcherRSVPTest method testCancellationByClosing.

protected void testCancellationByClosing(boolean unicast, Thread closer) throws Exception {
    DISCARD discard = channels[0].getProtocolStack().findProtocol(DISCARD.class);
    discard.discardAll(true);
    try {
        Address target = unicast ? channels[1].getAddress() : null;
        byte[] data = "bla".getBytes();
        ByteArray buf = new ByteArray(data, 0, data.length);
        Message msg = new BytesMessage(target, "bla");
        msg.setFlag(Message.Flag.RSVP);
        closer.start();
        if (unicast) {
            System.out.println("sending unicast message to " + target);
            dispatchers[0].sendMessage(new BytesMessage(target, buf), RequestOptions.SYNC().flags(Message.Flag.RSVP));
            assert false : "sending the message on a closed channel should have thrown an exception";
        } else {
            System.out.println("sending multicast message");
            Address dst = channels[1].getAddress();
            RspList<Object> rsps = dispatchers[0].castMessage(Collections.singleton(dst), new BytesMessage(dst, buf), RequestOptions.SYNC());
            System.out.println("rsps = " + rsps);
            assert rsps.size() == 1;
            Rsp<Object> rsp = rsps.iterator().next();
            System.out.println("rsp = " + rsp);
            assert rsp.hasException();
            Throwable ex = rsp.getException();
            assert ex instanceof IllegalStateException;
        }
    } catch (IllegalStateException t) {
        System.out.println("received \"" + t + "\" as expected");
    }
}
Also used : ByteArray(org.jgroups.util.ByteArray)

Example 15 with ByteArray

use of org.jgroups.util.ByteArray in project JGroups by belaban.

the class MessageDispatcherRSVPTest method sendMessageOnClosedChannel.

protected void sendMessageOnClosedChannel(Address dest, Message.Flag... flags) throws Exception {
    RequestOptions opts = RequestOptions.SYNC().timeout(2000).flags(flags);
    byte[] data = "bla".getBytes();
    ByteArray buf = new ByteArray(data, 0, data.length);
    channels[0].close();
    try {
        if (dest == null) {
            // multicast
            Address dst = channels[1].getAddress();
            dispatchers[0].castMessage(Collections.singleton(dst), new BytesMessage(dst, buf), opts);
        } else
            dispatchers[0].sendMessage(new BytesMessage(dest, buf), opts);
        assert false : "sending the message on a closed channel should have thrown an exception";
    } catch (IllegalStateException t) {
        System.out.println("received \"" + t + "\" as expected");
    }
}
Also used : RequestOptions(org.jgroups.blocks.RequestOptions) ByteArray(org.jgroups.util.ByteArray)

Aggregations

ByteArray (org.jgroups.util.ByteArray)15 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 RequestOptions (org.jgroups.blocks.RequestOptions)2 GMS (org.jgroups.protocols.pbcast.GMS)2 DataInput (java.io.DataInput)1 LinkedHashSet (java.util.LinkedHashSet)1 Deflater (java.util.zip.Deflater)1 BytesMessage (org.jgroups.BytesMessage)1 Message (org.jgroups.Message)1 MessageDispatcher (org.jgroups.blocks.MessageDispatcher)1 PingData (org.jgroups.protocols.PingData)1 PingHeader (org.jgroups.protocols.PingHeader)1 NAKACK2 (org.jgroups.protocols.pbcast.NAKACK2)1 STABLE (org.jgroups.protocols.pbcast.STABLE)1 IpAddress (org.jgroups.stack.IpAddress)1 ByteArrayDataInputStream (org.jgroups.util.ByteArrayDataInputStream)1