org.apache.catalina.tribes.io.XByteBuffer Java Examples

The following examples show how to use org.apache.catalina.tribes.io.XByteBuffer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractReplicatedMap.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * apply a diff, or an entire object
 * @param data byte[]
 * @param offset int
 * @param length int
 * @param diff boolean
 * @throws IOException
 * @throws ClassNotFoundException
 */
@SuppressWarnings("unchecked")
public void apply(byte[] data, int offset, int length, boolean diff) throws IOException, ClassNotFoundException {
    if (isDiffable() && diff) {
        ReplicatedMapEntry rentry = (ReplicatedMapEntry) value;
        rentry.lock();
        try {
            rentry.applyDiff(data, offset, length);
        } finally {
            rentry.unlock();
        }
    } else if (length == 0) {
        value = null;
        proxy = true;
    } else {
        value = (V) XByteBuffer.deserialize(data, offset, length);
    }
}
 
Example #2
Source File: OrderInterceptor.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void messageReceived(ChannelMessage msg) {
    if ( !okToProcess(msg.getOptions()) ) {
        super.messageReceived(msg);
        return;
    }
    int msgnr = XByteBuffer.toInt(msg.getMessage().getBytesDirect(),msg.getMessage().getLength()-4);
    msg.getMessage().trim(4);
    MessageOrder order = new MessageOrder(msgnr,(ChannelMessage)msg.deepclone());
    try {
        inLock.writeLock().lock();
        if ( processIncoming(order) ) processLeftOvers(msg.getAddress(),false);
    }finally {
        inLock.writeLock().unlock();
    }
}
 
Example #3
Source File: AbstractReplicatedMap.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * apply a diff, or an entire object
 * @param data byte[]
 * @param offset int
 * @param length int
 * @param diff boolean
 * @throws IOException
 * @throws ClassNotFoundException
 */
@SuppressWarnings("unchecked")
public void apply(byte[] data, int offset, int length, boolean diff) throws IOException, ClassNotFoundException {
    if (isDiffable() && diff) {
        ReplicatedMapEntry rentry = (ReplicatedMapEntry) value;
        rentry.lock();
        try {
            rentry.applyDiff(data, offset, length);
        } finally {
            rentry.unlock();
        }
    } else if (length == 0) {
        value = null;
        proxy = true;
    } else {
        value = (V) XByteBuffer.deserialize(data, offset, length);
    }
}
 
Example #4
Source File: ThroughputInterceptor.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void sendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload) throws ChannelException {
    if ( access.addAndGet(1) == 1 ) txStart = System.currentTimeMillis();
    long bytes = XByteBuffer.getDataPackageLength(((ChannelData)msg).getDataPackageLength());
    try {
        super.sendMessage(destination, msg, payload);
    }catch ( ChannelException x ) {
        msgTxErr.addAndGet(1);
        if ( access.get() == 1 ) access.addAndGet(-1);
        throw x;
    }
    mbTx += (bytes*destination.length)/(1024d*1024d);
    mbAppTx += bytes/(1024d*1024d);
    if ( access.addAndGet(-1) == 0 ) {
        long stop = System.currentTimeMillis();
        timeTx += (stop - txStart) / 1000d;
        if ((msgTxCnt.get() / (double) interval) >= lastCnt) {
            lastCnt++;
            report(timeTx);
        }
    }
    msgTxCnt.addAndGet(1);
}
 
Example #5
Source File: ThroughputInterceptor.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void sendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload) throws ChannelException {
    if ( access.addAndGet(1) == 1 ) txStart = System.currentTimeMillis();
    long bytes = XByteBuffer.getDataPackageLength(((ChannelData)msg).getDataPackageLength());
    try {
        super.sendMessage(destination, msg, payload);
    }catch ( ChannelException x ) {
        msgTxErr.addAndGet(1);
        if ( access.get() == 1 ) access.addAndGet(-1);
        throw x;
    } 
    mbTx += (bytes*destination.length)/(1024d*1024d);
    mbAppTx += bytes/(1024d*1024d);
    if ( access.addAndGet(-1) == 0 ) {
        long stop = System.currentTimeMillis();
        timeTx += (stop - txStart) / 1000d;
        if ((msgTxCnt.get() / interval) >= lastCnt) {
            lastCnt++;
            report(timeTx);
        }
    }
    msgTxCnt.addAndGet(1);
}
 
