From 7703fc9fcc847208b1803a58d9eaa5938d2c77a1 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Wed, 29 Sep 2021 15:02:38 +0200 Subject: refactor(simulator): Do not expose SimResourceState This change hides the SimResourceState from public API since it is not actively used outside of the `SimResourceContextImpl` class. --- .../resources/SimResourceControllableContext.kt | 5 --- .../opendc/simulator/resources/SimResourceState.kt | 43 ------------------- .../resources/impl/SimResourceContextImpl.kt | 50 +++++++++++++++------- 3 files changed, 34 insertions(+), 64 deletions(-) delete mode 100644 opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceState.kt diff --git a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceControllableContext.kt b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceControllableContext.kt index ba52b597..b406b896 100644 --- a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceControllableContext.kt +++ b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceControllableContext.kt @@ -28,11 +28,6 @@ package org.opendc.simulator.resources * This interface is used by resource providers to control the resource context. */ public interface SimResourceControllableContext : SimResourceContext { - /** - * The state of the resource context. - */ - public val state: SimResourceState - /** * The capacity of the resource. */ diff --git a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceState.kt b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceState.kt deleted file mode 100644 index c72951d0..00000000 --- a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceState.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2021 AtLarge Research - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.opendc.simulator.resources - -/** - * The state of a resource provider. - */ -public enum class SimResourceState { - /** - * The resource provider is pending and the resource is waiting to be consumed. - */ - Pending, - - /** - * The resource provider is active and the resource is currently being consumed. - */ - Active, - - /** - * The resource provider is stopped and the resource cannot be consumed anymore. - */ - Stopped -} diff --git a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/impl/SimResourceContextImpl.kt b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/impl/SimResourceContextImpl.kt index 5a9ffe2d..cbfa7afd 100644 --- a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/impl/SimResourceContextImpl.kt +++ b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/impl/SimResourceContextImpl.kt @@ -60,9 +60,7 @@ internal class SimResourceContextImpl( /** * A flag to indicate the state of the context. */ - override val state: SimResourceState - get() = _state - private var _state = SimResourceState.Pending + private var _state = State.Pending /** * The current processing speed of the resource. @@ -106,21 +104,21 @@ internal class SimResourceContextImpl( private val _timers: ArrayDeque = ArrayDeque() override fun start() { - check(_state == SimResourceState.Pending) { "Consumer is already started" } + check(_state == State.Pending) { "Consumer is already started" } interpreter.batch { consumer.onEvent(this, SimResourceEvent.Start) - _state = SimResourceState.Active + _state = State.Active interrupt() } } override fun close() { - if (_state == SimResourceState.Stopped) { + if (_state == State.Stopped) { return } interpreter.batch { - _state = SimResourceState.Stopped + _state = State.Stopped if (!_updateActive) { val now = clock.millis() val delta = max(0, now - _lastUpdate) @@ -134,7 +132,7 @@ internal class SimResourceContextImpl( } override fun interrupt() { - if (_state == SimResourceState.Stopped) { + if (_state == State.Stopped) { return } @@ -143,7 +141,7 @@ internal class SimResourceContextImpl( } override fun invalidate() { - if (_state == SimResourceState.Stopped) { + if (_state == State.Stopped) { return } @@ -152,7 +150,7 @@ internal class SimResourceContextImpl( } override fun flush() { - if (_state == SimResourceState.Stopped) { + if (_state == State.Stopped) { return } @@ -186,7 +184,7 @@ internal class SimResourceContextImpl( */ fun doUpdate(now: Long) { val oldState = _state - if (oldState != SimResourceState.Active) { + if (oldState != State.Active) { return } @@ -206,14 +204,14 @@ internal class SimResourceContextImpl( // Check whether the state has changed after [consumer.onNext] when (_state) { - SimResourceState.Active -> { + State.Active -> { logic.onConsume(this, now, delta, _limit, duration) // Schedule an update at the new deadline scheduleUpdate(now, newDeadline) } - SimResourceState.Stopped -> doStop(now, delta) - SimResourceState.Pending -> throw IllegalStateException("Illegal transition to pending state") + State.Stopped -> doStop(now, delta) + State.Pending -> throw IllegalStateException("Illegal transition to pending state") } // Note: pending limit might be changed by [logic.onConsume], so re-fetch the value @@ -262,7 +260,7 @@ internal class SimResourceContextImpl( _lastConvergence = timestamp try { - if (_state == SimResourceState.Active) { + if (_state == State.Active) { consumer.onEvent(this, SimResourceEvent.Run) } @@ -308,7 +306,7 @@ internal class SimResourceContextImpl( */ private fun onCapacityChange() { // Do not inform the consumer if it has not been started yet - if (state != SimResourceState.Active) { + if (_state != State.Active) { return } @@ -337,6 +335,26 @@ internal class SimResourceContextImpl( } } + /** + * The state of a resource context. + */ + private enum class State { + /** + * The resource context is pending and the resource is waiting to be consumed. + */ + Pending, + + /** + * The resource context is active and the resource is currently being consumed. + */ + Active, + + /** + * The resource context is stopped and the resource cannot be consumed anymore. + */ + Stopped + } + /** * A flag to indicate that the context should be invalidated. */ -- cgit v1.2.3