STEEM Server Finder: How To Use The JSON

in #utopian-io5 years ago

image.png

In previous post, STEEM Server Checker: To Check Public RPC Servers (Full Nodes), we keep the JSON format of the full nodes in our site below.

http://servers.puncakbukit.net/servers.json

We may use this JSON in various ways in any language, and here we will give an example in Node.js and Java. In this example, we want to pick the random server that is up, and then use it. The overview of the scenario is as follows:

  • Download the JSON from URL above.
  • Parse it into the list of STEEM servers.
  • Shuffle the server list randomly.
  • Return the first server that is up.

Node.js

const request = require("request");
const url = "http://servers.puncakbukit.net/servers.json";
//
// Download the JSON from URL
//
request({
    url: url,
    json: true
}, function (error, response, body) {
    if (!error && response.statusCode === 200) {
        //
        // Parse it into the list of STEEM servers, and
        // Shuffle the server list randomly
        //
        const servers = JSON.parse(body).sort( () => Math.random() - 0.5);
        //
        // Return the first server that is up
        //
        for(var server of servers) {
          if(server.status == 'UP'){
            node = 'https://' + server.server;
            break;
          }
        }
        //
        // use the node here
        //
    }
})

Java

//
// Download the JSON from URL
//
String url = "http://servers.puncakbukit.net/servers.json";
Connection connection = Jsoup.connect(url).ignoreContentType(true);
connection.timeout(timeout);
String json = connection.execute().body();
//
// Parse it into the list of STEEM servers
//
SteemServer[] arr = new Gson().fromJson(json, SteemServer[].class);
List servers = Arrays.asList(arr == null ? new SteemServer[0] : arr);
//
// Shuffle the server list randomly
//
Collections.shuffle(servers);
//
// Return the first server that is up
//
SteemServer node;
for (SteemServer server : servers) {
    if (server.status.equals(SteemServer.UP)) {
        node = server;
        break;
    }
}
//
// use the node here
//

STEEM Server Finder

Moreover, we have developed a small Java library just for this purpose, called STEEM Server Finder. To pick the random up server using this library, we could simply do as below:

String url = "http://servers.puncakbukit.net/servers.json";
int timeout = 10000;
ServerFinder finder = new DefaultServerFinder.Builder().withUrl(url)
        .withTimeout(timeout)
        .build()
        .find();
SteemServer node = finder.findRandom();
//
// use the node here
//

We may use the library for another purpose as well, for example:

//
// finds up server with longest time
//
SteemServer longestUp = finder.findLongestUp();
//
// finds down server with longest time
//
SteemServer longestDown = finder.findLongestDown();

Find the library in GitHub below:

https://github.com/puncakbukit/server-checker

Hope it may help people to find STEEM server more easily.. :-)

Image: https://pixabay.com/illustrations/analytics-information-innovation-3088958/

Voting bot @kakibukit: send 0.002 - 0.04 SBD + memo = URL, voting each 2.4 hours, min ROI 10%, max age 3.5 days, post type not comment.

Approve @puncakbukit As Your Witness - every vote counts.

  • Access page Witness Voting.
  • Scroll down to bottom.
  • Type "puncakbukit" int the textbox.
  • Click "VOTE" button.
  • We will follow you.. ;-)
  • My Witness Update

See also:

Sort:  

Sneaky-Ninja-Throwing-Coin 125px.jpg
Defended (77.78%)
Summoned by @lerengbukit
Sneaky Ninja supports @youarehope and @tarc with a percentage of all bids.
Everything You Need To Know About Sneaky Ninja


woosh

Coin Marketplace

STEEM 0.30
TRX 0.11
JST 0.033
BTC 64271.38
ETH 3157.43
USDT 1.00
SBD 4.25