package net.f4fs.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.f4fs.bootstrapserver.BootstrapServerAccess;
import net.f4fs.config.Config;
import net.f4fs.filesystem.P2PFS;
import net.f4fs.fspeer.FSPeer;
import net.f4fs.util.DhtOperationsCommand;
import net.f4fs.util.IpAddressJsonParser;
import net.f4fs.util.ShutdownHookThread;

import org.json.simple.parser.ParseException;


public class Main {

    public static void main(String[] args) {
        BootstrapServerAccess boostrapServerAccess = new BootstrapServerAccess();
        FSPeer fsPeer = new FSPeer();

        String myIp = fsPeer.findLocalIp();

        List<Map<String, String>> ipList = new ArrayList<>();
        try {
            ipList = IpAddressJsonParser.parse(boostrapServerAccess.getIpAddressList());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int nrOfIpAddresses = ipList.size();
        boostrapServerAccess.postIpPortPair(myIp, Config.DEFAULT.getPort());

        // Connect to other peers if any are available, otherwise start as bootstrap peer
        try {
            boolean success = false;

            if (nrOfIpAddresses == 0) {
                fsPeer.startAsBootstrapPeer(myIp, Config.DEFAULT.getPort());
                success = true;
            } else {
                int counter = 0;

                while (!success && (counter < nrOfIpAddresses)) {
                    success = fsPeer.startPeer(myIp,
                            Config.DEFAULT.getPort(),
                            ipList.get(counter).get("address"),
                            Integer.parseInt(ipList.get(counter).get("port")));
                    counter++;
                }
            }
            
            if (!success) {
                boostrapServerAccess.removeIpPortPair(myIp, Config.DEFAULT.getPort());
                fsPeer.shutdown();
                System.out.println("[Shutdown]: Bootstrap failed");
                System.exit(1);
            }
            
            // start file system with the connected peer
            new P2PFS(fsPeer).mountAndCreateIfNotExists(Config.DEFAULT.getMountPoint());
            
            // maybe start command line interface
            if (Config.DEFAULT.getStartCommandLineInterface()) {
                DhtOperationsCommand.startCommandLineInterface(fsPeer);   
            }

            // Add shutdown hook so that IP address gets removed from server when
            // user does not terminate program correctly on
            Runtime.getRuntime().addShutdownHook(new ShutdownHookThread(myIp, Config.DEFAULT.getPort()));

            boostrapServerAccess.removeIpPortPair(myIp, Config.DEFAULT.getPort());
            fsPeer.shutdown();
        } catch (Exception pEx) {
            pEx.printStackTrace();
        }
    }
}

@@ -27,1190 +27,315 @@
 r;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0Aimport net.f4fs.bootstrapserver.BootstrapServerAccess;%0Aimport net.f4fs.config.Config;%0Aimport net.f4fs.filesystem.P2PFS;%0Aimport net.f4fs.fspeer.FSPeer;%0Aimport net.f4fs.util.DhtOperationsCommand;%0Aimport net.f4fs.util.IpAddressJsonParser;%0Aimport net.f4fs.util.ShutdownHookThread;%0A%0Aimport org.json.simple.parser.ParseException;%0A%0A%0Apublic class Main %7B%0A%0A    public static void main(String%5B%5D args) %7B%0A        BootstrapServerAccess boostrapServerAccess = new BootstrapServerAccess();%0A        FSPeer fsPeer = new FSPeer();%0A%0A        String myIp = fsPeer.findLocalIp();%0A%0A        List%3CMap%3CString, String%3E%3E ipList = new ArrayList%3C%3E();%0A        try %7B%0A            ipList = IpAddressJsonParser.parse(boostrapServerAccess.getIpAddressList());%0A        %7D catch (ParseException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        int nrOfIpAddresses = ipList.size();%0A        boostrapServerAccess.postIpPortPair(myIp, Config.DEFAULT.getPort());%0A%0A        // Connect to other peers if any are available, otherwise start as bootstrap peer%0A        try %7B%0A            boolean success = false;%0A%0A            if (nrOfIpAddresses == 0) %7B
+%0Apublic class Main %7B%0A%0A    public static void main(String%5B%5D args) %7B%0A        Startup startup = new Startup();%0A        %0A        if(args.length == 0)%7B%0A            // normal startup, get information of bootstrap peer (if one exists) from server,%0A            // or start as bootstrap peer if no information exists
 %0A   
@@ -347,26 +347,25 @@
     
-    fsPeer
+startup
 .start
-As
+With
 Boot
@@ -373,44 +373,16 @@
 trap
-Peer(myIp, Config.DEFAULT.getPort
+Server
 ()
-)
 ;%0A  
@@ -391,507 +391,172 @@
     
-        success = true;%0A            %7D else %7B%0A                int counter = 0;%0A%0A                while (!success && (counter %3C nrOfIpAddresses)) %7B%0A                    success = fsPeer.startPeer(myIp,%0A                            Config.DEFAULT.getPort(),%0A                            ipList.get(counter).get(%22address%22),%0A                            Integer.parseInt(ipList.get(counter).get(%22port%22)));%0A                    counter++;%0A                %7D%0A            %7D%0A            %0A            if (!success) %7B
+%7D else if (args.length == 2)%7B%0A            // Bootstrap peer is known in advance%0A            // Direct connection to bootstrap peer with bootstrap peer ip = args%5B1%5D 
 %0A   
@@ -568,691 +568,243 @@
     
-   
+// and
  boo
+t
 strap
-ServerAccess.removeIpPortPair(myIp, Config.DEFAULT.getPort());%0A                fsPeer.shutdown();%0A                System.out.println(%22%5BShutdown%5D: Bootstrap failed%22);%0A                System.exit(1);%0A            %7D%0A            %0A            // start file system with the connected peer%0A            new P2PFS(fsPeer).mountAndCreateIfNotExists(Config.DEFAULT.getMountPoint());%0A            %0A            // maybe start command line interface%0A            if (Config.DEFAULT.getStartCommandLineInterface()) %7B%0A                DhtOperationsCommand.startCommandLineInterface(fsPeer);   %0A            %7D%0A%0A            // Add shutdown hook so that IP address gets removed from
+ peer port = args%5B2%5D%0A            startup.startWithoutBootstrapServer(args%5B1%5D, args%5B2%5D);%0A        %7D else %7B%0A            System.out.println(%22%5BSTART%5D Wrong arguments: No arguments for normal start with bootrstrap
  server
- when%0A
+%22%0A   
     
@@ -815,343 +815,114 @@
     
-// user does not terminate program correctly on%0A            Runtime.getRuntime().addShutdownHook(new ShutdownHookThread(myIp, Config.DEFAULT.getPort()));%0A%0A            boostrapServerAccess.removeIpPortPair(myIp, Config.DEFAULT.getPort());%0A            fsPeer.shutdown();%0A        %7D catch (Exception pEx) %7B%0A            pEx.printStackTrace(
+     + %22or %3CConnectionIP%3E%3CConnectionPort%3E for start with known bootstrap peer%22);%0A            System.exit(1
 );%0A 

update main to use startup class