package fucoin; import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.Props; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import fucoin.actions.join.ActionJoinAnswer; import fucoin.actions.join.ServerActionJoin; import fucoin.supervisor.SuperVisorImpl; import fucoin.wallet.WalletImpl; import java.io.File; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; public class Main { private static int numberOfWallets = 2; private static ActorSystem cSystem; private static ActorRef cSuperVisorActor; private static List<ActorRef> cActiveActors = new ArrayList<>(); static { String hostname = "127.0.0.1"; try { // Fetch IP address to enable network connection hostname = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); } //Load configuration from current directory or from resources directory of jar File file = new File("application.conf"); Config config = ConfigFactory.parseFile(file); if (!file.exists()) { System.out.println("Load default application.conf"); config = ConfigFactory.parseResources("application.conf"); } else { System.out.println("Load local application.conf"); } //Init System Actor System cSystem = ActorSystem.create("Core", ConfigFactory.parseString("akka.remote.netty.tcp.hostname=" + hostname).withFallback(config)); cSuperVisorActor = cSystem.actorOf(SuperVisorImpl.props(), "SuperVisorImpl"); System.out.print("Supervisor address: "); System.out.println(cSuperVisorActor.path().toStringWithAddress(cSystem.provider().getDefaultAddress())); } public static void main(String[] args) throws InterruptedException { createWallets(); } private static void createWallets() { //Init Wallets for (int i = 0; i < numberOfWallets; i++) { String nameOfTheWallet = "Wallet" + String.valueOf(i); //chain the wallets. wallet2 knows wallet1, wallet3 knows wallet2 and so on. String nameOfThePreviousWallet = ""; Props props; if (i > 0) { props = WalletImpl.props(cActiveActors.get(i - 1), nameOfTheWallet); } else { props = WalletImpl.props(null, nameOfTheWallet); } ActorRef actorRef = cSystem.actorOf(props, nameOfTheWallet); // first wallet does not have a neighbour, so it can't send a ActionJoin to anybody // instead we send directly an ActionJoinAnswer with the supervisor reference if (i == 0) { actorRef.tell(new ActionJoinAnswer(cSuperVisorActor), cSuperVisorActor); } cActiveActors.add(actorRef); } } }