Example #6
Source File: McastService.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void broadcast(ChannelMessage message) throws ChannelException {
    if (impl==null || (impl.startLevel & Channel.MBR_TX_SEQ)!=Channel.MBR_TX_SEQ )
        throw new ChannelException("Multicast send is not started or enabled.");
    
    byte[] data = XByteBuffer.createDataPackage((ChannelData)message);
    if (data.length>McastServiceImpl.MAX_PACKET_SIZE) {
        throw new ChannelException("Packet length["+data.length+"] exceeds max packet size of "+McastServiceImpl.MAX_PACKET_SIZE+" bytes.");
    }
    DatagramPacket packet = new DatagramPacket(data,0,data.length);
    try {
        impl.send(false, packet);
    } catch (Exception x) {
        throw new ChannelException(x);
    }
}
 
Example #7
Source File: EncryptInterceptor.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@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);
    }
}
 
Example #8
Source File: EncryptInterceptor.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@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);
    }
}
 
Example #9
Source File: TestUdpPackages.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
public static Data createRandomData(int size, int number) {
    int i = r.nextInt();
    i = ( i % 127 );
    int length = Math.abs(r.nextInt() % size);
    if (length<100) length += 100;
    Data d = new Data();
    d.length = length;
    d.key = (byte)i;
    d.data = new byte[length];
    Arrays.fill(d.data,d.key);
    if (number>0 && d.data.length>=4) {
        //populate number
        d.hasNr = true;
        XByteBuffer.toBytes(number,d.data, 0);
    }
    return d;
}
 
Example #10
Source File: McastService.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void broadcast(ChannelMessage message) throws ChannelException {
    if (impl==null || (impl.startLevel & Channel.MBR_TX_SEQ)!=Channel.MBR_TX_SEQ )
        throw new ChannelException("Multicast send is not started or enabled.");
    
    byte[] data = XByteBuffer.createDataPackage((ChannelData)message);
    if (data.length>McastServiceImpl.MAX_PACKET_SIZE) {
        throw new ChannelException("Packet length["+data.length+"] exceeds max packet size of "+McastServiceImpl.MAX_PACKET_SIZE+" bytes.");
    }
    DatagramPacket packet = new DatagramPacket(data,0,data.length);
    try {
        impl.send(false, packet);
    } catch (Exception x) {
        throw new ChannelException(x);
    }
}
 
Example #11
Source File: McastServiceImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Receive a datagram packet, locking wait
 * @throws IOException
 */
public void receive() throws IOException {
    boolean checkexpired = true;
    try {
        
        socket.receive(receivePacket);
        if(receivePacket.getLength() > MAX_PACKET_SIZE) {
            log.error("Multicast packet received was too long, dropping package:"+receivePacket.getLength());
        } else {
            byte[] data = new byte[receivePacket.getLength()];
            System.arraycopy(receivePacket.getData(), receivePacket.getOffset(), data, 0, data.length);
            if (XByteBuffer.firstIndexOf(data,0,MemberImpl.TRIBES_MBR_BEGIN)==0) {
                memberDataReceived(data);
            } else {
                memberBroadcastsReceived(data);
            }
            
        }
    } catch (SocketTimeoutException x ) { 
        //do nothing, this is normal, we don't want to block forever
        //since the receive thread is the same thread
        //that does membership expiration
    }
    if (checkexpired) checkExpired();
}
 
