From 96ea95394c8897b73ed1fb0d6d96f0f1d9f15294 Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 22 Dec 2020 21:46:22 +0000 Subject: [PATCH] Add JAGGRAB request codec Signed-off-by: Graham --- .../protocol/jaggrab/JaggrabRequest.kt | 3 +++ .../protocol/jaggrab/JaggrabRequestDecoder.kt | 19 +++++++++++++ .../protocol/jaggrab/JaggrabRequestEncoder.kt | 12 +++++++++ .../jaggrab/JaggrabRequestDecoderTest.kt | 27 +++++++++++++++++++ .../jaggrab/JaggrabRequestEncoderTest.kt | 16 +++++++++++ 5 files changed, 77 insertions(+) create mode 100644 protocol/src/main/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequest.kt create mode 100644 protocol/src/main/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestDecoder.kt create mode 100644 protocol/src/main/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestEncoder.kt create mode 100644 protocol/src/test/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestDecoderTest.kt create mode 100644 protocol/src/test/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestEncoderTest.kt diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequest.kt b/protocol/src/main/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequest.kt new file mode 100644 index 00000000..02027d6a --- /dev/null +++ b/protocol/src/main/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequest.kt @@ -0,0 +1,3 @@ +package org.openrs2.protocol.jaggrab + +public data class JaggrabRequest(public val path: String) diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestDecoder.kt b/protocol/src/main/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestDecoder.kt new file mode 100644 index 00000000..23f5ec04 --- /dev/null +++ b/protocol/src/main/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestDecoder.kt @@ -0,0 +1,19 @@ +package org.openrs2.protocol.jaggrab + +import io.netty.channel.ChannelHandler +import io.netty.channel.ChannelHandlerContext +import io.netty.handler.codec.DecoderException +import io.netty.handler.codec.MessageToMessageDecoder + +@ChannelHandler.Sharable +public object JaggrabRequestDecoder : MessageToMessageDecoder() { + private const val PREFIX = "JAGGRAB " + + override fun decode(ctx: ChannelHandlerContext, msg: String, out: MutableList) { + if (!msg.startsWith(PREFIX)) { + throw DecoderException("JAGGRAB request has invalid prefix") + } + + out += JaggrabRequest(msg.substring(PREFIX.length)) + } +} diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestEncoder.kt b/protocol/src/main/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestEncoder.kt new file mode 100644 index 00000000..0cfe4db2 --- /dev/null +++ b/protocol/src/main/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestEncoder.kt @@ -0,0 +1,12 @@ +package org.openrs2.protocol.jaggrab + +import io.netty.channel.ChannelHandler +import io.netty.channel.ChannelHandlerContext +import io.netty.handler.codec.MessageToMessageEncoder + +@ChannelHandler.Sharable +public object JaggrabRequestEncoder : MessageToMessageEncoder() { + override fun encode(ctx: ChannelHandlerContext, msg: JaggrabRequest, out: MutableList) { + out += "JAGGRAB ${msg.path}\n\n" + } +} diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestDecoderTest.kt b/protocol/src/test/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestDecoderTest.kt new file mode 100644 index 00000000..6c554d03 --- /dev/null +++ b/protocol/src/test/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestDecoderTest.kt @@ -0,0 +1,27 @@ +package org.openrs2.protocol.jaggrab + +import io.netty.channel.embedded.EmbeddedChannel +import io.netty.handler.codec.DecoderException +import org.junit.jupiter.api.assertThrows +import kotlin.test.Test +import kotlin.test.assertEquals + +object JaggrabRequestDecoderTest { + @Test + fun testDecode() { + val channel = EmbeddedChannel(JaggrabRequestDecoder) + channel.writeInbound("JAGGRAB /runescape.pack200") + + val actual = channel.readInbound() + assertEquals(JaggrabRequest("/runescape.pack200"), actual) + } + + @Test + fun testInvalid() { + val channel = EmbeddedChannel(JaggrabRequestDecoder) + + assertThrows { + channel.writeInbound("Hello, world!") + } + } +} diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestEncoderTest.kt b/protocol/src/test/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestEncoderTest.kt new file mode 100644 index 00000000..34457012 --- /dev/null +++ b/protocol/src/test/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestEncoderTest.kt @@ -0,0 +1,16 @@ +package org.openrs2.protocol.jaggrab + +import io.netty.channel.embedded.EmbeddedChannel +import kotlin.test.Test +import kotlin.test.assertEquals + +object JaggrabRequestEncoderTest { + @Test + fun testEncode() { + val channel = EmbeddedChannel(JaggrabRequestEncoder) + channel.writeOutbound(JaggrabRequest("/runescape.pack200")) + + val actual = channel.readOutbound() + assertEquals("JAGGRAB /runescape.pack200\n\n", actual) + } +}