Commit d58791b4 authored by Benoit Lavenier's avatar Benoit Lavenier

[fix] Code refactoring, on peer beans and peer dao

parent fb2f7e32
Pipeline #4584 passed with stage
in 27 seconds
......@@ -47,15 +47,15 @@ public interface PeerDao extends EntityDao<String, Peer> {
* @param pubkeys
* @return
*/
List<Peer> getPeersByCurrencyIdAndApiAndPubkeys(String currencyId, String endpointApi, String[] pubkeys);
List<Peer> getPeersByCurrencyIdAndApiAndPubkeys(String currencyId, String endpointApi, String[] includePubkeys);
/**
* Get peers as BMA /network/peers format
* Get all UP peers
* @param currencyId
* @param pubkeys use to filter on specific pubkeys. If null, not filtering
* @return
*/
List<NetworkPeers.Peer> getBmaPeersByCurrencyId(String currencyId, String[] pubkeys);
List<Peer> getUpPeersByCurrencyId(String currencyId, String[] includePubkeys);
/**
* Get WS2p heads as BMA /network/ws2p/head format
......@@ -63,7 +63,7 @@ public interface PeerDao extends EntityDao<String, Peer> {
* @param pubkeys use to filter on specific pubkeys. If null, not filtering
* @return
*/
List<NetworkWs2pHeads.Head> getWs2pPeersByCurrencyId(String currencyId, String[] pubkeys);
List<NetworkWs2pHeads.Head> getWs2pPeersByCurrencyId(String currencyId, String[] includePubkeys);
boolean isExists(String currencyId, String peerId);
......
......@@ -116,10 +116,13 @@ public class MemoryPeerDaoImpl implements PeerDao {
}
@Override
public List<NetworkPeers.Peer> getBmaPeersByCurrencyId(String currencyId, String[] pubkeys) {
public List<Peer> getUpPeersByCurrencyId(String currencyId, String[] pubkeys) {
Preconditions.checkNotNull(currencyId);
return Peers.toBmaPeers(getPeersByCurrencyIdAndApiAndPubkeys(currencyId, null, pubkeys));
return getPeersByCurrencyIdAndApiAndPubkeys(currencyId, null, pubkeys)
.stream()
.filter(Peers::isReacheable)
.collect(Collectors.toList());
}
@Override
......
......@@ -37,11 +37,10 @@ public class NetworkPeering implements Serializable {
private String block;
private String signature;
private String status;
private String pubkey;
private String raw;
private String pubkey;
public Endpoint[] endpoints;
public String getVersion() {
......
......@@ -43,76 +43,9 @@ public class NetworkPeers implements Serializable {
return s;
}
public static class Peer implements Serializable {
public String version;
public String currency;
public String status;
public Long statusTS;
public String block;
public String signature;
public String pubkey;
public static class Peer extends NetworkPeering implements Serializable {
public Long firstDown;
public Long lastTry;
public String raw;
public NetworkPeering.Endpoint[] endpoints;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@JsonGetter("statusTS")
public Long getStatusTS() {
return statusTS;
}
@JsonSetter("statusTS")
public void setStatusTS(Long statusTS) {
this.statusTS = statusTS;
}
public String getBlock() {
return block;
}
public void setBlock(String block) {
this.block = block;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
public String getPubkey() {
return pubkey;
}
public void setPubkey(String pubkey) {
this.pubkey = pubkey;
}
@JsonGetter("first_down")
public Long getFirstDown() {
......@@ -142,22 +75,13 @@ public class NetworkPeers implements Serializable {
this.endpoints = endpoints;
}
@JsonIgnore
public String getRaw() {
return raw;
}
public void setRaw(String raw) {
this.raw = raw;
}
@Override
public String toString() {
String s = "version=" + version + "\n" +
"currency=" + currency + "\n" +
"pubkey=" + pubkey + "\n" +
"status=" + status + "\n" +
"block=" + block + "\n";
String s = "version=" + getVersion() + "\n" +
"currency=" + getCurrency() + "\n" +
"pubkey=" + getPubkey() + "\n" +
"status=" + getStatus() + "\n" +
"block=" + getBlock() + "\n";
for(NetworkPeering.Endpoint endpoint: endpoints) {
if (endpoint != null) {
s += endpoint.toString() + "\n";
......
......@@ -154,15 +154,15 @@ public class Peer implements LocalEntity<String>, Serializable {
return this;
}
public Builder setPeering(NetworkPeers.Peer remotePeer) {
public Builder setPeering(NetworkPeering remotePeering) {
this.peering = this.peering != null ? this.peering : new Peering();
this.peering.setVersion(remotePeer.getVersion());
this.peering.setSignature(remotePeer.getSignature());
this.peering.setVersion(remotePeering.getVersion());
this.peering.setSignature(remotePeering.getSignature());
// Block number+hash
if (remotePeer.getBlock() != null) {
String[] blockParts = remotePeer.getBlock().split("-");
if (remotePeering.getBlock() != null) {
String[] blockParts = remotePeering.getBlock().split("-");
if (blockParts.length == 2) {
this.peering.setBlockNumber(Integer.parseInt(blockParts[0]));
this.peering.setBlockHash(blockParts[1]);
......
......@@ -26,6 +26,7 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.duniter.core.client.model.bma.*;
import org.duniter.core.util.CollectionUtils;
import org.duniter.core.util.Preconditions;
import org.duniter.core.util.StringUtils;
import java.util.List;
......@@ -112,10 +113,6 @@ public final class Peers {
result.setSignature(null);
}
// Default values (not stored yet)
// TODO check if still used by clients
result.setStatusTS(0L);
// Compute status (=UP is at least one endpoint is UP)
String status = endpoints.stream()
.map(Peers::getStatus)
......@@ -190,4 +187,50 @@ public final class Peers {
bmaEp.setPath(ep.getPath());
return bmaEp;
}
public static Peer setPeeringAndStats(Peer peer, NetworkPeering peeringDocument) {
Preconditions.checkNotNull(peer);
Preconditions.checkNotNull(peeringDocument);
Peer.Stats stats = peer.getStats() != null ? peer.getStats() : new Peer.Stats();
Peer.Peering peering = (peer.getPeering() != null) ? peer.getPeering() : new Peer.Peering();
// Copy some fields
peer.setPubkey(peeringDocument.getPubkey());
peer.setCurrency(peeringDocument.getCurrency());
peering.setVersion(peeringDocument.getVersion());
peering.setSignature(peeringDocument.getSignature());
// Copy block infos
String blockstamp = peeringDocument.getBlock();
if (StringUtils.isNotBlank(blockstamp)) {
String[] blockParts = blockstamp.split("-");
if (blockParts.length == 2) {
int blockNumber = Integer.parseInt(blockParts[0]);
String blockHash = blockParts[1];
// Fill peering block
peering.setBlockNumber(blockNumber);
peering.setBlockHash(blockHash);
// use peering block as default stats (if empty)
if (stats.getBlockNumber() == null) {
stats.setBlockNumber(blockNumber);
stats.setBlockHash(blockHash);
}
}
}
// Update peer status UP/DOWN
if ("UP".equalsIgnoreCase(peeringDocument.getStatus())) {
stats.setStatus(Peer.PeerStatus.UP);
stats.setLastUpTime((long)Math.round(System.currentTimeMillis() / 1000));
}
else {
stats.setStatus(Peer.PeerStatus.DOWN);
}
return peer;
}
}
......@@ -37,7 +37,6 @@ import org.duniter.core.client.config.Configuration;
import org.duniter.core.client.model.bma.*;
import org.duniter.core.client.model.bma.jackson.JacksonUtils;
import org.duniter.core.client.model.local.Peer;
import org.duniter.core.client.service.ServiceLocator;
import org.duniter.core.exception.TechnicalException;
import org.duniter.core.util.Preconditions;
import org.duniter.core.util.websocket.WebsocketClientEndpoint;
......@@ -135,7 +134,7 @@ public class NetworkRemoteServiceImpl extends BaseRemoteServiceImpl implements N
NetworkPeers remoteResult = httpService.executeRequest(peer, URL_PEERS, NetworkPeers.class, config.getNetworkLargerTimeout());
for (NetworkPeers.Peer remotePeer: remoteResult.peers) {
boolean match = (status == null || status.equalsIgnoreCase(remotePeer.status))
boolean match = (status == null || status.equalsIgnoreCase(remotePeer.getStatus()))
&& (currentBlockNumber == null || currentBlockNumber.equals(parseBlockNumber(remotePeer)))
&& (currentBlockHash == null || currentBlockHash.equals(parseBlockHash(remotePeer)));
......@@ -244,15 +243,15 @@ public class NetworkRemoteServiceImpl extends BaseRemoteServiceImpl implements N
protected Integer parseBlockNumber(NetworkPeers.Peer remotePeer) {
Preconditions.checkNotNull(remotePeer);
if (remotePeer.block == null) {
if (remotePeer.getBlock() == null) {
return null;
}
int index = remotePeer.block.indexOf("-");
int index = remotePeer.getBlock().indexOf("-");
if (index == -1) {
return null;
}
String str = remotePeer.block.substring(0, index);
String str = remotePeer.getBlock().substring(0, index);
try {
return Integer.parseInt(str);
} catch(NumberFormatException e) {
......@@ -263,15 +262,15 @@ public class NetworkRemoteServiceImpl extends BaseRemoteServiceImpl implements N
protected String parseBlockHash(NetworkPeers.Peer remotePeer) {
Preconditions.checkNotNull(remotePeer);
if (remotePeer.block == null) {
if (remotePeer.getBlock()== null) {
return null;
}
int index = remotePeer.block.indexOf("-");
int index = remotePeer.getBlock().indexOf("-");
if (index == -1) {
return null;
}
String hash = remotePeer.block.substring(index+1);
String hash = remotePeer.getBlock().substring(index+1);
return hash;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment