diff --git a/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureInstance.java b/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureInstance.java index 1a79bd03..71e19419 100644 --- a/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureInstance.java +++ b/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureInstance.java @@ -187,12 +187,7 @@ public void bind() throws IOException { * methods */ public void sendMsgs(byte[] data) throws IOException { - if (rxMsgsSocket == null) { - throw new IllegalStateException("Attempted to send data before opening socket"); - } - DatagramPacket packet = new DatagramPacket(data, data.length, targetAddress, rxMessagePort); - rxMsgsSocket.send(packet); - + sendPacket(data, rxMessagePort); } /** @@ -201,20 +196,18 @@ public void sendMsgs(byte[] data) throws IOException { * @throws IOException If there is an issue with the underlying socket object or methods */ public void sendTimeSyncMsgs(byte[] data) throws IOException { - if (rxMsgsSocket == null) { - throw new IllegalStateException("Attempted to send data before opening socket"); - } - - DatagramPacket packet = new DatagramPacket(data, data.length, targetAddress, timeSyncPort); - rxMsgsSocket.send(packet); - + sendPacket(data, timeSyncPort); } public void sendInteraction(byte[] data) throws IOException { - if (rxMsgsSocket == null) { + sendPacket(data, simulatedInteractionPort); + } + + public void sendPacket(byte[] data, int port) throws IOException { + if (rxMsgsSocket == null) { throw new IllegalStateException("Attempted to send data before opening socket"); } - DatagramPacket packet = new DatagramPacket(data, data.length, targetAddress, simulatedInteractionPort); + DatagramPacket packet = new DatagramPacket(data, data.length, targetAddress, port); rxMsgsSocket.send(packet); } }