Skip to content

Commit

Permalink
feat: run windows-mosaic with wsl-ns3
Browse files Browse the repository at this point in the history
  • Loading branch information
hoelger committed Oct 16, 2024
1 parent 6d67846 commit 53b8b14
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public FederateExecutor createFederateExecutor(String host, int port, OperatingS
case LINUX:
return new ExecutableFederateExecutor(this.descriptor, "./run.sh", Integer.toString(port));
case WINDOWS:
return new ExecutableFederateExecutor(this.descriptor, "wsl.exe", "./run.sh", Integer.toString(port));
case UNKNOWN:
default:
log.error("Operating system not supported by ns3");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Comparator;
Expand All @@ -61,6 +62,7 @@
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -201,6 +203,40 @@ public void connectToFederate(String host, InputStream in, InputStream err) thro
log.trace("{} finished ConnectToFederate", ambassadorName);
}

/**
*
*/
private ClientServerChannel waitForClientServerChannel(String host, int port) {
InetAddress h;
try {
h = InetAddress.getByName(host);
} catch (UnknownHostException ex) {
this.log.error("Unknown host: {}", ex.toString());
throw new RuntimeException(ex);
}
return waitForClientServerChannel(h, port);
}

/**
*
*/
private ClientServerChannel waitForClientServerChannel(InetAddress host, int port) {
int MAX_TRIES = 50;
int WAIT_MS = 100;
int tries = 0;
RuntimeException lastException = null;
while (tries++ < MAX_TRIES) {
try {
return new ClientServerChannel(host, port, this.log);
} catch (IOException ex) {
lastException = new RuntimeException(ex);
}
try {TimeUnit.MILLISECONDS.sleep(WAIT_MS);} catch (InterruptedException e) {}
}
this.log.error(lastException.toString());
throw lastException;
}

/**
* Connects the incoming channel with the federate, waits for INIT message and a port number,
* connects the outgoing channel to the received port number.
Expand All @@ -212,24 +248,18 @@ public void connectToFederate(String host, InputStream in, InputStream err) thro
*/
@Override
public void connectToFederate(String host, int port) {
try { // Connect to the network federate for reading
this.federateAmbassadorChannel = new ClientServerChannel(host, port, log);
this.log.info("Connected to {} for reading on port {}", federateName, port);
} catch (UnknownHostException ex) {
this.log.error("Unknown host: " + ex.toString());
throw new RuntimeException(ex);
} catch (IOException ex) {
this.log.error(ex.toString());
throw new RuntimeException(ex);
}
// Connect to the network federate for reading
this.federateAmbassadorChannel = this.waitForClientServerChannel(host, port);
this.log.info("Connected to {} for reading on port {}", federateName, port);

try { // Read the initial command and the port number to connect incoming channel
int cmd = this.federateAmbassadorChannel.readCommand();
if (cmd == CMD.INIT) {
// This is the port the federate listens on for the second channel
int remotePort = this.federateAmbassadorChannel.readPortBody();
remotePort = getHostPortFromDockerPort(remotePort);
// Connect the second channel
ambassadorFederateChannel = new ClientServerChannel(federateAmbassadorChannel.socket.getInetAddress(), remotePort, log);
this.ambassadorFederateChannel = waitForClientServerChannel(federateAmbassadorChannel.socket.getInetAddress(), remotePort);
this.log.info("Connected to {} for commands on port {}", federateName, remotePort);
} else {
throw new RuntimeException("Could not connect to federate. Federate response is " + cmd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,6 @@ public static final class ADDRESSTYPE {
*/ //TODO: implement usage
private String lastStatusMessage = "";

/**
* Constructor.
*
* @param host the remote host address as a String
* @param port the remote port number
* @param log logger to log on
* @throws IOException if the streams cannot be opened.
*/
public ClientServerChannel(String host, int port, Logger log) throws IOException {
this.socket = new Socket(host, port);
this.socket.setTcpNoDelay(true);
this.in = new BufferedInputStream(socket.getInputStream());
this.out = new BufferedOutputStream(socket.getOutputStream());
this.log = log;
}

/**
* Constructor.
*
Expand Down

0 comments on commit 53b8b14

Please sign in to comment.