use of org.apache.catalina.tribes.io.XByteBuffer in project tomcat by apache.
the class TestEncryptInterceptor method testMultithreaded.
/*
* This test isn't guaranteed to catch any multithreaded issues, but it
* gives a good exercise.
*/
@Test
public void testMultithreaded() throws Exception {
String inputValue = "A test string to fight over.";
final byte[] bytes = inputValue.getBytes("UTF-8");
int numThreads = 100;
final int messagesPerThread = 10;
dest.setPrevious(new ValuesCaptureInterceptor());
src.start(Channel.SND_TX_SEQ);
dest.start(Channel.SND_TX_SEQ);
Runnable job = new Runnable() {
@Override
public void run() {
try {
ChannelData msg = new ChannelData(false);
XByteBuffer xbb = new XByteBuffer(1024, false);
xbb.append(bytes, 0, bytes.length);
msg.setMessage(xbb);
for (int i = 0; i < messagesPerThread; ++i) {
src.sendMessage(null, msg, null);
}
} catch (ChannelException e) {
Assert.fail("Encountered exception sending messages: " + e.getMessage());
}
}
};
Thread[] threads = new Thread[numThreads];
for (int i = 0; i < numThreads; ++i) {
threads[i] = new Thread(job);
threads[i].setName("Message-Thread-" + i);
}
for (int i = 0; i < numThreads; ++i) {
threads[i].start();
}
for (int i = 0; i < numThreads; ++i) {
threads[i].join();
}
// Check all received messages to make sure they are not corrupted
Collection<byte[]> messages = ((ValuesCaptureInterceptor) dest.getPrevious()).getValues();
Assert.assertEquals("Did not receive all expected messages", numThreads * messagesPerThread, messages.size());
for (byte[] message : messages) {
Assert.assertArrayEquals("Message is corrupted", message, bytes);
}
}
use of org.apache.catalina.tribes.io.XByteBuffer in project tomcat by apache.
the class NioSenderTest method getMessage.
public synchronized ChannelData getMessage(Member mbr) {
String msg = "Thread-" + Thread.currentThread().getName() + " Message:" + inc();
ChannelData data = new ChannelData(true);
data.setMessage(new XByteBuffer(msg.getBytes(), false));
data.setAddress(mbr);
return data;
}
use of org.apache.catalina.tribes.io.XByteBuffer in project tomcat by apache.
the class EncryptInterceptor method messageReceived.
@Override
public void messageReceived(ChannelMessage msg) {
try {
byte[] data = msg.getMessage().getBytes();
data = encryptionManager.decrypt(data);
XByteBuffer xbb = msg.getMessage();
// Completely replace the message with the decrypted one
xbb.clear();
xbb.append(data, 0, data.length);
super.messageReceived(msg);
} catch (GeneralSecurityException gse) {
log.error(sm.getString("encryptInterceptor.decrypt.failed"), gse);
}
}
use of org.apache.catalina.tribes.io.XByteBuffer in project tomcat by apache.
the class EncryptInterceptor method sendMessage.
@Override
public void sendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload) throws ChannelException {
try {
byte[] data = msg.getMessage().getBytes();
// See #encrypt(byte[]) for an explanation of the return value
byte[][] bytes = encryptionManager.encrypt(data);
XByteBuffer xbb = msg.getMessage();
// Completely replace the message
xbb.clear();
xbb.append(bytes[0], 0, bytes[0].length);
xbb.append(bytes[1], 0, bytes[1].length);
super.sendMessage(destination, msg, payload);
} catch (GeneralSecurityException gse) {
log.error(sm.getString("encryptInterceptor.encrypt.failed"));
throw new ChannelException(gse);
}
}
use of org.apache.catalina.tribes.io.XByteBuffer in project tomcat by apache.
the class StaticMembershipInterceptor method sendMemberMessage.
protected void sendMemberMessage(Member[] members, byte[] message) throws ChannelException {
if (members == null || members.length == 0) {
return;
}
ChannelData data = new ChannelData(true);
data.setAddress(getLocalMember(false));
data.setTimestamp(System.currentTimeMillis());
data.setOptions(getOptionFlag());
data.setMessage(new XByteBuffer(message, false));
super.sendMessage(members, data, null);
}
Aggregations