Skip to content

Commit

Permalink
Migrated Admin interface to REST
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadym authored and Vadym committed Jan 8, 2023
1 parent 3a6c9e5 commit 2e1d3fc
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 342 deletions.
2 changes: 1 addition & 1 deletion WebContent/WEB-INF/classes/map.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ KEY_API_HOST=9d8e2be77578d93a89c32a07ce14bad6a7527b9aebe138f767011683290c019c

ADMIN_API_HOST=localhost

ADMIN_API_PORT=9001
ADMIN_API_PORT=19019

#crawling period in sec
period=60
Expand Down
237 changes: 85 additions & 152 deletions src/main/java/org/riv/crawler/NetworkDHTCrawler.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package org.riv.crawler;

import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Arrays;

import java.util.Iterator;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.concurrent.Callable;
Expand All @@ -26,16 +25,14 @@
import org.riv.NodeData2JSGraphConverter;
import org.riv.node.Link;
import org.riv.node.NodeDataPair;
import org.rivmesh.api.ApiNodeInfoResponse;
import org.rivmesh.api.ApiPeersResponse;
import org.rivmesh.api.ApiRequest;
import org.rivmesh.api.ApiResponse;
import org.rivmesh.api.ApiSelfResponse;
import org.riv.node.RemoteNodeInfoReponse;
import org.riv.node.RemotePeerResponse;
import org.riv.node.RemoteSelfReponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

