Search in sources :

Example 21 with XByteBuffer

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);
    }
}
Also used : XByteBuffer(org.apache.catalina.tribes.io.XByteBuffer) ChannelData(org.apache.catalina.tribes.io.ChannelData) ChannelException(org.apache.catalina.tribes.ChannelException) Test(org.junit.Test)

Example 22 with XByteBuffer

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;
}
Also used : XByteBuffer(org.apache.catalina.tribes.io.XByteBuffer) ChannelData(org.apache.catalina.tribes.io.ChannelData)

Example 23 with XByteBuffer

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);
    }
}
Also used : XByteBuffer(org.apache.catalina.tribes.io.XByteBuffer) GeneralSecurityException(java.security.GeneralSecurityException)

Example 24 with XByteBuffer

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);
    }
}
Also used : XByteBuffer(org.apache.catalina.tribes.io.XByteBuffer) GeneralSecurityException(java.security.GeneralSecurityException) ChannelException(org.apache.catalina.tribes.ChannelException)

Example 25 with XByteBuffer

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);
}
Also used : XByteBuffer(org.apache.catalina.tribes.io.XByteBuffer) ChannelData(org.apache.catalina.tribes.io.ChannelData)

Aggregations

XByteBuffer (org.apache.catalina.tribes.io.XByteBuffer)27 ChannelData (org.apache.catalina.tribes.io.ChannelData)23 ChannelException (org.apache.catalina.tribes.ChannelException)8 BigDecimal (java.math.BigDecimal)6 Socket (java.net.Socket)6 Member (org.apache.catalina.tribes.Member)6 MemberImpl (org.apache.catalina.tribes.membership.MemberImpl)6 DecimalFormat (java.text.DecimalFormat)4 Test (org.junit.Test)4 RemoteProcessException (org.apache.catalina.tribes.RemoteProcessException)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ConnectException (java.net.ConnectException)2 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 ServerSocket (java.net.ServerSocket)2 SocketTimeoutException (java.net.SocketTimeoutException)2 SelectionKey (java.nio.channels.SelectionKey)2