001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.transport.reliable;
018    
019    import java.io.IOException;
020    
021    /**
022     * Throws an exception if packets are dropped causing the transport to be
023     * closed.
024     * 
025     * 
026     */
027    public class DefaultReplayStrategy implements ReplayStrategy {
028    
029        private int maximumDifference = 5;
030    
031        public DefaultReplayStrategy() {
032        }
033    
034        public DefaultReplayStrategy(int maximumDifference) {
035            this.maximumDifference = maximumDifference;
036        }
037    
038        public boolean onDroppedPackets(ReliableTransport transport, int expectedCounter, int actualCounter, int nextAvailableCounter) throws IOException {
039            int difference = actualCounter - expectedCounter;
040            long count = Math.abs(difference);
041            if (count > maximumDifference) {
042                int upperLimit = actualCounter - 1;
043                if (upperLimit < expectedCounter) {
044                    upperLimit = expectedCounter;
045                }
046                transport.requestReplay(expectedCounter, upperLimit);
047            }
048    
049            // lets discard old commands
050            return difference > 0;
051        }
052    
053        public void onReceivedPacket(ReliableTransport transport, long expectedCounter) {
054            // TODO we could pro-actively evict stuff from the buffer if we knew there was only one client
055        }
056    
057        public int getMaximumDifference() {
058            return maximumDifference;
059        }
060    
061        /**
062         * Sets the maximum allowed difference between an expected packet and an
063         * actual packet before an error occurs
064         */
065        public void setMaximumDifference(int maximumDifference) {
066            this.maximumDifference = maximumDifference;
067        }
068    
069    }