nim-libp2p-experimental/tests/test.nim

79 lines
2.6 KiB
Nim
Raw Normal View History

2019-09-12 02:10:38 +00:00
import tables, options, sequtils
2019-09-10 02:15:52 +00:00
import chronos, chronicles
import ../libp2p/switch,
../libp2p/multistream,
../libp2p/protocols/identify,
../libp2p/connection,
../libp2p/transports/[transport, tcptransport],
../libp2p/multiaddress,
../libp2p/peerinfo,
../libp2p/crypto/crypto,
../libp2p/peer,
../libp2p/protocols/protocol,
../libp2p/muxers/muxer,
../libp2p/muxers/mplex/mplex,
../libp2p/muxers/mplex/types,
../libp2p/protocols/secure/secure,
2019-09-12 02:10:38 +00:00
../libp2p/protocols/secure/secio,
../libp2p/protocols/pubsub/pubsub,
../libp2p/protocols/pubsub/floodsub
2019-09-10 02:15:52 +00:00
type
TestProto = ref object of LPProtocol
2019-09-12 02:10:38 +00:00
switch*: Switch
2019-09-10 02:15:52 +00:00
method init(p: TestProto) {.gcsafe.} =
2019-09-12 02:10:38 +00:00
proc handle(stream: Connection, proto: string) {.async, gcsafe.} = discard
2019-09-10 02:15:52 +00:00
p.codec = "/test/proto/1.0.0"
p.handler = handle
2019-09-12 02:10:38 +00:00
proc newTestProto(switch: Switch): TestProto =
2019-09-10 02:15:52 +00:00
new result
2019-09-12 02:10:38 +00:00
result.switch = switch
2019-09-10 02:15:52 +00:00
result.init()
proc main() {.async.} =
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/30333")
let seckey = PrivateKey.random(RSA)
var peerInfo: PeerInfo
2019-09-12 02:10:38 +00:00
peerInfo.peerId = some(PeerID.init(seckey))
2019-09-10 02:15:52 +00:00
peerInfo.addrs.add(Multiaddress.init("/ip4/127.0.0.1/tcp/55055"))
proc createMplex(conn: Connection): Muxer =
result = newMplex(conn)
let mplexProvider = newMuxerProvider(createMplex, MplexCodec)
let transports = @[Transport(newTransport(TcpTransport))]
let muxers = [(MplexCodec, mplexProvider)].toTable()
let identify = newIdentify(peerInfo)
2019-09-12 02:10:38 +00:00
# let secureManagers = @[Secure(newSecIo(seckey.getKey()))]
let pubSub = some(PubSub(newFloodSub(peerInfo)))
let switch = newSwitch(peerInfo, transports, identify, muxers, pubSub = pubSub)
2019-09-10 02:15:52 +00:00
2019-09-12 02:10:38 +00:00
var libp2pFuts = await switch.start()
echo "Right after start"
for item in libp2pFuts:
echo item.finished
2019-09-10 02:15:52 +00:00
var remotePeer: PeerInfo
2019-09-12 02:10:38 +00:00
remotePeer.peerId = some(PeerID.init("QmUA1Ghihi5u3gDwEDxhbu49jU42QPbvHttZFwB6b4K5oC"))
2019-09-10 02:15:52 +00:00
remotePeer.addrs.add(ma)
2019-09-12 02:10:38 +00:00
switch.mount(newTestProto(switch))
echo "PeerID: " & peerInfo.peerId.get().pretty
# let conn = await switch.dial(remotePeer, "/test/proto/1.0.0")
await switch.subscribeToPeer(remotePeer)
proc handler(topic: string, data: seq[byte]): Future[void] {.closure, gcsafe.} =
debug "IN HANDLER"
await switch.subscribe("mytesttopic", handler)
# let msg = cast[seq[byte]]("hello from nim")
# await switch.publish("mytesttopic", msg)
# debug "published message from test"
# TODO: for some reason the connection closes unless I do a forever loop
await allFutures(libp2pFuts)
2019-09-10 02:15:52 +00:00
waitFor(main())