use of co.paralleluniverse.actors.behaviors.AbstractServerHandler 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);
}
}
use of co.paralleluniverse.actors.behaviors.AbstractServerHandler in project quasar by puniverse.
the class Server method main.
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.setProperty("galaxy.nodeId", Integer.toString(nodeId));
System.setProperty("galaxy.port", Integer.toString(7050 + nodeId));
System.setProperty("galaxy.slave_port", Integer.toString(8050 + nodeId));
new Fiber(new ServerActor(new AbstractServerHandler<SumRequest, Integer, SumRequest>() {
@Override
public void init() throws SuspendExecution {
super.init();
Actor.currentActor().register("myServer");
System.out.println(this.toString() + " is ready");
}
@Override
public Integer handleCall(ActorRef<?> from, Object id, SumRequest m) {
System.out.println(this.toString() + " is handling " + m);
if (m.a == 0 && m.b == 0)
ServerActor.currentServerActor().shutdown();
return m.a + m.b;
}
})).start().join();
System.exit(0);
}
Aggregations