nim-libp2p-experimental/examples/directchat.nim

199 lines
5.1 KiB
Nim
Raw Normal View History

2019-09-12 23:20:30 +00:00
when not(compileOption("threads")):
{.fatal: "Please, compile this program with the --threads:on option!".}
import
strformat, strutils, bearssl,
stew/byteutils,
chronos,
../libp2p
2019-09-12 21:54:12 +00:00
const DefaultAddr = "/ip4/127.0.0.1/tcp/0"
2019-09-13 00:05:20 +00:00
2019-09-12 23:20:30 +00:00
const Help = """
Commands: /[?|help|connect|disconnect|exit]
2019-09-12 23:20:30 +00:00
help: Prints this help
connect: dials a remote peer
disconnect: ends current session
exit: closes the chat
"""
type
Chat = ref object
switch: Switch # a single entry point for dialing and listening to peer
stdinReader: StreamTransport # transport streams between read & write file descriptor
conn: Connection # connection to the other peer
connected: bool # if the node is connected to another peer
##
# Stdout helpers, to write the prompt
##
proc writePrompt(c: Chat) =
if c.connected:
stdout.write '\r' & $c.switch.peerInfo.peerId & ": "
stdout.flushFile()
proc writeStdout(c: Chat, str: string) =
echo '\r' & str
c.writePrompt()
##
# Chat Protocol
##
const ChatCodec = "/nim-libp2p/chat/1.0.0"
2019-09-13 00:05:20 +00:00
type
ChatProto = ref object of LPProtocol
2019-09-12 21:54:12 +00:00
proc new(T: typedesc[ChatProto], c: Chat): T =
let chatproto = T()
# create handler for incoming connection
proc handle(stream: Connection, proto: string) {.async.} =
if c.connected and not c.conn.closed:
c.writeStdout "a chat session is already in progress - refusing incoming peer!"
await stream.close()
else:
await c.handlePeer(stream)
await stream.close()
# assign the new handler
chatproto.handler = handle
chatproto.codec = ChatCodec
return chatproto
##
# Chat application
##
proc handlePeer(c: Chat, conn: Connection) {.async.} =
# Handle a peer (incoming or outgoing)
try:
c.conn = conn
c.connected = true
c.writeStdout $conn.peerId & " connected"
# Read loop
while true:
let
strData = await conn.readLp(1024)
str = string.fromBytes(strData)
c.writeStdout $conn.peerId & ": " & $str
except LPStreamEOFError:
defer: c.writeStdout $conn.peerId & " disconnected"
await c.conn.close()
c.connected = false
proc dialPeer(c: Chat, address: string) {.async.} =
# Parse and dial address
let
multiAddr = MultiAddress.init(address).tryGet()
# split the peerId part /p2p/...
peerIdBytes = multiAddr[multiCodec("p2p")]
.tryGet()
.protoAddress()
.tryGet()
2021-12-16 10:05:20 +00:00
remotePeer = PeerId.init(peerIdBytes).tryGet()
# split the wire address
ip4Addr = multiAddr[multiCodec("ip4")].tryGet()
tcpAddr = multiAddr[multiCodec("tcp")].tryGet()
wireAddr = ip4Addr & tcpAddr
2019-09-12 21:54:12 +00:00
echo &"dialing peer: {multiAddr}"
asyncSpawn c.handlePeer(await c.switch.dial(remotePeer, @[wireAddr], ChatCodec))
proc readLoop(c: Chat) {.async.} =
2019-09-12 21:54:12 +00:00
while true:
if not c.connected:
2019-09-12 21:54:12 +00:00
echo "type an address or wait for a connection:"
2019-09-12 23:20:30 +00:00
echo "type /[help|?] for help"
2019-09-12 21:54:12 +00:00
c.writePrompt()
let line = await c.stdinReader.readLine()
if line.startsWith("/help") or line.startsWith("/?"):
2019-09-12 23:20:30 +00:00
echo Help
continue
2019-09-12 23:20:30 +00:00
if line.startsWith("/disconnect"):
c.writeStdout "Ending current session"
if c.connected and c.conn.closed.not:
await c.conn.close()
c.connected = false
2019-09-12 23:20:30 +00:00
elif line.startsWith("/connect"):
c.writeStdout "enter address of remote peer"
let address = await c.stdinReader.readLine()
2019-09-12 23:20:30 +00:00
if address.len > 0:
await c.dialPeer(address)
2019-09-12 23:20:30 +00:00
elif line.startsWith("/exit"):
if c.connected and c.conn.closed.not:
await c.conn.close()
c.connected = false
2019-09-13 00:05:20 +00:00
await c.switch.stop()
c.writeStdout "quitting..."
return
2019-09-12 21:54:12 +00:00
else:
if c.connected:
await c.conn.writeLp(line)
2019-09-12 23:20:30 +00:00
else:
try:
if line.startsWith("/") and "p2p" in line:
await c.dialPeer(line)
except CatchableError as exc:
echo &"unable to dial remote peer {line}"
echo exc.msg
2019-09-12 21:54:12 +00:00
proc readInput(wfd: AsyncFD) {.thread.} =
## This thread performs reading from `stdin` and sends data over
2019-09-12 21:54:12 +00:00
## pipe to main thread.
let transp = fromPipe(wfd)
2019-09-12 21:54:12 +00:00
while true:
let line = stdin.readLine()
2019-09-25 19:33:50 +00:00
discard waitFor transp.write(line & "\r\n")
2019-09-12 21:54:12 +00:00
proc main() {.async.} =
let
rng = newRng() # Single random number source for the whole application
2019-09-12 21:54:12 +00:00
# Pipe to read stdin from main thread
(rfd, wfd) = createAsyncPipe()
stdinReader = fromPipe(rfd)
var thread: Thread[AsyncFD]
try:
thread.createThread(readInput, wfd)
except Exception as exc:
quit("Failed to create thread: " & exc.msg)
var localAddress = MultiAddress.init(DefaultAddr).tryGet()
2019-09-13 00:05:20 +00:00
var switch = SwitchBuilder
.new()
.withRng(rng) # Give the application RNG
.withAddress(localAddress)
.withTcpTransport() # Use TCP as transport
.withMplex() # Use Mplex as muxer
.withNoise() # Use Noise as secure manager
.build()
2019-09-12 21:54:12 +00:00
let chat = Chat(
switch: switch,
stdinReader: stdinReader)
switch.mount(ChatProto.new(chat))
await switch.start()
2019-09-12 23:20:30 +00:00
let id = $switch.peerInfo.peerId
2021-12-16 10:05:20 +00:00
echo "PeerId: " & id
2019-09-12 21:54:12 +00:00
echo "listening on: "
for a in switch.peerInfo.addrs:
echo &"{a}/p2p/{id}"
2019-09-12 21:54:12 +00:00
await chat.readLoop()
quit(0)
2019-09-12 21:54:12 +00:00
waitFor(main())