Skip to content
This repository was archived by the owner on Aug 6, 2024. It is now read-only.

Commit fc9b353

Browse files
committed
better console parser, logger, better socket utils, more
1 parent 7a3ca30 commit fc9b353

File tree

6 files changed

+56
-22
lines changed

6 files changed

+56
-22
lines changed

src/main/java/ir/xenoncommunity/Main.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ir.xenoncommunity;
22

33
import ir.xenoncommunity.utils.CommandLineParser;
4-
import lombok.Getter;
54

65
public class Main {
76
public static MainRunner runner;

src/main/java/ir/xenoncommunity/MainRunner.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ir.xenoncommunity;
22

33
import ir.xenoncommunity.utils.CommandLineParser;
4+
import ir.xenoncommunity.utils.Logger;
45
import ir.xenoncommunity.utils.SocketUtils;
56
import ir.xenoncommunity.utils.TaskManager;
67
import lombok.Getter;
@@ -11,14 +12,28 @@ public class MainRunner {
1112
public final CommandLineParser parser;
1213
public final TaskManager taskManager;
1314
public final SocketUtils socketUtils;
15+
public final Logger logger = new Logger(true, true);
1416

1517
public MainRunner(CommandLineParser parser) {
1618
this.parser = parser;
1719
this.taskManager = new TaskManager();
1820
this.socketUtils = new SocketUtils();
1921
}
22+
2023
public void run() {
24+
getLogger().print(Logger.LEVEL.INFO,"", "test");
25+
val ip = Main.runner.parser.get("--ip", String.class);
26+
val port = Main.runner.parser.get("--port", Integer.class);
27+
val maxThreads = Main.runner.parser.get("--threads", Integer.class);
2128
val isResult = Main.runner.parser.get("--sendResult", Boolean.class);
2229
val isKeepAlive = Main.runner.parser.get("--keepAlive", Boolean.class);
30+
for(int i = 0; i <= maxThreads; i++){
31+
getTaskManager().add(new Thread(() -> {
32+
while (true) {
33+
socketUtils.connect(ip, port, isResult, isKeepAlive);
34+
}
35+
}));
36+
}
37+
getTaskManager().doTasks();
2338
}
2439
}

src/main/java/ir/xenoncommunity/utils/CommandLineParser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ public CommandLineParser(String[] args) {
1212

1313
@Nullable
1414
public <T> T get(String arg, Class<T> type) {
15-
for (int i = 0; i < args.length - 1; i++) {
16-
if (args[i].equals(arg))
15+
for (int i = 0; i < args.length ; i++) {
16+
if(args[i + 1].startsWith("-"))
17+
return (T) Boolean.TRUE;
18+
else if (args[i].equals(arg))
1719
return convertToType(args[i + 1], type);
1820
}
1921
return null;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ir.xenoncommunity.utils;
2+
3+
import lombok.val;
4+
5+
public class Logger {
6+
public String osName = System.getProperty("os.name");
7+
public String osVersion = System.getProperty("os.version");
8+
private boolean sendOsName;
9+
private boolean sendOsVersion;
10+
public Logger(final boolean sendOsName, final boolean sendOSVersion){
11+
this.sendOsName = sendOsName;
12+
this.sendOsVersion = sendOSVersion;
13+
}
14+
public void print(final LEVEL levelIn, final String sectionIn, final String message){
15+
val osName = sendOsName ? this.osName : "";
16+
val osVersion = sendOsVersion ? this.osVersion : "";
17+
val section = sectionIn.equals("") ? "NONE" : sectionIn;
18+
System.out.println(String.format("[%s-%s] [%s] [%s]>> %s", osName, osVersion, levelIn, section, message));
19+
20+
}
21+
22+
public enum LEVEL{
23+
ERROR,
24+
WARN,
25+
INFO
26+
}
27+
}
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
package ir.xenoncommunity.utils;
22

33
import ir.xenoncommunity.Main;
4-
import ir.xenoncommunity.MainRunner;
54
import lombok.SneakyThrows;
6-
import lombok.experimental.UtilityClass;
75
import lombok.val;
8-
9-
import java.net.InetSocketAddress;
106
import java.net.Socket;
11-
import java.net.SocketAddress;
127

138
public class SocketUtils {
14-
159
@SneakyThrows
1610
public void connect(final String ipIn, final int port, final boolean sendResult, final boolean keepAlive){
17-
val socket = new Socket(ipIn, port);
18-
if(sendResult && socket.isConnected()) System.out.println(String.format("Connected! ip: %s, port: %s, threads: %s", ipIn, port, Main.runner.getTaskManager().tasks.size()));
19-
if(keepAlive) {
20-
socket.setKeepAlive(true);
21-
} else{
22-
socket.close();
11+
try(val socket = new Socket(ipIn, port)){
12+
if (sendResult && socket.isConnected()) {
13+
System.out.printf("Connected! ip: %s, port: %s, threads: %s\n", ipIn, port, Main.runner.getTaskManager().tasks.size());
14+
}
15+
if (keepAlive) {
16+
socket.setKeepAlive(true);
17+
} else{
18+
socket.close();
19+
}
2320
}
24-
2521
}
2622
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package ir.xenoncommunity.utils;
22

3-
import lombok.experimental.UtilityClass;
4-
import lombok.val;
5-
63
import java.util.ArrayList;
74
import java.util.List;
85

@@ -15,8 +12,6 @@ public void remove(final int idIn){
1512
tasks.get(idIn).interrupt();
1613
}
1714
public void doTasks(){
18-
tasks.forEach(e ->{
19-
e.start();
20-
});
15+
tasks.forEach(Thread::start);
2116
}
2217
}

0 commit comments

Comments
 (0)