diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-10-01 10:24:17 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-01 10:24:17 +0200 |
| commit | 5d71ef4bd6ca84cf5f445f7ba4bb5f7a1e181b64 (patch) | |
| tree | 378ee03042636dde384e4c7eb98aef00f5d3213c /simulator/opendc-utils | |
| parent | 1656a77645079754fc31cffad9d0af6d92e184ee (diff) | |
| parent | a283fac5e4d2a6be229acba191acdcbf7eba6dcd (diff) | |
Merge pull request #42 from atlarge-research/refactor/clean-up
Remove odcsim component from OpenDC
Diffstat (limited to 'simulator/opendc-utils')
3 files changed, 207 insertions, 0 deletions
diff --git a/simulator/opendc-utils/build.gradle.kts b/simulator/opendc-utils/build.gradle.kts new file mode 100644 index 00000000..d66148c4 --- /dev/null +++ b/simulator/opendc-utils/build.gradle.kts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2020 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. + */ + +description = "Utilities used across OpenDC modules" + +/* Build configuration */ +plugins { + `kotlin-library-convention` +} + +dependencies { + api("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Library.KOTLINX_COROUTINES}") +} diff --git a/simulator/opendc-utils/src/main/kotlin/org/opendc/utils/flow/EventFlow.kt b/simulator/opendc-utils/src/main/kotlin/org/opendc/utils/flow/EventFlow.kt new file mode 100644 index 00000000..948595b1 --- /dev/null +++ b/simulator/opendc-utils/src/main/kotlin/org/opendc/utils/flow/EventFlow.kt @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2020 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.utils.flow + +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.FlowPreview +import kotlinx.coroutines.InternalCoroutinesApi +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.channels.SendChannel +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.FlowCollector +import kotlinx.coroutines.flow.consumeAsFlow + +/** + * A [Flow] that can be used to emit events. + */ +public interface EventFlow<T> : Flow<T> { + /** + * Emit the specified [event]. + */ + public fun emit(event: T) + + /** + * Close the flow. + */ + public fun close() +} + +/** + * Creates a new [EventFlow]. + */ +@Suppress("FunctionName") +public fun <T> EventFlow(): EventFlow<T> = EventFlowImpl() + +/** + * Internal implementation of the [EventFlow] class. + */ +@OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class) +private class EventFlowImpl<T> : EventFlow<T> { + private var closed: Boolean = false + private val subscribers = HashMap<SendChannel<T>, Unit>() + + override fun emit(event: T) { + synchronized(this) { + for ((chan, _) in subscribers) { + chan.offer(event) + } + } + } + + override fun close() { + synchronized(this) { + closed = true + + for ((chan, _) in subscribers) { + chan.close() + } + } + } + + @InternalCoroutinesApi + override suspend fun collect(collector: FlowCollector<T>) { + val channel: Channel<T> + synchronized(this) { + if (closed) { + return + } + + channel = Channel(Channel.UNLIMITED) + subscribers[channel] = Unit + } + channel.consumeAsFlow().collect(collector) + } + + override fun toString(): String = "EventFlow" +} diff --git a/simulator/opendc-utils/src/main/kotlin/org/opendc/utils/flow/StateFlow.kt b/simulator/opendc-utils/src/main/kotlin/org/opendc/utils/flow/StateFlow.kt new file mode 100644 index 00000000..996e7700 --- /dev/null +++ b/simulator/opendc-utils/src/main/kotlin/org/opendc/utils/flow/StateFlow.kt @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2020 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.utils.flow + +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.FlowPreview +import kotlinx.coroutines.InternalCoroutinesApi +import kotlinx.coroutines.channels.BroadcastChannel +import kotlinx.coroutines.channels.ConflatedBroadcastChannel +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.FlowCollector +import kotlinx.coroutines.flow.asFlow + +/** + * A [Flow] that contains a single value that changes over time. + * + * This class exists to implement the DataFlow/StateFlow functionality that will be implemented in `kotlinx-coroutines` + * in the future, but is not available yet. + * See: https://github.com/Kotlin/kotlinx.coroutines/pull/1354 + */ +public interface StateFlow<T> : Flow<T> { + /** + * The current value of this flow. + * + * Setting a value that is [equal][Any.equals] to the previous one does nothing. + */ + public var value: T +} + +/** + * Creates a [StateFlow] with a given initial [value]. + */ +@Suppress("FunctionName") +public fun <T> StateFlow(value: T): StateFlow<T> = StateFlowImpl(value) + +/** + * Internal implementation of the [StateFlow] interface. + */ +@OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class) +private class StateFlowImpl<T>(initialValue: T) : StateFlow<T> { + /** + * The [BroadcastChannel] to back this flow. + */ + private val chan = ConflatedBroadcastChannel(initialValue) + + /** + * The internal [Flow] backing this flow. + */ + private val flow = chan.asFlow() + + public override var value: T = initialValue + set(value) { + chan.offer(value) + field = value + } + + @InternalCoroutinesApi + override suspend fun collect(collector: FlowCollector<T>) = flow.collect(collector) +} |
