Browse Source

Added GET /peers/summary API which returns counts of the number of inbound and outbound connections that currently exist.

qdn
CalDescent 3 years ago
parent
commit
1f9f949a8c
  1. 15
      src/main/java/org/qortal/api/model/PeersSummary.java
  2. 42
      src/main/java/org/qortal/api/resource/PeersResource.java

15
src/main/java/org/qortal/api/model/PeersSummary.java

@ -0,0 +1,15 @@
package org.qortal.api.model;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
@XmlAccessorType(XmlAccessType.FIELD)
public class PeersSummary {
public int inboundConnections;
public int outboundConnections;
public PeersSummary() {
}
}

42
src/main/java/org/qortal/api/resource/PeersResource.java

@ -23,12 +23,9 @@ import javax.ws.rs.Path;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import org.qortal.api.ApiError; import org.qortal.api.*;
import org.qortal.api.ApiErrors;
import org.qortal.api.ApiException;
import org.qortal.api.ApiExceptionFactory;
import org.qortal.api.Security;
import org.qortal.api.model.ConnectedPeer; import org.qortal.api.model.ConnectedPeer;
import org.qortal.api.model.PeersSummary;
import org.qortal.controller.Controller; import org.qortal.controller.Controller;
import org.qortal.controller.Synchronizer; import org.qortal.controller.Synchronizer;
import org.qortal.controller.Synchronizer.SynchronizationResult; import org.qortal.controller.Synchronizer.SynchronizationResult;
@ -338,4 +335,39 @@ public class PeersResource {
} }
} }
@GET
@Path("/summary")
@Operation(
summary = "Returns total inbound and outbound connections for connected peers",
responses = {
@ApiResponse(
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
array = @ArraySchema(
schema = @Schema(
implementation = PeersSummary.class
)
)
)
)
}
)
@SecurityRequirement(name = "apiKey")
public PeersSummary peersSummary() {
Security.checkApiCallAllowed(request);
PeersSummary peersSummary = new PeersSummary();
List<Peer> connectedPeers = Network.getInstance().getConnectedPeers().stream().collect(Collectors.toList());
for (Peer peer : connectedPeers) {
if (peer.isOutbound()) {
peersSummary.inboundConnections++;
}
else {
peersSummary.outboundConnections++;
}
}
return peersSummary;
}
} }

Loading…
Cancel
Save