Skip to content

Commit

Permalink
Some optimization and randomKill feature
Browse files Browse the repository at this point in the history
  • Loading branch information
matteobaccan committed Nov 30, 2023
1 parent 5098190 commit c9fe928
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 241 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cSpell.words": [
"Baccan",
"sockredirector"
]
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Ini file is divided in several section For each section you can define these par
| inWriteWait | long | 0 | writing to destination pause |
| outReadWait | long | 0 | reading from source pause |
| outWriteWait | long | 0 | write to source pause |
| randomKill | long | 0 | random kill of thread within N seconds |

## Example
### Configuration Example (sockRedirector.ini)
Expand Down
20 changes: 10 additions & 10 deletions nb-configuration.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.licensePath>${project.basedir}/licenseheader.txt</netbeans.hint.licensePath>
</properties>
</project-shared-configuration>
5 changes: 2 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<!-- Allineare la versione con graalvm.yml -->
<graalvm.version>23.1.1</graalvm.version>
<native-image-maven-plugin.version>21.2.0</native-image-maven-plugin.version>
<skipNativeVersion>true</skipNativeVersion>
<maven.compiler.release>11</maven.compiler.release>
</properties>

<dependencies>
Expand Down Expand Up @@ -163,7 +162,7 @@
<preCp>anything</preCp>
</classPath>
<jre>
<minVersion>1.8.0</minVersion>
<minVersion>11</minVersion>
<path>%JAVA_HOME%</path>
</jre>
<versionInfo>
Expand Down
8 changes: 4 additions & 4 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": [
"config:base"
],
"ignoreDeps": ["org.graalvm.sdk"]
"extends": [
"config:base"
],
"ignoreDeps": ["org.graalvm.sdk"]
}
9 changes: 5 additions & 4 deletions src/main/config/sockRedirector.ini
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<redirection>
<source>127.0.0.1</source>
<sourceport>8080</sourceport>
<destination>www.comune.novara.it</destination>
<destinationport>80</destinationport>
<source>0.0.0.0</source>
<sourceport>8081</sourceport>
<destination>127.0.0.1</destination>
<destinationport>8080</destinationport>
<log>true</log>
<timeout>0</timeout>
<client>10</client>
<randomKill>10</randomKill>
</redirection>
147 changes: 60 additions & 87 deletions src/main/java/it/baccan/sockredirector/AdminThread.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
/*
* Copyright (c) 2019 Matteo Baccan
* http://www.baccan.it
* https://www.baccan.it
*
* Distributed under the GPL v3 software license, see the accompanying
* file LICENSE or http://www.gnu.org/licenses/gpl.html.
* file LICENSE or https://www.gnu.org/licenses/gpl-3.0.html.
*
*/
package it.baccan.sockredirector;

import it.baccan.sockredirector.util.SocketFlow;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.extern.slf4j.Slf4j;

/**
* Admin thread.
*
* @author Matteo Baccan
* @version 1.0
*/
@Slf4j
public class AdminThread extends Thread {

private static final Logger LOG = LoggerFactory.getLogger(AdminThread.class);

/** Admin thread constructor. */
/**
* Admin thread constructor.
*/
public AdminThread() {
super();
setName("AdminThread");
}

/**
* Run admin thread.
*/
@Override
public final void run() {
try {

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
var input = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String line = input.readLine();

if (!line.isEmpty()) {
LOG.info("Admin> [{}]", line);
log.info("Admin> [{}]", line);
}

line = line.trim();
Expand All @@ -61,34 +61,34 @@ public final void run() {
}

if (command.equalsIgnoreCase("help")) {
LOG.info("");
LOG.info("help - this help");
LOG.info("exit - exit program");
LOG.info("thread [filter] - thread list");
LOG.info("kill <nth> [... [<nth>]] - kill nth thread");
LOG.info("pause <nth> <readPause> <writePause> - set pause on nth thread");
LOG.info("");
log.info("");
log.info("help - this help");
log.info("exit - exit program");
log.info("thread [filter] - thread list");
log.info("kill <nth> [... [<nth>]] - kill nth thread");
log.info("pause <nth> <readPause> <writePause> - set pause on nth thread");
log.info("");
} else if (command.equalsIgnoreCase("exit")) {
Runtime.getRuntime().halt(0);
} else if (command.equalsIgnoreCase("thread")) {
LOG.info("");
log.info("");
dumpThread(parameter);
LOG.info("");
log.info("");
} else if (command.equalsIgnoreCase("pause")) {
LOG.info("");
log.info("");
pauseThread(parameter);
LOG.info("");
log.info("");
} else if (command.equalsIgnoreCase("kill")) {
LOG.info("");
log.info("");
String[] th = parameter.split(" ");
for (String t : th) {
kill(t);
}
LOG.info("");
log.info("");
}
}
} catch (IOException e) {
LOG.error("adminThread error", e);
log.error("adminThread error", e);
}
}

