Skip to content

Commit

Permalink
occupy any free port instead of hardcoded 12100
Browse files Browse the repository at this point in the history
  • Loading branch information
artpar committed Apr 15, 2024
1 parent ac4e989 commit 4fe1324
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/main/java/io/unlogged/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
*/
public class Runtime {

public static final int AGENT_SERVER_PORT = 12100;
private static Runtime instance;
private static List<Pair<String, String>> pendingClassRegistrations = new ArrayList<>();
private final ScheduledExecutorService probeReaderExecutor = Executors.newSingleThreadScheduledExecutor();
Expand Down Expand Up @@ -57,9 +56,10 @@ private Runtime(String args) {

ServerMetadata serverMetadata =
new ServerMetadata(weaveParameters.getIncludedNames().toString(), Constants.AGENT_VERSION,
AGENT_SERVER_PORT);
0);

httpServer = new AgentCommandServer(AGENT_SERVER_PORT, serverMetadata);
httpServer = new AgentCommandServer(0, serverMetadata);
httpServer.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);


outputDir = new File(weaveParameters.getOutputDirname());
Expand All @@ -84,6 +84,9 @@ private Runtime(String args) {
errorLogger.log("Java version: " + System.getProperty("java.version"));
errorLogger.log("Agent version: " + Constants.AGENT_VERSION);
errorLogger.log("Params: " + args);

serverMetadata.setAgentServerUrl("http://localhost:" + httpServer.getListeningPort());
serverMetadata.setAgentServerPort(httpServer.getListeningPort());
errorLogger.log(serverMetadata.toString());

System.out.println("[unlogged]" +
Expand Down Expand Up @@ -143,7 +146,8 @@ private Runtime(String args) {


httpServer.setAgentCommandExecutor(new AgentCommandExecutorImpl(logger.getObjectMapper(), logger));
httpServer.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);

System.out.println("[unlogged] agent server started at port " + httpServer.getListeningPort());

java.lang.Runtime.getRuntime()
.addShutdownHook(new Thread(this::close));
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/io/unlogged/command/ServerMetadata.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package io.unlogged.command;

public class ServerMetadata {
private final int agentServerPort;
public void setAgentServerPort(int agentServerPort) {
this.agentServerPort = agentServerPort;
}

private int agentServerPort;
String includePackageName;
String agentVersion;
String agentServerUrl;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/unlogged/weaver/RuntimeWeaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public class RuntimeWeaver implements ClassFileTransformer {
* @param instrumentation object provided to the java agent for hooking up the class loader
*/
public RuntimeWeaver(String args, Instrumentation instrumentation) {
System.out.println("[unlogged] agent server started at port " + AGENT_SERVER_PORT);
this.instrumentation = instrumentation;
runtime = Runtime.getInstance(args);
logger = runtime.getLogger();
Expand Down

0 comments on commit 4fe1324

Please sign in to comment.