Example #12
Source File: OrderInterceptor.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void messageReceived(ChannelMessage msg) {
    if ( !okToProcess(msg.getOptions()) ) {
        super.messageReceived(msg);
        return;
    }
    int msgnr = XByteBuffer.toInt(msg.getMessage().getBytesDirect(),msg.getMessage().getLength()-4);
    msg.getMessage().trim(4);
    MessageOrder order = new MessageOrder(msgnr,(ChannelMessage)msg.deepclone());
    try {
        inLock.writeLock().lock();
        if ( processIncoming(order) ) processLeftOvers(msg.getAddress(),false);
    }finally {
        inLock.writeLock().unlock();
    }
}
 
Example #13
Source File: AbstractReplicatedMap.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * apply a diff, or an entire object
 * @param data byte[]
 * @param offset int
 * @param length int
 * @param diff boolean
 * @throws IOException IO error
 * @throws ClassNotFoundException Deserialization error
 */
@SuppressWarnings("unchecked")
public void apply(byte[] data, int offset, int length, boolean diff) throws IOException, ClassNotFoundException {
    if (isDiffable() && diff) {
        ReplicatedMapEntry rentry = (ReplicatedMapEntry) value;
        rentry.lock();
        try {
            rentry.applyDiff(data, offset, length);
        } finally {
            rentry.unlock();
        }
    } else if (length == 0) {
        value = null;
        proxy = true;
    } else {
        value = (V) XByteBuffer.deserialize(data, offset, length);
    }
}
 
Example #14
Source File: TestMulticastPackages.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
public static Data createRandomData(int size, int number) {
    int i = r.nextInt();
    i = ( i % 127 );
    int length = Math.abs(r.nextInt() % size);
    if (length<100) length += 100;
    Data d = new Data();
    d.length = length;
    d.key = (byte)i;
    d.data = new byte[length];
    Arrays.fill(d.data,d.key);
    if (number>0 && d.data.length>=4) {
        //populate number
        d.hasNr = true;
        XByteBuffer.toBytes(number,d.data, 0);
    }
    return d;
}
 
Example #15
Source File: McastServiceImpl.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Receive a datagram packet, locking wait
 * @throws IOException
 */
public void receive() throws IOException {
    boolean checkexpired = true;
    try {
        
        socket.receive(receivePacket);
        if(receivePacket.getLength() > MAX_PACKET_SIZE) {
            log.error("Multicast packet received was too long, dropping package:"+receivePacket.getLength());
        } else {
            byte[] data = new byte[receivePacket.getLength()];
            System.arraycopy(receivePacket.getData(), receivePacket.getOffset(), data, 0, data.length);
            if (XByteBuffer.firstIndexOf(data,0,MemberImpl.TRIBES_MBR_BEGIN)==0) {
                memberDataReceived(data);
            } else {
                memberBroadcastsReceived(data);
            }
            
        }
    } catch (SocketTimeoutException x ) { 
        //do nothing, this is normal, we don't want to block forever
        //since the receive thread is the same thread
        //that does membership expiration
    }
    if (checkexpired) checkExpired();
}
 
Example #16
Source File: TestMulticastPackages.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
public static Data createRandomData(int size, int number) {
    int i = r.nextInt();
    i = ( i % 127 );
    int length = Math.abs(r.nextInt() % size);
    if (length<100) length += 100;
    Data d = new Data();
    d.length = length;
    d.key = (byte)i;
    d.data = new byte[length];
    Arrays.fill(d.data,d.key);
    if (number>0 && d.data.length>=4) {
        //populate number
        d.hasNr = true;
        XByteBuffer.toBytes(number,d.data, 0);
    }
    return d;
}
 
