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,
|
2019-09-24 16:16:39 +00:00
|
|
|
../libp2p/protocols/pubsub/floodsub,
|
|
|
|
../libp2p/base58
|
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-24 16:16:39 +00:00
|
|
|
proc handle(stream: Connection, proto: string) {.async, gcsafe.} =
|
|
|
|
echo "IN PROTO HANDLER!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
|
|
echo cast[string](await stream.readLp())
|
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.} =
|
2019-09-24 16:16:39 +00:00
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/52521")
|
2019-09-10 02:15:52 +00:00
|
|
|
|
|
|
|
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-24 16:16:39 +00:00
|
|
|
let secureManagers = [(SecioCodec, Secure(newSecio(seckey)))].toTable()
|
2019-09-12 02:10:38 +00:00
|
|
|
let pubSub = some(PubSub(newFloodSub(peerInfo)))
|
2019-09-24 16:16:39 +00:00
|
|
|
let switch = newSwitch(peerInfo,
|
|
|
|
transports,
|
|
|
|
identify,
|
|
|
|
muxers,
|
|
|
|
secureManagers,
|
|
|
|
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-24 16:16:39 +00:00
|
|
|
remotePeer.peerId = some(PeerID.init("QmPT854SM2WqCAXm4KsYkJs1NPft64m7ubaa8mgV5Tvvqg"))
|
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")
|
2019-09-24 16:16:39 +00:00
|
|
|
# await conn.writeLp(cast[seq[byte]]("Hello from nim!!"))
|
2019-09-12 02:10:38 +00:00
|
|
|
await switch.subscribeToPeer(remotePeer)
|
|
|
|
|
|
|
|
proc handler(topic: string, data: seq[byte]): Future[void] {.closure, gcsafe.} =
|
|
|
|
debug "IN HANDLER"
|
|
|
|
|
2019-09-24 16:16:39 +00:00
|
|
|
let topic = Base58.encode(cast[seq[byte]]("chat"))
|
|
|
|
await switch.subscribe(topic, handler)
|
|
|
|
let msg = cast[seq[byte]]("hello from nim")
|
|
|
|
await switch.publish(topic, msg)
|
2019-09-12 02:10:38 +00:00
|
|
|
# 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())
|