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;
}
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);
}
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);
}
}
}
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();
}
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();
}
Aggregations