Skip to content

Commit 8aa477d

Browse files
Move QUIC implementation to prototype 🍋 (#423)
--------- Co-authored-by: Anton Nashatyrev <[email protected]>
1 parent 1419d27 commit 8aa477d

File tree

17 files changed

+472
-148
lines changed

17 files changed

+472
-148
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ List of components in the Libp2p spec and their JVM implementation status
1515
| | Component | Status |
1616
|--------------------------|-------------------------------------------------------------------------------------------------|:----------------:|
1717
| **Transport** | tcp | :green_apple: |
18-
| | [quic](https://github.com/libp2p/specs/tree/master/quic) | :tomato: |
18+
| | [quic](https://github.com/libp2p/specs/tree/master/quic) | :lemon: |
1919
| | websocket | :lemon: |
2020
| | [webtransport](https://github.com/libp2p/specs/tree/master/webtransport) | |
2121
| | [webrtc-browser-to-server](https://github.com/libp2p/specs/blob/master/webrtc/webrtc-direct.md) | |
File renamed without changes.
File renamed without changes.

libp2p/src/main/kotlin/io/libp2p/core/dsl/Builders.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ open class Builder {
9090
*/
9191
open fun transports(fn: TransportsBuilder.() -> Unit): Builder = apply { fn(transports) }
9292

93+
/**
94+
* Manipulates the secure transports for this host.
95+
*/
96+
open fun secureTransports(fn: SecureTransportsBuilder.() -> Unit): Builder = apply { fn(secureTransports) }
97+
9398
/**
9499
* [AddressBook] implementation
95100
*/

libp2p/src/main/kotlin/io/libp2p/etc/types/NettyExt.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ fun ChannelFuture.toCompletableFuture(): CompletableFuture<Channel> {
2121
return ret
2222
}
2323

24-
fun Future<*>.toVoidCompletableFuture(): CompletableFuture<Unit> {
25-
val ret = CompletableFuture<Unit>()
24+
fun Future<*>.toVoidCompletableFuture(): CompletableFuture<Unit> = toCompletableFuture().thenApply { }
25+
26+
fun <T> Future<T>.toCompletableFuture(): CompletableFuture<T> {
27+
val ret = CompletableFuture<T>()
2628
this.addListener {
2729
if (it.isSuccess) {
28-
ret.complete(Unit)
30+
@Suppress("UNCHECKED_CAST")
31+
ret.complete(it.get() as T)
2932
} else {
3033
ret.completeExceptionally(it.cause())
3134
}
@@ -45,5 +48,5 @@ fun ChannelPipeline.getHandlerName(handler: ChannelHandler) = (
4548
?: throw IllegalArgumentException("Handler $handler not found in pipeline $this")
4649
)
4750

48-
fun ChannelPipeline.addAfter(handler: ChannelHandler, newHandlerName: String, newHandler: ChannelHandler) =
51+
fun ChannelPipeline.addAfter(handler: ChannelHandler, newHandlerName: String, newHandler: ChannelHandler): ChannelPipeline =
4952
addAfter(getHandlerName(handler), newHandlerName, newHandler)

libp2p/src/main/kotlin/io/libp2p/etc/util/netty/NettyUtil.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package io.libp2p.etc.util.netty
22

33
import io.libp2p.etc.types.addAfter
4-
import io.libp2p.etc.types.fromHex
54
import io.netty.channel.Channel
65
import io.netty.channel.ChannelHandler
76
import io.netty.channel.ChannelInitializer
87
import io.netty.util.internal.StringUtil
98

10-
class NettyInit(val channel: Channel, val thisHandler: ChannelHandler) {
9+
class NettyInit(val channel: Channel, thisHandler: ChannelHandler) {
1110
private var lastLocalHandler = thisHandler
1211
fun addLastLocal(handler: ChannelHandler) {
1312
channel.pipeline().addAfter(lastLocalHandler, generateName(channel, handler), handler)
@@ -23,13 +22,6 @@ fun nettyInitializer(initer: (NettyInit) -> Unit): ChannelInitializer<Channel> {
2322
}
2423
}
2524

26-
private val regex = Regex("\\|[0-9a-fA-F]{8}\\| ")
27-
fun String.fromLogHandler() = lines()
28-
.filter { it.contains(regex) }
29-
.map { it.substring(11, 59).replace(" ", "") }
30-
.flatMap { it.fromHex().asList() }
31-
.toByteArray()
32-
3325
private fun generateName(ch: Channel, handler: ChannelHandler): String {
3426
val className = StringUtil.simpleClassName(handler.javaClass)
3527
val names = ch.pipeline().names().toSet()

libp2p/src/main/kotlin/io/libp2p/protocol/ProtocolMessageHandler.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ interface ProtocolMessageHandler<TMessage> {
66
fun onActivated(stream: Stream) = Unit
77
fun onMessage(stream: Stream, msg: TMessage) = Unit
88
fun onClosed(stream: Stream) = Unit
9+
fun onReadClosed(stream: Stream) = Unit
910
fun onException(cause: Throwable?) = Unit
10-
11-
fun fireMessage(stream: Stream, msg: Any) {
12-
@Suppress("UNCHECKED_CAST")
13-
onMessage(stream, msg as TMessage)
14-
}
1511
}

libp2p/src/main/kotlin/io/libp2p/protocol/ProtocolMessageHandlerAdapter.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.libp2p.protocol
22

33
import io.libp2p.core.Stream
4+
import io.libp2p.etc.util.netty.mux.RemoteWriteClosed
45
import io.netty.channel.ChannelHandlerContext
56
import io.netty.channel.SimpleChannelInboundHandler
67
import io.netty.util.ReferenceCounted
@@ -33,7 +34,8 @@ class ProtocolMessageHandlerAdapter<TMessage>(
3334
}
3435

3536
override fun channelRead0(ctx: ChannelHandlerContext?, msg: Any) {
36-
pmh.fireMessage(stream, msg)
37+
@Suppress("UNCHECKED_CAST")
38+
pmh.onMessage(stream, msg as TMessage)
3739
}
3840

3941
override fun channelUnregistered(ctx: ChannelHandlerContext?) {
@@ -44,6 +46,13 @@ class ProtocolMessageHandlerAdapter<TMessage>(
4446
pmh.onException(cause)
4547
}
4648

49+
override fun userEventTriggered(ctx: ChannelHandlerContext, evt: Any) {
50+
if (evt == RemoteWriteClosed) {
51+
pmh.onReadClosed(stream)
52+
}
53+
super.userEventTriggered(ctx, evt)
54+
}
55+
4756
// ///////////////////////
4857
private fun refCount(obj: Any): Int {
4958
return if (obj is ReferenceCounted) {

libp2p/src/main/kotlin/io/libp2p/security/tls/TLSSecureChannel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fun buildTlsHandler(
104104
handshakeComplete: CompletableFuture<SecureChannel.Session>,
105105
ctx: ChannelHandlerContext
106106
): SslHandler {
107-
val connectionKeys = if (certAlgorithm.equals("ECDSA")) generateEcdsaKeyPair() else generateEd25519KeyPair()
107+
val connectionKeys = if (certAlgorithm == "ECDSA") generateEcdsaKeyPair() else generateEd25519KeyPair()
108108
val javaPrivateKey = getJavaKey(connectionKeys.first)
109109
val sslContext = (
110110
if (isInitiator) {

libp2p/src/main/kotlin/io/libp2p/transport/implementation/StreamOverNetty.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import io.libp2p.etc.types.toVoidCompletableFuture
88
import io.netty.channel.Channel
99
import java.util.concurrent.CompletableFuture
1010

11-
class StreamOverNetty(
11+
open class StreamOverNetty(
1212
ch: Channel,
1313
override val connection: Connection,
1414
initiator: Boolean

0 commit comments

Comments
 (0)