List of usage examples for java.nio.channels SocketChannel isBlocking
public final boolean isBlocking()
From source file:com.buaa.cfs.net.SocketIOWithTimeout.java
/** * The contract is similar to {@link SocketChannel#connect(SocketAddress)} with a timeout. * * @param channel - this should be a {@link SelectableChannel} * @param endpoint//from w w w .j a va 2s . com * * @throws IOException * @see SocketChannel#connect(SocketAddress) */ static void connect(SocketChannel channel, SocketAddress endpoint, int timeout) throws IOException { boolean blockingOn = channel.isBlocking(); if (blockingOn) { channel.configureBlocking(false); } try { if (channel.connect(endpoint)) { return; } long timeoutLeft = timeout; long endTime = (timeout > 0) ? (Time.now() + timeout) : 0; while (true) { // we might have to call finishConnect() more than once // for some channels (with user level protocols) int ret = selector.select((SelectableChannel) channel, SelectionKey.OP_CONNECT, timeoutLeft); if (ret > 0 && channel.finishConnect()) { return; } if (ret == 0 || (timeout > 0 && (timeoutLeft = (endTime - Time.now())) <= 0)) { throw new SocketTimeoutException( timeoutExceptionString(channel, timeout, SelectionKey.OP_CONNECT)); } } } catch (IOException e) { // javadoc for SocketChannel.connect() says channel should be closed. try { channel.close(); } catch (IOException ignored) { } throw e; } finally { if (blockingOn && channel.isOpen()) { channel.configureBlocking(true); } } }
From source file:eu.stratosphere.nephele.net.SocketIOWithTimeout.java
/** * The contract is similar to {@link SocketChannel#connect(SocketAddress)} with a timeout. * /*ww w . jav a 2 s .co m*/ * @see SocketChannel#connect(SocketAddress) * @param channel * - this should be a {@link SelectableChannel} * @param endpoint * @throws IOException */ static void connect(SocketChannel channel, SocketAddress endpoint, int timeout) throws IOException { boolean blockingOn = channel.isBlocking(); if (blockingOn) { channel.configureBlocking(false); } try { if (channel.connect(endpoint)) { return; } long timeoutLeft = timeout; long endTime = (timeout > 0) ? (System.currentTimeMillis() + timeout) : 0; while (true) { // we might have to call finishConnect() more than once // for some channels (with user level protocols) int ret = selector.select((SelectableChannel) channel, SelectionKey.OP_CONNECT, timeoutLeft); if (ret > 0 && channel.finishConnect()) { return; } if (ret == 0 || (timeout > 0 && (timeoutLeft = (endTime - System.currentTimeMillis())) <= 0)) { throw new SocketTimeoutException( timeoutExceptionString(channel, timeout, SelectionKey.OP_CONNECT)); } } } catch (IOException e) { // javadoc for SocketChannel.connect() says channel should be closed. try { channel.close(); } catch (IOException ignored) { } throw e; } finally { if (blockingOn && channel.isOpen()) { channel.configureBlocking(true); } } }
From source file:gridool.util.xfer.RecievedFileWriter.java
public final void handleRequest(@Nonnull final SocketChannel inChannel, @Nonnull final Socket socket) throws IOException { final StopWatch sw = new StopWatch(); if (!inChannel.isBlocking()) { inChannel.configureBlocking(true); }/*from w w w.j av a2 s . co m*/ InputStream in = socket.getInputStream(); DataInputStream dis = new DataInputStream(in); String fname = IOUtils.readString(dis); String dirPath = IOUtils.readString(dis); long len = dis.readLong(); boolean append = dis.readBoolean(); boolean ackRequired = dis.readBoolean(); boolean hasAdditionalHeader = dis.readBoolean(); if (hasAdditionalHeader) { readAdditionalHeader(dis, fname, dirPath, len, append, ackRequired); } final File file; if (dirPath == null) { file = new File(baseDir, fname); } else { File dir = FileUtils.resolvePath(baseDir, dirPath); file = new File(dir, fname); } preFileAppend(file, append); final FileOutputStream dst = new FileOutputStream(file, append); final String fp = file.getAbsolutePath(); final ReadWriteLock filelock = accquireLock(fp, locks); final FileChannel fileCh = dst.getChannel(); final long startPos = file.length(); try { NIOUtils.transferFully(inChannel, 0, len, fileCh); // REVIEWME really an atomic operation? } finally { IOUtils.closeQuietly(fileCh, dst); releaseLock(fp, filelock, locks); postFileAppend(file, startPos, len); } if (ackRequired) { OutputStream out = socket.getOutputStream(); DataOutputStream dos = new DataOutputStream(out); dos.writeLong(len); postAck(file, startPos, len); } if (LOG.isDebugEnabled()) { SocketAddress remoteAddr = socket.getRemoteSocketAddress(); LOG.debug("Received a " + (append ? "part of file '" : "file '") + file.getAbsolutePath() + "' of " + len + " bytes from " + remoteAddr + " in " + sw.toString()); } }
From source file:com.sun.grizzly.http.jk.common.ChannelNioSocket.java
public void accept(MsgContext ep) throws IOException { if (sSocket == null) { return;/*w w w. j a v a 2s.co m*/ } synchronized (this) { while (paused) { try { wait(); } catch (InterruptedException ie) { //Ignore, since can't happen } } } SocketChannel sc = sSocket.getChannel().accept(); Socket s = sc.socket(); ep.setNote(socketNote, s); if (LoggerUtils.getLogger().isLoggable(Level.FINEST)) { LoggerUtils.getLogger().log(Level.FINEST, "Accepted socket " + s + " channel " + sc.isBlocking()); } try { setSocketOptions(s); } catch (SocketException sex) { LoggerUtils.getLogger().log(Level.FINEST, "Error initializing Socket Options", sex); } requestCount++; sc.configureBlocking(false); InputStream is = new SocketInputStream(sc); OutputStream os = new SocketOutputStream(sc); ep.setNote(isNote, is); ep.setNote(osNote, os); ep.setControl(tp); }
From source file:org.apache.htrace.impl.PackedBufferManager.java
private SelectionKey doConnect() throws IOException { SocketChannel sock = SocketChannel.open(); SelectionKey sockKey = null;//from w w w.j a va 2s .c o m boolean success = false; try { if (sock.isBlocking()) { sock.configureBlocking(false); } InetSocketAddress resolvedEndpoint = new InetSocketAddress(conf.endpoint.getHostString(), conf.endpoint.getPort()); resolvedEndpoint.getHostName(); // trigger DNS resolution sock.connect(resolvedEndpoint); sockKey = sock.register(selector, SelectionKey.OP_CONNECT, sock); long startMs = TimeUtil.nowMs(); long remainingMs = conf.connectTimeoutMs; while (true) { selector.select(remainingMs); for (SelectionKey key : selector.keys()) { if (key.isConnectable()) { SocketChannel s = (SocketChannel) key.attachment(); s.finishConnect(); if (LOG.isTraceEnabled()) { LOG.trace("Successfully connected to " + conf.endpointStr + "."); } success = true; return sockKey; } } remainingMs = updateRemainingMs(startMs, conf.connectTimeoutMs); if (remainingMs == 0) { throw new IOException("Attempt to connect to " + conf.endpointStr + " timed out after " + TimeUtil.deltaMs(startMs, TimeUtil.nowMs()) + " ms."); } } } finally { if (!success) { if (sockKey != null) { sockKey.cancel(); } sock.close(); } } }
From source file:xbird.util.xfer.RecievedFileWriter.java
public final void handleRequest(@Nonnull final SocketChannel inChannel, @Nonnull final Socket socket) throws IOException { final StopWatch sw = new StopWatch(); if (!inChannel.isBlocking()) { inChannel.configureBlocking(true); }//from www . j a v a 2 s .c om InputStream in = socket.getInputStream(); DataInputStream dis = new DataInputStream(in); String fname = IOUtils.readString(dis); String dirPath = IOUtils.readString(dis); long len = dis.readLong(); boolean append = dis.readBoolean(); boolean ackRequired = dis.readBoolean(); boolean hasAdditionalHeader = dis.readBoolean(); if (hasAdditionalHeader) { readAdditionalHeader(dis, fname, dirPath, len, append, ackRequired); } final File file; if (dirPath == null) { file = new File(baseDir, fname); } else { File dir = FileUtils.resolvePath(baseDir, dirPath); file = new File(dir, fname); } preFileAppend(file, append); final FileOutputStream dst = new FileOutputStream(file, append); final String fp = file.getAbsolutePath(); final ReadWriteLock filelock = accquireLock(fp, locks); final FileChannel fileCh = dst.getChannel(); final long startPos = file.length(); try { NIOUtils.transferFullyFrom(inChannel, 0, len, fileCh); // REVIEWME really an atomic operation? } finally { IOUtils.closeQuietly(fileCh, dst); releaseLock(fp, filelock, locks); postFileAppend(file, startPos, len); } if (ackRequired) { OutputStream out = socket.getOutputStream(); DataOutputStream dos = new DataOutputStream(out); dos.writeLong(len); postAck(file, startPos, len); } if (LOG.isDebugEnabled()) { SocketAddress remoteAddr = socket.getRemoteSocketAddress(); LOG.debug("Received a " + (append ? "part of file '" : "file '") + file.getAbsolutePath() + "' of " + len + " bytes from " + remoteAddr + " in " + sw.toString()); } }