Search in sources :

Example 11 with Peer

use of org.xel.peer.Peer in project elastic-core-maven by OrdinaryDude.

the class APIProxy method getServingPeer.

Peer getServingPeer(String requestType) {
    if (forcedPeerHost != null) {
        return Peers.getPeer(forcedPeerHost);
    }
    APIEnum requestAPI = APIEnum.fromName(requestType);
    if (!peersHosts.isEmpty()) {
        for (String host : peersHosts) {
            Peer peer = Peers.getPeer(host);
            if (peer != null && peer.isApiConnectable() && !peer.getDisabledAPIs().contains(requestAPI)) {
                return peer;
            }
        }
    }
    List<Peer> connectablePeers = Peers.getPeers(p -> p.isApiConnectable() && !blacklistedPeers.containsKey(p.getHost()));
    if (connectablePeers.isEmpty()) {
        return null;
    }
    // subset of connectable peers that have at least one new API enabled, which was disabled for the
    // The first peer (element 0 of peersHosts) is chosen at random. Next peers are chosen randomly from a
    // previously chosen peers. In worst case the size of peersHosts will be the number of APIs
    Peer peer = getRandomAPIPeer(connectablePeers);
    if (peer == null) {
        return null;
    }
    Peer resultPeer = null;
    List<String> currentPeersHosts = new ArrayList<>();
    EnumSet<APIEnum> disabledAPIs = EnumSet.noneOf(APIEnum.class);
    currentPeersHosts.add(peer.getHost());
    mainPeerAnnouncedAddress = peer.getAnnouncedAddress();
    if (!peer.getDisabledAPIs().contains(requestAPI)) {
        resultPeer = peer;
    }
    while (!disabledAPIs.isEmpty() && !connectablePeers.isEmpty()) {
        // remove all peers that do not introduce new enabled APIs
        connectablePeers.removeIf(p -> p.getDisabledAPIs().containsAll(disabledAPIs));
        peer = getRandomAPIPeer(connectablePeers);
        if (peer != null) {
            currentPeersHosts.add(peer.getHost());
            if (!peer.getDisabledAPIs().contains(requestAPI)) {
                resultPeer = peer;
            }
            disabledAPIs.retainAll(peer.getDisabledAPIs());
        }
    }
    peersHosts = Collections.unmodifiableList(currentPeersHosts);
    Logger.logInfoMessage("Selected API peer " + resultPeer + " peer hosts selected " + currentPeersHosts);
    return resultPeer;
}
Also used : Peer(org.xel.peer.Peer) ArrayList(java.util.ArrayList)

Example 12 with Peer

use of org.xel.peer.Peer in project elastic-core-maven by OrdinaryDude.

the class GetPeers method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) {
    boolean active = "true".equalsIgnoreCase(req.getParameter("active"));
    String stateValue = Convert.emptyToNull(req.getParameter("state"));
    String[] serviceValues = req.getParameterValues("service");
    boolean includePeerInfo = "true".equalsIgnoreCase(req.getParameter("includePeerInfo"));
    Peer.State state;
    if (stateValue != null) {
        try {
            state = Peer.State.valueOf(stateValue);
        } catch (RuntimeException exc) {
            return JSONResponses.incorrect("state", "- '" + stateValue + "' is not defined");
        }
    } else {
        state = null;
    }
    long serviceCodes = 0;
    if (serviceValues != null) {
        for (String serviceValue : serviceValues) {
            try {
                serviceCodes |= Peer.Service.valueOf(serviceValue).getCode();
            } catch (RuntimeException exc) {
                return JSONResponses.incorrect("service", "- '" + serviceValue + "' is not defined");
            }
        }
    }
    Collection<? extends Peer> peers = active ? Peers.getActivePeers() : state != null ? Peers.getPeers(state) : Peers.getAllPeers();
    JSONArray peersJSON = new JSONArray();
    if (serviceCodes != 0) {
        final long services = serviceCodes;
        if (includePeerInfo) {
            peers.forEach(peer -> {
                if (peer.providesServices(services)) {
                    peersJSON.add(JSONData.peer(peer));
                }
            });
        } else {
            peers.forEach(peer -> {
                if (peer.providesServices(services)) {
                    peersJSON.add(peer.getHost());
                }
            });
        }
    } else {
        if (includePeerInfo) {
            peers.forEach(peer -> peersJSON.add(JSONData.peer(peer)));
        } else {
            peers.forEach(peer -> peersJSON.add(peer.getHost()));
        }
    }
    JSONObject response = new JSONObject();
    response.put("peers", peersJSON);
    return response;
}
Also used : JSONObject(org.json.simple.JSONObject) Peer(org.xel.peer.Peer) JSONArray(org.json.simple.JSONArray)

