Skip to content
Snippets Groups Projects
Commit b35ca25f authored by Luca Keidel's avatar Luca Keidel
Browse files

Resolved merge conflict

parents f45af81d a4428494
No related branches found
No related tags found
1 merge request!5Configuration system
...@@ -51,5 +51,10 @@ ...@@ -51,5 +51,10 @@
<artifactId>akka-remote_2.11</artifactId> <artifactId>akka-remote_2.11</artifactId>
<version>2.4.7</version> <version>2.4.7</version>
</dependency> </dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
package fucoin; package fucoin;
import akka.actor.ActorRef;
import akka.actor.ActorSystem; import akka.actor.ActorSystem;
import akka.actor.Props;
import com.typesafe.config.Config; import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory; import com.typesafe.config.ConfigFactory;
import fucoin.actions.join.ActionTellSupervisor; import fucoin.configurations.AbstractConfiguration;
import fucoin.setup.NetworkInterfaceReader; import fucoin.setup.NetworkInterfaceReader;
import fucoin.supervisor.SuperVisorImpl; import org.reflections.Reflections;
import fucoin.wallet.WalletImpl; import org.reflections.scanners.ResourcesScanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
import javax.swing.*;
import java.io.File; import java.io.File;
import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Set;
public class Main { public class Main {
private static int numberOfWallets = 2;
private static boolean createGUI = true;
private static ActorSystem cSystem; private static ActorSystem cSystem;
private static ActorRef cSuperVisorActor;
private static List<ActorRef> cActiveActors = new ArrayList<>();
static { static {
String hostname = NetworkInterfaceReader.readDefaultHostname(); String hostname = NetworkInterfaceReader.readDefaultHostname();
...@@ -41,37 +40,45 @@ public class Main { ...@@ -41,37 +40,45 @@ public class Main {
//Init System Actor System //Init System Actor System
cSystem = ActorSystem.create("Core", ConfigFactory.parseString("akka.remote.netty.tcp.hostname=" + hostname).withFallback(config)); cSystem = ActorSystem.create("Core", ConfigFactory.parseString("akka.remote.netty.tcp.hostname=" + hostname).withFallback(config));
cSuperVisorActor = cSystem.actorOf(SuperVisorImpl.props(), "SuperVisorImpl");
}
public static void main(String[] args) throws InterruptedException {
createWallets();
} }
private static void createWallets() { public static void main(String[] args) throws InterruptedException, IllegalAccessException, InstantiationException {
//Init Wallets List<AbstractConfiguration> configurations = getAbstractConfigurations();
for (int i = 0; i < numberOfWallets; i++) {
String nameOfTheWallet = "Wallet" + String.valueOf(i); AbstractConfiguration[] configs = new AbstractConfiguration[configurations.size()];
Props props; configurations.toArray(configs);
if (i > 0) { AbstractConfiguration selectedConfig = (AbstractConfiguration) JOptionPane.showInputDialog(null, "Select a configuration to run", "Configuration Selection", JOptionPane.QUESTION_MESSAGE, null, configs, configurations.get(0));
//chain the wallets. wallet2 knows wallet1, wallet3 knows wallet2 and so on. if (selectedConfig != null) {
props = WalletImpl.props(cActiveActors.get(i - 1), nameOfTheWallet, createGUI); selectedConfig.setSystem(cSystem);
selectedConfig.run();
} else { } else {
props = WalletImpl.props(null, nameOfTheWallet, createGUI); cSystem.terminate();
}
} }
ActorRef actorRef = cSystem.actorOf(props, nameOfTheWallet); private static List<AbstractConfiguration> getAbstractConfigurations() throws InstantiationException, IllegalAccessException {
List<ClassLoader> classLoadersList = new LinkedList<>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
// first wallet does not have a neighbour, so it can't send a ActionJoin to anybody Reflections reflections = new Reflections(new ConfigurationBuilder()
// instead we send directly an ActionJoinAnswer with the supervisor reference .setScanners(new SubTypesScanner(false), new ResourcesScanner())
if (i == 0) { .setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))
//actorRef.tell(new ActionJoinAnswer(cSuperVisorActor), cSuperVisorActor); .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("fucoin.configurations"))));
actorRef.tell(new ActionTellSupervisor(cSuperVisorActor), cSuperVisorActor);
} Set<Class<? extends Object>> allClasses =
reflections.getSubTypesOf(Object.class);
cActiveActors.add(actorRef); List<AbstractConfiguration> configurations = new ArrayList<>();
for (Class<? extends Object> oneClass: allClasses){
if (!Modifier.isAbstract(oneClass.getModifiers())) {
AbstractConfiguration cfg = (AbstractConfiguration)oneClass.newInstance();
cfg.setSystem(cSystem);
configurations.add(cfg);
}
} }
return configurations;
} }
} }
package fucoin.configurations;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import fucoin.actions.join.ActionTellSupervisor;
import fucoin.supervisor.SuperVisorImpl;
import fucoin.wallet.WalletImpl;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public abstract class AbstractConfiguration {
protected ActorSystem cSystem;
protected ActorRef superVisor;
protected final List<ActorRef> activeActors = new ArrayList<>();
public void setSystem(ActorSystem system) {
cSystem = system;
}
public ActorRef spawnWallet(String name, boolean createGUI) {
Props props;
int numOfWallets = activeActors.size();
if (numOfWallets == 0) {
props = WalletImpl.props(null, name, createGUI);
} else {
props = WalletImpl.props(activeActors.get(numOfWallets - 1), name, createGUI);
}
ActorRef actorRef = cSystem.actorOf(props, name);
activeActors.add(actorRef);
if (numOfWallets == 0) {
//actorRef.tell(new ActionJoinAnswer(cSuperVisorActor), cSuperVisorActor);
actorRef.tell(new ActionTellSupervisor(superVisor), superVisor);
}
return actorRef;
}
public void spawnWallets(int n, boolean createGUI) {
for (int i = 0; i < n; i++) {
String nameOfTheWallet = "Wallet" + String.valueOf(activeActors.size());
spawnWallet(nameOfTheWallet, createGUI);
}
}
public ActorRef initSupervisor() {
superVisor = cSystem.actorOf(SuperVisorImpl.props(), "SuperVisorImpl");
// Don't ask.
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
return superVisor;
}
public abstract void run();
public abstract String getName();
@Override
public String toString() {
return getName();
}
}
package fucoin.configurations;
/**
*
*/
public class DefaultConfiguration extends AbstractConfiguration {
@Override
public void run() {
initSupervisor();
spawnWallets(2, true);
}
@Override
public String getName() {
return "Default Configuration";
}
}
package fucoin.configurations;
/**
*
*/
public class MassWalletConfiguration extends AbstractConfiguration {
@Override
public void run() {
initSupervisor();
spawnWallets(200, false);
}
@Override
public String getName() {
return "Lots of wallets";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment