Package java.nio.channels

Examples of java.nio.channels.Pipe


  }
 
  public void testSocketIOWithTimeout() throws IOException {
   
    // first open pipe:
    Pipe pipe = Pipe.open();
    Pipe.SourceChannel source = pipe.source();
    Pipe.SinkChannel sink = pipe.sink();
   
    try {
      InputStream in = new SocketInputStream(source, TIMEOUT);
      OutputStream out = new SocketOutputStream(sink, TIMEOUT);
     
View Full Code Here


    private InternalKeyMap<EpollSelectionKeyImpl> quickMap = new InternalKeyMap<EpollSelectionKeyImpl>();

    public EpollSelectorImpl(SelectorProvider selectorProvider) {
        super(selectorProvider);
        try {
            Pipe mockSelector = selectorProvider.openPipe();
            sink = mockSelector.sink();
            source = mockSelector.source();
            sourcefd = ((FileDescriptorHandler) source).getFD();
            source.configureBlocking(false);

            fileDescriptorClass = sourcefd.getClass();
View Full Code Here

  }
 
  public void testSocketIOWithTimeout() throws IOException {
   
    // first open pipe:
    Pipe pipe = Pipe.open();
    Pipe.SourceChannel source = pipe.source();
    Pipe.SinkChannel sink = pipe.sink();
   
    try {
      InputStream in = new SocketInputStream(source, TIMEOUT);
      OutputStream out = new SocketOutputStream(sink, TIMEOUT);
     
View Full Code Here

  public final void test ()
      throws Exception
  {
    final Transcript transcript = Transcript.create (this);
    BasicThreadingSecurityManager.initialize ();
    final Pipe pipe = Pipe.open ();
    final QueueingExceptionTracer exceptionsQueue = QueueingExceptionTracer.create (NullExceptionTracer.defaultInstance);
    final TranscriptExceptionTracer exceptions = TranscriptExceptionTracer.create (transcript, exceptionsQueue);
    final BasicThreadingContext threading = BasicThreadingContext.create (this, exceptions, exceptions.catcher);
    Assert.assertTrue (threading.initialize (BasicComponentTest.defaultPollTimeout));
    final BasicCallbackReactor reactor = BasicCallbackReactor.create (threading, exceptions);
    Assert.assertTrue (reactor.initialize (BasicComponentTest.defaultPollTimeout));
    final DefaultChannelMessageCoder coder = DefaultChannelMessageCoder.defaultInstance;
    final BasicChannel channel = BasicChannel.create (pipe.source (), pipe.sink (), coder, reactor, threading, exceptions);
    final BasicComponent component = BasicComponent.create (reactor, exceptions);
    Assert.assertTrue (channel.initialize (BasicComponentTest.defaultPollTimeout));
    Assert.assertTrue (component.initialize (BasicComponentTest.defaultPollTimeout));
    final ComponentController componentController = component.getController ();
    final ComponentCallbacks componentCallbacksProxy = reactor.createProxy (ComponentCallbacks.class);
    Assert.assertTrue (componentController.bind (componentCallbacksProxy, channel.getController ()).await (BasicComponentTest.defaultPollTimeout));
    final QueueingComponentCallbacks componentCallbacks = QueueingComponentCallbacks.create (componentController, exceptions);
    final CallbackIsolate componentCallbacksIsolate = reactor.createIsolate ();
    Assert.assertTrue (reactor.assignHandler (componentCallbacksProxy, componentCallbacks, componentCallbacksIsolate).await (BasicComponentTest.defaultPollTimeout));
    final ComponentIdentifier peer = ComponentIdentifier.resolve (Strings.repeat ("00", 20));
    for (int index = 0; index < BasicComponentTest.defaultTries; index++) {
      final ComponentCallRequest outboundRequest = RandomMessageGenerator.defaultInstance.generateComponentCallRequest ();
      Assert.assertTrue (componentController.call (peer, outboundRequest).await (BasicComponentTest.defaultPollTimeout));
      final ComponentCallRequest inboundRequest = (ComponentCallRequest) componentCallbacks.queue.poll (BasicComponentTest.defaultPollTimeout, TimeUnit.MILLISECONDS);
      Assert.assertNotNull (inboundRequest);
      Assert.assertEquals (outboundRequest.operation, inboundRequest.operation);
      Assert.assertEquals (outboundRequest.inputs, inboundRequest.inputs);
      Assert.assertEquals (outboundRequest.data, inboundRequest.data);
      final ComponentCallReply outboundReply = RandomMessageGenerator.defaultInstance.generateComponentCallReply (inboundRequest);
      Assert.assertTrue (componentController.callReturn (outboundReply).await (BasicComponentTest.defaultPollTimeout));
      final ComponentCallReply inboundReply = (ComponentCallReply) componentCallbacks.queue.poll (BasicComponentTest.defaultPollTimeout, TimeUnit.MILLISECONDS);
      Assert.assertNotNull (inboundReply);
      Assert.assertEquals (outboundRequest.reference, inboundReply.reference);
      Assert.assertEquals (outboundRequest.inputs, inboundReply.outputsOrError);
      Assert.assertEquals (outboundRequest.data, inboundReply.data);
    }
    pipe.sink ().close ();
    Assert.assertTrue (component.await (BasicComponentTest.defaultPollTimeout * 10));
    Assert.assertTrue (component.destroy (BasicComponentTest.defaultPollTimeout));
    Assert.assertTrue (channel.destroy (BasicComponentTest.defaultPollTimeout));
    Assert.assertTrue (componentCallbacksIsolate.destroy ().await (BasicComponentTest.defaultPollTimeout));
    Assert.assertTrue (reactor.destroy (BasicComponentTest.defaultPollTimeout));
View Full Code Here

  public final void test ()
      throws Exception
  {
    final Transcript transcript = Transcript.create (this);
    BasicThreadingSecurityManager.initialize ();
    final Pipe pipe = Pipe.open ();
    final QueueingExceptionTracer exceptionsQueue = QueueingExceptionTracer.create (NullExceptionTracer.defaultInstance);
    final TranscriptExceptionTracer exceptions = TranscriptExceptionTracer.create (transcript, exceptionsQueue);
    final BasicThreadingContext threading = BasicThreadingContext.create (this, exceptions, exceptions.catcher);
    Assert.assertTrue (threading.initialize (BasicChannelTest.defaultPollTimeout));
    final BasicCallbackReactor reactor = BasicCallbackReactor.create (threading, exceptions);
    Assert.assertTrue (reactor.initialize (BasicChannelTest.defaultPollTimeout));
    final DefaultChannelMessageCoder coder = DefaultChannelMessageCoder.defaultInstance;
    final BasicChannel channel = BasicChannel.create (pipe.source (), pipe.sink (), coder, reactor, threading, exceptions);
    Assert.assertTrue (channel.initialize (BasicChannelTest.defaultPollTimeout));
    final ChannelController channelController = channel.getController ();
    final ChannelCallbacks channelCallbacksProxy = reactor.createProxy (ChannelCallbacks.class);
    Assert.assertTrue (channelController.bind (channelCallbacksProxy).await (BasicChannelTest.defaultPollTimeout));
    final QueueingChannelCallbacks channelCallbacks = QueueingChannelCallbacks.create (channelController, exceptions);
    final CallbackIsolate channelCallbacksIsolate = reactor.createIsolate ();
    Assert.assertTrue (reactor.assignHandler (channelCallbacksProxy, channelCallbacks, channelCallbacksIsolate).await (BasicChannelTest.defaultPollTimeout));
    for (int index = 0; index < BasicChannelTest.defaultTries; index++) {
      final ChannelMessage outboundMessage = RandomMessageGenerator.defaultInstance.generateChannelMessage ();
      Assert.assertTrue (channelController.send (outboundMessage).await (BasicChannelTest.defaultPollTimeout));
      final ChannelMessage inboundMessage = channelCallbacks.queue.poll (BasicChannelTest.defaultPollTimeout, TimeUnit.MILLISECONDS);
      Assert.assertNotNull (inboundMessage);
      Assert.assertEquals (outboundMessage.metaData, inboundMessage.metaData);
      Assert.assertEquals (outboundMessage.data, inboundMessage.data);
    }
    pipe.sink ().close ();
    Assert.assertTrue (channel.destroy (BasicChannelTest.defaultPollTimeout));
    Assert.assertTrue (channelCallbacksIsolate.destroy ().await (BasicChannelTest.defaultPollTimeout));
    Assert.assertTrue (reactor.destroy (BasicChannelTest.defaultPollTimeout));
    Assert.assertTrue (threading.destroy (BasicChannelTest.defaultPollTimeout));
    Assert.assertNull (exceptionsQueue.queue.poll ());
View Full Code Here

      outputStream = channelConnection.getOutputStream ();
      environment.transcript.traceDebugging ("accepted socket.");
    } else
      throw (new IllegalArgumentException ());
    environment.transcript.traceDebugging ("creating pipes...");
    final Pipe inputPipe = Pipe.open ();
    final Pipe outputPipe = Pipe.open ();
    environment.transcript.traceDebugging ("creating pipers...");
    final Piper inputPiper = new Piper (Channels.newChannel (inputStream), inputPipe.sink (), environment.threading, environment.exceptions);
    final Piper outputPiper = new Piper (outputPipe.source (), Channels.newChannel (outputStream), environment.threading, environment.exceptions);
    environment.transcript.traceDebugging ("creating coder...");
    final DefaultChannelMessageCoder coder = DefaultChannelMessageCoder.create ();
    environment.transcript.traceDebugging ("creating channel...");
    final BasicChannel channel = BasicChannel.create (inputPipe.source (), outputPipe.sink (), coder, environment.reactor, environment.threading, environment.exceptions);
    environment.transcript.traceDebugging ("initializing channel...");
    channel.initialize ();
    environment.transcript.traceInformation ("prepared channel.");
    return (channel);
  }
View Full Code Here

  public final void test ()
      throws Throwable
  {
    final Transcript transcript = Transcript.create (this);
    BasicThreadingSecurityManager.initialize ();
    final Pipe pipe1 = Pipe.open ();
    final Pipe pipe2 = Pipe.open ();
    final QueueingExceptionTracer exceptionsQueue = QueueingExceptionTracer.create (NullExceptionTracer.defaultInstance);
    final TranscriptExceptionTracer exceptions = TranscriptExceptionTracer.create (transcript, exceptionsQueue);
    final BasicThreadingContext threading = BasicThreadingContext.create (this, exceptions, exceptions.catcher);
    Assert.assertTrue (threading.initialize (AbacusTest.defaultPollTimeout));
    final BasicCallbackReactor reactor = BasicCallbackReactor.create (threading, exceptions);
    Assert.assertTrue (reactor.initialize (AbacusTest.defaultPollTimeout));
    final DefaultChannelMessageCoder coder = DefaultChannelMessageCoder.defaultInstance;
    final BasicChannel serverChannel = BasicChannel.create (pipe1.source (), pipe2.sink (), coder, reactor, threading, exceptions);
    final BasicChannel clientChannel = BasicChannel.create (pipe2.source (), pipe1.sink (), coder, reactor, threading, exceptions);
    final BasicComponent serverComponent = BasicComponent.create (reactor, exceptions);
    final BasicComponent clientComponent = BasicComponent.create (reactor, exceptions);
    Assert.assertTrue (serverChannel.initialize (AbacusTest.defaultPollTimeout));
    Assert.assertTrue (clientChannel.initialize (AbacusTest.defaultPollTimeout));
    Assert.assertTrue (serverComponent.initialize (AbacusTest.defaultPollTimeout));
    Assert.assertTrue (clientComponent.initialize (AbacusTest.defaultPollTimeout));
    final ComponentController serverComponentController = serverComponent.getController ();
    final ComponentController clientComponentController = clientComponent.getController ();
    final ComponentCallbacks serverComponentCallbacksProxy = reactor.createProxy (ComponentCallbacks.class);
    final ComponentCallbacks clientComponentCallbacksProxy = reactor.createProxy (ComponentCallbacks.class);
    Assert.assertTrue (serverComponentController.bind (serverComponentCallbacksProxy, serverChannel.getController ()).await (AbacusTest.defaultPollTimeout));
    Assert.assertTrue (clientComponentController.bind (clientComponentCallbacksProxy, clientChannel.getController ()).await (AbacusTest.defaultPollTimeout));
    final AbacusComponentCallbacks serverComponentCallbacks = new AbacusComponentCallbacks (ComponentEnvironment.create (ComponentIdentifier.standalone, this.getClass ().getClassLoader (), reactor, threading, exceptions));
    final QueueingComponentCallbacks clientComponentCallbacks = QueueingComponentCallbacks.create (clientComponentController, exceptions);
    final CallbackIsolate serverComponentCallbacksIsolate = reactor.createIsolate ();
    final CallbackIsolate clientComponentCallbacksIsolate = reactor.createIsolate ();
    Assert.assertTrue (reactor.assignHandler (serverComponentCallbacksProxy, serverComponentCallbacks, serverComponentCallbacksIsolate).await (AbacusTest.defaultPollTimeout));
    Assert.assertTrue (reactor.assignHandler (clientComponentCallbacksProxy, clientComponentCallbacks, clientComponentCallbacksIsolate).await (AbacusTest.defaultPollTimeout));
    final ComponentIdentifier peer = ComponentIdentifier.resolve (Strings.repeat ("00", 20));
    for (int index = 0; index < AbacusTest.defaultTries; index++) {
      final double operandA = (int) (Math.random () * 10);
      final double operandB = (int) (Math.random () * 10);
      final ComponentCallRequest request = ComponentCallRequest.create ("+", Arrays.asList (Double.valueOf (operandA), Double.valueOf (operandB)), ByteBuffer.allocate (0), ComponentCallReference.create ());
      Assert.assertTrue (clientComponentController.call (peer, request).await (AbacusTest.defaultPollTimeout));
      final ComponentCallReply reply = (ComponentCallReply) clientComponentCallbacks.queue.poll (AbacusTest.defaultPollTimeout, TimeUnit.MILLISECONDS);
      Assert.assertNotNull (reply);
      Assert.assertTrue (reply.ok);
      Assert.assertNotNull (reply.outputsOrError);
      Assert.assertEquals (request.reference, reply.reference);
      Assert.assertTrue ((operandA + operandB) == ((Number) reply.outputsOrError).doubleValue ());
    }
    pipe1.sink ().close ();
    pipe2.sink ().close ();
    Assert.assertTrue (serverComponent.await (AbacusTest.defaultPollTimeout));
    Assert.assertTrue (clientComponent.await (AbacusTest.defaultPollTimeout));
    Assert.assertTrue (serverComponent.destroy (AbacusTest.defaultPollTimeout));
    Assert.assertTrue (clientComponent.destroy (AbacusTest.defaultPollTimeout));
    Assert.assertTrue (serverChannel.destroy (AbacusTest.defaultPollTimeout));
View Full Code Here

        done.await(5000, TimeUnit.MILLISECONDS));
  }

  @Test
  public void testNewPipeBlockingSemantics() throws IOException {
    Pipe pipe = Pipe.open();
    ByteBuffer buf = ByteBuffer.allocate(10);
    assertTrue(pipe.source().isBlocking());
    pipe.source().configureBlocking(false);
    assertTrue(!pipe.source().isBlocking());
    int rdSz = pipe.source().read(buf); // this operation blocks!
    assertTrue(rdSz <= 0);
  }
View Full Code Here

    assertTrue(rdSz <= 0);
  }

  @Test
  public void testNewPipePartialReadSemantics() throws IOException {
    Pipe pipe = Pipe.open();
    int sz = 10;
    ByteBuffer buf = ByteBuffer.allocate(sz);
    pipe.sink().write(ByteBuffer.wrap("small".getBytes()));
    int rdSz = pipe.source().read(buf); // this operation blocks!
    assertTrue(rdSz <= sz);
  }
View Full Code Here

import java.nio.channels.Pipe;

public class Basic {
    public static void main(String[] args) throws Exception {
        Pipe p = Pipe.open();
    }
View Full Code Here

TOP

Related Classes of java.nio.channels.Pipe

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.