Commit c23dd490 authored by Benoit Lavenier's avatar Benoit Lavenier

[fix] Fix endpoint parsing. And add GCHANGE_API

parent bbbf68a0
......@@ -32,5 +32,7 @@ public enum EndpointApi {
ES_USER_API,
ES_SUBSCRIPTION_API,
MONIT_API,
UNDEFINED
UNDEFINED,
// TODO: remove this ?
GCHANGE_API
}
......@@ -34,7 +34,7 @@ import java.util.regex.Pattern;
*/
public class Endpoints {
public static final String EP_END_REGEXP = "(?:[ ]+([a-z0-9-_]+[.][a-z0-9-_.]*))?(?:[ ]+([0-9.]+))?(?:[ ]+([0-9a-f:]+))?(?:[ ]+([0-9]+))$";
public static final String EP_END_REGEXP = "(?:[ ]+([a-z0-9-_]+[.][a-z0-9-_.]*))?(?:[ ]+([0-9.]+))?(?:[ ]+([0-9a-f:]+))?(?:[ ]+([0-9]+))(?:[ ]+(/[^/]+))?$";
public static final String BMA_API_REGEXP = "^BASIC_MERKLED_API" + EP_END_REGEXP;
public static final String BMAS_API_REGEXP = "^BMAS" + EP_END_REGEXP;
public static final String WS2P_API_REGEXP = "^WS2P[ ]+([a-z0-9]+)[ ]+" + EP_END_REGEXP;
......@@ -104,8 +104,10 @@ public class Endpoints {
endpoint.ipv4 = word;
} else if (InetAddressUtils.isIPv6Address(word)) {
endpoint.ipv6 = word;
} else if (i == matcher.groupCount() && word.matches("\\d+")){
} else if (i == matcher.groupCount() || (i == matcher.groupCount() -1) && word.matches("\\d+")){
endpoint.port = Integer.parseInt(word);
} else if (word.startsWith("/")) {
endpoint.path = word;
} else {
endpoint.dns = word;
}
......
......@@ -23,6 +23,8 @@ package org.duniter.core.client.model.bma;
*/
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.duniter.core.util.CollectionUtils;
import org.duniter.core.util.StringUtils;
import java.io.Serializable;
......@@ -108,17 +110,28 @@ public class NetworkPeering implements Serializable {
}
public String toString() {
String s = "version=" + version + "\n" +
"currency=" + currency + "\n" +
"pubkey=" + pubkey + "\n" +
"signature=" + signature + "\n" +
"status=" + status + "\n" +
"block=" + block + "\n";
for(Endpoint endpoint : endpoints) {
s += endpoint.toString() + "\n";
StringBuilder sb = new StringBuilder();
// Version
sb.append("Version: ").append(Protocol.VERSION).append("\n");
// Type
sb.append("Type: ").append(Protocol.TYPE_PEER).append("\n");
// Type
sb.append("Currency: ").append(currency).append("\n");
// PublicKey
sb.append("PublicKey: ").append(pubkey).append("\n");
// Block
sb.append("Block: ").append(block).append("\n");
// Endpoints
sb.append("Endpoints:\n");
if (CollectionUtils.isNotEmpty(endpoints)) {
for (Endpoint ep: endpoints) {
sb.append(ep.toString()).append("\n");
}
}
return s;
if (StringUtils.isNotBlank(signature)) {
sb.append(signature).append("\n");
}
return sb.toString();
}
public static class Endpoint implements Serializable {
......@@ -128,6 +141,7 @@ public class NetworkPeering implements Serializable {
public String ipv6;
public Integer port;
public String id;
public String path;
public EndpointApi getApi() {
return api;
......@@ -177,15 +191,46 @@ public class NetworkPeering implements Serializable {
this.id = id;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
@Override
public String toString() {
String s = "api=" + api.name() + "\n" +
(id != null ? ("id=" + id + "\n") : "" ) +
"dns=" + dns + "\n" +
"ipv4=" + ipv4 + "\n" +
"ipv6=" + ipv6 + "\n" +
"port=" + port + "\n";
return s;
StringBuilder sb = new StringBuilder();
// API
sb.append(api.name());
// Id (use for WS2P)
if (StringUtils.isNotBlank(id)) {
sb.append(" ").append(id);
}
// DNS
if (StringUtils.isNotBlank(dns)) {
sb.append(" ").append(dns);
}
// IPv4
if (StringUtils.isNotBlank(ipv4)) {
sb.append(" ").append(ipv4);
}
// IPv6
if (StringUtils.isNotBlank(ipv6)) {
sb.append(" ").append(ipv6);
}
// Port
if (port != null) {
sb.append(" ").append(port);
}
// path
if (StringUtils.isNotBlank(path)) {
sb.append(" ").append(path);
}
return sb.toString();
}
}
......
......@@ -37,5 +37,7 @@ public interface Protocol {
String TYPE_TRANSACTION = "Transaction";
String TYPE_PEER = "Peer";
String BMA_API = "BASIC_MERKLED_API";
}
......@@ -49,4 +49,6 @@ public interface NetworkRemoteService extends Service {
WebsocketClientEndpoint addPeerListener(String currencyId, WebsocketClientEndpoint.MessageListener listener, boolean autoReconnect);
WebsocketClientEndpoint addPeerListener(Peer peer, WebsocketClientEndpoint.MessageListener listener, boolean autoReconnect);
String postPeering(Peer peer, NetworkPeering peering);
}
......@@ -23,17 +23,23 @@ package org.duniter.core.client.service.bma;
*/
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.duniter.core.client.model.bma.EndpointApi;
import org.duniter.core.client.model.bma.NetworkPeering;
import org.duniter.core.client.model.bma.NetworkPeers;
import org.duniter.core.client.model.bma.jackson.JacksonUtils;
import org.duniter.core.client.model.local.Peer;
import org.duniter.core.client.model.local.Wallet;
import org.duniter.core.exception.TechnicalException;
import org.duniter.core.util.Preconditions;
import org.duniter.core.util.StringUtils;
......@@ -168,6 +174,38 @@ public class NetworkRemoteServiceImpl extends BaseRemoteServiceImpl implements N
return wsClientEndPoint;
}
@Override
public String postPeering(Peer peer, NetworkPeering peering) {
Preconditions.checkNotNull(peer);
Preconditions.checkNotNull(peering);
// http post /tx/process
HttpPost httpPost = new HttpPost(getPath(peer, URL_PEERING_PEERS));
String document = peering.toString();
if (log.isDebugEnabled()) {
log.debug(String.format(
"Will send peering document: \n------\n%s------",
document));
}
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("peer", document));
try {
httpPost.setEntity(new UrlEncodedFormEntity(urlParameters));
} catch (UnsupportedEncodingException e) {
throw new TechnicalException(e);
}
String result = executeRequest(httpPost, String.class);
if (log.isDebugEnabled()) {
log.debug("Received from " + URL_PEERING_PEERS + " (POST): " + result);
}
return result;
}
/* -- Internal methods -- */
protected Integer parseBlockNumber(NetworkPeers.Peer remotePeer) {
......
package org.duniter.core.client.model.bma;
import org.junit.Assert;
import org.junit.Test;
public class EndpointsTest {
@Test
public void parse() throws Exception {
NetworkPeering.Endpoint ep = Endpoints.parse("GCHANGE_API data.gchange.fr 443");
Assert.assertNotNull(ep);
Assert.assertEquals(ep.api, EndpointApi.GCHANGE_API);
}
}
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