From e3bef5aca53113f5b12aa367fcc2ff5e7c7138fc Mon Sep 17 00:00:00 2001 From: tomtzook Date: Tue, 16 Jan 2024 01:38:31 +0200 Subject: [PATCH] some fixes to watchdog + obsr usage --- .../flashlib/app/BasicFlashLibControl.java | 8 +++- .../flashlib/app/watchdog/WatchdogImpl.java | 44 ++++++++++++++++++- .../app/watchdog/WatchdogService.java | 7 ++- .../robot/base/GenericRobotControl.java | 7 ++- 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/flashlib.core.app/src/main/java/com/flash3388/flashlib/app/BasicFlashLibControl.java b/flashlib.core.app/src/main/java/com/flash3388/flashlib/app/BasicFlashLibControl.java index 79feb698b..b7702caf9 100644 --- a/flashlib.core.app/src/main/java/com/flash3388/flashlib/app/BasicFlashLibControl.java +++ b/flashlib.core.app/src/main/java/com/flash3388/flashlib/app/BasicFlashLibControl.java @@ -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; @@ -94,8 +96,9 @@ 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; @@ -103,7 +106,8 @@ public Watchdog newWatchdog(String name, Time timeout, FeedReporter reporter) { @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; diff --git a/flashlib.core.app/src/main/java/com/flash3388/flashlib/app/watchdog/WatchdogImpl.java b/flashlib.core.app/src/main/java/com/flash3388/flashlib/app/watchdog/WatchdogImpl.java index 76bb58534..22dd33b16 100644 --- a/flashlib.core.app/src/main/java/com/flash3388/flashlib/app/watchdog/WatchdogImpl.java +++ b/flashlib.core.app/src/main/java/com/flash3388/flashlib/app/watchdog/WatchdogImpl.java @@ -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; @@ -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 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); @@ -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(); } @@ -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(); @@ -133,6 +155,9 @@ public void feed() { mIsExpired = false; mLastFeedTime = now; mReports.clear(); + + mIsExpiredEntry.setBoolean(false); + mLastFeedTimeEntry.setDouble(mLastFeedTime.valueAsSeconds()); } finally { mLock.unlock(); } @@ -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 { @@ -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(); + } + } } diff --git a/flashlib.core.app/src/main/java/com/flash3388/flashlib/app/watchdog/WatchdogService.java b/flashlib.core.app/src/main/java/com/flash3388/flashlib/app/watchdog/WatchdogService.java index 7c8a6871e..69e59b5f6 100644 --- a/flashlib.core.app/src/main/java/com/flash3388/flashlib/app/watchdog/WatchdogService.java +++ b/flashlib.core.app/src/main/java/com/flash3388/flashlib/app/watchdog/WatchdogService.java @@ -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; @@ -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); diff --git a/flashlib.core.robot/src/main/java/com/flash3388/flashlib/robot/base/GenericRobotControl.java b/flashlib.core.robot/src/main/java/com/flash3388/flashlib/robot/base/GenericRobotControl.java index a4dc2f19e..de998b70d 100644 --- a/flashlib.core.robot/src/main/java/com/flash3388/flashlib/robot/base/GenericRobotControl.java +++ b/flashlib.core.robot/src/main/java/com/flash3388/flashlib/robot/base/GenericRobotControl.java @@ -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; @@ -241,8 +242,9 @@ 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; @@ -250,7 +252,8 @@ public Watchdog newWatchdog(String name, Time timeout, FeedReporter reporter) { @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;