-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updates for initial libp2p integration
- Loading branch information
Showing
17 changed files
with
441 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
app/src/main/java/org/witness/proofmode/share/p2p/ChatController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.witness.proofmode.org.witness.proofmode.share.p2p | ||
|
||
import io.libp2p.core.PeerId | ||
import io.libp2p.core.Stream | ||
import io.libp2p.core.multistream.ProtocolId | ||
import io.libp2p.core.multistream.StrictProtocolBinding | ||
import io.libp2p.etc.types.toByteBuf | ||
import io.libp2p.protocol.ProtocolHandler | ||
import io.libp2p.protocol.ProtocolMessageHandler | ||
import io.netty.buffer.ByteBuf | ||
import java.nio.charset.Charset | ||
import java.util.concurrent.CompletableFuture | ||
|
||
interface ChatController { | ||
fun send(message: String) | ||
} | ||
|
||
typealias OnChatMessage = (PeerId, String) -> Unit | ||
|
||
class Chat(chatCallback: OnChatMessage) : ChatBinding(ChatProtocol(chatCallback)) | ||
|
||
const val protocolId: ProtocolId = "/example/chat/0.1.0" | ||
|
||
open class ChatBinding(echo: ChatProtocol) : StrictProtocolBinding<ChatController>(protocolId, echo) | ||
|
||
open class ChatProtocol( | ||
private val chatCallback: OnChatMessage | ||
) : ProtocolHandler<ChatController>(Long.MAX_VALUE, Long.MAX_VALUE) { | ||
|
||
override fun onStartInitiator(stream: Stream) = onStart(stream) | ||
override fun onStartResponder(stream: Stream) = onStart(stream) | ||
|
||
private fun onStart(stream: Stream): CompletableFuture<ChatController> { | ||
val ready = CompletableFuture<Void>() | ||
val handler = Chatter(chatCallback, ready) | ||
stream.pushHandler(handler) | ||
return ready.thenApply { handler } | ||
} | ||
|
||
open inner class Chatter( | ||
private val chatCallback: OnChatMessage, | ||
val ready: CompletableFuture<Void> | ||
) : ProtocolMessageHandler<ByteBuf>, ChatController { | ||
lateinit var stream: Stream | ||
|
||
override fun onActivated(stream: Stream) { | ||
this.stream = stream | ||
ready.complete(null) | ||
} | ||
|
||
override fun onMessage(stream: Stream, msg: ByteBuf) { | ||
val msgStr = msg.toString(Charset.defaultCharset()) | ||
chatCallback(stream.remotePeerId(), msgStr) | ||
} | ||
|
||
override fun send(message: String) { | ||
val data = message.toByteArray(Charset.defaultCharset()) | ||
stream.writeAndFlush(data.toByteBuf()) | ||
} | ||
} | ||
} |
Oops, something went wrong.