Search in sources :

Example 1 with Val

use of co.paralleluniverse.strands.dataflow.Val in project quasar by puniverse.

the class PeerTKB method run.

public void run() throws ExecutionException, InterruptedException {
    switch(SCENARIO.testGenEvent) {
        case test:
            final Store store = Grid.getInstance().store();
            if (i == 1) {
                StoreTransaction tx = store.beginTransaction();
                try {
                    long root = store.getRoot("root", tx);
                    //"hello".getBytes();
                    byte[] buf = null;
                    store.set(root, buf, tx);
                    store.commit(tx);
                } catch (TimeoutException ex) {
                    throw new RuntimeException("set failed");
                }
                Thread.sleep(20000);
            } else {
                StoreTransaction tx = store.beginTransaction();
                byte[] get;
                try {
                    long root = store.getRoot("root", tx);
                    get = store.get(root);
                    store.commit(tx);
                } catch (TimeoutException ex) {
                    throw new RuntimeException("get failed");
                }
                System.out.println(get);
            }
            break;
        case testGenServer:
            if (i == 1) {
                spawnGenServer(new AbstractServerHandler<Message, Integer, Message>() {

                    @Override
                    public void init() throws SuspendExecution {
                        super.init();
                        ServerActor.currentServerActor().register("myServer");
                    }

                    @Override
                    public Integer handleCall(ActorRef<?> from, Object id, Message m) {
                        return m.a + m.b;
                    }
                }).join();
            } else {
                Integer get = spawnActor(new BasicActor<Message, Integer>(new MailboxConfig(10, Channels.OverflowPolicy.THROW)) {

                    protected Integer doRun() throws SuspendExecution, InterruptedException {
                        final Server<Message, Integer, Message> gs = (Server) ActorRegistry.getActor("myServer");
                        return gs.call(new Message(3, 4));
                    }
                }).get();
                System.out.println("value is " + get);
                assert get == 7;
            }
            break;
        case testGenEvent:
            if (i == 1) {
                final Val<String> dv = new Val<>();
                spawnGenEvent(new Initializer() {

                    @Override
                    public void init() throws SuspendExecution {
                        EventSourceActor.currentEventSourceActor().register("myEventServer");
                        try {
                            final EventSource<String> ge = LocalActor.self();
                            ge.addHandler(new EventHandler<String>() {

                                @Override
                                public void handleEvent(String event) {
                                    dv.set(event);
                                    System.out.println("sout " + event);
                                    ge.shutdown();
                                }
                            });
                        } catch (InterruptedException ex) {
                            System.out.println(ex);
                        }
                    }

                    @Override
                    public void terminate(Throwable cause) throws SuspendExecution {
                        System.out.println("terminated");
                    }
                });
                String get = dv.get();
                System.out.println("got msg " + get);
                assert get.equals("hello world");
            } else {
                spawnActor(new BasicActor<Message, Void>() {

                    protected Void doRun() throws SuspendExecution, InterruptedException {
                        final EventSource<String> ge = (EventSource) ActorRegistry.getActor("myEventServer");
                        ge.notify("hello world");
                        return null;
                    }
                }).join();
            }
            break;
        case testMultiGetActor:
            if (i == 1) {
                spawnGenEvent(new Initializer() {

                    AtomicInteger ai = new AtomicInteger();

                    @Override
                    public void init() throws SuspendExecution {
                        Actor.currentActor().register("myEventServer");
                        try {
                            final EventSource<String> ge = LocalActor.self();
                            ge.addHandler(new EventHandler<String>() {

                                @Override
                                public void handleEvent(String event) {
                                    System.out.println("msg no " + ai.incrementAndGet() + ": " + event);
                                }
                            });
                        } catch (InterruptedException ex) {
                            System.out.println(ex);
                        }
                    }

                    @Override
                    public void terminate(Throwable cause) throws SuspendExecution {
                        System.out.println("terminated");
                    }
                }).join();
            } else {
                Queue<Actor> queue = new LinkedList<>();
                for (int j = 0; j < 1000; j++) {
                    final BasicActor<Message, Void> actor = spawnActor(new BasicActor<Message, Void>("actor-" + j) {

                        protected Void doRun() throws SuspendExecution, InterruptedException {
                            try {
                                final EventSource<String> ge = (EventSource) ActorRegistry.getActor("myEventServer");
                                ge.notify("hwf " + getName());
                            } catch (Exception e) {
                                System.out.println("error in " + getName());
                                throw e;
                            }
                            return null;
                        }
                    });
                    queue.add(actor);
                //                        actor.join();
                }
                for (Actor localActor : queue) localActor.join();
                Thread.sleep(500);
            }
            break;
        case testOrdering:
            if (i == 1) {
                spawnGenEvent(new Initializer() {

                    AtomicInteger ai = new AtomicInteger();

                    @Override
                    public void init() throws SuspendExecution {
                        EventSourceActor.currentEventSourceActor().register("myEventServer");
                        try {
                            EventSourceActor<String> ge = EventSourceActor.currentEventSourceActor();
                            ge.ref().addHandler(new EventHandler<String>() {

                                @Override
                                public void handleEvent(String event) {
                                    System.out.println("msg no " + ai.incrementAndGet() + ": " + event);
                                }
                            });
                        } catch (InterruptedException ex) {
                            System.out.println(ex);
                        }
                    }

                    @Override
                    public void terminate(Throwable cause) throws SuspendExecution {
                        System.out.println("terminated");
                    }
                }).join();
            } else {
                Queue<Actor> queue = new LinkedList<>();
                for (int j = 0; j < 1; j++) {
                    final BasicActor<Message, Void> actor = spawnActor(new BasicActor<Message, Void>("actor-" + j) {

                        protected Void doRun() throws SuspendExecution, InterruptedException {
                            try {
                                final EventSource<String> ge = (EventSource) ActorRegistry.getActor("myEventServer");
                                for (int k = 0; k < 3000; k++) ge.notify("hw " + k + " f" + getName());
                            } catch (Exception e) {
                                System.out.println("error in " + getName());
                                throw e;
                            }
                            return null;
                        }
                    });
                    queue.add(actor);
                //                        actor.join();
                }
                for (Actor localActor : queue) localActor.join();
                Thread.sleep(5000);
            }
            break;
        default:
    }
    System.out.println("finished");
    System.exit(0);
    while (true) {
        System.out.println("==================");
        ThreadUtil.dumpThreads();
        Thread.sleep(5000);
    }
}
Also used : Val(co.paralleluniverse.strands.dataflow.Val) Server(co.paralleluniverse.actors.behaviors.Server) StoreTransaction(co.paralleluniverse.galaxy.StoreTransaction) ActorRef(co.paralleluniverse.actors.ActorRef) Store(co.paralleluniverse.galaxy.Store) EventHandler(co.paralleluniverse.actors.behaviors.EventHandler) MailboxConfig(co.paralleluniverse.actors.MailboxConfig) LocalActor(co.paralleluniverse.actors.LocalActor) BasicActor(co.paralleluniverse.actors.BasicActor) EventSourceActor(co.paralleluniverse.actors.behaviors.EventSourceActor) Actor(co.paralleluniverse.actors.Actor) ServerActor(co.paralleluniverse.actors.behaviors.ServerActor) AbstractServerHandler(co.paralleluniverse.actors.behaviors.AbstractServerHandler) TimeoutException(co.paralleluniverse.galaxy.TimeoutException) SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) BasicActor(co.paralleluniverse.actors.BasicActor) LinkedList(java.util.LinkedList) TimeoutException(co.paralleluniverse.galaxy.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EventSource(co.paralleluniverse.actors.behaviors.EventSource) Initializer(co.paralleluniverse.actors.behaviors.Initializer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

Actor (co.paralleluniverse.actors.Actor)1 ActorRef (co.paralleluniverse.actors.ActorRef)1 BasicActor (co.paralleluniverse.actors.BasicActor)1 LocalActor (co.paralleluniverse.actors.LocalActor)1 MailboxConfig (co.paralleluniverse.actors.MailboxConfig)1 AbstractServerHandler (co.paralleluniverse.actors.behaviors.AbstractServerHandler)1 EventHandler (co.paralleluniverse.actors.behaviors.EventHandler)1 EventSource (co.paralleluniverse.actors.behaviors.EventSource)1 EventSourceActor (co.paralleluniverse.actors.behaviors.EventSourceActor)1 Initializer (co.paralleluniverse.actors.behaviors.Initializer)1 Server (co.paralleluniverse.actors.behaviors.Server)1 ServerActor (co.paralleluniverse.actors.behaviors.ServerActor)1 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)1 Store (co.paralleluniverse.galaxy.Store)1 StoreTransaction (co.paralleluniverse.galaxy.StoreTransaction)1 TimeoutException (co.paralleluniverse.galaxy.TimeoutException)1 Val (co.paralleluniverse.strands.dataflow.Val)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 ExecutionException (java.util.concurrent.ExecutionException)1