Search in sources :

Example 1 with IntChannel

use of co.paralleluniverse.strands.channels.IntChannel in project quasar by puniverse.

the class FiberAsyncIOTest method testFiberAsyncSocket.

@Test
public void testFiberAsyncSocket() throws Exception {
    final IntChannel sync = Channels.newIntChannel(0);
    final Fiber server = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            try (FiberServerSocketChannel socket = FiberServerSocketChannel.open().bind(new InetSocketAddress(PORT))) {
                // Start client
                sync.send(0);
                try (FiberSocketChannel ch = socket.accept()) {
                    ByteBuffer buf = ByteBuffer.allocateDirect(1024);
                    // long-typed reqeust/response
                    int n = ch.read(buf);
                    // we assume the message is sent in a single packet
                    assertThat(n, is(8));
                    buf.flip();
                    long req = buf.getLong();
                    assertThat(req, is(12345678L));
                    buf.clear();
                    long res = 87654321L;
                    buf.putLong(res);
                    buf.flip();
                    n = ch.write(buf);
                    assertThat(n, is(8));
                    // String reqeust/response
                    buf.clear();
                    // we assume the message is sent in a single packet
                    ch.read(buf);
                    buf.flip();
                    String req2 = decoder.decode(buf).toString();
                    assertThat(req2, is("my request"));
                    String res2 = "my response";
                    ch.write(encoder.encode(CharBuffer.wrap(res2)));
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }).start();
    final Fiber client = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution {
            try {
                // Wait that the server is ready
                sync.receive();
            } catch (InterruptedException ex) {
                // This should never happen
                throw new AssertionError(ex);
            }
            try (FiberSocketChannel ch = FiberSocketChannel.open(new InetSocketAddress(PORT))) {
                ByteBuffer buf = ByteBuffer.allocateDirect(1024);
                // long-typed reqeust/response
                long req = 12345678L;
                buf.putLong(req);
                buf.flip();
                int n = ch.write(buf);
                assertThat(n, is(8));
                buf.clear();
                n = ch.read(buf);
                // we assume the message is sent in a single packet
                assertThat(n, is(8));
                buf.flip();
                long res = buf.getLong();
                assertThat(res, is(87654321L));
                // String reqeust/response
                String req2 = "my request";
                ch.write(encoder.encode(CharBuffer.wrap(req2)));
                buf.clear();
                // we assume the message is sent in a single packet
                ch.read(buf);
                buf.flip();
                String res2 = decoder.decode(buf).toString();
                assertThat(res2, is("my response"));
                // verify that the server has closed the socket
                buf.clear();
                n = ch.read(buf);
                assertThat(n, is(-1));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }).start();
    client.join();
    server.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) InetSocketAddress(java.net.InetSocketAddress) Fiber(co.paralleluniverse.fibers.Fiber) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) IntChannel(co.paralleluniverse.strands.channels.IntChannel) Test(org.junit.Test)

Example 2 with IntChannel

use of co.paralleluniverse.strands.channels.IntChannel in project quasar by puniverse.

the class VerificationTest method testVerifyUninstrumentedCallSiteDeclaringAndOwnerOK.

@Test
public final void testVerifyUninstrumentedCallSiteDeclaringAndOwnerOK() throws ExecutionException, InterruptedException, SuspendExecution {
    assumeTrue(SystemProperties.isEmptyOrTrue("co.paralleluniverse.fibers.verifyInstrumentation"));
    // From https://github.com/puniverse/quasar/issues/255
    final IntChannel intChannel = Channels.newIntChannel(1);
    try {
        new Fiber<>(new SuspendableCallable<Integer>() {

            @Override
            public Integer run() throws SuspendExecution, InterruptedException {
                return intChannel.receive();
            }
        }).start().join(100, TimeUnit.MILLISECONDS);
    } catch (final TimeoutException ignored) {
    }
// Should complete without verification exceptions
}
Also used : IntChannel(co.paralleluniverse.strands.channels.IntChannel) SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) Fiber(co.paralleluniverse.fibers.Fiber) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 3 with IntChannel

use of co.paralleluniverse.strands.channels.IntChannel in project quasar by puniverse.

the class PrimitiveChannelRingBenchmark method run.

