Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * 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 io.undertow.conduits;
 
 
Conduit that adds support to close a channel once for a specified time no reads and no writes were performed.

Author(s):
Norman Maurer
 
 
     private static final int DELTA = 100;
     private volatile XnioExecutor.Key handle;
     private volatile long idleTimeout;
     private volatile long expireTime = -1;
     private volatile boolean timedOut = false;
 
     private final StreamSinkConduit sink;
     private final StreamSourceConduit source;
 
     private volatile WriteReadyHandler writeReadyHandler;
     private volatile ReadReadyHandler readReadyHandler;
 
     private final Runnable timeoutCommand = new Runnable() {
         @Override
         public void run() {
              = null;
             if( == -1) {
                 return;
             }
             long current = System.currentTimeMillis();
             if(current  < ) {
                 //timeout has been bumped, re-schedule
                  = .getWriteThread().executeAfter(, ( - current) + .);
                 return;
             }
 
             ..tracef("Timing out channel %s due to inactivity");
              = true;
             doClose();
             if (.isWriteResumed()) {
                 if( != null) {
                     .writeReady();
                 }
             }
             if (.isReadResumed()) {
                 if( != null) {
                     .readReady();
                 }
             }
         }
     };
 
     protected void doClose() {
         safeClose();
         safeClose();
     }
 
     public IdleTimeoutConduit(StreamSinkConduit sinkStreamSourceConduit source) {
         this. = sink;
         this. = source;
     }
 
     private void handleIdleTimeout() throws ClosedChannelException {
         if() {
             return;
        }
        long idleTimeout = this.;
        if(idleTimeout <= 0) {
            return;
        }
        long currentTime = System.currentTimeMillis();
        long expireTimeVar = ;
        if(expireTimeVar != -1 && currentTime > expireTimeVar) {
             = true;
            doClose();
            throw new ClosedChannelException();
        }
         = currentTime + idleTimeout;
        XnioExecutor.Key key = ;
        if (key == null) {
             = .getWriteThread().executeAfter(idleTimeout.);
        }
    }
    @Override
    public int write(ByteBuffer srcthrows IOException {
        handleIdleTimeout();
        int w = .write(src);
        return w;
    }
    @Override
    public long write(ByteBuffer[] srcsint offsetint lengththrows IOException {
        handleIdleTimeout();
        long w = .write(srcsoffsetlength);
        return w;
    }
    @Override
    public int writeFinal(ByteBuffer srcthrows IOException {
        handleIdleTimeout();
        int w = .writeFinal(src);
        return w;
    }
    @Override
    public long writeFinal(ByteBuffer[] srcsint offsetint lengththrows IOException {
        handleIdleTimeout();
        long w = .writeFinal(srcsoffsetlength);
        return w;
    }
    @Override
    public long transferTo(long positionlong countFileChannel targetthrows IOException {
        handleIdleTimeout();
        long w = .transferTo(positioncounttarget);
        return w;
    }
    @Override
    public long transferTo(long countByteBuffer throughBufferStreamSinkChannel targetthrows IOException {
        handleIdleTimeout();
        long w = .transferTo(countthroughBuffertarget);
        return w;
    }
    @Override
    public long read(ByteBuffer[] dstsint offsetint lengththrows IOException {
        handleIdleTimeout();
        long r = .read(dstsoffsetlength);
        return r;
    }
    @Override
    public int read(ByteBuffer dstthrows IOException {
        handleIdleTimeout();
        int r = .read(dst);
        return r;
    }
    @Override
    public long transferFrom(FileChannel srclong positionlong countthrows IOException {
        handleIdleTimeout();
        long r = .transferFrom(srcpositioncount);
        return r;
    }
    @Override
    public long transferFrom(StreamSourceChannel sourcelong countByteBuffer throughBufferthrows IOException {
        handleIdleTimeout();
        long r = .transferFrom(sourcecountthroughBuffer);
        return r;
    }
    @Override
    public void suspendReads() {
        .suspendReads();
    }
    @Override
    public void terminateReads() throws IOException {
        .terminateReads();
    }
    @Override
    public boolean isReadShutdown() {
        return .isReadShutdown();
    }
    @Override
    public void resumeReads() {
        .resumeReads();
    }
    @Override
    public boolean isReadResumed() {
        return .isReadResumed();
    }
    @Override
    public void wakeupReads() {
        .wakeupReads();
    }
    @Override
    public void awaitReadable() throws IOException {
        .awaitReadable();
    }
    @Override
    public void awaitReadable(long timeTimeUnit timeUnitthrows IOException {
        .awaitReadable(timetimeUnit);
    }
    @Override
    public XnioIoThread getReadThread() {
        return .getReadThread();
    }
    @Override
    public void setReadReadyHandler(ReadReadyHandler handler) {
        this. = handler;
        .setReadReadyHandler(handler);
    }
    private static void safeClose(final StreamSourceConduit sink) {
        try {
            sink.terminateReads();
        } catch (IOException e) {
        }
    }
    private static void safeClose(final StreamSinkConduit sink) {
        try {
            sink.truncateWrites();
        } catch (IOException e) {
        }
    }
    @Override
    public void terminateWrites() throws IOException {
        .terminateWrites();
    }
    @Override
    public boolean isWriteShutdown() {
        return .isWriteShutdown();
    }
    @Override
    public void resumeWrites() {
        .resumeWrites();
    }
    @Override
    public void suspendWrites() {
        .suspendWrites();
    }
    @Override
    public void wakeupWrites() {
        .wakeupWrites();
    }
    @Override
    public boolean isWriteResumed() {
        return .isWriteResumed();
    }
    @Override
    public void awaitWritable() throws IOException {
        .awaitWritable();
    }
    @Override
    public void awaitWritable(long timeTimeUnit timeUnitthrows IOException {
        .awaitWritable();
    }
    @Override
    public XnioIoThread getWriteThread() {
        return .getWriteThread();
    }
    @Override
    public void setWriteReadyHandler(WriteReadyHandler handler) {
        this. = handler;
        .setWriteReadyHandler(handler);
    }
    @Override
    public void truncateWrites() throws IOException {
        .truncateWrites();
    }
    @Override
    public boolean flush() throws IOException {
        return .flush();
    }
    @Override
    public XnioWorker getWorker() {
        return .getWorker();
    }
    public long getIdleTimeout() {
        return ;
    }
    public void setIdleTimeout(long idleTimeout) {
        this. = idleTimeout;
        if(idleTimeout > 0) {
             = System.currentTimeMillis() + idleTimeout;
        } else {
             = -1;
        }
        if (idleTimeout > 0 &&  == null) {
             = .getWriteThread().executeAfter(idleTimeout + .);
        }
    }
New to GrepCode? Check out our FAQ X