Skip to content

Commit

Permalink
some fixes to watchdog + obsr usage
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtzook committed Jan 15, 2024
1 parent a65a686 commit e3bef5a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.flash3388.flashlib.app.watchdog.Watchdog;
import com.flash3388.flashlib.app.watchdog.WatchdogImpl;
import com.flash3388.flashlib.app.watchdog.WatchdogService;
import com.flash3388.flashlib.net.obsr.ObjectStorage;
import com.flash3388.flashlib.net.obsr.StoredObject;
import com.flash3388.flashlib.time.Clock;
import com.flash3388.flashlib.time.SystemNanoClock;
import com.flash3388.flashlib.time.Time;
Expand Down Expand Up @@ -94,16 +96,18 @@ public FlashLibMainThread getMainThread() {

@Override
public Watchdog newWatchdog(String name, Time timeout, FeedReporter reporter) {
StoredObject rootObject = WatchdogImpl.getWatchdogStoredObject(this, name);
FeedReporter feedReporter = new MultiFeedReporters(Arrays.asList(new LoggingFeedReporter(), reporter));
InternalWatchdog watchdog = new WatchdogImpl(getClock(), name, timeout, feedReporter);
InternalWatchdog watchdog = new WatchdogImpl(getClock(), name, timeout, feedReporter, rootObject);
mWatchdogService.register(watchdog);

return watchdog;
}

@Override
public Watchdog newWatchdog(String name, Time timeout) {
InternalWatchdog watchdog = new WatchdogImpl(getClock(), name, timeout, new LoggingFeedReporter());
StoredObject rootObject = WatchdogImpl.getWatchdogStoredObject(this, name);
InternalWatchdog watchdog = new WatchdogImpl(getClock(), name, timeout, new LoggingFeedReporter(), rootObject);
mWatchdogService.register(watchdog);

return watchdog;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.flash3388.flashlib.app.watchdog;

import com.flash3388.flashlib.app.FlashLibControl;
import com.flash3388.flashlib.app.net.NetworkInterface;
import com.flash3388.flashlib.net.obsr.ObjectStorage;
import com.flash3388.flashlib.net.obsr.StoredEntry;
import com.flash3388.flashlib.net.obsr.StoredObject;
import com.flash3388.flashlib.time.Clock;
import com.flash3388.flashlib.time.Time;
import com.flash3388.flashlib.util.logging.Logging;
Expand All @@ -19,18 +24,26 @@ public class WatchdogImpl implements InternalWatchdog {
private final Time mTimeout;
private final FeedReporter mReporter;

private final StoredEntry mLastFeedTimeEntry;
private final StoredEntry mIsEnabledEntry;
private final StoredEntry mIsExpiredEntry;

private final Lock mLock;
private final List<TimestampReport> mReports;
private Time mLastFeedTime;
private boolean mDisabled;
private boolean mIsExpired;

public WatchdogImpl(Clock clock, String name, Time timeout, FeedReporter reporter) {
public WatchdogImpl(Clock clock, String name, Time timeout, FeedReporter reporter, StoredObject rootObject) {
mClock = clock;
mName = name;
mTimeout = timeout;
mReporter = reporter;

mLastFeedTimeEntry = rootObject.getEntry("LastFeedTime");
mIsEnabledEntry = rootObject.getEntry("IsEnabled");
mIsExpiredEntry = rootObject.getEntry("IsExpired");

mLock = new ReentrantLock();
mReports = new ArrayList<>(5);

Expand Down Expand Up @@ -70,6 +83,10 @@ public void disable() {
mIsExpired = false;
mLastFeedTime = Time.INVALID;
mReports.clear();

mIsEnabledEntry.setBoolean(false);
mIsExpiredEntry.setBoolean(false);
mLastFeedTimeEntry.setDouble(-1);
} finally {
mLock.unlock();
}
Expand All @@ -86,6 +103,11 @@ public void enable() {
mReports.clear();
mIsExpired = false;
mLastFeedTime = mClock.currentTime();

mIsEnabledEntry.setBoolean(true);
mIsExpiredEntry.setBoolean(false);
mLastFeedTimeEntry.setDouble(mLastFeedTime.valueAsSeconds());

mDisabled = false;
} finally {
mLock.unlock();
Expand Down Expand Up @@ -133,6 +155,9 @@ public void feed() {
mIsExpired = false;
mLastFeedTime = now;
mReports.clear();

mIsExpiredEntry.setBoolean(false);
mLastFeedTimeEntry.setDouble(mLastFeedTime.valueAsSeconds());
} finally {
mLock.unlock();
}
Expand All @@ -142,6 +167,10 @@ public void feed() {
public Time getTimeLeftToTimeout() {
mLock.lock();
try {
if (mDisabled) {
return Time.INVALID;
}

Time now = mClock.currentTime();
return now.sub(mLastFeedTime);
} finally {
Expand All @@ -168,9 +197,22 @@ public void checkFed() {

Time overrun = timePassed.sub(mTimeout);
mReporter.reportFeedExpired(mName, overrun, mReports);

mIsExpiredEntry.setBoolean(true);
}
} finally {
mLock.unlock();
}
}

public static StoredObject getWatchdogStoredObject(FlashLibControl control, String name) {
NetworkInterface networkInterface = control.getNetworkInterface();
if (networkInterface.getMode().isObjectStorageEnabled()) {
ObjectStorage objectStorage = networkInterface.getObjectStorage();
return objectStorage.getInstanceRoot().getChild("Watchdogs").getChild(name);
} else {
control.getLogger().warn("OBSR not enabled, creating non attached Watchdog");
return new StoredObject.Stub();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.castle.concurrent.service.TerminalServiceBase;
import com.castle.exceptions.ServiceException;
import com.flash3388.flashlib.time.Time;
import com.flash3388.flashlib.util.concurrent.Sleeper;
import com.flash3388.flashlib.util.logging.Logging;
import org.slf4j.Logger;
Expand Down Expand Up @@ -61,7 +62,11 @@ public void run() {
}

// TODO: BETTER WAIT FOR TIMEOUT
mSleeper.sleep(watchdog.getTimeLeftToTimeout());
Time waitTime = watchdog.getTimeLeftToTimeout();
if (waitTime.isValid()) {
mSleeper.sleep(waitTime);
}

watchdog.checkFed();
} finally {
mWatchdogs.add(watchdog);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.flash3388.flashlib.net.hfcs.HfcsRegistry;
import com.flash3388.flashlib.net.hfcs.ping.HfcsPing;
import com.flash3388.flashlib.net.obsr.ObjectStorage;
import com.flash3388.flashlib.net.obsr.StoredObject;
import com.flash3388.flashlib.robot.RobotControl;
import com.flash3388.flashlib.robot.RobotFactory;
import com.flash3388.flashlib.robot.hfcs.control.HfcsRobotControl;
Expand Down Expand Up @@ -241,16 +242,18 @@ public FlashLibMainThread getMainThread() {

@Override
public Watchdog newWatchdog(String name, Time timeout, FeedReporter reporter) {
StoredObject rootObject = WatchdogImpl.getWatchdogStoredObject(this, name);
FeedReporter feedReporter = new MultiFeedReporters(Arrays.asList(new LoggingFeedReporter(), reporter));
InternalWatchdog watchdog = new WatchdogImpl(getClock(), name, timeout, feedReporter);
InternalWatchdog watchdog = new WatchdogImpl(getClock(), name, timeout, feedReporter, rootObject);
mWatchdogService.register(watchdog);

return watchdog;
}

@Override
public Watchdog newWatchdog(String name, Time timeout) {
InternalWatchdog watchdog = new WatchdogImpl(getClock(), name, timeout, new LoggingFeedReporter());
StoredObject rootObject = WatchdogImpl.getWatchdogStoredObject(this, name);
InternalWatchdog watchdog = new WatchdogImpl(getClock(), name, timeout, new LoggingFeedReporter(), rootObject);
mWatchdogService.register(watchdog);

return watchdog;
Expand Down

0 comments on commit e3bef5a

Please sign in to comment.