/** * Start new client protocol session. It doesn't load network configuration. Only creates client protocol session with given node * @param rootUrlString node url * @param clientPrivateKey client private key * @param nodePublicKey node key * @param session set to null or to the reconstructed instance * @throws IOException */ public Client(String rootUrlString, PrivateKey clientPrivateKey, PublicKey nodePublicKey, BasicHttpClientSession session) throws IOException { httpClient = new BasicHttpClient(rootUrlString); this.clientPrivateKey = clientPrivateKey; this.nodePublicKey = nodePublicKey; httpClient.start(clientPrivateKey, nodePublicKey, session); }
/** * Execude custom command on the node * @param name name of the command * @param params parameters of the command * @return execution result */ public Binder command(String name, Object... params) throws IOException { return httpClient.command(name, params); }
/** * Get number of node cliet connection is established with * @return number of node */ public int getNodeNumber() { return httpClient.getNodeNumber(); } /**
public void restart() throws IOException { synchronized (this) { System.err.println("Restarting connection to $"+this.nodeNumber+": "+this.getUrl()); PrivateKey privateKey = session.getPrivateKey(); PublicKey nodePublicKey = session.getNodePublicKey(); session = null; start(privateKey, nodePublicKey, null); } }
@Test public void testError() throws Exception { PrivateKey nodeKey = TestKeys.privateKey(1); PrivateKey clientKey = TestKeys.privateKey(2); BasicHttpServer s = new BasicHttpServer(nodeKey, 15600, 32, log); BasicHttpClient c = new BasicHttpClient("http://localhost:15600"); c.start(clientKey, nodeKey.getPublicKey(), null); assertThrows(CommandFailedException.class, ()->c.command("test_error")); }
private static void doGetURate() throws IOException { BasicHttpClient httpClient = new BasicHttpClient(URS_ROOT_URL); BasicHttpClient.Answer answer = httpClient.commonRequest("uutn/info"); System.out.println("Current U per UTN rate is " + answer.data.getBinder("rates").getString("U_UTN")); System.out.println("Minimum U to reserve " + answer.data.getBinder("limits").getBinder("U").getString("min")); System.out.println("Maximum U to reserve " + answer.data.getBinder("limits").getBinder("U").getString("max")); finish(); }
Binder result = command("hello"); Answer a = requestOrThrow("connect", "client_key", privateKey.getPublicKey().pack()); )); a = requestOrThrow("get_token", "signature", privateKey.sign(data, HashType.SHA512), "data", data, Binder result = command("hello");
/** * Start http client (used with constructor passing delayedStart = true) * @param session set to null or to the reconstructed instance * @throws IOException */ public void start(BasicHttpClientSession session) throws IOException { httpClient.start(clientPrivateKey, nodePublicKey, session); }
public Answer commonRequest(String path, Object... keysValues) throws IOException { JsonObject json = new JsonObject(); for (int i = 0; i < keysValues.length; i += 2) { if(keysValues[i + 1] == null) { json.add(keysValues[i].toString(), Json.NULL); } else if(keysValues[i + 1] instanceof Integer) { json.add(keysValues[i].toString(), (Integer) keysValues[i + 1]); } else if(keysValues[i + 1] instanceof Double) { json.add(keysValues[i].toString(), (Double) keysValues[i + 1]); } else if(keysValues[i + 1] instanceof Float) { json.add(keysValues[i].toString(), (Float) keysValues[i + 1]); } else if(keysValues[i + 1] instanceof Boolean) { json.add(keysValues[i].toString(), (Boolean) keysValues[i + 1]); } else if(keysValues[i + 1] instanceof Long) { json.add(keysValues[i].toString(), (Long) keysValues[i + 1]); } else { json.add(keysValues[i].toString(), keysValues[i + 1].toString()); } } return commonRequest(path, json); }
public BasicHttpClient.Answer request(String name, Object... params) throws IOException { return httpClient.request(name, params); }
/** * Get url of the node client is connected to * * @return url of the node */ public String getUrl() { return httpClient.getUrl(); }
/** * Get session of http client * * @return session */ public BasicHttpClientSession getSession() throws IllegalStateException { return httpClient.getSession(); }
public Answer commonRequest(String path, JsonObject params) throws IOException { synchronized (this) { String charset = "UTF-8"; URLConnection connection = new URL(url + "/" + path).openConnection(); connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(CONNECTION_READ_TIMEOUT); connection.setRequestProperty("User-Agent", "Universa JAVA API Client"); if(!params.isEmpty()) { connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); try ( OutputStream output = connection.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true); ) { String json = params.toString(); writer.append(json); } } HttpURLConnection httpConnection = (HttpURLConnection) connection; int responseCode = httpConnection.getResponseCode(); byte[] answer = Do.read(httpConnection.getInputStream()); httpConnection.disconnect(); JsonValue json = Json.parse(new String(answer)); return new Answer(responseCode, Binder.of("response",ofJson(json))); } }
@Test public void handshakeAndSecureCommand() throws Exception { PrivateKey nodeKey = TestKeys.privateKey(1); PrivateKey clientKey = TestKeys.privateKey(2); BasicHttpServer s = new BasicHttpServer(nodeKey, 15600, 32, log); BasicHttpClient c = new BasicHttpClient("http://localhost:15600"); c.start(clientKey, nodeKey.getPublicKey(), null); Binder res = c.command("sping"); assertEquals("spong", res.getStringOrThrow("sping")); s.addSecureEndpoint("getSessionInfo", (params,session)-> { // System.out.println("in sec, "+session); // System.out.println("\t "+session.getPublicKey()); return Binder.of("publicKey", session.getPublicKey().info().toString()); }); res = c.command("getSessionInfo"); s.shutdown(); }
System.out.println("U balance is less than threshold of " + minBalance + ". Trying to get more Us"); BasicHttpClient httpClient = new BasicHttpClient(URS_ROOT_URL); BasicHttpClient.Answer answer = httpClient.commonRequest("uutn/info");
public Answer request(String path, Object... keysValues) throws IOException { return request(path, Binder.fromKeysValues(keysValues)); }
@Override public String toString() { return "HTTPClient<" +getUrl() + ">"; }
/** * Get session of http client connected to node with given number * * @param i number of the node to return session with * @return session */ public BasicHttpClientSession getSession(int i) throws IllegalStateException, IOException { return getClient(i).httpClient.getSession(); }
public static Object ofJson(JsonValue json) { if(json.isObject()) { Iterator<JsonObject.Member> iterator = json.asObject().iterator(); Binder b = new Binder(); while (iterator.hasNext()) { JsonObject.Member m = iterator.next(); b.put(m.getName(),ofJson(m.getValue())); } return b; } else if(json.isBoolean()) { return json.asBoolean(); } else if(json.isNumber()) { return json.asDouble(); } else if(json.isString()) { return json.asString(); } else if(json.isArray()) { JsonArray ja = json.asArray(); List<Object> list = new ArrayList<>(); for(int i = 0; i < ja.size();i++) { list.add(ofJson(ja.get(i))); } return list; } return null; }
public boolean ping() throws IOException { httpClient.command("sping"); return true; }