From 66d673df736b2dee1945fef53612b78cab7358a5 Mon Sep 17 00:00:00 2001 From: Edoardo La Greca Date: Wed, 30 Jul 2025 17:58:58 +0200 Subject: add TagGenerator --- src/main/kotlin/TagGenerator.kt | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/main/kotlin/TagGenerator.kt diff --git a/src/main/kotlin/TagGenerator.kt b/src/main/kotlin/TagGenerator.kt new file mode 100644 index 0000000..d7662e6 --- /dev/null +++ b/src/main/kotlin/TagGenerator.kt @@ -0,0 +1,62 @@ +import kotlin.random.Random + +/** + * Generate tags for 9P messages. + */ +class TagGenerator(val method: TagGenerationMethod, val initial: UShort) { + + private var current: UShort = initial + private val rng: Random = Random(initial.toInt()) + private val randomReturned: Set = emptySet() + + private val generationFunctions: Map UShort> = mapOf( + TagGenerationMethod.INCREMENTAL to this::generateIncremental, + TagGenerationMethod.RANDOM to this::generateRandom, + TagGenerationMethod.RANDOM_CHECKED to this::generateRandomChecked + ) + + /** + * How are tags generated? + */ + enum class TagGenerationMethod { + /** + * Return the initial value on the first generation. Increment the value on each generation. + */ + INCREMENTAL, + + /** + * Use the initial value as a seed and generate random values from it. + */ + RANDOM, + + /** + * Same as [RANDOM], but checks are added to avoid generating the same value twice. + */ + RANDOM_CHECKED, + } + + /** + * Generate a new tag. + */ + fun generate(): UShort { + return this.generationFunctions.getValue(method).invoke() + } + + private fun generateIncremental(): UShort { + val tmp = this.current + this.current++ + return tmp + } + + private fun generateRandom(): UShort { + return this.rng.nextBits(16).toUShort() + } + + private fun generateRandomChecked(): UShort { + var v: UShort + do { + v = generateRandom() + } while (v in randomReturned) + return v + } +} \ No newline at end of file -- cgit v1.2.3