Skip to content
This repository was archived by the owner on Apr 12, 2023. It is now read-only.

Commit a6e98cb

Browse files
committed
Delete the main class and replace it with main controller 😊
1 parent b0dcc65 commit a6e98cb

File tree

4 files changed

+97
-101
lines changed

4 files changed

+97
-101
lines changed
Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,100 @@
11
package com.anas.jdosattacker;
22

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+
310
/**
4-
* Entry point of the program.
11+
* The main class.
512
*/
613
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+
*/
729
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;
999
}
10100
}

src/main/java/com/anas/jdosattacker/MainController.java

Lines changed: 0 additions & 94 deletions
This file was deleted.

src/main/java/com/anas/jdosattacker/args/ArgumentsProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.anas.jdosattacker.args;
22

33
import com.anas.jdosattacker.FieldException;
4-
import com.anas.jdosattacker.MainController;
4+
import com.anas.jdosattacker.Main;
55
import com.anas.jdosattacker.request.Requester;
66
import org.apache.commons.cli.*;
77

@@ -54,7 +54,7 @@ public void process(final String[] args) {
5454
if (commandLine.hasOption("url"))
5555
Requester.setUrl(commandLine.getOptionValue("url"));
5656
if (commandLine.hasOption("threads"))
57-
MainController.setThreadsNum(commandLine.getOptionValue("threads"));
57+
Main.setThreadsNum(commandLine.getOptionValue("threads"));
5858
if (commandLine.hasOption("number"))
5959
Requester.setReqNumber(commandLine.getOptionValue("number"));
6060
if (commandLine.hasOption("connectTimeout"))
@@ -75,7 +75,7 @@ public void process(final String[] args) {
7575
* If the user types in the command line argument -v, this method will be called.
7676
*/
7777
private void printVersion() {
78-
System.out.println("Version: " + MainController.VERSION);
78+
System.out.println("Version: " + Main.VERSION);
7979
System.exit(0);
8080
}
8181

src/main/java/com/anas/jdosattacker/tui/GetData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.anas.jdosattacker.tui;
22

33
import com.anas.jdosattacker.FieldException;
4-
import com.anas.jdosattacker.MainController;
4+
import com.anas.jdosattacker.Main;
55
import com.anas.jdosattacker.request.Requester;
66
import com.googlecode.lanterna.TerminalSize;
77
import com.googlecode.lanterna.TextColor;
@@ -64,7 +64,7 @@ private void addButtonListener() {
6464
button.addListener(b -> {
6565
try {
6666
Requester.setReqNumber(numberOfRequestsTextBox.getText());
67-
MainController.setThreadsNum(threadsTextBox.getText());
67+
Main.setThreadsNum(threadsTextBox.getText());
6868
Requester.setConnectTimeout(connectionTimeoutTextBox.getText());
6969
Requester.setUrl(urlTextBox.getText());
7070
Requester.setUserAgent(userAgentTextBox.getText());

0 commit comments

Comments
 (0)