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