Skip to content

Commit

Permalink
ResponsePath
Browse files Browse the repository at this point in the history
  • Loading branch information
Tilmann Zäschke committed Jun 19, 2024
1 parent 79f04af commit b6293c0
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 39 deletions.
Binary file added doc/Design-Path-Datamodel.odg
Binary file not shown.
24 changes: 10 additions & 14 deletions src/main/java/org/scion/jpan/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@
*/
public abstract class Path {
private final byte[] pathRaw;
private final long dstIsdAs;
private final InetAddress dstAddress;
private final int dstPort;
private final ScionSocketAddress dstAddress;

protected Path(byte[] rawPath, long dstIsdAs, InetAddress dstIP, int dstPort) {
this.pathRaw = rawPath;
this.dstIsdAs = dstIsdAs;
this.dstAddress = dstIP;
this.dstPort = dstPort;
this.dstAddress = ScionSocketAddress.from(this, dstIsdAs, dstIP, dstPort);
}

public byte[] getRawPath() {
Expand All @@ -42,27 +38,27 @@ public byte[] getRawPath() {
public abstract InetSocketAddress getFirstHopAddress() throws UnknownHostException;

public int getRemotePort() {
return dstPort;
return dstAddress.getPort();
}

public InetAddress getRemoteAddress() {
return dstAddress;
return dstAddress.getAddress();
}

public long getRemoteIsdAs() {
return dstIsdAs;
return dstAddress.getIsdAs();
}

public ScionSocketAddress getRemoteSocketAddress() {
return dstAddress;
}

@Override
public String toString() {
try {
return "Path{"
+ "rmtIsdAs="
+ ScionUtil.toStringIA(dstIsdAs)
+ ", rmtAddress="
+ "rmtAddress="
+ dstAddress
+ ", rmtPort="
+ dstPort
+ ", firstHop="
+ getFirstHopAddress()
+ ", pathRaw="
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/scion/jpan/ScionDatagramChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public ScionSocketAddress receive(ByteBuffer userBuffer) throws IOException {
}
ScionHeaderParser.extractUserPayload(buffer, userBuffer);
buffer.clear();
return ScionSocketAddress.from(receivePath);
return receivePath.getRemoteSocketAddress();
} finally {
readLock().unlock();
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/scion/jpan/ScionDatagramSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,12 @@ public synchronized void receive(DatagramPacket packet) throws IOException {
// timeout occurred
throw new SocketTimeoutException();
}
ResponsePath path = responseAddress.getPath();
Path path = responseAddress.getPath();
// TODO this is not ideal, a client may not be connected. Use getService()==null?
if (!channel.isConnected()) {
synchronized (pathCache) {
InetAddress ip = path.getRemoteAddress();
InetSocketAddress addr = new InetSocketAddress(ip, path.getRemotePort());
pathCache.put(addr, path);
pathCache.put(new InetSocketAddress(ip, path.getRemotePort()), path);
}
}
receiveBuffer.flip();
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/org/scion/jpan/ScionSocketAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,34 @@

package org.scion.jpan;

import java.net.InetAddress;
import java.net.InetSocketAddress;

public class ScionSocketAddress extends InetSocketAddress {

private final ResponsePath responsePath;
private final Path path;
private final long isdAs;

public static ScionSocketAddress from(ResponsePath path) {
return new ScionSocketAddress(path);
public static ScionSocketAddress from(Path path, long dstIsdAs, InetAddress dstIP, int dstPort) {
return new ScionSocketAddress(path, dstIsdAs, dstIP, dstPort);
}

private ScionSocketAddress(ResponsePath path) {
super(path.getRemoteAddress(), path.getRemotePort());
this.responsePath = path;
private ScionSocketAddress(Path path, long dstIsdAs, InetAddress dstIP, int dstPort) {
super(dstIP, dstPort);
this.path = path;
this.isdAs = dstIsdAs;
}

public ResponsePath getPath() {
return responsePath;
public Path getPath() {
return path;
}

public long getIsdAs() {
return isdAs;
}

@Override
public String toString() {
return ScionUtil.toStringIA(isdAs) + "," + super.toString();

Check warning on line 45 in src/main/java/org/scion/jpan/ScionSocketAddress.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/scion/jpan/ScionSocketAddress.java#L45

Added line #L45 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import java.util.Iterator;
import org.scion.jpan.ResponsePath;
import org.scion.jpan.ScionDatagramChannel;
import org.scion.jpan.ScionSocketAddress;
import org.scion.jpan.ScionService;
import org.scion.jpan.ScionSocketAddress;

/**
* DatagramChannel with support for timeout.
Expand Down Expand Up @@ -100,7 +100,7 @@ public ScionSocketAddress receive(ByteBuffer userBuffer) throws IOException {
}
ScionHeaderParser.extractUserPayload(buffer, userBuffer);
buffer.clear();
return ScionSocketAddress.from(receivePath);
return receivePath.getRemoteSocketAddress();
} finally {
readLock().unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.scion.jpan.ScionDatagramChannel;
import org.scion.jpan.ScionSocketAddress;
import org.scion.jpan.ScionService;
import org.scion.jpan.ScionSocketAddress;
import org.scion.jpan.testutil.MockDNS;
import org.scion.jpan.testutil.MockDaemon;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import org.junit.jupiter.api.Test;
import org.scion.jpan.Path;
import org.scion.jpan.ScionDatagramChannel;
import org.scion.jpan.ScionSocketAddress;
import org.scion.jpan.ScionService;
import org.scion.jpan.ScionSocketAddress;
import org.scion.jpan.testutil.PingPongChannelHelper;

/** Test receive()/send(InetAddress) operations on DatagramChannel. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.junit.jupiter.api.Test;
import org.scion.jpan.Path;
import org.scion.jpan.ScionDatagramChannel;
import org.scion.jpan.ScionSocketAddress;
import org.scion.jpan.ScionService;
import org.scion.jpan.ScionSocketAddress;
import org.scion.jpan.testutil.PingPongChannelHelper;

/** Test receive()/send(Path) operations on DatagramChannel. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.junit.jupiter.api.Test;
import org.scion.jpan.Path;
import org.scion.jpan.ScionDatagramChannel;
import org.scion.jpan.ScionSocketAddress;
import org.scion.jpan.ScionService;
import org.scion.jpan.ScionSocketAddress;
import org.scion.jpan.testutil.MockNetwork;
import org.scion.jpan.testutil.PingPongChannelHelper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static void service() throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(100);
println("Waiting for packet ... ");
ScionSocketAddress responseAddress = channel.receive(buffer);
ResponsePath path = responseAddress.getPath();
Path path = responseAddress.getPath();
String msg = extractMessage(buffer);
String remoteAddress = path.getRemoteAddress() + ":" + path.getRemotePort();
String borderRouterInterfaces = ScionUtil.toStringPath(path.getRawPath());
Expand Down
8 changes: 2 additions & 6 deletions src/test/java/org/scion/jpan/testutil/MockNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import org.scion.jpan.PackageVisibilityHelper;
import org.scion.jpan.ResponsePath;
import org.scion.jpan.ScionUtil;
import org.scion.jpan.Scmp;
import org.scion.jpan.*;
import org.scion.jpan.demo.inspector.HopField;
import org.scion.jpan.demo.inspector.PathHeaderScion;
import org.scion.jpan.demo.inspector.ScionPacketInspector;
Expand Down Expand Up @@ -366,8 +363,7 @@ private void handleScmp(
InetSocketAddress dstAddress = PackageVisibilityHelper.getDstAddress(buffer);
// From here on we use linear reading using the buffer's position() mechanism
buffer.position(ScionHeaderParser.extractHeaderLength(buffer));
ResponsePath path =
PackageVisibilityHelper.getResponsePath(buffer, (InetSocketAddress) srcAddress);
Path path = PackageVisibilityHelper.getResponsePath(buffer, (InetSocketAddress) srcAddress);
Scmp.Type type = ScmpParser.extractType(buffer);
Scmp.Message scmpMsg = PackageVisibilityHelper.createMessage(type, path);
ScmpParser.consume(buffer, scmpMsg);
Expand Down

0 comments on commit b6293c0

Please sign in to comment.