Add JAGGRAB request codec

Signed-off-by: Graham <gpe@openrs2.org>
pull/132/head
Graham 3 years ago
parent 30b605d719
commit 96ea95394c
  1. 3
      protocol/src/main/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequest.kt
  2. 19
      protocol/src/main/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestDecoder.kt
  3. 12
      protocol/src/main/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestEncoder.kt
  4. 27
      protocol/src/test/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestDecoderTest.kt
  5. 16
      protocol/src/test/kotlin/org/openrs2/protocol/jaggrab/JaggrabRequestEncoderTest.kt

@ -0,0 +1,3 @@
package org.openrs2.protocol.jaggrab
public data class JaggrabRequest(public val path: String)

@ -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<String>() {
private const val PREFIX = "JAGGRAB "
override fun decode(ctx: ChannelHandlerContext, msg: String, out: MutableList<Any>) {
if (!msg.startsWith(PREFIX)) {
throw DecoderException("JAGGRAB request has invalid prefix")
}
out += JaggrabRequest(msg.substring(PREFIX.length))
}
}

@ -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<JaggrabRequest>() {
override fun encode(ctx: ChannelHandlerContext, msg: JaggrabRequest, out: MutableList<Any>) {
out += "JAGGRAB ${msg.path}\n\n"
}
}

@ -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<JaggrabRequest>()
assertEquals(JaggrabRequest("/runescape.pack200"), actual)
}
@Test
fun testInvalid() {
val channel = EmbeddedChannel(JaggrabRequestDecoder)
assertThrows<DecoderException> {
channel.writeInbound("Hello, world!")
}
}
}

@ -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<String>()
assertEquals("JAGGRAB /runescape.pack200\n\n", actual)
}
}
Loading…
Cancel
Save