nim-libp2p/docs/helloworld.nim

95 lines
2.8 KiB
Nim
Raw Normal View History

2024-06-13 10:30:22 +00:00
import chronos # an efficient library for async
import stew/byteutils # various utils
2023-09-07 14:09:36 +00:00
import libp2p
##
# Create our custom protocol
##
const TestCodec = "/test/proto/1.0.0" # custom protocol string identifier
2024-06-13 10:30:22 +00:00
type TestProto = ref object of LPProtocol # declare a custom protocol
2023-09-07 14:09:36 +00:00
proc new(T: typedesc[TestProto]): T =
# every incoming connections will be in handled in this closure
2024-06-13 10:30:22 +00:00
proc handle(conn: Connection, proto: string) {.async.} =
2023-09-07 14:09:36 +00:00
echo "Got from remote - ", string.fromBytes(await conn.readLp(1024))
await conn.writeLp("Roger p2p!")
# We must close the connections ourselves when we're done with it
await conn.close()
2024-06-13 10:30:22 +00:00
return T.new(codecs = @[TestCodec], handler = handle)
2023-09-07 14:09:36 +00:00
##
# Helper to create a switch/node
##
proc createSwitch(ma: MultiAddress, rng: ref HmacDrbgContext): Switch =
var switch = SwitchBuilder
.new()
2024-06-13 10:30:22 +00:00
.withRng(rng)
# Give the application RNG
.withAddress(ma)
# Our local address(es)
.withTcpTransport()
# Use TCP as transport
.withMplex()
# Use Mplex as muxer
.withNoise()
# Use Noise as secure manager
2023-09-07 14:09:36 +00:00
.build()
result = switch
##
# The actual application
##
2024-06-13 10:30:22 +00:00
proc main() {.async.} =
2023-09-07 14:09:36 +00:00
let
rng = newRng() # Single random number source for the whole application
# port 0 will take a random available port
# `tryGet` will throw an exception if the MultiAddress failed
# (for instance, if the address is not well formatted)
ma1 = MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
ma2 = MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
# setup the custom proto
let testProto = TestProto.new()
# setup the two nodes
let
switch1 = createSwitch(ma1, rng) #Create the two switches
switch2 = createSwitch(ma2, rng)
# mount the proto on switch1
# the node will now listen for this proto
# and call the handler everytime a client request it
switch1.mount(testProto)
# Start the nodes. This will start the transports
# and listen on each local addresses
await switch1.start()
await switch2.start()
# the node addrs is populated with it's
# actual port during the start
# use the second node to dial the first node
# using the first node peerid and address
# and specify our custom protocol codec
2024-06-13 10:30:22 +00:00
let conn =
await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec)
2023-09-07 14:09:36 +00:00
# conn is now a fully setup connection, we talk directly to the node1 custom protocol handler
await conn.writeLp("Hello p2p!") # writeLp send a length prefixed buffer over the wire
# readLp reads length prefixed bytes and returns a buffer without the prefix
echo "Remote responded with - ", string.fromBytes(await conn.readLp(1024))
# We must close the connection ourselves when we're done with it
await conn.close()
2024-06-13 10:30:22 +00:00
await allFutures(switch1.stop(), switch2.stop())
# close connections and shutdown all transports
2023-09-07 14:09:36 +00:00
waitFor(main())