Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*-
   * See the file LICENSE for redistribution information.
   *
   * Copyright (c) 2002, 2013 Oracle and/or its affiliates.  All rights reserved.
   *
   */
  package com.sleepycat.je.rep.utilint;
  
 
NamedChannelWithTimeout permits association of timeouts with a SocketChannel. This mechanism is necessary, since the standard mechanism for associating timeouts with sockets using Socket.setSoTimeout is not supported by nio SocketChannels.
 
 public class NamedChannelWithTimeout
     extends NamedChannel {
 
     /*
      * Denotes read activity associated with the channel. It's set each time a
      * read is successfully executed on the channel. The presence of heartbeats
      * is typically used to guarantee some minimum level of activity over the
      * channel.
      */
     private volatile boolean readActivity;
 
     /*
      * The timeout associated with the channel. A value of zero indicates no
      * timeout.
      */
     private volatile int timeoutMs;
 
     /* Values to help with logging. */
     private final EnvironmentImpl envImpl;
     private final Logger logger;
 
     /* The "time" of the last check for read activity on the channel. */
     private long lastCheckMs = 0l;
 
     public NamedChannelWithTimeout(RepNode repNode,
                                    SocketChannel channel,
                                    int timeoutMs) {
         super(channel);
         this. = timeoutMs;
         this. = repNode.getRepImpl();
         this. = repNode.getLogger();
          = true;
         if (timeoutMs > 0) {
             /* Only register with a timer, if a timeout is being requested. */
             repNode.getChannelTimeoutTask().register(this);
         }
     }

    
Used to modify the timeout associated with the channel.

Parameters:
timeoutMs the new timeout value
 
     public void setTimeoutMs(int timeoutMs) {
         this. = timeoutMs;
         /* Ensure that the next tick resets the time and counter. */
          = true;
     }
 
     /*
      * Methods below supply the Protocol for byte channel. The implementations
      * wrap read operations to track i/o activity by setting readActivity.
      * getCurrentActivity() can be used to determine if there was any read
      * activity since an earlier call to the method.
      */
     @Override
     public int read(ByteBuffer dst)
         throws IOException {
 
         final int bytes = .read(dst);
         if (bytes > 0) {
              = true;
         }
         return bytes;
     }
 
     @Override
     public void close()
         throws IOException {
 
         .close();
          = false;
     }
 
     private void resetActivityCounter(long timeMs) {
          = timeMs;
         = false;
    }

    
Method invoked by the time thread to check on the channel on a periodic basis. Note that the time that is passed in is a "pseudo" time that is only meaningful for calculating time differences.

Parameters:
timeMs the pseudo time
Returns:
true if the channel is active, false if it isn't and has been closed
    public boolean isActive(long timeMs) {
        if (!.isOpen()) {
            /* some thread closed it. */
            return false;
        }
        if (!.isConnected()) {
            /* Not yet connected, wait for it to be connected. */
            return true;
        }
        if () {
            resetActivityCounter(timeMs);
            return true;
        }
        if (( == 0) || (timeMs - ) < ) {
            return true;
        }
        /*
         * No activity, force the channel closed thus generating an
         * AsynchronousCloseException in the read/write threads.
         */
        LoggerUtils.info(,
                         "Inactive channel: " + getNameIdPair() +
                         " forced close. Timeout: " +  + "ms.");
        try {
            .close();
        } catch (IOException e) {
            /* Ignore the exception. */
        }
        return false;
    }
New to GrepCode? Check out our FAQ X