package com.midm.model.device; import com.midm.model.org.User; import com.mitac.cell.db.DbObject; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import java.util.UUID; import org.joda.time.LocalDateTime; public class ReconnectMonitor { private static final int BLOCKING_HOURS = 12; private static final int RECONNECT_FOR = 10; private Device device; private Date startedAt; private long prevMins; private int count; private int total; private Date blockedAt; private Date unblockAt; private Timer unblockTimer; private OnAbusedListener listener; public ReconnectMonitor(Device device, OnAbusedListener listener) { this.device = device; this.listener = listener; this.init(); } private void init() { this.startedAt = new Date(); this.blockedAt = null; this.unblockAt = null; this.prevMins = this.getCurMins(); this.total = this.count = 1; } public Device getDevice() { return this.device; } public UUID getDeviceUuid() { return this.device.getUuid(); } public Date getStartedAt() { return this.startedAt; } public Date getBlockedAt() { return this.blockedAt; } public Date getUnblockAt() { return this.unblockAt; } public int getCount() { return this.count; } public int getTotal() { return this.total; } public void connected() { long curMins = this.getCurMins(); long gap = curMins - this.prevMins; if (gap <= 1L) { ++this.total; if (gap == 1L) { ++this.count; this.prevMins = curMins; if (this.count >= 10) { this.blockedAt = new Date(); this.onAbused(); } } } else { this.init(); } } public boolean isBlocked() { return this.blockedAt != null; } public void block() { this.blockedAt = new Date(); this.unblockAt = (new LocalDateTime(this.blockedAt)).plusHours(12).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0).toDate(); this.startUnblockTimer(); } public void block(Device device) { if (device.isBlocked()) { DbObject blockedState = device.getStates().getDbObject(new String[]{"blocked"}); this.blockedAt = blockedState.getDate("from"); this.unblockAt = blockedState.getDate("to"); if (this.unblockAt.getTime() < (new Date()).getTime()) { this.unblock((User)null); } else { this.startUnblockTimer(); } } else { this.block(); } } private void startUnblockTimer() { this.stopUnblockTimer(); String var10003 = this.device.getUuid().toString(); this.unblockTimer = new Timer(var10003 + (new Date()).getTime()); this.unblockTimer.schedule(new TimerTask() { public void run() { ReconnectMonitor.this.unblock((User)null); } }, this.unblockAt); } private void stopUnblockTimer() { if (this.unblockTimer != null) { try { this.unblockTimer.cancel(); this.unblockTimer.purge(); this.unblockTimer = null; } catch (Exception var2) { } } } public void unblock(User user) { this.stopUnblockTimer(); this.startedAt = null; this.blockedAt = null; this.unblockAt = null; this.prevMins = 0L; this.total = this.count = 0; if (this.listener != null) { this.listener.onUnblocked(this, user); } } private void onAbused() { this.block(); if (this.listener != null) { this.listener.onAbused(this); } } private long getCurMins() { return (new Date()).getTime() / 60000L; } public interface OnAbusedListener { void onAbused(ReconnectMonitor monitor); void onUnblocked(ReconnectMonitor monitor, User user); } }