use of co.paralleluniverse.actors.behaviors.Server in project quasar by puniverse.
the class Main method main.
public static void main(String[] args) throws Exception {
final int nodeId = Integer.parseInt(args[0]);
System.setProperty("galaxy.nodeId", Integer.toString(nodeId));
System.setProperty("galaxy.port", Integer.toString(7050 + nodeId));
System.setProperty("galaxy.slave_port", Integer.toString(8050 + nodeId));
// com.esotericsoftware.minlog.Log.set(1);
ActorRegistry.hasGlobalRegistry();
// final ActorRef<Message> actor = ActorRegistry.getOrRegisterActor("migrant", new Callable<Actor<Message, ?>>() {
//
// @Override
// public Actor<Message, Void> call() throws Exception {
// return new Migrant();
// }
// });
final Server<Message, Integer, Message> actor = (Server<Message, Integer, Message>) ActorRegistry.getOrRegisterActor("migrant", new Callable() {
@Override
public ServerActor call() throws Exception {
return new Migrant();
}
});
int i;
for (i = 0; i < 500; i++) {
final double r = ThreadLocalRandom.current().nextDouble();
if (r < 0.05) {
System.out.println(new Date() + " Hiring actor...");
new Thread() {
@Override
public void run() {
try {
Strand.sleep(1000);
System.out.println(new Date() + " Acturally hiring...");
Actor.hire(actor);
System.out.println(new Date() + " Hired!");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}.start();
Message m = new Message(nodeId, i, MIGRATE);
System.out.println(new Date() + " Sending " + m);
actor.call(m);
// actor.send(m);
} else {
Message m = new Message(nodeId, i, PRINT);
System.out.println(new Date() + " Sending " + m);
// actor.send(m);
actor.cast(m);
}
Thread.sleep(500);
}
// actor.send(new Message(nodeId, i, FINISHED));
actor.cast(new Message(nodeId, i, FINISHED));
System.out.println("Done");
ActorRegistry.shutdown();
}
use of co.paralleluniverse.actors.behaviors.Server 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);
}
}
Aggregations