2021-11-08 12:00:44 +00:00
|
|
|
import chronos # an efficient library for async
|
|
|
|
import stew/byteutils # various utils
|
2022-09-28 08:40:53 +00:00
|
|
|
import libp2p
|
2021-11-08 12:00:44 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Create our custom protocol
|
|
|
|
##
|
|
|
|
const TestCodec = "/test/proto/1.0.0" # custom protocol string identifier
|
|
|
|
|
|
|
|
type
|
|
|
|
TestProto = ref object of LPProtocol # declare a custom protocol
|
|
|
|
|
|
|
|
proc new(T: typedesc[TestProto]): T =
|
|
|
|
|
|
|
|
# every incoming connections will be in handled in this closure
|
2023-12-05 07:05:32 +00:00
|
|
|
proc handle(conn: Connection, proto: string) {.async.} =
|
2021-11-08 12:00:44 +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-03-28 08:42:31 +00:00
|
|
|
return T.new(codecs = @[TestCodec], handler = handle)
|
2021-11-08 12:00:44 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Helper to create a switch/node
|
|
|
|
##
|
2022-06-16 08:08:52 +00:00
|
|
|
proc createSwitch(ma: MultiAddress, rng: ref HmacDrbgContext): Switch =
|
2021-11-08 12:00:44 +00:00
|
|
|
var switch = SwitchBuilder
|
|
|
|
.new()
|
|
|
|
.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
|
|
|
|
.build()
|
|
|
|
|
|
|
|
result = switch
|
|
|
|
|
|
|
|
##
|
|
|
|
# The actual application
|
|
|
|
##
|
2023-12-05 07:05:32 +00:00
|
|
|
proc main() {.async.} =
|
2021-11-08 12:00:44 +00:00
|
|
|
let
|
|
|
|
rng = newRng() # Single random number source for the whole application
|
|
|
|
# port 0 will take a random available port
|
2021-12-16 10:05:20 +00:00
|
|
|
# `tryGet` will throw an exception if the MultiAddress failed
|
2021-11-08 12:00:44 +00:00
|
|
|
# (for instance, if the address is not well formatted)
|
2021-12-16 10:05:20 +00:00
|
|
|
ma1 = MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
|
|
|
ma2 = MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
2021-11-08 12:00:44 +00:00
|
|
|
|
|
|
|
# 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
|
2021-12-03 18:23:12 +00:00
|
|
|
await switch1.start()
|
|
|
|
await switch2.start()
|
2021-11-08 12:00:44 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec)
|
|
|
|
|
|
|
|
# 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()
|
|
|
|
|
|
|
|
await allFutures(switch1.stop(), switch2.stop()) # close connections and shutdown all transports
|
|
|
|
|
|
|
|
waitFor(main())
|