void run() throws ExecutionException, InterruptedException {
    final long start = System.nanoTime();
    final IntChannel managerChannel = Channels.newIntChannel(mailboxSize);
    IntChannel a = managerChannel;
    for (int i = 0; i < N - 1; i++) a = createRelayActor(a);
    final IntChannel lastChannel = a;
    Fiber<Integer> manager = new Fiber<Integer>() {

        @Override
        protected Integer run() throws InterruptedException, SuspendExecution {
            // start things off
            lastChannel.send(1);
            int msg = 0;
            try {
                for (int i = 0; i < M; i++) {
                    msg = managerChannel.receiveInt();
                    lastChannel.send(msg + 1);
                }
                return msg;
            } catch (ReceivePort.EOFException e) {
                return null;
            }
        }
    };
    // managerChannel.setStrand(manager);
    manager.start();
    int totalCount = manager.get();
    final long time = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS);
    System.out.println("Messages: " + totalCount + " Time (ms): " + time);
}
Also used : IntChannel(co.paralleluniverse.strands.channels.IntChannel) ReceivePort(co.paralleluniverse.strands.channels.ReceivePort) Fiber(co.paralleluniverse.fibers.Fiber)

Example 4 with IntChannel

use of co.paralleluniverse.strands.channels.IntChannel in project useful-java-links by Vedenin.

the class FibersAndChanelHelloWorld method doTest.

public static Integer doTest() throws Exception {
    // Synchronizing channel (buffer = 0)
    final IntChannel fiber1ToFiber2 = Channels.newIntChannel(0);
    // Synchronizing channel (buffer = 0)
    final IntChannel fiber2ToFiber1 = Channels.newIntChannel(0);
    Fiber<Integer> fiber1 = new Fiber<>("Fiber1", new SuspendableCallable<Integer>() {

        @Override
        public Integer run() throws SuspendExecution, InterruptedException {
            Fiber.sleep(1000);
            fiber1ToFiber2.send(1);
            Integer i1 = fiber2ToFiber1.receive();
            System.out.println(" Hello words " + i1);
            fiber1ToFiber2.send(9);
            Integer i2 = fiber2ToFiber1.receive();
            System.out.println(" Hello words " + i2);
            fiber1ToFiber2.send(0);
            return i2;
        }
    }).start();
    Fiber<Void> fiber2 = new Fiber<Void>("Fiber2", new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            Integer i;
            i = fiber1ToFiber2.receive();
            while (i != 0) {
                fiber2ToFiber1.send(i + 1);
                i = fiber1ToFiber2.receive();
            }
        }
    }).start();
    fiber1.join();
    fiber2.join();
    return fiber1.get();
}
Also used : IntChannel(co.paralleluniverse.strands.channels.IntChannel) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) SuspendableCallable(co.paralleluniverse.strands.SuspendableCallable)

Example 5 with IntChannel

use of co.paralleluniverse.strands.channels.IntChannel in project quasar by puniverse.

the class PrimitiveChannelRingBenchmark method createRelayActor.

private IntChannel createRelayActor(final IntChannel prev) {
    final IntChannel channel = Channels.newIntChannel(mailboxSize);
    Fiber<Void> fiber = new Fiber<Void>() {

        @Override
        protected Void run() throws InterruptedException, SuspendExecution {
            try {
                for (; ; ) prev.send(channel.receiveInt() + 1);
            } catch (ReceivePort.EOFException e) {
                return null;
            }
        }
    };
    // channel.setStrand(fiber);
    fiber.start();
    return channel;
}
Also used : IntChannel(co.paralleluniverse.strands.channels.IntChannel) ReceivePort(co.paralleluniverse.strands.channels.ReceivePort) Fiber(co.paralleluniverse.fibers.Fiber)

Aggregations

IntChannel (co.paralleluniverse.strands.channels.IntChannel)5 Fiber (co.paralleluniverse.fibers.Fiber)4 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)2 ReceivePort (co.paralleluniverse.strands.channels.ReceivePort)2 Test (org.junit.Test)2 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)1 SuspendableCallable (co.paralleluniverse.strands.SuspendableCallable)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 TimeoutException (java.util.concurrent.TimeoutException)1