Search in sources :

Example 1 with Work

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

the class GetMineableWork method processRequest.

@Override
protected JSONStreamAware processRequest(final HttpServletRequest req) throws NxtException {
    int n = ParameterParser.getInt(req, "n", 1, 10000, true);
    final JSONArray work_packages = new JSONArray();
    try (DbIterator<? extends Work> iterator = Work.getActiveWork(0, n)) {
        while (iterator.hasNext()) {
            final Work transaction = iterator.next();
            work_packages.add(Work.toJson(transaction));
        }
    }
    final JSONObject response = new JSONObject();
    response.put("work_packages", work_packages);
    return response;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) Work(org.xel.Work)

Example 2 with Work

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

the class SubmitSolution method processRequest.

@Override
protected JSONStreamAware processRequest(final HttpServletRequest req) throws NxtException {
    final long workId = ParameterParser.getUnsignedLong(req, "work_id", true);
    byte[] data = ParameterParser.getBytes(req, "data", false);
    final byte[] multiplicator = ParameterParser.getBytes(req, "multiplicator", true);
    int storageId = ParameterParser.getInt(req, "storage_id", 0, Integer.MAX_VALUE, true);
    if (data == null || data.length == 0)
        storageId = -1;
    final boolean is_pow = ParameterParser.getBooleanByString(req, "is_pow", true);
    if (is_pow)
        data = new byte[0];
    byte[] hash = ParameterParser.getBytes(req, "hash", false);
    Work w = Work.getWork(workId);
    if (w == null || w.isClosed()) {
        return JSONResponses.ERROR_WORK_INCORRECT;
    }
    CommandPowBty work = new CommandPowBty(workId, is_pow, multiplicator, hash, data, storageId, w.getCurrentRound());
    try {
        MessageEncoder.push(work, ParameterParser.getSecretPhrase(req, true));
        return JSONResponses.EVERYTHING_ALRIGHT;
    } catch (IOException e) {
        Logger.logInfoMessage("Work " + String.valueOf(w.getId()) + " submission failed. IO Exception");
        return JSONResponses.ERROR_INCORRECT_REQUEST;
    } catch (NxtException.ValidationException e) {
        Logger.logInfoMessage("Work " + String.valueOf(w.getId()) + " submission failed: " + e.getMessage());
        JSONObject response = new JSONObject();
        response.put("errorCode", 6009);
        response.put("errorDescription", e.getMessage());
        return response;
    }
}
Also used : JSONObject(org.json.simple.JSONObject) CommandPowBty(org.xel.computation.CommandPowBty) CommandCancelWork(org.xel.computation.CommandCancelWork) Work(org.xel.Work) NxtException(org.xel.NxtException) IOException(java.io.IOException)

Example 3 with Work

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

the class CommandCancelWork method apply.

@Override
void apply(Transaction transaction) {
    if (!validate(transaction))
        return;
    // Here, apply the actual package
    Logger.logInfoMessage("cancelling work: id=" + Long.toUnsignedString(this.cancel_work_id));
    Work w = Work.getWork(this.cancel_work_id);
    if (w != null) {
        w.CloseManual(transaction.getBlock());
    }
}
Also used : Work(org.xel.Work)

Example 4 with Work

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

the class CancelWork method processRequest.

@Override
protected JSONStreamAware processRequest(final HttpServletRequest req) throws NxtException {
    final long workId = ParameterParser.getUnsignedLong(req, "work_id", true);
    Work w = Work.getWork(workId);
    if (w == null || w.isClosed())
        return JSONResponses.ERROR_WORK_UNKNOWN;
    CommandCancelWork work = new CommandCancelWork(workId);
    try {
        MessageEncoder.push(work, ParameterParser.getSecretPhrase(req, true));
        return JSONResponses.EVERYTHING_ALRIGHT;
    } catch (IOException e) {
        return JSONResponses.ERROR_INCORRECT_REQUEST;
    }
}
Also used : CommandCancelWork(org.xel.computation.CommandCancelWork) CommandCancelWork(org.xel.computation.CommandCancelWork) Work(org.xel.Work) IOException(java.io.IOException)

Example 5 with Work

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

the class GetWork method processRequest.

@Override
protected JSONStreamAware processRequest(final HttpServletRequest req) throws NxtException {
    long just_account;
    try {
        final String readParam = ParameterParser.getParameterMultipart(req, "account");
        final BigInteger b = new BigInteger(readParam);
        just_account = b.longValue();
    } catch (final Exception e) {
        just_account = 0;
    }
    long wid_filter;
    try {
        final String readParam = ParameterParser.getParameterMultipart(req, "work_id");
        final BigInteger b = new BigInteger(readParam);
        wid_filter = b.longValue();
    } catch (final Exception e) {
        wid_filter = 0;
    }
    long storage_slot = -1;
    try {
        final String readParam = ParameterParser.getParameterMultipart(req, "storage_id");
        final BigInteger b = new BigInteger(readParam);
        storage_slot = b.longValue();
    } catch (final Exception e) {
        storage_slot = -1;
    }
    boolean include_finished = false;
    try {
        final String readParam = ParameterParser.getParameterMultipart(req, "with_finished");
        final BigInteger b = new BigInteger(readParam);
        int res = b.intValue();
        if (res != 0) {
            include_finished = true;
        }
    } catch (final Exception e) {
        include_finished = false;
    }
    boolean with_source = false;
    try {
        final String readParam = ParameterParser.getParameterMultipart(req, "with_source");
        final BigInteger b = new BigInteger(readParam);
        int res = b.intValue();
        if (res != 0) {
            with_source = true;
        }
    } catch (final Exception e) {
        with_source = false;
    }
    final int firstIndex = ParameterParser.getFirstIndex(req);
    final int lastIndex = ParameterParser.getLastIndex(req);
    final List<Work> work = Work.getWork(just_account, include_finished, firstIndex, lastIndex, wid_filter);
    JSONArray work_packages = null;
    if (wid_filter == 0) {
        JSONArray jsonArray = new JSONArray();
        for (Work work1 : work) {
            JSONObject jsonObject = Work.toJson(work1);
            jsonArray.add(jsonObject);
        }
        work_packages = jsonArray;
    } else {
        {
            int finalStorage_slot = (int) storage_slot;
            boolean finalWith_source1 = with_source;
            JSONArray jsonArray = new JSONArray();
            for (Work work1 : work) {
                int gostor = finalStorage_slot;
                if (storage_slot == -1 && work1.getStorage_size() > 0) {
                    gostor = ThreadLocalRandom.current().nextInt(0, work1.getStorage_size());
                }
                JSONObject jsonObject = Work.toJsonWithStorage(work1, gostor, finalWith_source1);
                jsonArray.add(jsonObject);
            }
            work_packages = jsonArray;
        }
    }
    final JSONObject response = new JSONObject();
    response.put("work_packages", work_packages);
    return response;
}
Also used : JSONObject(org.json.simple.JSONObject) Work(org.xel.Work) JSONArray(org.json.simple.JSONArray) BigInteger(java.math.BigInteger) NxtException(org.xel.NxtException)

Aggregations

Work (org.xel.Work)5 JSONObject (org.json.simple.JSONObject)3 IOException (java.io.IOException)2 JSONArray (org.json.simple.JSONArray)2 NxtException (org.xel.NxtException)2 CommandCancelWork (org.xel.computation.CommandCancelWork)2 BigInteger (java.math.BigInteger)1 CommandPowBty (org.xel.computation.CommandPowBty)1