Skip to content

Commit

Permalink
change flag to flags for yamux
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Aug 15, 2023
1 parent 8761a01 commit 05a87b3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions libp2p/src/main/kotlin/io/libp2p/mux/yamux/YamuxFrame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import io.netty.buffer.Unpooled
/**
* Contains the fields that comprise a yamux frame.
* @param id the ID of the stream.
* @param flag the flag value for this frame.
* @param flags the flags value for this frame.
* @param length the length field for this frame.
* @param data the data segment.
*/
class YamuxFrame(val id: MuxId, val type: Int, val flag: Int, val length: Long, val data: ByteBuf? = null) :
class YamuxFrame(val id: MuxId, val type: Int, val flags: Int, val length: Long, val data: ByteBuf? = null) :
DefaultByteBufHolder(data ?: Unpooled.EMPTY_BUFFER) {

override fun toString(): String {
if (data == null) {
return "YamuxFrame(id=$id, type=$type, flag=$flag, length=$length)"
return "YamuxFrame(id=$id, type=$type, flags=$flags, length=$length)"
}
return "YamuxFrame(id=$id, type=$type, flag=$flag, length=$length, data=${String(data.toByteArray())})"
return "YamuxFrame(id=$id, type=$type, flags=$flags, length=$length, data=${String(data.toByteArray())})"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class YamuxFrameCodec(
override fun encode(ctx: ChannelHandlerContext, msg: YamuxFrame, out: ByteBuf) {
out.writeByte(0) // version
out.writeByte(msg.type)
out.writeShort(msg.flag)
out.writeShort(msg.flags)
out.writeInt(msg.id.id.toInt())
out.writeInt(msg.data?.readableBytes() ?: msg.length.toInt())
out.writeBytes(msg.data ?: Unpooled.EMPTY_BUFFER)
Expand Down
10 changes: 5 additions & 5 deletions libp2p/src/main/kotlin/io/libp2p/mux/yamux/YamuxHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ open class YamuxHandler(

private fun handlePing(msg: YamuxFrame) {
val ctx = getChannelHandlerContext()
when (msg.flag) {
when (msg.flags) {
YamuxFlags.SYN -> ctx.writeAndFlush(
YamuxFrame(
MuxId(msg.id.parentId, 0, msg.id.initiator),
Expand All @@ -88,9 +88,9 @@ open class YamuxHandler(
}
}

private fun handleFlag(msg: YamuxFrame) {
private fun handleFlags(msg: YamuxFrame) {
val ctx = getChannelHandlerContext()
when (msg.flag) {
when (msg.flags) {
YamuxFlags.SYN -> {
// ACK the new stream
onRemoteYamuxOpen(msg.id)
Expand All @@ -105,7 +105,7 @@ open class YamuxHandler(
private fun handleDataRead(msg: YamuxFrame) {
val ctx = getChannelHandlerContext()
val size = msg.length
handleFlag(msg)
handleFlags(msg)
if (size.toInt() == 0) {
return
}
Expand All @@ -125,7 +125,7 @@ open class YamuxHandler(
}

private fun handleWindowUpdate(msg: YamuxFrame) {
handleFlag(msg)
handleFlags(msg)
val size = msg.length.toInt()
if (size == 0) {
return
Expand Down
16 changes: 8 additions & 8 deletions libp2p/src/test/kotlin/io/libp2p/mux/yamux/YamuxHandlerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class YamuxHandlerTest : MuxHandlerAbstractTest() {
override fun readFrame(): AbstractTestMuxFrame? {
val yamuxFrame = readYamuxFrame()
if (yamuxFrame != null) {
when (yamuxFrame.flag) {
when (yamuxFrame.flags) {
YamuxFlags.SYN -> readFrameQueue += AbstractTestMuxFrame(yamuxFrame.id.id, Open)
}

Expand All @@ -66,7 +66,7 @@ class YamuxHandlerTest : MuxHandlerAbstractTest() {
readFrameQueue += AbstractTestMuxFrame(yamuxFrame.id.id, Data, data)
}

when (yamuxFrame.flag) {
when (yamuxFrame.flags) {
YamuxFlags.FIN -> readFrameQueue += AbstractTestMuxFrame(yamuxFrame.id.id, Close)
YamuxFlags.RST -> readFrameQueue += AbstractTestMuxFrame(yamuxFrame.id.id, Reset)
}
Expand All @@ -91,7 +91,7 @@ class YamuxHandlerTest : MuxHandlerAbstractTest() {
val ackFrame = readYamuxFrameOrThrow()

// receives ack stream
assertThat(ackFrame.flag).isEqualTo(YamuxFlags.ACK)
assertThat(ackFrame.flags).isEqualTo(YamuxFlags.ACK)
assertThat(ackFrame.type).isEqualTo(YamuxType.WINDOW_UPDATE)

closeStream(12)
Expand All @@ -109,7 +109,7 @@ class YamuxHandlerTest : MuxHandlerAbstractTest() {

val windowUpdateFrame = readYamuxFrameOrThrow()

assertThat(windowUpdateFrame.flag).isZero()
assertThat(windowUpdateFrame.flags).isZero()
assertThat(windowUpdateFrame.type).isEqualTo(YamuxType.WINDOW_UPDATE)
assertThat(windowUpdateFrame.length).isEqualTo((INITIAL_WINDOW_SIZE + 1).toLong())

Expand All @@ -129,7 +129,7 @@ class YamuxHandlerTest : MuxHandlerAbstractTest() {
YamuxType.WINDOW_UPDATE,
YamuxFlags.ACK,
-INITIAL_WINDOW_SIZE.toLong(),
),
)
)

handler.ctx.writeAndFlush("1984".fromHex().toByteBuf(allocateBuf()))
Expand All @@ -152,15 +152,15 @@ class YamuxHandlerTest : MuxHandlerAbstractTest() {
YamuxFlags.SYN,
// opaque value, echoed back
3,
),
)
)

// ignore ack stream frame
readYamuxFrameOrThrow()

val pingFrame = readYamuxFrameOrThrow()

assertThat(pingFrame.flag).isEqualTo(YamuxFlags.ACK)
assertThat(pingFrame.flags).isEqualTo(YamuxFlags.ACK)
assertThat(pingFrame.type).isEqualTo(YamuxType.PING)
assertThat(pingFrame.length).isEqualTo(3)

Expand All @@ -178,7 +178,7 @@ class YamuxHandlerTest : MuxHandlerAbstractTest() {
0,
// normal termination
0x0,
),
)
)

// verify session termination
Expand Down

0 comments on commit 05a87b3

Please sign in to comment.