package cl.niclabs.skandium.gcm.examples.nqueensnaive;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import org.etsi.uri.gcm.api.type.GCMTypeFactory;
import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.IllegalBindingException;
import org.objectweb.fractal.api.control.IllegalContentException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.api.factory.InstantiationException;
import org.objectweb.fractal.api.type.ComponentType;
import org.objectweb.fractal.api.type.InterfaceType;
import org.objectweb.proactive.core.ProActiveException;
import org.objectweb.proactive.core.component.Constants;
import org.objectweb.proactive.core.component.ContentDescription;
import org.objectweb.proactive.core.component.ControllerDescription;
import org.objectweb.proactive.core.component.Utils;
import org.objectweb.proactive.core.component.control.PABindingController;
import org.objectweb.proactive.core.component.control.PAContentController;
import org.objectweb.proactive.core.component.factory.PAGenericFactory;
import org.objectweb.proactive.core.component.type.PAGCMTypeFactory;
import org.objectweb.proactive.core.node.Node;
import org.objectweb.proactive.extensions.gcmdeployment.PAGCMDeployment;
import org.objectweb.proactive.gcmdeployment.GCMApplication;
import org.objectweb.proactive.gcmdeployment.GCMVirtualNode;
import cl.niclabs.skandium.gcm.DelegationCondition;
import cl.niclabs.skandium.gcm.SCReceiver;
import cl.niclabs.skandium.gcm.SCResultReceiver;
import cl.niclabs.skandium.gcm.SCTransmitter;
import cl.niclabs.skandium.gcm.SkandiumComponent;
import cl.niclabs.skandium.gcm.SkandiumComponentController;
import cl.niclabs.skandium.gcm.SkandiumComponentImp;
public class Main {
public static void main(String[] args) throws InstantiationException,
NoSuchInterfaceException, IllegalContentException,
IllegalLifeCycleException, IllegalBindingException,
ProActiveException, MalformedURLException, URISyntaxException {
String descriptorPath = "file:///user/mibanez/home/Workspace/" +
"skandium/src-extras/cl/niclabs/skandium/gcm/examples/GCMApp.xml";
descriptorPath = (new URL(descriptorPath)).toURI().getPath();
File appDescriptor = new File(descriptorPath);
GCMApplication gcmad;
gcmad = PAGCMDeployment.loadApplicationDescriptor(appDescriptor);
gcmad.startDeployment();
gcmad.waitReady();
GCMVirtualNode VN1 = gcmad.getVirtualNode("VN1");
GCMVirtualNode VN2 = gcmad.getVirtualNode("VN2");
VN1.waitReady();
VN2.waitReady();
Component boot = Utils.getBootstrapComponent();
PAGCMTypeFactory tf = Utils.getPAGCMTypeFactory(boot);
PAGenericFactory gf = Utils.getPAGenericFactory(boot);
ComponentType tComposite = tf.createFcType(new InterfaceType[] {
tf.createGCMItfType(
"runnable",
Runnable.class.getName(),
GCMTypeFactory.SERVER,
GCMTypeFactory.MANDATORY,
GCMTypeFactory.SINGLETON_CARDINALITY),
tf.createGCMItfType(
"scc",
SkandiumComponentController.class.getName(),
GCMTypeFactory.SERVER,
GCMTypeFactory.OPTIONAL,
GCMTypeFactory.SINGLETON_CARDINALITY)
});
ComponentType tExample = tf.createFcType(new InterfaceType[] {
tf.createGCMItfType(
"runnable",
Runnable.class.getName(),
GCMTypeFactory.SERVER,
GCMTypeFactory.MANDATORY,
GCMTypeFactory.SINGLETON_CARDINALITY),
tf.createGCMItfType(
"sc",
SkandiumComponent.class.getName(),
GCMTypeFactory.CLIENT,
GCMTypeFactory.MANDATORY,
GCMTypeFactory.SINGLETON_CARDINALITY),
tf.createGCMItfType(
"scrr",
SCResultReceiver.class.getName(),
GCMTypeFactory.SERVER,
GCMTypeFactory.MANDATORY,
GCMTypeFactory.SINGLETON_CARDINALITY)
});
ComponentType tSC = tf.createFcType(new InterfaceType[] {
tf.createGCMItfType(
"sc",
SkandiumComponent.class.getName(),
GCMTypeFactory.SERVER,
GCMTypeFactory.MANDATORY,
GCMTypeFactory.SINGLETON_CARDINALITY),
tf.createGCMItfType(
"scrr",
SCResultReceiver.class.getName(),
GCMTypeFactory.CLIENT,
GCMTypeFactory.OPTIONAL,
GCMTypeFactory.SINGLETON_CARDINALITY),
tf.createGCMItfType(
"sctS",
SCTransmitter.class.getName(),
GCMTypeFactory.SERVER,
GCMTypeFactory.MANDATORY,
GCMTypeFactory.SINGLETON_CARDINALITY),
tf.createGCMItfType(
"sct",
SCTransmitter.class.getName(),
GCMTypeFactory.CLIENT,
GCMTypeFactory.OPTIONAL,
GCMTypeFactory.SINGLETON_CARDINALITY),
tf.createGCMItfType(
"scrS",
SCReceiver.class.getName(),
GCMTypeFactory.SERVER,
GCMTypeFactory.MANDATORY,
GCMTypeFactory.SINGLETON_CARDINALITY),
tf.createGCMItfType(
"scr",
SCReceiver.class.getName(),
GCMTypeFactory.CLIENT,
GCMTypeFactory.OPTIONAL,
GCMTypeFactory.SINGLETON_CARDINALITY),
tf.createGCMItfType(
"scc",
SkandiumComponentController.class.getName(),
GCMTypeFactory.SERVER,
GCMTypeFactory.OPTIONAL,
GCMTypeFactory.SINGLETON_CARDINALITY)
});
Node N1composite = VN1.getANode();
Node N1example = VN1.getANode();
Node N1sc1 = VN1.getANode();
Node N2sc2 = VN2.getANode();
Component compositeComp = gf.newFcInstance(
tComposite,
new ControllerDescription("compositeComp", Constants.COMPOSITE),
null,
N1composite);
Component exampleComp = gf.newFcInstance(
tExample,
new ControllerDescription("exampleComp", Constants.PRIMITIVE),
new ContentDescription(NQueens.class.getName()),
N1example);
Component SC1 = gf.newFcInstance(
tSC,
new ControllerDescription("SC1", Constants.PRIMITIVE),
new ContentDescription(SkandiumComponentImp.class.getName()),
N1sc1);
Component SC2 = gf.newFcInstance(
tSC,
new ControllerDescription("SC2", Constants.PRIMITIVE),
new ContentDescription(SkandiumComponentImp.class.getName()),
N2sc2);
PAContentController cc = Utils.getPAContentController(compositeComp);
cc.addFcSubComponent(exampleComp);
cc.addFcSubComponent(SC1);
cc.addFcSubComponent(SC2);
PABindingController bcTest = Utils.getPABindingController(compositeComp);
bcTest.bindFc("runnable", exampleComp.getFcInterface("runnable"));
bcTest.bindFc("scc", SC1.getFcInterface("scc"));
PABindingController bcMergeSort = Utils.getPABindingController(exampleComp);
bcMergeSort.bindFc("sc", SC1.getFcInterface("sc"));
PABindingController bcSC1 = Utils.getPABindingController(SC1);
bcSC1.bindFc("sct", SC2.getFcInterface("sctS"));
bcSC1.bindFc("scrr", exampleComp.getFcInterface("scrr"));
PABindingController bcSC2 = Utils.getPABindingController(SC2);
bcSC2.bindFc("scr", SC1.getFcInterface("scrS"));
Utils.getPAGCMLifeCycleController(compositeComp).startFc();
SkandiumComponentController scc = (SkandiumComponentController)
SC1.getFcInterface("scc");
SkandiumComponentController scc2 = (SkandiumComponentController)
SC2.getFcInterface("scc");
DelegationCondition cond = new DelegationCondition() {
private static final long serialVersionUID = 2L;
public boolean condition(int stackSize, int maxThreads) {
return stackSize > 8;
}
};
scc.setDelegationCondition(cond);
scc2.setDelegationCondition(cond);
Runnable example =
(Runnable) compositeComp.getFcInterface("runnable");
example.run();
}
}