Search in sources :

Example 11 with JSONStreamAware

use of org.json.simple.JSONStreamAware in project elastic-core-maven by OrdinaryDude.

the class MessageEncoder method push.

public static long push(IComputationAttachment work, String secretPhrase) throws NxtException, IOException {
    Appendix.PrunablePlainMessage[] messages = MessageEncoder.encodeAttachment(work);
    JSONStreamAware[] individual_txs = MessageEncoder.encodeTransactions(messages, secretPhrase);
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    for (int i = 0; i < individual_txs.length; ++i) {
        individual_txs[i].writeJSONString(pw);
    }
    StringBuffer sb = sw.getBuffer();
    return MessageEncoder.pushThemAll(individual_txs);
}
Also used : StringWriter(java.io.StringWriter) JSONStreamAware(org.json.simple.JSONStreamAware) PrintWriter(java.io.PrintWriter)

Example 12 with JSONStreamAware

use of org.json.simple.JSONStreamAware in project elastic-core-maven by OrdinaryDude.

the class MessageEncryptionTest method encryptText.

@Test
public void encryptText() {
    JSONStreamAware json = JSONResponses.INCORRECT_ALIAS;
    EncryptedData encryptedData = encrypt(Convert.toBytes(json.toString()));
    Assert.assertEquals(json.toString(), Convert.toString(decrypt(encryptedData)));
}
Also used : EncryptedData(org.xel.crypto.EncryptedData) JSONStreamAware(org.json.simple.JSONStreamAware) Test(org.junit.Test) BlockchainTest(org.xel.BlockchainTest)

Example 13 with JSONStreamAware

use of org.json.simple.JSONStreamAware in project elastic-core-maven by OrdinaryDude.

the class User method send.

synchronized void send(JSONStreamAware response) {
    if (asyncContext == null) {
        if (isInactive) {
            // user not seen recently, no responses should be collected
            return;
        }
        if (pendingResponses.size() > 1000) {
            pendingResponses.clear();
            // stop collecting responses for this user
            isInactive = true;
            if (secretPhrase == null) {
                // but only completely remove users that don't have unlocked accounts
                Users.remove(this);
            }
            return;
        }
        pendingResponses.offer(response);
    } else {
        JSONArray responses = new JSONArray();
        JSONStreamAware pendingResponse;
        while ((pendingResponse = pendingResponses.poll()) != null) {
            responses.add(pendingResponse);
        }
        responses.add(response);
        JSONObject combinedResponse = new JSONObject();
        combinedResponse.put("responses", responses);
        asyncContext.getResponse().setContentType("text/plain; charset=UTF-8");
        try (Writer writer = asyncContext.getResponse().getWriter()) {
            combinedResponse.writeJSONString(writer);
        } catch (IOException e) {
            Logger.logMessage("Error sending response to user", e);
        }
        asyncContext.complete();
        asyncContext = null;
    }
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) IOException(java.io.IOException) JSONStreamAware(org.json.simple.JSONStreamAware) Writer(java.io.Writer)

Aggregations

JSONStreamAware (org.json.simple.JSONStreamAware)13 JSONObject (org.json.simple.JSONObject)7 IOException (java.io.IOException)5 Writer (java.io.Writer)4 JSONArray (org.json.simple.JSONArray)4 NxtException (org.xel.NxtException)3 StringWriter (java.io.StringWriter)2 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Test (org.junit.Test)1 Appendix (org.xel.Appendix)1 Attachment (org.xel.Attachment)1 Blockchain (org.xel.Blockchain)1