From 9fa68b9e988d9f8b35b600b5dfda9498406ddde8 Mon Sep 17 00:00:00 2001 From: js Date: Tue, 3 Mar 2020 19:47:21 +0100 Subject: [PATCH] multiple servers impl (does not work yet) --- src/clientP2P/ClientP2P.java | 24 +++++++++++++++++++++--- src/tools/HostList.java | 4 ++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index c8ee8a9..89594be 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -1,16 +1,25 @@ package clientP2P; import clientP2P.ClientManagementUDP; import clientP2P.ClientManagementTCP; +import tools.HostList; import tools.Directories; import java.util.Scanner; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.ListIterator; public class ClientP2P { private String host; private int port; private Directories directories; + private List hostList; + private HostList tool = new HostList(); + public ClientP2P() { directories = new Directories("P2P_JAVA_PROJECT_CLIENT"); host = "localhost"; + hostList = tool.getServList(); port = 40001; System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + directories.getDataHomeDirectory()); directories.askOpenDataHomeDirectory(null); @@ -27,8 +36,18 @@ public class ClientP2P { case "udp": case "2" : System.out.println("Starting with UDP"); - ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.host, c.port); - t = new Thread(cmudp); + /* ListIterator it = c.hostList.listIterator(); + do { + String str = it.next(); + ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), str, c.port); + t = new Thread(cmudp); + } while (it.hasNext()); + */ + for (String str : c.hostList){ + ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), str, c.port); + t = new Thread(cmudp); + } + //ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.host, c.port); break; case "TCP": case "tcp": @@ -39,7 +58,6 @@ public class ClientP2P { t = new Thread(cmtcp); break; } - t.setName("client P2P-JAVA-PROJECT"); t.start(); } diff --git a/src/tools/HostList.java b/src/tools/HostList.java index 4acaf92..979cbe9 100644 --- a/src/tools/HostList.java +++ b/src/tools/HostList.java @@ -17,7 +17,7 @@ public class HostList{ /** * Let the user enter all server and puts it in a list -* @return list of servers +* @return list of servers */ public List getServList(){ @@ -25,7 +25,7 @@ public class HostList{ Scanner scanner = new Scanner(System.in); String servName = ""; do { - System.out.println("Name of the next server:"); + System.out.println("Name of the next server: (or \"stop\" when finished)"); servName = scanner.nextLine(); if (servName != "stop"){ serverList.add(servName);