|
1 | 1 | package com.anas.jdosattacker; |
2 | 2 |
|
| 3 | +import com.anas.jdosattacker.args.ArgumentsProcessor; |
| 4 | +import com.anas.jdosattacker.request.Requester; |
| 5 | +import com.anas.jdosattacker.tui.GetData; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.ArrayList; |
| 9 | + |
3 | 10 | /** |
4 | | - * Entry point of the program. |
| 11 | + * The main class. |
5 | 12 | */ |
6 | 13 | public class Main { |
| 14 | + public static final String VERSION; |
| 15 | + private static int threadsNumber; |
| 16 | + private static final ArrayList<Thread> threads; |
| 17 | + |
| 18 | + static { |
| 19 | + VERSION = "1.2.0"; |
| 20 | + threadsNumber = 0; |
| 21 | + threads = new ArrayList<>(); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * The entry point of the program. |
| 26 | + * |
| 27 | + * @param args The arguments passed to the program |
| 28 | + */ |
7 | 29 | public static void main(final String[] args) { |
8 | | - new MainController(args); // Just it :D |
| 30 | + // It's processing arguments from command line. |
| 31 | + new ArgumentsProcessor().process(args); |
| 32 | + // It's checking if there are important empty fields in the request. |
| 33 | + if (Main.hasEmptyFields()) { |
| 34 | + try { |
| 35 | + // If true, start the tui and get the data from the user. |
| 36 | + new GetData(); |
| 37 | + } catch (final IOException e) { |
| 38 | + System.err.println("Error: " + e.getMessage()); |
| 39 | + System.exit(1); |
| 40 | + } |
| 41 | + } |
| 42 | + createThreads(); |
| 43 | + startThreads(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Start all the threads in the threads array. |
| 48 | + */ |
| 49 | + private static void startThreads() { |
| 50 | + for (final var thread : threads) { |
| 51 | + thread.start(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Set the number of threads to use. |
| 57 | + * |
| 58 | + * @param threadsNum number of threads. |
| 59 | + * @throws FieldException if the number of threads is less than 1. |
| 60 | + */ |
| 61 | + public static void setThreadsNum(final int threadsNum) throws FieldException { |
| 62 | + if (threadsNum < 1) { |
| 63 | + throw new FieldException("Number of threads must be greater than 0"); |
| 64 | + } |
| 65 | + threadsNumber = threadsNum; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Set the threads number from string. |
| 70 | + * |
| 71 | + * @param threadsNumber string number of threads. |
| 72 | + * @throws FieldException if the number of threads is less than 1, or if it's not an integer number. |
| 73 | + */ |
| 74 | + public static void setThreadsNum(final String threadsNumber) throws FieldException { |
| 75 | + try { |
| 76 | + Main.setThreadsNum(Integer.parseInt(threadsNumber)); |
| 77 | + } catch (final NumberFormatException e) { |
| 78 | + throw new FieldException("Number of threads must be an integer number"); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * It creates a new threads and set there names and add them to the threads array. |
| 84 | + */ |
| 85 | + public static void createThreads() { |
| 86 | + for (var i = 0; i < threadsNumber; i++) { |
| 87 | + threads.add(new Thread(new Requester())); |
| 88 | + threads.get(i).setName("Requester " + (i + 1)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * If the requester has empty fields or the number of threads is 0, then return true. |
| 94 | + * |
| 95 | + * @return A boolean value, representing if the requester has empty fields or the number of threads is 0. |
| 96 | + */ |
| 97 | + public static boolean hasEmptyFields() { |
| 98 | + return Requester.hasRequirementEmptyFields() || threadsNumber == 0; |
9 | 99 | } |
10 | 100 | } |
0 commit comments