Search in sources :

Example 1 with ConvertErrorCode

use of com.herewhite.sdk.domain.ConvertErrorCode in project whiteboard-android by netless-io.

the class ConverterV5 method checkProgress.

// Step 3: 轮询查询
private void checkProgress(String token) throws ConvertException {
    Request request = new Request.Builder().url(String.format(PROGRESS_URL_FORMAT, taskUuid, convertType(type))).header("token", token).header("region", convertRegion(region)).header("Content-Type", "application/json").header("Accept", "application/json").build();
    Call call = client.newCall(request);
    try {
        Response response = call.execute();
        String body = response.body().string();
        System.out.println(body);
        if (response.code() == 200) {
            QueryInfo queryInfo = gson.fromJson(body, QueryInfo.class);
            ConversionInfo info = queryInfo.progress;
            ConversionInfo.ServerConversionStatus status = queryInfo.status;
            if (status == ConversionInfo.ServerConversionStatus.Fail || status == ConversionInfo.ServerConversionStatus.NotFound) {
                ConvertErrorCode code = status == ConversionInfo.ServerConversionStatus.Fail ? ConvertErrorCode.ConvertFail : ConvertErrorCode.NotFound;
                onFailure(new ConvertException(code, queryInfo.failedReason));
                this.status = ConverterStatus.Fail;
            } else if (status == ConversionInfo.ServerConversionStatus.Finished) {
                onFinish(getPpt(info, type), info);
                this.status = ConverterStatus.Success;
            } else {
                onProgress(info.getConvertedPercentage(), queryInfo.progress);
            }
        } else {
            throw new ConvertException(ConvertErrorCode.ConvertFail, body);
        }
    } catch (IOException e) {
        throw new ConvertException(ConvertErrorCode.CheckFail, e);
    }
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) ConvertException(com.herewhite.sdk.domain.ConvertException) ConvertErrorCode(com.herewhite.sdk.domain.ConvertErrorCode) Request(okhttp3.Request) ConversionInfo(com.herewhite.sdk.domain.ConversionInfo) IOException(java.io.IOException)

Example 2 with ConvertErrorCode

use of com.herewhite.sdk.domain.ConvertErrorCode in project whiteboard-android by netless-io.

the class Converter method polling.

private void polling(String taskId, ConvertType type, final ConvertCallback callbacks) {
    boolean canCheck = this.status == ConverterStatus.Timeout || this.status == ConverterStatus.CheckingFail || this.status == ConverterStatus.GetDynamicFail;
    if (this.status != ConverterStatus.Created && !canCheck) {
        return;
    }
    final Converter that = this;
    Date expireDate = new Date(this.beginDate.getTime() + this.timeout);
    while (converting && expireDate.after(new Date())) {
        final CountDownLatch latch = new CountDownLatch(1);
        that.status = ConverterStatus.Checking;
        checkProgress(taskId, type, new CheckCallback() {

            @Override
            public void onCheckResponse(ConversionInfo info) {
                ConversionInfo.ServerConversionStatus status = info.getConvertStatus();
                if (status == ConversionInfo.ServerConversionStatus.Fail || status == ConversionInfo.ServerConversionStatus.NotFound) {
                    converting = false;
                    that.status = ConverterStatus.Fail;
                    ConvertErrorCode code = status == ConversionInfo.ServerConversionStatus.Fail ? ConvertErrorCode.ConvertFail : ConvertErrorCode.NotFound;
                    ConvertException e = new ConvertException(code, info.getReason());
                    callbacks.onConvertFailure(e);
                } else if (status == ConversionInfo.ServerConversionStatus.Finished) {
                    // 成功时,可能还要额外获取动态 ppt 内容,不直接设置状态
                    converting = false;
                    callbacks.onConvertFinish(info);
                } else {
                    that.status = ConverterStatus.WaitingForNextCheck;
                    callbacks.onConvertProgress(info.getConvertedPercentage(), info);
                }
                try {
                    Thread.sleep(interval);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                latch.countDown();
            }

            @Override
            public void onCheckFailure(Exception e) {
                ConvertException exp = new ConvertException(ConvertErrorCode.CheckFail);
                callbacks.onConvertFailure(exp);
                converting = false;
                latch.countDown();
            }
        });
        try {
            latch.await();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (this.status == ConverterStatus.WaitingForNextCheck) {
        this.status = ConverterStatus.Timeout;
        ConvertException exp = new ConvertException(ConvertErrorCode.CheckTimeout);
        callbacks.onConvertFailure(exp);
    }
}
Also used : ConvertException(com.herewhite.sdk.domain.ConvertException) ConvertErrorCode(com.herewhite.sdk.domain.ConvertErrorCode) ConversionInfo(com.herewhite.sdk.domain.ConversionInfo) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) IOException(java.io.IOException) ConvertException(com.herewhite.sdk.domain.ConvertException)

Aggregations

ConversionInfo (com.herewhite.sdk.domain.ConversionInfo)2 ConvertErrorCode (com.herewhite.sdk.domain.ConvertErrorCode)2 ConvertException (com.herewhite.sdk.domain.ConvertException)2 IOException (java.io.IOException)2 Date (java.util.Date)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Call (okhttp3.Call)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1