From 1f9f949a8c145996db3d42a62e0a13beb6c09a38 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Fri, 24 Dec 2021 15:25:58 +0000 Subject: [PATCH] Added GET /peers/summary API which returns counts of the number of inbound and outbound connections that currently exist. --- .../org/qortal/api/model/PeersSummary.java | 15 +++++++ .../qortal/api/resource/PeersResource.java | 42 ++++++++++++++++--- 2 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/qortal/api/model/PeersSummary.java diff --git a/src/main/java/org/qortal/api/model/PeersSummary.java b/src/main/java/org/qortal/api/model/PeersSummary.java new file mode 100644 index 00000000..28788550 --- /dev/null +++ b/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() { + } + +} diff --git a/src/main/java/org/qortal/api/resource/PeersResource.java b/src/main/java/org/qortal/api/resource/PeersResource.java index 244a1569..77c11b99 100644 --- a/src/main/java/org/qortal/api/resource/PeersResource.java +++ b/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.MediaType; -import org.qortal.api.ApiError; -import org.qortal.api.ApiErrors; -import org.qortal.api.ApiException; -import org.qortal.api.ApiExceptionFactory; -import org.qortal.api.Security; +import org.qortal.api.*; import org.qortal.api.model.ConnectedPeer; +import org.qortal.api.model.PeersSummary; import org.qortal.controller.Controller; import org.qortal.controller.Synchronizer; 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 connectedPeers = Network.getInstance().getConnectedPeers().stream().collect(Collectors.toList()); + for (Peer peer : connectedPeers) { + if (peer.isOutbound()) { + peersSummary.inboundConnections++; + } + else { + peersSummary.outboundConnections++; + } + } + return peersSummary; + } + }