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

Commit 7a3ca30

Browse files
committed
a bit cleaning + SocketUtils + TaskManager (will be more advance)
1 parent 0c896af commit 7a3ca30

File tree

6 files changed

+72
-23
lines changed

6 files changed

+72
-23
lines changed

pom.xml

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@
2929
<artifactId>annotations</artifactId>
3030
<version>24.1.0</version>
3131
</dependency>
32-
</dependencies>
32+
<dependency>
33+
<groupId>org.projectlombok</groupId>
34+
<artifactId>lombok</artifactId>
35+
<version>RELEASE</version>
36+
<scope>compile</scope>
37+
</dependency>
38+
</dependencies>
3339
<build>
3440
<pluginManagement>
3541
<plugins>
@@ -71,16 +77,5 @@
7177
</plugin>
7278
</plugins>
7379
</pluginManagement>
74-
<plugins>
75-
<plugin>
76-
<groupId>org.apache.maven.plugins</groupId>
77-
<artifactId>maven-compiler-plugin</artifactId>
78-
<configuration>
79-
<source>17</source>
80-
<target>17</target>
81-
<compilerArgs>--enable-preview</compilerArgs>
82-
</configuration>
83-
</plugin>
84-
</plugins>
8580
</build>
8681
</project>
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package ir.xenoncommunity;
22

33
import ir.xenoncommunity.utils.CommandLineParser;
4+
import lombok.Getter;
45

56
public class Main {
7+
public static MainRunner runner;
68
public static void main(String[] args) {
7-
CommandLineParser parser = new CommandLineParser(args);
8-
MainRunner runner = new MainRunner(parser);
9+
Main.runner = new MainRunner(new CommandLineParser(args));
910
runner.run();
1011
}
1112
}
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
package ir.xenoncommunity;
22

33
import ir.xenoncommunity.utils.CommandLineParser;
4+
import ir.xenoncommunity.utils.SocketUtils;
5+
import ir.xenoncommunity.utils.TaskManager;
6+
import lombok.Getter;
7+
import lombok.val;
48

9+
@Getter
510
public class MainRunner {
6-
private final CommandLineParser parser;
11+
public final CommandLineParser parser;
12+
public final TaskManager taskManager;
13+
public final SocketUtils socketUtils;
714

815
public MainRunner(CommandLineParser parser) {
916
this.parser = parser;
17+
this.taskManager = new TaskManager();
18+
this.socketUtils = new SocketUtils();
1019
}
11-
1220
public void run() {
13-
System.out.println(parser.get("-examplestring", String.class));
14-
System.out.println(parser.get("-exampleinteger", Integer.class));
15-
System.out.println(parser.get("-exampleboolean", Boolean.class));
21+
val isResult = Main.runner.parser.get("--sendResult", Boolean.class);
22+
val isKeepAlive = Main.runner.parser.get("--keepAlive", Boolean.class);
1623
}
1724
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ public CommandLineParser(String[] args) {
1313
@Nullable
1414
public <T> T get(String arg, Class<T> type) {
1515
for (int i = 0; i < args.length - 1; i++) {
16-
if (args[i].equals(arg)) {
17-
String value = args[i + 1];
18-
return convertToType(value, type);
19-
}
16+
if (args[i].equals(arg))
17+
return convertToType(args[i + 1], type);
2018
}
2119
return null;
2220
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ir.xenoncommunity.utils;
2+
3+
import ir.xenoncommunity.Main;
4+
import ir.xenoncommunity.MainRunner;
5+
import lombok.SneakyThrows;
6+
import lombok.experimental.UtilityClass;
7+
import lombok.val;
8+
9+
import java.net.InetSocketAddress;
10+
import java.net.Socket;
11+
import java.net.SocketAddress;
12+
13+
public class SocketUtils {
14+
15+
@SneakyThrows
16+
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();
23+
}
24+
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ir.xenoncommunity.utils;
2+
3+
import lombok.experimental.UtilityClass;
4+
import lombok.val;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class TaskManager {
10+
public List<Thread> tasks = new ArrayList<>();
11+
public void add(final Thread threadIn){
12+
tasks.add(threadIn);
13+
}
14+
public void remove(final int idIn){
15+
tasks.get(idIn).interrupt();
16+
}
17+
public void doTasks(){
18+
tasks.forEach(e ->{
19+
e.start();
20+
});
21+
}
22+
}

0 commit comments

Comments
 (0)