From b0971c8a98a6ea7df51a416b78e0be6103e4a5ed Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simon=20K=C3=B6nnecke?= <simonkoennecke@gmail.com>
Date: Tue, 14 Jun 2016 11:38:00 +0200
Subject: [PATCH] fixed maven source directory. configurable number of local
 wallets in main

---
 pom.xml                                       |  6 ++-
 src/main/java/fucoin/Main.java                | 47 +++++++++++++++----
 .../java/fucoin/gui/WalletGuiControlImpl.java |  8 +---
 src/main/resources/application.conf           | 13 +++++
 4 files changed, 56 insertions(+), 18 deletions(-)
 create mode 100644 src/main/resources/application.conf

diff --git a/pom.xml b/pom.xml
index fb8f81b..9bf2c6d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,6 @@
     <artifactId>JavaAkkaFuCoin</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <build>
-        <sourceDirectory>src</sourceDirectory>
         <plugins>
             <plugin>
                 <artifactId>maven-compiler-plugin</artifactId>
@@ -47,5 +46,10 @@
             <artifactId>akka-actor_2.11</artifactId>
             <version>2.4.7</version>
         </dependency>
+        <dependency>
+            <groupId>com.typesafe.akka</groupId>
+            <artifactId>akka-remote_2.11</artifactId>
+            <version>2.4.7</version>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file
diff --git a/src/main/java/fucoin/Main.java b/src/main/java/fucoin/Main.java
index 1441a4f..a0909bb 100644
--- a/src/main/java/fucoin/Main.java
+++ b/src/main/java/fucoin/Main.java
@@ -2,6 +2,7 @@ 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.ServerActionJoin;
@@ -15,21 +16,47 @@ import java.util.List;
 
 public class Main {
 
-    public static void main(String[] args) throws InterruptedException {
+    private static int numberOfWallets = 4;
+
+    private static ActorSystem cSystem;
+
+    private static ActorRef cSuperVisorActor;
 
+    private static List<ActorRef> cActiveActors = new ArrayList<>();
+
+    static {
+        //Load configuration from current directory or from resources directory of jar
         File file = new File("application.conf");
-        System.out.println("config found? " + file.exists());
         Config config = ConfigFactory.parseFile(file);
-        ActorSystem system = ActorSystem.create("Core", config);
-        ActorRef superVisorActor = system.actorOf(SuperVisorImpl.props(), "SuperVisorImpl");
-        List<ActorRef> activeActors = new ArrayList<>();
-        ActorRef a1 = system.actorOf(WalletImpl.props(null, "", "Main", superVisorActor), "Main");
-        ActorRef a2 = system.actorOf(WalletImpl.props(a1, "Main", "Main2", superVisorActor), "Main2");
-        superVisorActor.tell(new ServerActionJoin("Main"), a1);
-        superVisorActor.tell(new ServerActionJoin("Main2"), a2);
+        if (!file.exists()) {
+            System.out.println("Load default application.conf");
+            config = ConfigFactory.parseResources("application.config");
+        } else {
+            System.out.println("Load local application.conf");
+        }
+
+        //Init System Actor System
+        cSystem = ActorSystem.create("Core", config);
+        cSuperVisorActor = cSystem.actorOf(SuperVisorImpl.props(), "SuperVisorImpl");
     }
 
-    private static void startSupervisor() {
+    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 = "";
+            if (i > 0) {
+                nameOfThePreviousWallet = "Wallet" + String.valueOf(i - 1);
+            }
+            Props props = WalletImpl.props(null, nameOfThePreviousWallet, nameOfTheWallet, cSuperVisorActor);
+            ActorRef actorRef = cSystem.actorOf(props, nameOfTheWallet);
+            cSuperVisorActor.tell(new ServerActionJoin(nameOfTheWallet), actorRef);
+            cActiveActors.add(actorRef);
+        }
     }
 }
diff --git a/src/main/java/fucoin/gui/WalletGuiControlImpl.java b/src/main/java/fucoin/gui/WalletGuiControlImpl.java
index e9501ff..3cf3fb4 100644
--- a/src/main/java/fucoin/gui/WalletGuiControlImpl.java
+++ b/src/main/java/fucoin/gui/WalletGuiControlImpl.java
@@ -59,13 +59,7 @@ public class WalletGuiControlImpl implements WalletGuiControl {
         JTextField sendToNewEdt = new JTextField();
         centerup2.add(sendToNewEdt, BorderLayout.CENTER);
         JButton addNewButton = new JButton("Add");
-        addNewButton.addActionListener(new ActionListener() {
-
-            @Override
-            public void actionPerformed(ActionEvent e) {
-                txtSendTo.addItem(sendToNewEdt.getText());
-            }
-        });
+        addNewButton.addActionListener(e -> txtSendTo.addItem(sendToNewEdt.getText()));
         centerup2.add(addNewButton, BorderLayout.EAST);
         centerPanel.add(centerup2);
 
diff --git a/src/main/resources/application.conf b/src/main/resources/application.conf
new file mode 100644
index 0000000..2c8f881
--- /dev/null
+++ b/src/main/resources/application.conf
@@ -0,0 +1,13 @@
+akka {
+
+  actor {
+    provider = "akka.remote.RemoteActorRefProvider"
+  }
+
+  remote {
+    netty.tcp {
+      hostname = "127.0.0.1"
+    }
+  }
+
+}
-- 
GitLab