Skip to content
Snippets Groups Projects
MainRemote.java 2.77 KiB
Newer Older
Michael Kmoch's avatar
Michael Kmoch committed
package fucoin;

import java.io.File;
import java.util.concurrent.TimeUnit;
Michael Kmoch's avatar
Michael Kmoch committed

import akka.actor.ActorRef;
import akka.actor.ActorSelection;
Michael Kmoch's avatar
Michael Kmoch committed
import akka.actor.ActorSystem;

import akka.util.Timeout;
Michael Kmoch's avatar
Michael Kmoch committed
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import scala.concurrent.Await;

import javax.swing.*;
Michael Kmoch's avatar
Michael Kmoch committed

public class MainRemote {
    public static ActorRef remoteSuperVisorActor;

    private static JTextField walletNameField = new JTextField(5);
    private static JTextField pathField = new JTextField(5);

    public static void main(String[] args) throws InterruptedException {

        //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
        ActorSystem system = ActorSystem.create("Remote", config);

        JPanel dialogPanel = createDialogPanel();

        Timeout timeout = new Timeout(5, TimeUnit.SECONDS);
        ActorRef preknownNeighbour = null;

        String walletName = null;
        String path = null;

        while (preknownNeighbour == null) {

            int result = JOptionPane.showConfirmDialog(null, dialogPanel, "Connect to wallet network", JOptionPane.OK_CANCEL_OPTION);

            if (result == JOptionPane.OK_OPTION) {
                walletName = walletNameField.getText();
                path = pathField.getText();
            }

            // terminate if user clicked abort
            if (path == null) {
                system.terminate();
            }

            // tell the node that we want to join the network
            ActorSelection selection = system.actorSelection(path);

            try {
                preknownNeighbour = Await.result(selection.resolveOne(timeout), timeout.duration());
            } catch (Exception e) {
                System.err.println("Something went wrong while resolving: " + e.getMessage());
            }
        }

        // spawn wallet
        system.actorOf(WalletImpl.props(preknownNeighbour, walletName), walletName);

    }
    private static JPanel createDialogPanel() {
        JPanel dialogPanel = new JPanel();
        dialogPanel.setLayout(new BoxLayout(dialogPanel, BoxLayout.PAGE_AXIS));
        dialogPanel.add(new JLabel("Pick your wallet name: "));
        dialogPanel.add(walletNameField);
        dialogPanel.add(new JLabel("Enter a neighbour node address: "));
        dialogPanel.add(pathField);
        return dialogPanel;
Michael Kmoch's avatar
Michael Kmoch committed
}