Example 13 with Peer

use of org.xel.peer.Peer in project elastic-core-maven by OrdinaryDude.

the class APIProxyServlet method initRemoteRequest.

private boolean initRemoteRequest(HttpServletRequest clientRequest, String requestType) {
    StringBuilder uri;
    if (!APIProxy.forcedServerURL.isEmpty()) {
        uri = new StringBuilder();
        uri.append(APIProxy.forcedServerURL);
    } else {
        Peer servingPeer = APIProxy.getInstance().getServingPeer(requestType);
        if (servingPeer == null) {
            return false;
        }
        uri = servingPeer.getPeerApiUri();
        clientRequest.setAttribute(REMOTE_SERVER_IDLE_TIMEOUT, servingPeer.getApiServerIdleTimeout());
    }
    uri.append("/nxt");
    String query = clientRequest.getQueryString();
    if (query != null) {
        uri.append("?").append(query);
    }
    clientRequest.setAttribute(REMOTE_URL, uri.toString());
    return true;
}
Also used : Peer(org.xel.peer.Peer)

Example 14 with Peer

use of org.xel.peer.Peer in project elastic-core-maven by OrdinaryDude.

the class AddPeer method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest request) throws NxtException {
    String peerAddress = Convert.emptyToNull(request.getParameter("peer"));
    if (peerAddress == null) {
        return MISSING_PEER;
    }
    JSONObject response = new JSONObject();
    Peer peer = Peers.findOrCreatePeer(peerAddress, true);
    if (peer != null) {
        boolean isNewlyAdded = Peers.addPeer(peer, peerAddress);
        Peers.connectPeer(peer);
        response = JSONData.peer(peer);
        response.put("isNewlyAdded", isNewlyAdded);
    } else {
        response.put("errorCode", 8);
        response.put("errorDescription", "Failed to add peer");
    }
    return response;
}
Also used : JSONObject(org.json.simple.JSONObject) Peer(org.xel.peer.Peer)

Example 15 with Peer

use of org.xel.peer.Peer in project elastic-core-maven by OrdinaryDude.

the class BlacklistAPIProxyPeer method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest request) throws NxtException {
    String peerAddress = Convert.emptyToNull(request.getParameter("peer"));
    if (peerAddress == null) {
        return MISSING_PEER;
    }
    Peer peer = Peers.findOrCreatePeer(peerAddress, true);
    JSONObject response = new JSONObject();
    if (peer == null) {
        return UNKNOWN_PEER;
    } else {
        APIProxy.getInstance().blacklistHost(peer.getHost());
        response.put("done", true);
    }
    return response;
}
Also used : JSONObject(org.json.simple.JSONObject) Peer(org.xel.peer.Peer)

Aggregations

Peer (org.xel.peer.Peer)16 JSONObject (org.json.simple.JSONObject)9 JSONArray (org.json.simple.JSONArray)6 Block (org.xel.Block)2 ArrayList (java.util.ArrayList)1 JSONStreamAware (org.json.simple.JSONStreamAware)1 BlockchainProcessor (org.xel.BlockchainProcessor)1 Transaction (org.xel.Transaction)1