|
| 1 | +import java.util.*; |
| 2 | +import java.io.*; |
| 3 | +/** |
| 4 | + * @author Ikonija Bogojevic |
| 5 | + * |
| 6 | + */ |
| 7 | +public class Client |
| 8 | +{ |
| 9 | + private static String hostName; |
| 10 | + private static Thread thrd = null; |
| 11 | + |
| 12 | + // the list of threads is kept in a linked list |
| 13 | + private static LinkedList<Thread> list = new LinkedList<Thread>(); |
| 14 | + private static ListIterator<Thread> li = null; |
| 15 | + |
| 16 | + public static void main(String[] args) throws IOException |
| 17 | + { |
| 18 | + int menuSelection = 0; |
| 19 | + // load hostName from the command line arguments |
| 20 | + hostName = args[0]; |
| 21 | + |
| 22 | + // if no hostname is provided, quit |
| 23 | + if ((hostName.equals("")) || (hostName == null)) |
| 24 | + { |
| 25 | + System.out.println("User did not enter a host name. Client program exiting."); |
| 26 | + System.exit(1); |
| 27 | + } |
| 28 | + |
| 29 | + // until the user selects 7, the Exit option, keep looping and |
| 30 | + // offering the menu again after running the queries to the server |
| 31 | + else while (menuSelection != 7) { |
| 32 | + // display the menu and get the user's choice |
| 33 | + menuSelection = mainMenu(); |
| 34 | + |
| 35 | + // if 7, exit program |
| 36 | + if (menuSelection == 7) { |
| 37 | + System.out.println("Quitting."); |
| 38 | + System.exit(0); |
| 39 | + } |
| 40 | + |
| 41 | + // otherwise, create threads in the linked list |
| 42 | + // TO-DO: add method to ask user how many threads to make |
| 43 | + // for now, just create 100 |
| 44 | + for (int i = 0; i < 30; i++) { |
| 45 | + // make a new thread, tell it the hostname to connect to |
| 46 | + // and the command to run |
| 47 | + thrd = new Thread(new ClientThread(hostName, menuSelection)); |
| 48 | + thrd.start(); // start the thread |
| 49 | + list.add(thrd); // add the thread to the end of the linked list |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + for (int i = 0; i < 30; i++) { |
| 54 | + try { |
| 55 | + list.get(i).join(); |
| 56 | + } catch (InterruptedException e) { |
| 57 | + // TODO Auto-generated catch block |
| 58 | + e.printStackTrace(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + } // end while |
| 64 | + |
| 65 | + }//end main |
| 66 | +//---------------------------------------------------------------------------- |
| 67 | + public static int mainMenu() |
| 68 | + { |
| 69 | + int menuSelection = 0; |
| 70 | + System.out.println("The menu provides the following choices to the user: "); |
| 71 | + System.out.println("1. Host current Date and Time \n2. Host uptime\n" |
| 72 | + + "3. Host memory use \n4. Host Netstat \n5. Host current users " |
| 73 | + + "\n6. Host running processes \n7. Quit "); |
| 74 | + System.out.println("Please provide number corresponding to the action you want to be performed:"); |
| 75 | + Scanner sc = new Scanner(System.in); |
| 76 | + menuSelection = sc.nextInt(); |
| 77 | + System.out.println("Running command: " + menuSelection); |
| 78 | + return menuSelection; |
| 79 | + |
| 80 | + }//end mainMenu |
| 81 | + |
| 82 | +} |
0 commit comments