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 { public static void main(String[] args) throws InterruptedException, IllegalAccessException, InstantiationException {
createWallets(); List<AbstractConfiguration> configurations = getAbstractConfigurations();
AbstractConfiguration[] configs = new AbstractConfiguration[configurations.size()];
configurations.toArray(configs);
AbstractConfiguration selectedConfig = (AbstractConfiguration) JOptionPane.showInputDialog(null, "Select a configuration to run", "Configuration Selection", JOptionPane.QUESTION_MESSAGE, null, configs, configurations.get(0));
if (selectedConfig != null) {
selectedConfig.setSystem(cSystem);
selectedConfig.run();
} else {
cSystem.terminate();
}
} }
private static void createWallets() { private static List<AbstractConfiguration> getAbstractConfigurations() throws InstantiationException, IllegalAccessException {
//Init Wallets List<ClassLoader> classLoadersList = new LinkedList<>();
for (int i = 0; i < numberOfWallets; i++) { classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
String nameOfTheWallet = "Wallet" + String.valueOf(i); Reflections reflections = new Reflections(new ConfigurationBuilder()
Props props; .setScanners(new SubTypesScanner(false), new ResourcesScanner())
.setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))
.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("fucoin.configurations"))));
if (i > 0) { Set<Class<? extends Object>> allClasses =
//chain the wallets. wallet2 knows wallet1, wallet3 knows wallet2 and so on. reflections.getSubTypesOf(Object.class);
props = WalletImpl.props(cActiveActors.get(i - 1), nameOfTheWallet, createGUI);
} else {
props = WalletImpl.props(null, nameOfTheWallet, createGUI);
}
ActorRef actorRef = cSystem.actorOf(props, nameOfTheWallet); List<AbstractConfiguration> configurations = new ArrayList<>();
// first wallet does not have a neighbour, so it can't send a ActionJoin to anybody for (Class<? extends Object> oneClass: allClasses){
// instead we send directly an ActionJoinAnswer with the supervisor reference if (!Modifier.isAbstract(oneClass.getModifiers())) {
if (i == 0) { AbstractConfiguration cfg = (AbstractConfiguration)oneClass.newInstance();
//actorRef.tell(new ActionJoinAnswer(cSuperVisorActor), cSuperVisorActor); cfg.setSystem(cSystem);
actorRef.tell(new ActionTellSupervisor(cSuperVisorActor), cSuperVisorActor); configurations.add(cfg);
} }
cActiveActors.add(actorRef);
} }
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