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);
}
}
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);
}
}
Aggregations