public class NetworkDHTCrawler {

Expand Down Expand Up @@ -93,63 +90,43 @@ public String call() throws Exception {
nodes.put(targetNodeKey, new NodeDataPair(null, null));
}

Entry<String, Map<String, List<String>>> peer = getPeerInfo(targetNodeKey);
Entry<String, Map<String, Object>> nodeInfoReponse = getNodeInfo(targetNodeKey);
Entry<String, Map<String, Object>> selfNodeInfo = getSelf(targetNodeKey);
Map<String, RemotePeerResponse> peer = getRemotePeers(targetNodeKey);
Map<String, RemoteNodeInfoReponse> nodeInfoReponse = getRemoteNodeInfo(targetNodeKey);
Map<String, RemoteSelfReponse> selfNodeInfo = getRemoteSelf(targetNodeKey);

String ip = null;
if(peer!=null) {
ip = peer.getKey();
} else {
if(selfNodeInfo!=null) {
ip = selfNodeInfo.getKey();
} else {
if(nodeInfoReponse!=null) {
ip = nodeInfoReponse.getKey();
}
}
}
String ip = peer.entrySet().iterator().next().getKey();
NodeDataPair ndp = nodes.get(targetNodeKey);//new NodeDataPair(ip, targetNodeKey);
ndp.setIp(ip);
ndp.setKey(targetNodeKey);

if (nodeInfoReponse != null) {

Object buildarch = nodeInfoReponse.getValue().get("buildarch");
Object buildplatform = nodeInfoReponse.getValue().get("buildplatform");
Object buildversion = nodeInfoReponse.getValue().get("buildversion");
Object name = nodeInfoReponse.getValue().get("name");
Object icon = nodeInfoReponse.getValue().get("icon");
if (buildarch != null) {
ndp.setArch(buildarch.toString());
}
if (buildplatform != null) {
ndp.setPlatform(buildplatform.toString());
}
if (buildversion != null) {
ndp.setVersion(buildversion.toString());
}
if (name != null) {
ndp.setName(name.toString());
}
if (icon != null) {
ndp.setIcon(icon.toString());
}
String buildarch = nodeInfoReponse.entrySet().iterator().next().getValue().getBuildarch();
String buildplatform = nodeInfoReponse.entrySet().iterator().next().getValue().getBuildplatform();
String buildversion = nodeInfoReponse.entrySet().iterator().next().getValue().getBuildversion();
String name = nodeInfoReponse.entrySet().iterator().next().getValue().getName();
String icon = nodeInfoReponse.entrySet().iterator().next().getValue().getIcon();
if (buildarch != null) {
ndp.setArch(buildarch);
}

if(selfNodeInfo!=null) {
String coords = selfNodeInfo.getValue().get("coords").toString();
String coordsString = coords.substring(1, coords.length()-1);
ndp.setCoords(coordsString);
if (buildplatform != null) {
ndp.setPlatform(buildplatform);
}

//nodes.put(targetNodeKey, ndp);
if(peer==null) {
return null;
if (buildversion != null) {
ndp.setVersion(buildversion);
}
if (name != null) {
ndp.setName(name);
}
if (icon != null) {
ndp.setIcon(icon);
}
List<String> peerKeys = peer.getValue().get("keys");

String coords = selfNodeInfo.entrySet().iterator().next().getValue().getCoords();
String coordsString = coords.substring(1, coords.length()-1);
ndp.setCoords(coordsString);

List<String> peerKeys = peer.values().iterator().next().getKeys();
if(peerKeys==null) {
System.err.println(peer.getKey()+" returned no peers");
System.err.println(peer.keySet().iterator().next()+" returned no peers");
return null;
}
ConcurrentLinkedQueue<Future<String>> tasks = new ConcurrentLinkedQueue<Future<String>>();
Expand Down Expand Up @@ -177,136 +154,92 @@ public String call() throws Exception {
return future;
}

private Entry<String, Map<String, Object>> getSelf(String targetNodeKey) throws IOException {
String selfInfo = new ApiRequest().getSelf(targetNodeKey).serialize();
String selfInfoObject = socketRequest(selfInfo);
private Map<String, RemoteSelfReponse> getRemoteSelf(String targetNodeKey) throws IOException {
Type collectionType = new TypeToken<Map<String, RemoteSelfReponse>>(){}.getType();

ApiSelfResponse selfInfoReponse = null;
try {
selfInfoReponse = gson.fromJson(selfInfoObject, ApiSelfResponse.class);
} catch (JsonSyntaxException e) {
e.printStackTrace();
System.err.println("error response:\n" + selfInfoObject);
Map<String, RemoteSelfReponse> apiResponse = (Map<String, RemoteSelfReponse>) socketRequest("api/remote/self/"+targetNodeKey, collectionType);

if (apiResponse == null) {
return null;
}
if(selfInfoReponse.getStatus().equals("error")){
System.err.println(selfInfoReponse.getError());
if (apiResponse.values().isEmpty()) {
log.info("incorrect response: " + gson.toJson(apiResponse));
return null;
}
Entry<String, Map<String, Object>> selfNodeInfo = null;
if (selfInfoReponse != null && selfInfoReponse.getResponse() != null && !selfInfoReponse.getResponse().isEmpty()) {
selfNodeInfo = selfInfoReponse.getResponse().entrySet().iterator().next();
}
return selfNodeInfo;
return apiResponse;
}

private Entry<String, Map<String, Object>> getNodeInfo(String targetNodeKey) throws IOException {
String nodeInfo = new ApiRequest().getNodeInfo(targetNodeKey).serialize();
String nodeInfoObject = socketRequest(nodeInfo);
private Map<String, RemoteNodeInfoReponse> getRemoteNodeInfo(String targetNodeKey) throws IOException {
Type collectionType = new TypeToken<Map<String, RemoteNodeInfoReponse>>(){}.getType();

ApiNodeInfoResponse nodeInfoReponse = null;
try {
nodeInfoReponse = gson.fromJson(nodeInfoObject, ApiNodeInfoResponse.class);
} catch (JsonSyntaxException e) {
e.printStackTrace();
System.err.println("error response:\n" + nodeInfoObject);
Map<String, RemoteNodeInfoReponse> apiResponse = (Map<String, RemoteNodeInfoReponse>) socketRequest("api/remote/nodeinfo/"+targetNodeKey, collectionType);

if (apiResponse == null) {
return null;
}
if(nodeInfoReponse.getStatus().equals("error")){
System.err.println(nodeInfoReponse.getError());
if (apiResponse.values().isEmpty()) {
log.info("incorrect response: " + gson.toJson(apiResponse));
return null;
}
if (nodeInfoReponse != null && nodeInfoReponse.getResponse()!=null && !nodeInfoReponse.getResponse().isEmpty()) {
return nodeInfoReponse.getResponse().entrySet().iterator().next();
}
return null;
return apiResponse;
}

private Entry<String, Map<String, List<String>>> getPeerInfo(String targetNodeKey) throws IOException {
private Map<String, RemotePeerResponse> getRemotePeers(String targetNodeKey) throws IOException {

final String json = new ApiRequest().getPeers(targetNodeKey).serialize();
Type collectionType = new TypeToken<Map<String, RemotePeerResponse>>(){}.getType();

String object = socketRequest(json);
if (object == null) {
return null;
}
ApiResponse apiResponse = null;
try {
apiResponse = gson.fromJson(object, ApiResponse.class);
} catch (JsonSyntaxException e) {
e.printStackTrace();
System.err.println("error response:\n" + object);
return null;
}
Map<String, RemotePeerResponse> apiResponse = (Map<String, RemotePeerResponse>) socketRequest("api/remote/peers/"+targetNodeKey, collectionType);
if (apiResponse == null) {
System.out.println("No response received");
}

if(apiResponse.getStatus().equals("error")){
System.out.println("error");
return null;
}
ApiPeersResponse apiPeerResponse = gson.fromJson(object, ApiPeersResponse.class);
if (apiPeerResponse.getResponse().entrySet().isEmpty()) {
log.info("incorrect response: " + gson.toJson(apiPeerResponse));
if (apiResponse.values().isEmpty()) {
log.info("incorrect response: " + gson.toJson(apiResponse));
return null;
}
Iterator<Entry<String, Map<String, List<String>>>> it = apiPeerResponse.getResponse().entrySet().iterator();
Entry<String, Map<String, List<String>>> peer = it.next();
return peer;
return apiResponse;
}

private String socketRequest(String json) throws IOException {
String response = null;
byte[] cbuf = new byte[1024*64];
Socket clientSocket = null;
DataOutputStream os = null;
private Object socketRequest(String uri, Type collectionType) throws IOException {
String result = "";
InputStream is = null;
StringBuilder sb = new StringBuilder();
HttpURLConnection con = null;
try {
clientSocket = new Socket(ADMIN_API_HOST, ADMIN_API_PORT);
os = new DataOutputStream(clientSocket.getOutputStream());
os.writeBytes(json);
//System.out.println("Total nodes:" + NetworkDHTCrawler.nodes.size());
int i = 0;
is = clientSocket.getInputStream();
i = is.read(cbuf);
sb.append(new String(Arrays.copyOf(cbuf, i)));
response = sb.toString();
//System.out.println("Request:\n"+json+"\n"+"Response:\n"+response);
boolean exception=false;
do {
try {
gson.fromJson(response, ApiResponse.class);
} catch (JsonSyntaxException e) {
exception = true;
e.printStackTrace();
i = is.read(cbuf);
sb.append(new String(Arrays.copyOf(cbuf, i)));
response = sb.toString();
continue;
}
exception = false;
} while(exception);
URL url = new URL("http://"+ADMIN_API_HOST+":"+ADMIN_API_PORT+"/"+uri);
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setDoOutput(true);

is = con.getInputStream();

BufferedReader in = new BufferedReader(
new InputStreamReader(is));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
result = content.toString();
return gson.fromJson(result, collectionType);

} catch (java.net.ConnectException e) {
e.printStackTrace();
System.out.println(result);
} catch (java.net.SocketException e) {
e.printStackTrace();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
System.out.println(result);
} finally {
if(os!=null) {
os.close();
}
if(is!=null) {
is.close();
}
if(clientSocket!=null) {
clientSocket.close();
if(con!=null) {
con.disconnect();
}
}

return response;
return null;
}

public static void run(String dataPath)
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/org/riv/node/NodesResponse.java

This file was deleted.

Loading

0 comments on commit 2e1d3fc

Please sign in to comment.