Search in sources :

Example 1 with BytesMessage

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

the class UnicastTestTcpSlow method readMessage.

protected static Message readMessage(byte[] buf, int offset, int length) throws Exception {
    ByteArrayDataInputStream in = new ByteArrayDataInputStream(buf, offset, length);
    short ver = in.readShort();
    byte flags = in.readByte();
    // final boolean multicast=(flags & (byte)2) == (byte)2;
    // don't create headers, readFrom() will do this
    Message msg = new BytesMessage();
    msg.readFrom(in);
    return msg;
}
Also used : BytesMessage(org.jgroups.BytesMessage) Message(org.jgroups.Message) BytesMessage(org.jgroups.BytesMessage)

Example 2 with BytesMessage

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

the class UnicastTestTcpSlow method sendMessages.

void sendMessages() throws Exception {
    if (num_threads > 1 && num_msgs % num_threads != 0) {
        System.err.println("num_msgs (" + num_msgs + " ) has to be divisible by num_threads (" + num_threads + ")");
        return;
    }
    System.out.println("sending " + num_msgs + " messages (" + Util.printBytes(msg_size) + ") to " + remote + ": oob=" + oob + ", " + num_threads + " sender thread(s)");
    ByteBuffer buf = ByteBuffer.allocate(Global.BYTE_SIZE + Global.LONG_SIZE).put(START).putLong(num_msgs);
    Message msg = new BytesMessage(null, buf.array());
    // msg.writeTo(output);
    ByteArrayDataOutputStream dos = new ByteArrayDataOutputStream(msg.size());
    byte flags = 0;
    // write the version
    dos.writeShort(Version.version);
    if (msg.getDest() == null)
        flags += (byte) 2;
    dos.writeByte(flags);
    msg.writeTo(dos);
    ByteArray buffer = dos.getBuffer();
    // need to sync if we have more than 1 sender
    output_lock.lock();
    try {
        // msg.writeTo(output);
        output.writeInt(buffer.getLength());
        output.write(buffer.getArray(), buffer.getOffset(), buffer.getLength());
    } finally {
        output_lock.unlock();
    }
    int msgs_per_sender = num_msgs / num_threads;
    Sender[] senders = new Sender[num_threads];
    for (int i = 0; i < senders.length; i++) senders[i] = new Sender(msgs_per_sender, msg_size, num_msgs / 10);
    for (Sender sender : senders) sender.start();
    for (Sender sender : senders) sender.join();
    output.flush();
    System.out.println("done sending " + num_msgs + " to " + remote);
}
Also used : BytesMessage(org.jgroups.BytesMessage) Message(org.jgroups.Message) BytesMessage(org.jgroups.BytesMessage) ByteBuffer(java.nio.ByteBuffer)

Example 3 with BytesMessage

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

the class MPING method run.

public void run() {
    final byte[] receive_buf = new byte[65535];
    DatagramPacket packet = new DatagramPacket(receive_buf, receive_buf.length);
    while (mcast_receive_sock != null && Thread.currentThread().equals(receiver)) {
        packet.setData(receive_buf, 0, receive_buf.length);
        try {
            mcast_receive_sock.receive(packet);
            DataInput inp = new ByteArrayDataInputStream(packet.getData(), packet.getOffset(), packet.getLength());
            Message msg = new BytesMessage();
            msg.readFrom(inp);
            if (// discard discovery request from self
            !Objects.equals(local_addr, msg.getSrc()))
                up(msg);
        } catch (SocketException socketEx) {
            break;
        } catch (Throwable ex) {
            log.error(Util.getMessage("FailedReceivingPacketFrom"), packet.getSocketAddress(), ex);
        }
    }
}
Also used : DataInput(java.io.DataInput) BytesMessage(org.jgroups.BytesMessage) Message(org.jgroups.Message) BytesMessage(org.jgroups.BytesMessage)

Example 4 with BytesMessage

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

the class ENCRYPTKeystoreTest method testMessageUpNoBuffer.

public void testMessageUpNoBuffer() throws Exception {
    SYM_ENCRYPT encrypt = create("defaultStore.keystore");
    MockProtocol observer = new MockProtocol();
    encrypt.setUpProtocol(observer);
    encrypt.up((Message) new BytesMessage().putHeader(ENCRYPT_ID, new EncryptHeader((byte) 0, "bla".getBytes(), encrypt.makeIv())));
    assert observer.getUpMessages().isEmpty();
}
Also used : BytesMessage(org.jgroups.BytesMessage)

Example 5 with BytesMessage

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

the class ENCRYPTKeystoreTest method testMessageUpWrongKey.

public void testMessageUpWrongKey() throws Exception {
    SYM_ENCRYPT encrypt = create("defaultStore.keystore"), encrypt2 = create("defaultStore2.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(encrypt2.secretKey().getEncoded());
    byte[] symVersion = digest.digest();
    Message msg = new BytesMessage(null, encodedBytes).putHeader(ENCRYPT_ID, new EncryptHeader((byte) 0, symVersion, iv));
    encrypt.up(msg);
    assert observer.getUpMessages().isEmpty();
}
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