private static void scScatter() throws Exception { Pipe p = Pipe.open(); Pipe.SinkChannel sink = p.sink(); Pipe.SourceChannel source = p.source(); sink.configureBlocking(false); ByteBuffer outgoingdata = ByteBuffer.allocateDirect(30); byte[] someBytes = new byte[30]; generator.nextBytes(someBytes); outgoingdata.put(someBytes); outgoingdata.flip(); int totalWritten = 0; while (totalWritten < 30) { int written = sink.write(outgoingdata); if (written < 0) throw new Exception("Write failed"); totalWritten += written; } ByteBuffer[] bufs = new ByteBuffer[3]; for (int i = 0; i < 3; i++) bufs[i] = ByteBuffer.allocateDirect(10); long numBytesRead = source.read(bufs); if (numBytesRead < 30) throw new Exception("Pipe test failed"); sink.close(); source.close(); }
/** * A convenience method for the following code: * * <pre> * {@link Pipe} inputToOutput = {@link Pipe#open()}; * {@link Pipe.SourceChannel} source = {@link Pipe#source() inputToOutput.source()}; * {@link SelectableChannel#configureBlocking(boolean) source.configureBlocking(false)}; * {@link #addRoute(SelectableChannel, SelectableChannel) addRoute(source, output)}; * * {@link Pipe.SinkChannel} sink = {@link Pipe#sink() inputToOutput.sink()}; * return {@link #wrapChannelInObjectOutputStream(WritableByteChannel) wrapChannelInObjectOutputStream(sink)}; * </pre> * * Users must call {@link ObjectOutputStream#flush()} before attempting to construct a matching * {@link ObjectInputStream} to ensure a serialization header is available. * * @throws IOException pass-through for any {@link IOException} that occurs in the above code */ public <T extends SelectableChannel & WritableByteChannel> ObjectOutputStream addRouteFromObjectOutputStream(T output) throws IOException { Pipe inputToOutput = Pipe.open(); Pipe.SourceChannel source = inputToOutput.source(); source.configureBlocking(false); addRoute(source, output); Pipe.SinkChannel sink = inputToOutput.sink(); return wrapChannelInObjectOutputStream(sink); }
public Signaler() { // Create the socketpair for signaling. Pipe pipe; try { pipe = Pipe.open(); } catch (IOException e) { throw new ZError.IOException(e); } r = pipe.source(); w = pipe.sink(); // Set both fds to non-blocking mode. try { Utils.unblockSocket(w); Utils.unblockSocket(r); } catch (IOException e) { throw new ZError.IOException(e); } try { selector = Selector.open(); r.register(selector, SelectionKey.OP_READ); } catch (IOException e) { throw new ZError.IOException(e); } }
// Prepares and adds channels to list for selection private void prepareChannels() { readableFDs.add(sourcefd); List<SelectionKey> readChannelList = new ArrayList<SelectionKey>(); readChannelList.add(source.keyFor(this)); List<SelectionKey> writeChannelList = new ArrayList<SelectionKey>(); synchronized (keysLock) { for (Iterator<SelectionKey> i = keys.iterator(); i.hasNext(); ) { SelectionKeyImpl key = (SelectionKeyImpl) i.next(); key.oldInterestOps = key.interestOps(); boolean isReadableChannel = ((SelectionKey.OP_ACCEPT | SelectionKey.OP_READ) & key.oldInterestOps) != 0; boolean isWritableChannel = ((SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE) & key.oldInterestOps) != 0; SelectableChannel channel = key.channel(); if (isReadableChannel) { readChannelList.add(channel.keyFor(this)); readableFDs.add(((FileDescriptorHandler) channel).getFD()); } if (isWritableChannel) { writeChannelList.add(channel.keyFor(this)); writableFDs.add(((FileDescriptorHandler) channel).getFD()); } } } readableChannels = readChannelList.toArray(new SelectionKey[0]); writableChannels = writeChannelList.toArray(new SelectionKey[0]); readable = readableFDs.toArray(new FileDescriptor[0]); writable = writableFDs.toArray(new FileDescriptor[0]); }
public static void main(String[] argv) throws Exception { Pipe[] pipes = new Pipe[PIPES_COUNT]; Pipe pipe = Pipe.open(); Pipe.SinkChannel sink = pipe.sink(); Pipe.SourceChannel source = pipe.source(); Selector sel = Selector.open(); source.configureBlocking(false); source.register(sel, SelectionKey.OP_READ); for (int i = 0; i < PIPES_COUNT; i++) { pipes[i] = Pipe.open(); Pipe.SourceChannel sc = pipes[i].source(); sc.configureBlocking(false); sc.register(sel, SelectionKey.OP_READ); Pipe.SinkChannel sc2 = pipes[i].sink(); sc2.configureBlocking(false); sc2.register(sel, SelectionKey.OP_WRITE); } for (int i = 0; i < LOOPS; i++) { sink.write(ByteBuffer.allocate(BUF_SIZE)); int x = sel.selectNow(); sel.selectedKeys().clear(); source.read(ByteBuffer.allocate(BUF_SIZE)); } for (int i = 0; i < PIPES_COUNT; i++) { pipes[i].sink().close(); pipes[i].source().close(); } pipe.sink().close(); pipe.source().close(); sel.close(); }
public void recv() { int nbytes = 0; try { ByteBuffer dummy = ByteBuffer.allocate(1); nbytes = r.read(dummy); assert nbytes == 1; } catch (IOException e) { throw new ZError.IOException(e); } rcursor++; }
public SelectorImpl(SelectorProvider selectorProvider) { super(selectorProvider); try { Pipe mockSelector = selectorProvider.openPipe(); sink = mockSelector.sink(); source = mockSelector.source(); sourcefd = ((FileDescriptorHandler) source).getFD(); source.configureBlocking(false); } catch (IOException e) { // do nothing } }
// Analyses selected channels and adds keys of ready channels to // selectedKeys list private int processSelectResult(int[] readyChannels) throws IOException { if (0 == readyChannels.length) { return 0; } // if the mock channel is selected, read the content. if (READABLE == readyChannels[0]) { ByteBuffer readbuf = ByteBuffer.allocate(MOCK_READBUF_SIZE); while (source.read(readbuf) > 0) { readbuf.flip(); } } int selected = 0; for (int i = 1; i < readyChannels.length; i++) { SelectionKeyImpl key = (SelectionKeyImpl) (i >= readable.length ? writableChannels[i - readable.length] : readableChannels[i]); if (null == key) { continue; } boolean isOldSelectedKey = selectedKeys.contains(key); int selectedOp = 0; // set ready ops switch (readyChannels[i]) { case NA: selectedOp = 0; break; case READABLE: selectedOp = (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT) & key.oldInterestOps; break; case WRITEABLE: if (isConnected(key)) { selectedOp = SelectionKey.OP_WRITE & key.oldInterestOps; } else { selectedOp = SelectionKey.OP_CONNECT & key.oldInterestOps; } break; } if (0 != selectedOp) { if (isOldSelectedKey && key.readyOps() != selectedOp) { key.setReadyOps(key.readyOps() | selectedOp); selected++; } else if (!isOldSelectedKey) { key.setReadyOps(selectedOp); selectedKeys.add(key); selected++; } } } readableChannels = null; writableChannels = null; return selected; }
@Override public void close() throws IOException { IOException exception = null; try { r.close(); } catch (IOException e) { exception = e; } try { w.close(); } catch (IOException e) { exception = e; } try { selector.close(); } catch (IOException e) { exception = e; } if (exception != null) { throw exception; } }