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;
 
 
 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<Objectpending = new LinkedList<Object>();
     protected final Map<TransportLongtransports = new HashMap<TransportLong>();
     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);
 
     public void offer(final Object data) {
         if (!.get()) {
             throw new IllegalStateException("Transport pool stopped");
         }
         .execute(new Runnable() {
             public void run() {
                 Transport transport = getIdleTransport();
                 if (transport != null) {
                     transport.offer(data);
                     iftransport.full() ) {
                         .put(transport, 0L);
                     }
                 } else {
                     .add(data);
                 }
             }
         });
     }
 
     protected Transport getIdleTransport() {
         for (Map.Entry<TransportLongentry : .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) {
                                .clear();
                                onComplete.run();
                            }
                        }
                    };
                    for (Transport transport : .keySet()) {
                        transport.stop(coutDown);
                    }
                }
            });
        } else {
            onComplete.run();
        }
    }
    protected void startNewTransport() throws Exception {
..println("Creating new transport for: " + this.);
        Transport transport = createTransport(this.);
        transport.setDispatchQueue();
        transport.setProtocolCodec(createCodec());
        transport.setTransportListener(new Listener());
        .put(transport, 0L);
        transport.start();
    }
    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()) {
                boolean accepted = transport.offer(.removeFirst());
                assert accepted"Should have been accepted since the transport was not full";
            }
            iftransport.full() ) {
                .put(transport, 0L);
            } else {
                final long time = System.currentTimeMillis();
                .put(transporttime);
                if ( > 0) {
                    .executeAfter(.new Runnable() {
                        public void run() {
                            if (.get(transport) == time) {
                                .remove(transport);
                                transport.stop();
                            }
                        }
                    });
                }
            }
        }
        public void onTransportFailure(Transport transportIOException error) {
            if (!transport.isDisposed()) {
                .info("Transport failure"error);
                .remove(transport);
                transport.stop();
            }
        }
        public void onTransportConnected(Transport transport) {
            transport.resumeRead();
            onRefill(transport);
        }
        public void onTransportDisconnected(Transport transport) {
            .remove(transport);
        }
    }
New to GrepCode? Check out our FAQ X