Java Code Examples for io.particle.android.sdk.utils.EZ#closeThisThingOrMaybeDont()

The following examples show how to use io.particle.android.sdk.utils.EZ#closeThisThingOrMaybeDont() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommandClient.java    From particle-android with Apache License 2.0 6 votes vote down vote up
private <T> T sendAndMaybeReceive(Command command, Class<T> responseType) throws IOException {
    log.i("Preparing to send command '" + command.getCommandName() + "'");
    String commandData = buildCommandData(command);

    BufferedSink buffer = null;
    try {
        // send command
        Socket socket = socketFactory.createSocket(ipAddress, port);
        buffer = wrapSocket(socket, DEFAULT_TIMEOUT_SECONDS);
        log.d("Writing command data");
        buffer.writeUtf8(commandData);
        buffer.flush();

        // if no response defined, just exit early.
        if (responseType.equals(Void.class)) {
            log.d("Done.");
            return null;
        }

        return readResponse(socket, responseType, DEFAULT_TIMEOUT_SECONDS);

    } finally {
        EZ.closeThisThingOrMaybeDont(buffer);
    }
}
 
Example 2
Source File: CommandClient.java    From particle-android with Apache License 2.0 6 votes vote down vote up
private <T> T readResponse(Socket socket, Class<T> responseType, int timeoutValueInSeconds)
        throws IOException {
    BufferedSource buffer = Okio.buffer(Okio.source(socket));
    buffer.timeout().timeout(timeoutValueInSeconds, TimeUnit.SECONDS);

    log.d("Reading response data...");
    String line;
    do {
        // read (and throw away, for now) any headers
        line = buffer.readUtf8LineStrict();
    } while (truthy(line));

    String responseData = buffer.readUtf8();
    log.d("Command response (raw): " + CommandClientUtils.escapeJava(responseData));
    T tee;
    try {
        tee = gson.fromJson(responseData, responseType);
    } catch (Exception ex) {
        throw new IOException(ex);
    }

    log.d("Command response: " + tee);
    EZ.closeThisThingOrMaybeDont(buffer);
    return tee;
}
 
Example 3
Source File: ParticleCloudException.java    From particle-android with Apache License 2.0 6 votes vote down vote up
private String loadBody() {
    if (httpBodyInputStream == null) {
        return null;
    }
    BufferedSource buffer = null;
    try {
        buffer = Okio.buffer(Okio.source(httpBodyInputStream));
        return buffer.readUtf8();

    } catch (IOException e) {
        log.i("Error reading HTTP response body: ", e);
        return null;

    } finally {
        EZ.closeThisThingOrMaybeDont(buffer);
    }
}
 
Example 4
Source File: ParticleCloudException.java    From spark-sdk-android with Apache License 2.0 6 votes vote down vote up
private String loadBody() {
    if (httpBodyInputStream == null) {
        return null;
    }
    BufferedSource buffer = null;
    try {
        buffer = Okio.buffer(Okio.source(httpBodyInputStream));
        return buffer.readUtf8();

    } catch (IOException e) {
        log.i("Error reading HTTP response body: ", e);
        return null;

    } finally {
        EZ.closeThisThingOrMaybeDont(buffer);
    }
}
 
Example 5
Source File: CommandClient.java    From spark-setup-android with Apache License 2.0 6 votes vote down vote up
private <T> T sendAndMaybeReceive(Command command, Class<T> responseType) throws IOException {
    log.i("Preparing to send command '" + command.getCommandName() + "'");
    String commandData = buildCommandData(command);

    BufferedSink buffer = null;
    try {
        // send command
        Socket socket = socketFactory.createSocket(ipAddress, port);
        buffer = wrapSocket(socket, DEFAULT_TIMEOUT_SECONDS);
        log.d("Writing command data");
        buffer.writeUtf8(commandData);
        buffer.flush();

        // if no response defined, just exit early.
        if (responseType.equals(Void.class)) {
            log.d("Done.");
            return null;
        }

        return readResponse(socket, responseType, DEFAULT_TIMEOUT_SECONDS);

    } finally {
        EZ.closeThisThingOrMaybeDont(buffer);
    }
}
 
Example 6
Source File: CommandClient.java    From spark-setup-android with Apache License 2.0 6 votes vote down vote up
private <T> T readResponse(Socket socket, Class<T> responseType, int timeoutValueInSeconds)
        throws IOException {
    BufferedSource buffer = Okio.buffer(Okio.source(socket));
    buffer.timeout().timeout(timeoutValueInSeconds, TimeUnit.SECONDS);

    log.d("Reading response data...");
    String line;
    do {
        // read (and throw away, for now) any headers
        line = buffer.readUtf8LineStrict();
    } while (truthy(line));

    String responseData = buffer.readUtf8();
    log.d("Command response (raw): " + CommandClientUtils.escapeJava(responseData));
    T tee = gson.fromJson(responseData, responseType);
    log.d("Command response: " + tee);
    EZ.closeThisThingOrMaybeDont(buffer);
    return tee;
}