Example #17
Source File: TestEncryptInterceptor.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testPickup() throws Exception {
    File file = new File(MESSAGE_FILE);
    if(!file.exists()) {
        System.err.println("File message.bin does not exist. Skipping test.");
        return;
    }

    dest.start(Channel.SND_TX_SEQ);

    byte[] bytes = new byte[8192];
    int read;

    try (FileInputStream in = new FileInputStream(file)) {
        read = in.read(bytes);
    }

    ChannelData msg = new ChannelData(false);
    XByteBuffer xbb = new XByteBuffer(read, false);
    xbb.append(bytes, 0, read);
    msg.setMessage(xbb);

    dest.messageReceived(msg);
}
 
Example #18
Source File: TestEncryptInterceptor.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testMessageUniqueness() throws Exception {
    src.start(Channel.SND_TX_SEQ);
    src.setNext(new ValueCaptureInterceptor());

    String testInput = "The quick brown fox jumps over the lazy dog.";

    ChannelData msg = new ChannelData(false);
    msg.setMessage(new XByteBuffer(testInput.getBytes("UTF-8"), false));
    src.sendMessage(null, msg, null);

    byte[] cipherText1 = ((ValueCaptureInterceptor)src.getNext()).getValue();

    msg.setMessage(new XByteBuffer(testInput.getBytes("UTF-8"), false));
    src.sendMessage(null, msg, null);

    byte[] cipherText2 = ((ValueCaptureInterceptor)src.getNext()).getValue();

    Assert.assertThat("Two identical cleartexts encrypt to the same ciphertext",
            cipherText1, IsNot.not(IsEqual.equalTo(cipherText2)));
}
 
Example #19
Source File: McastService.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void broadcast(ChannelMessage message) throws ChannelException {
    if (impl==null || (impl.startLevel & Channel.MBR_TX_SEQ)!=Channel.MBR_TX_SEQ )
        throw new ChannelException(sm.getString("mcastService.noStart"));

    byte[] data = XByteBuffer.createDataPackage((ChannelData)message);
    if (data.length>McastServiceImpl.MAX_PACKET_SIZE) {
        throw new ChannelException(sm.getString("mcastService.exceed.maxPacketSize",
                Integer.toString(data.length) ,
                Integer.toString(McastServiceImpl.MAX_PACKET_SIZE)));
    }
    DatagramPacket packet = new DatagramPacket(data,0,data.length);
    try {
        impl.send(false, packet);
    } catch (Exception x) {
        throw new ChannelException(x);
    }
}
 
Example #20
Source File: McastServiceImpl.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Receive a datagram packet, locking wait
 * @throws IOException Received failed
 */
public void receive() throws IOException {
    boolean checkexpired = true;
    try {

        socket.receive(receivePacket);
        if(receivePacket.getLength() > MAX_PACKET_SIZE) {
            log.error(sm.getString("mcastServiceImpl.packet.tooLong",
                    Integer.toString(receivePacket.getLength())));
        } else {
            byte[] data = new byte[receivePacket.getLength()];
            System.arraycopy(receivePacket.getData(), receivePacket.getOffset(), data, 0, data.length);
            if (XByteBuffer.firstIndexOf(data,0,MemberImpl.TRIBES_MBR_BEGIN)==0) {
                memberDataReceived(data);
            } else {
                memberBroadcastsReceived(data);
            }

        }
    } catch (SocketTimeoutException x ) {
        //do nothing, this is normal, we don't want to block forever
        //since the receive thread is the same thread
        //that does membership expiration
    }
    if (checkexpired) checkExpired();
}
 
Example #21
Source File: OrderInterceptor.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void messageReceived(ChannelMessage msg) {
    if ( !okToProcess(msg.getOptions()) ) {
        super.messageReceived(msg);
        return;
    }
    int msgnr = XByteBuffer.toInt(msg.getMessage().getBytesDirect(),msg.getMessage().getLength()-4);
    msg.getMessage().trim(4);
    MessageOrder order = new MessageOrder(msgnr,(ChannelMessage)msg.deepclone());
    inLock.writeLock().lock();
    try {
        if ( processIncoming(order) ) processLeftOvers(msg.getAddress(),false);
    } finally {
        inLock.writeLock().unlock();
    }
}
 
