From be8874335e8516819fe1849bfee6361dc62f264f Mon Sep 17 00:00:00 2001 From: dev Date: Thu, 27 Jul 2023 15:30:10 -0400 Subject: [PATCH] Removed duplication in InfrastructureInstance by adding sendPacket method --- .../ambassador/InfrastructureInstance.java | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) 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); } }