Expand All @@ -100,10 +100,10 @@ private void dumpThread(String filter) {
String info = getThreadInfo(thread);
// If is in filter and the tread is not System
if ((filter.isEmpty() || info.contains(filter))
&& (thread.getThreadGroup() != null
&& !"system".equals(thread.getThreadGroup().getName()))
&& info.length() > 0) {
LOG.info(info);
&& (thread.getThreadGroup() != null
&& !"system".equals(thread.getThreadGroup().getName()))
&& info.length() > 0) {
log.info(info);
}
});
}
Expand All @@ -119,7 +119,6 @@ private void pauseThread(String parameter) {
if (thread instanceof FlowThread) {
FlowThread flowThread = (FlowThread) thread;

String info = getThreadInfo(thread);
// If is in filter and the tread is not System
if ((th[0].isEmpty() || ("" + thread.getId()).equals(th[0]))) {

Expand All @@ -128,14 +127,14 @@ private void pauseThread(String parameter) {
flowThread.setReadPause(Long.parseLong(th[1]));
}
} catch (NumberFormatException numberFormatException) {
LOG.error("Wrong number [{}]", th[1]);
log.error("Wrong number [{}]", th[1]);
}
try {
if (th.length > 2) {
flowThread.setWritePause(Long.parseLong(th[2]));
}
} catch (NumberFormatException numberFormatException) {
LOG.error("Wrong number [{}]", th[2]);
log.error("Wrong number [{}]", th[2]);
}
}
}
Expand All @@ -152,83 +151,57 @@ private void kill(final String cThread) {
try {
if (cThread.equals("" + thread.getId())) {
bKill.set(true);
LOG.info("Try to kill ID[{}]", cThread);
log.info("Try to kill ID[{}]", cThread);
thread.interrupt();
LOG.info("Thread killed");
log.info("Thread killed");
}
} catch (ThreadDeath td) {
LOG.error("ThreadDeath on admin kill [{}]", td.getMessage());
log.error("ThreadDeath on admin kill [{}]", td.getMessage());
} catch (Exception exception) {
LOG.error("Exception on admin kill", exception);
log.error("Exception on admin kill", exception);
}
});

if (!bKill.get()) {
LOG.info("Thread not found");
log.info("Thread not found");
}
}

private String getThreadInfo(final Thread thread) {
StringBuilder sb = new StringBuilder(128);
try {
if (thread instanceof FlowThread) {
FlowThread flowThread = (FlowThread) thread;
if (thread instanceof ServerSocketThread) {
ServerSocketThread serverSocketThread = (ServerSocketThread) thread;

sb.append(
padRight(
flowThread.getParentSockThread().getServerPojo().getSourceAddress(),
15));
sb.append("|");
sb.append(
padRight(
""
+ flowThread
.getParentSockThread()
.getServerPojo()
.getSourcePort(),
5));
sb.append("|");
if (flowThread.getSocketFlow() == SocketFlow.OUTBOUND) {
sb.append("->");
} else {
sb.append("<-");
}
sb.append("|");
sb.append(padRight(flowThread.getSocketFlow().name(), 20));
sb.append("|");
sb.append(padRight("" + flowThread.getReadPause(), 5));
sb.append("|");
sb.append(padRight("" + flowThread.getWritePause(), 5));
sb.append("|");
sb.append("|ID ");
sb.append(String.format("%1$5d", thread.getId()));
sb.append("|");
sb.append(padRight(thread.getClass().getSimpleName(), 20));
/*

sb.append(padRight(serverSocketThread.getServerPojo().getSourceAddress() + ":" + serverSocketThread.getServerPojo().getSourcePort(), 30));
sb.append("|ID ");
sb.append(String.format("%1$5d", serverSocketThread.getSourceOutputToDestinationInputThread().getId()));
sb.append("|");
sb.append(padRight(thread.getState().name(), 10));
sb.append(serverSocketThread.getSourceOutputToDestinationInputThread().getSocketFlow().name());
sb.append("|R PAUSE ");
sb.append(padRight("" + serverSocketThread.getSourceOutputToDestinationInputThread().getReadPause(), 5));
sb.append("|W PAUSE ");
sb.append(padRight("" + serverSocketThread.getSourceOutputToDestinationInputThread().getWritePause(), 5));
sb.append("|");
sb.append(padRight("" + thread.getPriority(), 10));
*/
/*
ThreadGroup threadGroup = thread.getThreadGroup();
if (threadGroup != null) {
sb.append(" GROUP[").append(threadGroup.getName()).append("]");
}
if (thread.isDaemon()) {
sb.append(" [daemon]");
}
if (thread.isAlive()) {
sb.append(" [alive]");
}
if (thread.isInterrupted()) {
sb.append(" [interrupted]");
}
*/
sb.append(padRight(thread.getName(), 20));

sb.append("|-|");
sb.append(serverSocketThread.getDestinationOutputToSourceInputThread().getSocketFlow().name());
sb.append("|ID ");
sb.append(String.format("%1$5d", serverSocketThread.getDestinationOutputToSourceInputThread().getId()));
sb.append("|R PAUSE ");
sb.append(padRight("" + serverSocketThread.getDestinationOutputToSourceInputThread().getReadPause(), 5));
sb.append("|W PAUSE ");
sb.append(padRight("" + serverSocketThread.getDestinationOutputToSourceInputThread().getWritePause(), 5));
sb.append("|");
sb.append(padRight(thread.getName(), 20));
}
} catch (Exception exception) {
LOG.error("getThreadInfo error", exception);
log.error("getThreadInfo error", exception);
}
return sb.toString();
}
Expand Down
Loading

0 comments on commit c9fe928

Please sign in to comment.