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) + } +}