package cl.niclabs.skandium.gcm;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.PriorityBlockingQueue;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.BindingController;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.api.control.LifeCycleController;
import org.objectweb.proactive.Body;
import org.objectweb.proactive.core.component.body.ComponentInitActive;
import cl.niclabs.skandium.skeletons.Skeleton;
import cl.niclabs.skandium.system.StackBuilder;
import cl.niclabs.skandium.system.Task;
/*
@DefineGroups({
@Group(name="delegation", selfCompatible = false),
@Group(name="client", selfCompatible = false)
})
@DefineRules({
@Compatible({"delegation", "client"})
})
*/
public class SkandiumComponentImp implements SkandiumComponent,
SkandiumComponentController, SCReceiver, SCTransmitter,
LifeCycleController, BindingController, ComponentInitActive {
// Client Interfaces
private SCTransmitter sct;
private SCReceiver scr;
private SCResultReceiver scrr;
private String hostname;
// Internal Resources
private SCTaskExecutor executor;
private DelegationRegistry registry;
private final boolean VERBOSE = true;
@Override
public <P extends Serializable, R extends Serializable> void execute(
Skeleton<P, R> skeleton, P param) {
StackBuilder builder = new StackBuilder();
skeleton.accept(builder);
Task task = new Task(param, builder.stack, executor);
if(VERBOSE)
System.out.println("Task from user accepted on host " + hostname);
executor.execute(task);
}
@Override
public void transmitTask(TaskHead head) {
try {
Task task = registry.registerReceivedTask(head, executor);
if(VERBOSE) System.out.println("Task received on host " + hostname);
executor.execute(task);
}
catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void receiveResult(TaskHead head) {
Task task = registry.eraseTransmittedTask(head);
if(task == null) {
System.err.println("Received result of non transmitted task or " +
"already received result");
(new NullPointerException()).printStackTrace();
return;
}
if(VERBOSE) System.out.println("Result received on host " + hostname);
if( !task.isRoot() ) {
task.notifyParent();
return;
}
returnFinishedTaskResult(task);
}
protected void returnFinishedTaskResult(Task task) {
TaskHead originalHead = registry.eraseReceivedTask(task);
try {
if(originalHead == null) {
if(VERBOSE) System.out.println("Returning Execution Result...");
scrr.receive(task.getP());
}
else {
originalHead.setData(task.getP());
scr.receiveResult(originalHead);
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void delegateTask(Task task) {
TaskHead head = registry.registerTransmittedTask(task);
try {
sct.transmitTask(head);
task.getStack().clear();
}
catch(Exception e){
e.printStackTrace();
}
}
// SKANDIUMCOMPONENT CONTROLLER
@Override
public void setMaxThreads(int maxThreads) {
executor.setMaximumPoolSize(maxThreads);
}
@Override
public void setDelegationCondition(DelegationCondition condition) {
executor.setDelegationCondition(condition);
}
// LIFECYCLE CONTROLLER
@Override
public void startFc() throws IllegalLifeCycleException {
registry = new DelegationRegistry();
executor = new SCTaskExecutor(
Runtime.getRuntime().availableProcessors(),
new PriorityBlockingQueue<Runnable>(),
this);
}
@Override
public void stopFc() throws IllegalLifeCycleException {
executor.shutdown();
}
@Override
public String getFcState() {
return null;
}
// BINDING CONTROLLER
@Override
public void bindFc(String name, Object itf)
throws NoSuchInterfaceException {
if(name.compareTo("sct") == 0)
sct = (SCTransmitter) itf;
else if(name.compareTo("scr") == 0)
scr = (SCReceiver) itf;
else if(name.compareTo("scrr") == 0)
scrr = (SCResultReceiver) itf;
else throw new NoSuchInterfaceException(name);
}
@Override
public String[] listFc() {
return new String[] { "sct", "scr", "scrr" };
}
@Override
public Object lookupFc(String name) throws NoSuchInterfaceException {
if(name.compareTo("sct") == 0)
return sct;
else if(name.compareTo("scr") == 0)
return scr;
else if(name.compareTo("scrr") == 0)
return scrr;
else throw new NoSuchInterfaceException(name);
}
@Override
public void unbindFc(String name) throws NoSuchInterfaceException {
if(name.compareTo("sct") == 0)
sct = null;
else if(name.compareTo("scr") == 0)
scr = null;
else if(name.compareTo("scrr") == 0)
scrr = null;
else throw new NoSuchInterfaceException(name);
}
@Override
public void initComponentActivity(Body arg0) {
try {
hostname = InetAddress.getLocalHost().getHostName();
System.out.println("InetAddress.getLocalHost().getHostName(); ==> " + hostname);
} catch (UnknownHostException e) {
hostname = "unknown";
e.printStackTrace();
}
}
}