TraplineMonitorService.java 文件源码

java
阅读 59 收藏 0 点赞 0 评论 0

项目:Capstone2016 作者:
/**
 * Sends logged catches to the server
 * @return A {@link ReadOnlyObjectProperty} which is set to {@link RequestStatus#SUCCESS} if all catches were sent successfully, or to an error if any requests failed.
 */
public ReadOnlyObjectProperty<RequestStatus> sendCatchesToServer () {
    ReadOnlyObjectWrapper<RequestStatus> status = new ReadOnlyObjectWrapper<>();
    if (loggedCatches.isEmpty()) {
        //Run the status update later, so the calling method has a chance to register a listener first
        Platform.runLater(() -> status.set(RequestStatus.SUCCESS));
    } else {
        Platform.runLater(() -> status.set(RequestStatus.PENDING));
        IntegerProperty remaining = new SimpleIntegerProperty(loggedCatches.size());
        remaining.addListener((obs, oldVal, newVal) -> {
            if (newVal.intValue() == 0) {
                status.set(RequestStatus.SUCCESS);
            }
        });
        for (Pair<Integer, Catch> c : loggedCatches) {
networkService.sendLoggedCatch(c.getKey(), c.getValue()).addListener((obs, oldStatus, newStatus) -> {
    switch (newStatus) {
    case PENDING:
        break;
    case SUCCESS:
        remaining.set(remaining.get()-1);
        loggedCatches.remove(c);
        break;
    case FAILED_OTHER:
        LOG.log(Level.WARNING, "Failed to send logged catch "+c+" (server responded with an error code) - removing from cache.");
        loggedCatches.remove(c);
        trapline.getTrap(c.getKey()).getCatches().remove(c.getValue());
        remaining.set(remaining.get()-1);
        break;
    case FAILED_NETWORK:
    case FAILED_UNAUTHORISED:
        status.set(newStatus);
        break;
    }
});
    }
    LOG.log(Level.INFO, "Sent "+loggedCatches.size()+" logged catches to the server.");
    }
    return status.getReadOnlyProperty();
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号