| | |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.concurrent.atomic.AtomicLong; |
| | | import java.util.concurrent.atomic.AtomicReference; |
| | | import java.util.concurrent.locks.Lock; |
| | | import java.util.concurrent.locks.ReentrantLock; |
| | | |
| | | import org.opends.messages.Message; |
| | | import org.opends.messages.MessageBuilder; |
| | |
| | | */ |
| | | private class TimeoutWriteByteChannel implements ByteChannel |
| | | { |
| | | // Synchronize concurrent writes to the same connection. |
| | | private final Lock writeLock = new ReentrantLock(); |
| | | |
| | | public int read(ByteBuffer byteBuffer) throws IOException |
| | | { |
| | | return clientChannel.read(byteBuffer); |
| | |
| | | clientChannel.close(); |
| | | } |
| | | |
| | | |
| | | |
| | | public int write(ByteBuffer byteBuffer) throws IOException |
| | | { |
| | | writeLock.lock(); |
| | | try |
| | | { |
| | | int bytesToWrite = byteBuffer.remaining(); |
| | | clientChannel.write(byteBuffer); |
| | | if(!byteBuffer.hasRemaining()) |
| | | { |
| | | return bytesToWrite; |
| | | } |
| | | |
| | | long startTime = System.currentTimeMillis(); |
| | | long waitTime = getMaxBlockedWriteTimeLimit(); |
| | | if (waitTime <= 0) |
| | |
| | | // blocking a worker thread. |
| | | waitTime = 300000L; |
| | | } |
| | | |
| | | long stopTime = startTime + waitTime; |
| | | |
| | | Selector selector = getWriteSelector(); |
| | |
| | | } |
| | | |
| | | // Register with the selector for handling write operations. |
| | | SelectionKey key = |
| | | clientChannel.register(selector, SelectionKey.OP_WRITE); |
| | | |
| | | SelectionKey key = clientChannel.register(selector, |
| | | SelectionKey.OP_WRITE); |
| | | try |
| | | { |
| | | selector.select(waitTime); |
| | |
| | | waitTime = stopTime - currentTime; |
| | | } |
| | | |
| | | Iterator<SelectionKey> iterator = |
| | | selector.selectedKeys().iterator(); |
| | | Iterator<SelectionKey> iterator = selector.selectedKeys() |
| | | .iterator(); |
| | | while (iterator.hasNext()) |
| | | { |
| | | SelectionKey k = iterator.next(); |
| | |
| | | } |
| | | } |
| | | } |
| | | finally |
| | | { |
| | | writeLock.unlock(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |