Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) FuseSource, Inc. http://fusesource.com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.fusesource.fabric.dosgi.tcp;
 
 import java.util.Map;
 import java.util.Set;
 
 
 public abstract class TransportPool implements Service {
 
     protected static final Logger LOGGER = LoggerFactory.getLogger(TransportPool.class);
 
     public static final int DEFAULT_POOL_SIZE = 2;
 
     public static final long DEFAULT_EVICTION_DELAY = ..toMillis(5);
 
     protected final String uri;
     protected final DispatchQueue queue;
     protected final LinkedList<Pairpending = new LinkedList<Pair>();
     protected final Map<TransportTransportStatetransports = new HashMap<TransportTransportState>();
     protected AtomicBoolean running = new AtomicBoolean(false);
 
     protected int poolSize;
     protected long evictionDelay;
 
     public TransportPool(String uriDispatchQueue queue) {
         this(uriqueue);
     }
 
     public TransportPool(String uriDispatchQueue queueint poolSizelong evictionDelay) {
         this. = uri;
         this. = queue;
         this. = poolSize;
         this. = evictionDelay;
     }
 
     protected abstract Transport createTransport(String urithrows Exception;
 
     protected abstract ProtocolCodec createCodec();
 
     protected abstract void onCommand(Object command);
 
     protected abstract void onFailure(Object idThrowable throwable);
 
     protected void onDone(Object id) {
         for (TransportState state : .values()) {
             if (state.inflight.remove(id)) {
                 break;
             }
         }
     }
 
     public void offer(final Object datafinal Object id) {
         if (!.get()) {
             throw new IllegalStateException("Transport pool stopped");
         }
         .execute(new Runnable() {
             public void run() {
                 Transport transport = getIdleTransport();
                 if (transport != null) {
                     doOffer(transportdataid);
                     iftransport.full() ) {
                         .get(transport). = 0L;
                     }
                 } else {
                     .add(new Pair(dataid));
                 }
             }
         });
     }
 
    protected boolean doOffer(Transport transportObject commandObject id) {
        .get(transport)..add(id);
        return transport.offer(command);
    }
    protected Transport getIdleTransport() {
        for (Map.Entry<TransportTransportStateentry : .entrySet()) {
            if (entry.getValue(). > 0) {
                return entry.getKey();
            }
        }
        if (.size() < ) {
            try {
                startNewTransport();
            } catch (Exception e) {
                .info("Unable to start new transport"e);
            }
        }
        return null;
    }
    public void start() throws Exception {
        start(null);
    }
    public void start(Runnable onCompletethrows Exception {
        .set(true);
    }
    public void stop() {
        stop(null);
    }
    public void stop(final Runnable onComplete) {
        if (.compareAndSet(truefalse)) {
            .execute(new Runnable() {
                public void run() {
                    final AtomicInteger latch = new AtomicInteger(.size());
                    final Runnable coutDown = new Runnable() {
                        public void run() {
                            if (latch.decrementAndGet() == 0) {
                                while (!.isEmpty()) {
                                    Pair p = .removeFirst();
                                    onFailure(p.idnew IOException("Transport stopped"));
                                }
                                onComplete.run();
                            }
                        }
                    };
                    while (!.isEmpty()) {
                        Transport transport = .keySet().iterator().next();
                        TransportState state = .remove(transport);
                        if (state != null) {
                            for (Object id : state.inflight) {
                                onFailure(idnew IOException("Transport stopped"));
                            }
                        }
                        transport.stop(coutDown);
                    }
                }
            });
        } else {
            onComplete.run();
        }
    }
    protected void startNewTransport() throws Exception {
        .debug("Creating new transport for: {}"this.);
        Transport transport = createTransport(this.);
        transport.setDispatchQueue();
        transport.setProtocolCodec(createCodec());
        transport.setTransportListener(new Listener());
        .put(transportnew TransportState());
        transport.start();
    }
    protected static class Pair {
        Object command;
        Object id;
        public Pair(Object commandObject id) {
            this. = command;
            this. = id;
        }
    }
    protected static class TransportState {
        long time;
        final Set<Objectinflight;
        public TransportState() {
             = 0;
             = new HashSet<Object>();
        }
    }
    protected class Listener implements TransportListener {
        public void onTransportCommand(Transport transportObject command) {
            TransportPool.this.onCommand(command);
        }
        public void onRefill(final Transport transport) {
            while (.size() > 0 &&  !transport.full()) {
                Pair pair = .removeFirst();
                boolean accepted = doOffer(transportpair.commandpair.id);
                assert accepted"Should have been accepted since the transport was not full";
            }
            iftransport.full() ) {
                .get(transport). = 0L;
            } else {
                final long time = System.currentTimeMillis();
                .get(transport). = time;
                if ( > 0) {
                    .executeAfter(.new Runnable() {
                        public void run() {
                            TransportState state = .get(transport);
                            if (state != null && state.time == time) {
                                .remove(transport);
                                transport.stop();
                            }
                        }
                    });
                }
            }
        }
        public void onTransportFailure(Transport transportIOException error) {
            if (!transport.isDisposed()) {
                .info("Transport failure"error);
                TransportState state = .remove(transport);
                if (state != null) {
                    for (Object id : state.inflight) {
                        onFailure(iderror);
                    }
                }
                transport.stop();
                if (.isEmpty()) {
                    while (!.isEmpty()) {
                        Pair p = .removeFirst();
                        onFailure(p.iderror);
                    }
                }
            }
        }
        public void onTransportConnected(Transport transport) {
            transport.resumeRead();
            onRefill(transport);
        }
        public void onTransportDisconnected(Transport transport) {
            onTransportFailure(transportnew IOException("Transport disconnected"));
        }
    }
New to GrepCode? Check out our FAQ X