Load servers from ecosystems.json or command line
This commit is contained in:
@ -16,16 +16,23 @@ package nearenough.examples;
|
||||
|
||||
import static nearenough.util.BytesUtil.hexToBytes;
|
||||
|
||||
import net.i2p.data.Base64;
|
||||
import net.i2p.data.DataHelper;
|
||||
import net.i2p.util.RandomSource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.StandardProtocolFamily;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.DatagramChannel;
|
||||
import java.time.Instant;
|
||||
import java.util.Collection;
|
||||
|
||||
import net.i2p.data.Base64;
|
||||
import org.json.simple.Jsoner;
|
||||
import org.json.simple.JsonObject;
|
||||
|
||||
import nearenough.client.RoughtimeClient;
|
||||
import nearenough.protocol.RtMessage;
|
||||
@ -55,8 +62,42 @@ public final class NioClient {
|
||||
);
|
||||
|
||||
@SuppressWarnings("Duplicates")
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
InetSocketAddress addr = new InetSocketAddress(INT08H_SERVER_HOST, INT08H_SERVER_PORT);
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length == 3) {
|
||||
test(args[0], Integer.parseInt(args[1]), Base64.decode(args[2], true));
|
||||
return;
|
||||
}
|
||||
InputStream is = NioClient.class.getResourceAsStream("/net/i2p/router/util/resources/ecosystem.json");
|
||||
if (is == null)
|
||||
throw new RuntimeException("ecosystem.json resource not found");
|
||||
Reader r = new InputStreamReader(is, "ISO-8859-1");
|
||||
JsonObject map = (JsonObject) Jsoner.deserialize(r);
|
||||
r.close();
|
||||
Collection servers = map.getCollection("servers");
|
||||
for (Object server : servers) {
|
||||
JsonObject s = (JsonObject) server;
|
||||
String name = s.getString("name");
|
||||
String skey = s.getString("publicKey");
|
||||
byte[] key = Base64.decode(skey, true);
|
||||
Collection addresses = s.getCollection("addresses");
|
||||
String addr = null;
|
||||
for (Object a : addresses) {
|
||||
addr = ((JsonObject) a).getString("address");
|
||||
break;
|
||||
}
|
||||
String[] split = DataHelper.split(addr, ":", 2);
|
||||
String host = split[0];
|
||||
int port = Integer.parseInt(split[1]);
|
||||
System.out.println("Testing " + host);
|
||||
System.out.println("---------------------");
|
||||
test(host, port, key);
|
||||
System.out.println("---------------------");
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private static void test(String host, int port, byte[] key) throws IOException, InterruptedException {
|
||||
InetSocketAddress addr = new InetSocketAddress(host, port);
|
||||
System.out.printf("Sending request to %s\n", addr);
|
||||
|
||||
// Nonblocking NIO UDP channel for the remote Roughtime server
|
||||
@ -64,7 +105,7 @@ public final class NioClient {
|
||||
channel.configureBlocking(false);
|
||||
|
||||
// Create a new RoughtimeClient instance
|
||||
RoughtimeClient client = new RoughtimeClient(INT08H_SERVER_PUBKEY, RandomSource.getInstance());
|
||||
RoughtimeClient client = new RoughtimeClient(key, RandomSource.getInstance());
|
||||
|
||||
// Create a request message
|
||||
RtMessage request = client.createRequest();
|
||||
@ -137,7 +178,5 @@ public final class NioClient {
|
||||
System.out.println("No response from " + addr);
|
||||
}
|
||||
}
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user