Example #22
Source File: AbstractReplicatedMap.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public Serializable key(ClassLoader[] cls) throws IOException, ClassNotFoundException {
    if ( key!=null ) return key;
    if ( keydata == null || keydata.length == 0 ) return null;
    key = XByteBuffer.deserialize(keydata,0,keydata.length,cls);
    keydata = null;
    return key;
}
 
Example #23
Source File: AbstractReplicatedMap.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public Serializable value(ClassLoader[] cls) throws IOException, ClassNotFoundException  {
    if ( value!=null ) return value;
    if ( valuedata == null || valuedata.length == 0 ) return null;
    value = XByteBuffer.deserialize(valuedata,0,valuedata.length,cls);
    valuedata = null;
    return value;
}
 
Example #24
Source File: AbstractReplicatedMap.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public void setKey(Serializable key) {
    try {
        if (key != null) keydata = XByteBuffer.serialize(key);
        this.key = key;
    } catch (IOException x) {
        throw new RuntimeException(x);
    }
}
 
Example #25
Source File: AbstractReplicatedMap.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public void setKey(Serializable key) {
    try {
        if (key != null) keydata = XByteBuffer.serialize(key);
        this.key = key;
    } catch (IOException x) {
        throw new RuntimeException(x);
    }
}
 
Example #26
Source File: AbstractReplicatedMap.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public void setValue(Serializable value) {
    try {
        if ( value != null ) valuedata = XByteBuffer.serialize(value);
        this.value = value;
    }catch ( IOException x ) {
        throw new RuntimeException(x);
    }
}
 
Example #27
Source File: ThroughputInterceptor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(ChannelMessage msg) {
    if ( rxStart == 0 ) rxStart = System.currentTimeMillis();
    long bytes = XByteBuffer.getDataPackageLength(((ChannelData)msg).getDataPackageLength());
    mbRx += bytes/(1024d*1024d);
    msgRxCnt.addAndGet(1);
    if ( msgRxCnt.get() % interval == 0 ) report(timeTx);
    super.messageReceived(msg);
    
}
 
Example #28
Source File: AbstractReplicatedMap.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public Serializable key(ClassLoader[] cls) throws IOException, ClassNotFoundException {
    if ( key!=null ) return key;
    if ( keydata == null || keydata.length == 0 ) return null;
    key = XByteBuffer.deserialize(keydata,0,keydata.length,cls);
    keydata = null;
    return key;
}
 
Example #29
Source File: SocketNioReceive.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Member mbr = new MemberImpl("localhost", 9999, 0);
    ChannelData data = new ChannelData();
    data.setAddress(mbr);
    byte[] buf = new byte[8192 * 4];
    data.setMessage(new XByteBuffer(buf, false));
    buf = XByteBuffer.createDataPackage(data);
    len = buf.length;
    NioReceiver receiver = new NioReceiver();
    receiver.setPort(9999);
    receiver.setHost("localhost");
    MyList list = new MyList();
    receiver.setMessageListener(list);
    receiver.start();
    System.out.println("Listening on 9999");
    while (true) {
        try {
            synchronized (mutex) {
                mutex.wait(5000);
                if ( start != 0 ) {
                    System.out.println("Throughput " + df.format(mb / seconds) + " MB/seconds, messages "+count+" accepts "+accept+", total "+mb+" MB.");
                }
            }
        }catch (Throwable x) {
            x.printStackTrace();
        }
    }
}
 
Example #30
Source File: FragmentationInterceptor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(ChannelMessage msg) {
    boolean isFrag = XByteBuffer.toBoolean(msg.getMessage().getBytesDirect(),msg.getMessage().getLength()-1);
    msg.getMessage().trim(1);
    if ( isFrag ) {
        defrag(msg);
    } else {
        super.messageReceived(msg);
    }
}