/** * 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); }
public boolean ping() throws IOException { httpClient.command("sping"); return true; }
/** * Execute command over the authenticated and encrypted connection. See {@link #command(String, Binder)} for more. * @param name command name * @param keysValues keys (strings) and values of the command arguments. Values can be anything {@link Boss} protocol * and registered adapters support * @return command result * @throws IOException if the command can't be executed (also if remote returns an error) */ public Binder command(String name, Object... keysValues) throws IOException { return command(name, Binder.fromKeysValues(keysValues)); }
/** * Get extended statistics of the node including by-day payments in "U". Accessible to node owners (with node {@link PrivateKey} as session key) and network admins * @return dictionary containing uptime, ledger size, and number of contracts approved for a minute, hour and since restart. it also contains by-day payments information * @param showPaymentsDays the number of days to provide payments volume for * @throws ClientError */ public Binder getStats(Integer showPaymentsDays) throws ClientError { return protect(() -> httpClient.command("getStats","showDays",showPaymentsDays)); }
/** * Get current network rate for operating FOLLOWER1 contracts * @return {@link Binder} containing origins-days per U rate and callback price in U * @throws ClientError */ public Binder followerGetRate() throws ClientError { return protect(() -> { Binder result = httpClient.command("followerGetRate"); return result; }); }
/** * Look for the name assosiated with a given address * @param address to look for * @return {@link Binder} containing name, description and url associated with address or null * @throws ClientError */ public Binder queryNameRecord(String address) throws ClientError { return protect(() -> { Binder result = httpClient.command("queryNameRecord", "address", address); return result; }); }
/** * Ping remote side to ensure it is connected * * @return true is remote side answers properly. false generally meant we have to reconnect. */ public boolean ping() { try { return command("sping").getStringOrThrow("sping").equals("spong"); } catch (Exception e) { e.printStackTrace(); return false; } }
/** * Look for the name assosiated with a given origin * @param origin to look for * @return {@link Binder} containing name, description and url associated with origin or null * @throws ClientError */ public Binder queryNameRecord(HashId origin) throws ClientError { return protect(() -> { Binder result = httpClient.command("queryNameRecord", "origin", origin.getDigest()); return result; }); }
public ItemResult getExtendedState(HashId itemId, int nodeNumber) throws ClientError { return protect(() -> { // for (int i = 0; i < nodes.size(); i++) { // System.out.println("checking node " + i); // ItemResult r = getClient(i).command("getState", // "itemId", itemId).getOrThrow("itemResult"); // System.out.println(">> " + r); // } return (ItemResult) httpClient.command("getState", "itemId", itemId).getOrThrow("itemResult"); }); }
/** * Force synchronization with the rest of the network of given item by its id. May require special client key / network configuration * @param itemId to synchronize * @return known {@link ItemState} before synchronization. Query the state later to get it synchronized * @throws ClientError */ public ItemResult resyncItem(HashId itemId) throws ClientError { return protect(() -> { Binder result = httpClient.command("resyncItem", "itemId", itemId); Object ir = result.getOrThrow("itemResult"); if (ir instanceof ItemResult) return (ItemResult) ir; if (ir instanceof String) System.out.println(">> " + ir); return ItemResult.UNDEFINED; }); }
/** * Get current network rate for operating SLOT1 contracts * @return kilobyte-days per U rate * @throws ClientError */ public Decimal storageGetRate() throws ClientError { return protect(() -> { Binder result = httpClient.command("storageGetRate"); String U = result.getStringOrThrow("U"); return new Decimal(U); }); }
/** * Get current network rate for operating UNS1 contracts * @return name-days per U rate * @throws ClientError */ public Decimal unsRate() throws ClientError { return protect(() -> { Binder result = httpClient.command("unsRate"); String U = result.getStringOrThrow("U"); return new Decimal(U); }); }
/** * Look for state data of slot contract * @param slotId slot contract id * @return {@link Binder} containing state data of slot contract * @throws ClientError */ public Binder querySlotInfo(HashId slotId) throws ClientError { return protect(() -> { Binder result = httpClient.command("querySlotInfo", "slot_id", slotId.getDigest()); Binder binder = result.getBinder("slot_state", null); return binder; }); }
/** * Look for state data of follower contract * @param followerId follower contract id * @return {@link Binder} containing state data of follower contract * @throws ClientError */ public Binder queryFollowerInfo(HashId followerId) throws ClientError { return protect(() -> { Binder result = httpClient.command("queryFollowerInfo", "follower_id", followerId.getDigest()); Binder binder = result.getBinder("follower_state", null); return binder; }); } }
/** * Get the contract with the given contract id. * @param itemId contract hash * @return contract with the given contract id * @throws ClientError */ public byte[] getBody(HashId itemId) throws ClientError { return protect(() -> { Binder result = httpClient.command("getBody", "itemId", itemId); try { Bytes bytes = result.getBytesOrThrow("packedContract"); return bytes.getData(); } catch (IllegalArgumentException e) { return null; } }); }
/** * Look for {@link com.icodici.universa.contract.services.UnsContract} that registers the name given * @param name to look for * @return packed {@link com.icodici.universa.contract.services.UnsContract} if found. Otherwise null * @throws ClientError */ public byte[] queryNameContract(String name) throws ClientError { return protect(() -> { Binder result = httpClient.command("queryNameContract", "name", name); try { Bytes bytes = result.getBytesOrThrow("packedContract"); return bytes.getData(); } catch (IllegalArgumentException e) { return null; } }); }
public ItemResult setVerboseLevel(int node, int network, int udp) throws ClientError { return protect(() -> { Binder result = httpClient.command("setVerbose", "node", DatagramAdapter.VerboseLevel.intToString(node), "network", DatagramAdapter.VerboseLevel.intToString(network), "udp", DatagramAdapter.VerboseLevel.intToString(udp)); Object ir = result.getOrThrow("itemResult"); if (ir instanceof ItemResult) return (ItemResult) ir; if (ir instanceof String) System.out.println(">> " + ir); return ItemResult.UNDEFINED; }); }
/** * Get processing state of given parcel * @param parcelId id of the parcel to get state of * @return processing state of the parcel * @throws ClientError */ public Node.ParcelProcessingState getParcelProcessingState(HashId parcelId) throws ClientError { return protect(() -> { Binder result = httpClient.command("getParcelProcessingState", "parcelId", parcelId); Object ps = result.getOrThrow("processingState"); if (ps instanceof Node.ParcelProcessingState) return (Node.ParcelProcessingState) ps; return Node.ParcelProcessingState.valueOf(result.getBinder("processingState").getStringOrThrow("state")); }); }
@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")); }
@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(); }