Skip to content

Commit

Permalink
RequestPath
Browse files Browse the repository at this point in the history
  • Loading branch information
Tilmann Zäschke committed Jun 19, 2024
1 parent b6293c0 commit 30a53d4
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 36 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/scion/jpan/AbstractDatagramChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void setPathPolicy(PathPolicy pathPolicy) throws IOException {
synchronized (stateLock) {
this.pathPolicy = pathPolicy;
if (isConnected()) {
connectionPath = pathPolicy.filter(getOrCreateService().getPaths(connectionPath));
connectionPath = (RequestPath) pathPolicy.filter(getOrCreateService().getPaths(connectionPath));
updateConnection(connectionPath, true);
}
}
Expand Down Expand Up @@ -235,7 +235,8 @@ public C connect(SocketAddress addr) throws IOException {
throw new IllegalArgumentException(
"connect() requires an InetSocketAddress or a ScionSocketAddress.");
}
return connect(getOrCreateService().lookupAndGetPath((InetSocketAddress) addr, pathPolicy));
Path path = getOrCreateService().lookupAndGetPath((InetSocketAddress) addr, pathPolicy);
return connect((RequestPath) path);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/scion/jpan/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public ScionSocketAddress getRemoteSocketAddress() {
return dstAddress;
}

public abstract PathMetadata getMetadata();

@Override
public String toString() {
try {
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/org/scion/jpan/PathPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ public interface PathPolicy {
PathPolicy DEFAULT = MIN_HOPS;

class First implements PathPolicy {
public RequestPath filter(List<RequestPath> paths) {
public Path filter(List<Path> paths) {
return paths.stream().findFirst().orElseThrow(NoSuchElementException::new);
}
}

class MaxBandwith implements PathPolicy {
public RequestPath filter(List<RequestPath> paths) {
public Path filter(List<Path> paths) {
return paths.stream()
.max(Comparator.comparing(path -> Collections.min(path.getMetadata().getBandwidthList())))
.orElseThrow(NoSuchElementException::new);
}
}

class MinLatency implements PathPolicy {
public RequestPath filter(List<RequestPath> paths) {
public Path filter(List<Path> paths) {
// A 0-value indicates that the AS did not announce a latency for this hop.
// We use Integer.MAX_VALUE for comparison of these ASes.
return paths.stream()
Expand All @@ -53,7 +53,7 @@ public RequestPath filter(List<RequestPath> paths) {
}

class MinHopCount implements PathPolicy {
public RequestPath filter(List<RequestPath> paths) {
public Path filter(List<Path> paths) {
return paths.stream()
.min(Comparator.comparing(path -> path.getMetadata().getInternalHopsList().size()))
.orElseThrow(NoSuchElementException::new);
Expand All @@ -68,14 +68,14 @@ public IsdAllow(Set<Integer> allowedIsds) {
}

@Override
public RequestPath filter(List<RequestPath> paths) {
public Path filter(List<Path> paths) {
return paths.stream()
.filter(this::checkPath)
.findAny()
.orElseThrow(NoSuchElementException::new);
}

private boolean checkPath(RequestPath path) {
private boolean checkPath(Path path) {
for (PathMetadata.PathInterface pif : path.getMetadata().getInterfacesList()) {
int isd = (int) (pif.getIsdAs() >>> 48);
if (!allowedIsds.contains(isd)) {
Expand All @@ -94,14 +94,14 @@ public IsdDisallow(Set<Integer> disallowedIsds) {
}

@Override
public RequestPath filter(List<RequestPath> paths) {
public Path filter(List<Path> paths) {
return paths.stream()
.filter(this::checkPath)
.findAny()
.orElseThrow(NoSuchElementException::new);
}

private boolean checkPath(RequestPath path) {
private boolean checkPath(Path path) {
for (PathMetadata.PathInterface pif : path.getMetadata().getInterfacesList()) {
int isd = (int) (pif.getIsdAs() >>> 48);
if (disallowedIsds.contains(isd)) {
Expand All @@ -117,5 +117,5 @@ private boolean checkPath(RequestPath path) {
* @return The "best" path according to the filter's policy.
* @throws NoSuchElementException if no matching path could be found.
*/
RequestPath filter(List<RequestPath> paths);
Path filter(List<Path> paths);
}
5 changes: 5 additions & 0 deletions src/main/java/org/scion/jpan/ResponsePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public InetSocketAddress getFirstHopAddress() {
return firstHopAddress;
}

@Override
public PathMetadata getMetadata() {
return null;
}

public long getLocalIsdAs() {
return srcIsdAs;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/scion/jpan/ScionDatagramChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public int send(ByteBuffer srcBuffer, SocketAddress destination) throws IOExcept
synchronized (stateLock()) {
path = resolvedDestinations.get(dst);
if (path == null) {
path = getOrCreateService().lookupAndGetPath(dst, getPathPolicy());
path = (RequestPath) getOrCreateService().lookupAndGetPath(dst, getPathPolicy());
resolvedDestinations.put(dst, path);
}
}
Expand Down Expand Up @@ -251,7 +251,7 @@ private RequestPath refreshPath(RequestPath path, RefreshPolicy refreshPolicy) {
return null;
}
// expired, get new path
List<RequestPath> paths = getOrCreateService().getPaths(path);
List<Path> paths = getOrCreateService().getPaths(path);
switch (refreshPolicy) {
case OFF:
// let this pass until it is ACTUALLY expired
Expand All @@ -260,17 +260,17 @@ private RequestPath refreshPath(RequestPath path, RefreshPolicy refreshPolicy) {
}
throw new ScionRuntimeException("Path is expired");
case POLICY:
return getPathPolicy().filter(getOrCreateService().getPaths(path));
return (RequestPath) getPathPolicy().filter(getOrCreateService().getPaths(path));
case SAME_LINKS:
return findPathSameLinks(paths, path);
default:
throw new UnsupportedOperationException();
}
}

private RequestPath findPathSameLinks(List<RequestPath> paths, RequestPath path) {
private RequestPath findPathSameLinks(List<Path> paths, RequestPath path) {
List<PathMetadata.PathInterface> reference = path.getMetadata().getInterfacesList();
for (RequestPath newPath : paths) {
for (Path newPath : paths) {
List<PathMetadata.PathInterface> ifs = newPath.getMetadata().getInterfacesList();
if (ifs.size() != reference.size()) {
continue;
Expand All @@ -286,7 +286,7 @@ private RequestPath findPathSameLinks(List<RequestPath> paths, RequestPath path)
}
}
if (isSame) {
return newPath;
return (RequestPath) newPath;
}
}
return null;
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/scion/jpan/ScionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ List<Daemon.Path> getPathListDaemon(long srcIsdAs, long dstIsdAs) {
* @throws IOException if an errors occurs while querying paths.
*/
@Deprecated // Please use lookup() instead
public List<RequestPath> getPaths(InetSocketAddress dstAddress) throws IOException {
public List<Path> getPaths(InetSocketAddress dstAddress) throws IOException {
// Use getHostString() to avoid DNS reverse lookup.
ScionAddress sa = getScionAddress(dstAddress.getHostString());
return getPaths(sa.getIsdAs(), sa.getInetAddress(), dstAddress.getPort());
Expand All @@ -320,7 +320,7 @@ public List<RequestPath> getPaths(InetSocketAddress dstAddress) throws IOExcepti
* @param dstScionAddress Destination IP address. Must belong to a SCION enabled end host.
* @return All paths returned by the path service.
*/
public List<RequestPath> getPaths(long dstIsdAs, InetSocketAddress dstScionAddress) {
public List<Path> getPaths(long dstIsdAs, InetSocketAddress dstScionAddress) {
return getPaths(dstIsdAs, dstScionAddress.getAddress(), dstScionAddress.getPort());
}

Expand All @@ -330,7 +330,7 @@ public List<RequestPath> getPaths(long dstIsdAs, InetSocketAddress dstScionAddre
* @param path A path
* @return All paths returned by the path service.
*/
public List<RequestPath> getPaths(RequestPath path) {
public List<Path> getPaths(Path path) {
return getPaths(path.getRemoteIsdAs(), path.getRemoteAddress(), path.getRemotePort());
}

Expand All @@ -342,7 +342,7 @@ public List<RequestPath> getPaths(RequestPath path) {
* @param dstPort Destination port
* @return All paths returned by the path service. Returns an empty list if no paths are found.
*/
public List<RequestPath> getPaths(long dstIsdAs, InetAddress dstAddress, int dstPort) {
public List<Path> getPaths(long dstIsdAs, InetAddress dstAddress, int dstPort) {
return getPaths(ScionAddress.create(dstIsdAs, dstAddress), dstPort);
}

Expand All @@ -354,7 +354,7 @@ public List<RequestPath> getPaths(long dstIsdAs, InetAddress dstAddress, int dst
* @return All paths returned by the path service.
* @throws ScionException if the DNS/TXT lookup did not return a (valid) SCION address.
*/
public RequestPath lookupAndGetPath(InetSocketAddress dstAddr, PathPolicy policy)
public Path lookupAndGetPath(InetSocketAddress dstAddr, PathPolicy policy)
throws ScionException {
if (policy == null) {
policy = PathPolicy.DEFAULT;
Expand All @@ -368,10 +368,10 @@ public RequestPath lookupAndGetPath(InetSocketAddress dstAddr, PathPolicy policy
* @param dstAddress Destination SCION address
* @return All paths returned by the path service.
*/
public List<RequestPath> getPaths(ScionAddress dstAddress, int dstPort) {
public List<Path> getPaths(ScionAddress dstAddress, int dstPort) {
long srcIsdAs = getLocalIsdAs();
List<Daemon.Path> paths = getPathList(srcIsdAs, dstAddress.getIsdAs());
List<RequestPath> scionPaths = new ArrayList<>(paths.size());
List<Path> scionPaths = new ArrayList<>(paths.size());
for (int i = 0; i < paths.size(); i++) {
scionPaths.add(
RequestPath.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ void testErrorHandling() throws IOException {
System.out.println("msg: " + message.getTypeCode());
throw new IllegalArgumentException();
});
List<RequestPath> paths = Scion.defaultService().getPaths(ExamplePacket.DST_IA, dstAddr);
List<Path> paths = Scion.defaultService().getPaths(ExamplePacket.DST_IA, dstAddr);
assertEquals(2, paths.size());
RequestPath path0 = paths.get(0);
RequestPath path1 = paths.get(0);
Path path0 = paths.get(0);
Path path1 = paths.get(0);
channel.connect(path0);
channel.write(ByteBuffer.allocate(0));
assertEquals(path0, channel.getConnectionPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DatagramChannelPathSwitchTest {
private int count = 0;

@Override
public RequestPath filter(List<RequestPath> paths) {
public Path filter(List<Path> paths) {
return paths.get(count++ % 2);
}
};
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/org/scion/jpan/api/ScionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void defaultService_daemon() throws IOException {
System.setProperty(Constants.PROPERTY_DAEMON, "[::1]:" + DEFAULT_PORT);
try {
ScionService service = Scion.defaultService();
RequestPath path = service.getPaths(dstIA, dstAddress).get(0);
Path path = service.getPaths(dstIA, dstAddress).get(0);
assertNotNull(path);
// local AS + path
assertEquals(2, MockDaemon.getAndResetCallCount());
Expand All @@ -109,7 +109,7 @@ void defaultService_topoFile() {
try {
System.setProperty(Constants.PROPERTY_BOOTSTRAP_TOPO_FILE, TOPO_FILE);
ScionService service = Scion.defaultService();
RequestPath path = service.getPaths(dstIA, dstAddress).get(0);
Path path = service.getPaths(dstIA, dstAddress).get(0);
assertNotNull(path);
assertEquals(0, MockDaemon.getAndResetCallCount()); // Daemon is not used!
} finally {
Expand All @@ -135,7 +135,7 @@ void defaultService_bootstrapAddress() {

System.setProperty(Constants.PROPERTY_BOOTSTRAP_HOST, host);
ScionService service = Scion.defaultService();
RequestPath path = service.getPaths(dstIA, dstAddress).get(0);
Path path = service.getPaths(dstIA, dstAddress).get(0);
assertNotNull(path);
assertEquals(0, MockDaemon.getAndResetCallCount()); // Daemon is not used!
} finally {
Expand All @@ -150,7 +150,7 @@ void defaultService_bootstrapNaptrRecord() {
MockNetwork.startTiny(MockNetwork.Mode.NAPTR);
try {
ScionService service = Scion.defaultService();
RequestPath path = service.getPaths(dstIA, dstAddress).get(0);
Path path = service.getPaths(dstIA, dstAddress).get(0);
assertNotNull(path);
assertEquals(0, MockDaemon.getAndResetCallCount()); // Daemon is not used!
} finally {
Expand All @@ -166,7 +166,7 @@ void defaultService_bootstrapTopoFile() {
try {
System.setProperty(Constants.PROPERTY_BOOTSTRAP_TOPO_FILE, TOPO_FILE);
ScionService service = Scion.defaultService();
RequestPath path = service.getPaths(dstIA, dstAddress).get(0);
Path path = service.getPaths(dstIA, dstAddress).get(0);
assertNotNull(path);
assertEquals(0, MockDaemon.getAndResetCallCount()); // Daemon is not used!
} finally {
Expand Down Expand Up @@ -295,7 +295,7 @@ void newServiceWithDNS() throws IOException {
try (Scion.CloseableService ss = Scion.newServiceWithDNS(MockTopologyServer.TOPO_HOST)) {
// destination address = 123.123.123.123 because we don´t care for getting a path
InetAddress ip123 = InetAddress.getByAddress(new byte[] {123, 123, 123, 123});
List<RequestPath> paths = ss.getPaths(iaDst, ip123, 12345);
List<Path> paths = ss.getPaths(iaDst, ip123, 12345);
assertNotNull(paths);
assertFalse(paths.isEmpty());
assertEquals(1, MockNetwork.getTopoServer().getAndResetCallCount());
Expand All @@ -315,7 +315,7 @@ void newServiceWithBootstrapServer() throws IOException {
Scion.newServiceWithBootstrapServer(ToStringUtil.toAddressPort(topoAddr))) {
// destination address = 123.123.123.123 because we don´t care for getting a path
InetAddress ip123 = InetAddress.getByAddress(new byte[] {123, 123, 123, 123});
List<RequestPath> paths = ss.getPaths(iaDst, ip123, 12345);
List<Path> paths = ss.getPaths(iaDst, ip123, 12345);
assertNotNull(paths);
assertFalse(paths.isEmpty());
assertEquals(1, MockNetwork.getTopoServer().getAndResetCallCount());
Expand All @@ -336,7 +336,7 @@ void defaultService_bootstrapTopoFile_ScionProto_11() {
System.setProperty(
Constants.PROPERTY_BOOTSTRAP_TOPO_FILE, "topologies/topology-scionproto-0.11.json");
ScionService service = Scion.defaultService();
RequestPath path = service.getPaths(dstIA, dstAddress).get(0);
Path path = service.getPaths(dstIA, dstAddress).get(0);
assertNotNull(path);
assertEquals(0, MockDaemon.getAndResetCallCount()); // Daemon is not used!
} finally {
Expand Down

0 comments on commit 30a53d4

Please sign in to comment.