2019-03-05 12:09:26 +00:00
|
|
|
import chronos, nimcrypto, strutils
|
2020-03-16 07:43:08 +00:00
|
|
|
import ../../libp2p/daemon/daemonapi
|
2018-11-22 14:12:46 +00:00
|
|
|
|
2020-03-16 07:43:08 +00:00
|
|
|
## nim c -r --threads:on chat.nim
|
2019-03-05 20:28:46 +00:00
|
|
|
when not(compileOption("threads")):
|
|
|
|
{.fatal: "Please, compile this program with the --threads:on option!".}
|
|
|
|
|
2018-11-22 14:12:46 +00:00
|
|
|
const
|
|
|
|
ServerProtocols = @["/test-chat-stream"]
|
|
|
|
|
|
|
|
type
|
|
|
|
CustomData = ref object
|
|
|
|
api: DaemonAPI
|
|
|
|
remotes: seq[StreamTransport]
|
2019-08-14 05:31:06 +00:00
|
|
|
consoleFd: AsyncFD
|
|
|
|
serveFut: Future[void]
|
2018-11-22 14:12:46 +00:00
|
|
|
|
2019-08-14 05:31:06 +00:00
|
|
|
proc threadMain(wfd: AsyncFD) {.thread.} =
|
2018-11-22 14:12:46 +00:00
|
|
|
## This procedure performs reading from `stdin` and sends data over
|
2019-08-14 05:31:06 +00:00
|
|
|
## pipe to main thread.
|
|
|
|
var transp = fromPipe(wfd)
|
2021-06-14 23:21:44 +00:00
|
|
|
|
2018-11-22 14:12:46 +00:00
|
|
|
while true:
|
|
|
|
var line = stdin.readLine()
|
|
|
|
let res = waitFor transp.write(line & "\r\n")
|
|
|
|
|
2019-08-14 05:31:06 +00:00
|
|
|
proc serveThread(udata: CustomData) {.async.} =
|
|
|
|
## This procedure perform reading on pipe and sends data to remote clients.
|
|
|
|
var transp = fromPipe(udata.consoleFd)
|
2018-11-22 15:35:44 +00:00
|
|
|
|
|
|
|
proc remoteReader(transp: StreamTransport) {.async.} =
|
|
|
|
while true:
|
|
|
|
var line = await transp.readLine()
|
|
|
|
if len(line) == 0:
|
|
|
|
break
|
|
|
|
echo ">> ", line
|
|
|
|
|
2018-11-22 14:12:46 +00:00
|
|
|
while true:
|
|
|
|
try:
|
|
|
|
var line = await transp.readLine()
|
|
|
|
if line.startsWith("/connect"):
|
|
|
|
var parts = line.split(" ")
|
|
|
|
if len(parts) == 2:
|
2021-12-16 10:05:20 +00:00
|
|
|
var peerId = PeerId.init(parts[1])
|
2019-03-05 12:09:26 +00:00
|
|
|
var address = MultiAddress.init(multiCodec("p2p-circuit"))
|
|
|
|
address &= MultiAddress.init(multiCodec("p2p"), peerId)
|
|
|
|
echo "= Searching for peer ", peerId.pretty()
|
2018-11-27 12:16:04 +00:00
|
|
|
var id = await udata.api.dhtFindPeer(peerId)
|
2018-11-27 20:58:16 +00:00
|
|
|
echo "= Peer " & parts[1] & " found at addresses:"
|
2018-11-27 12:16:04 +00:00
|
|
|
for item in id.addresses:
|
|
|
|
echo $item
|
|
|
|
echo "= Connecting to peer ", $address
|
|
|
|
await udata.api.connect(peerId, @[address], 30)
|
|
|
|
echo "= Opening stream to peer chat ", parts[1]
|
|
|
|
var stream = await udata.api.openStream(peerId, ServerProtocols)
|
2018-11-22 15:35:44 +00:00
|
|
|
udata.remotes.add(stream.transp)
|
2018-11-27 12:16:04 +00:00
|
|
|
echo "= Connected to peer chat ", parts[1]
|
2021-06-14 23:21:44 +00:00
|
|
|
asyncSpawn remoteReader(stream.transp)
|
2018-11-27 12:33:41 +00:00
|
|
|
elif line.startsWith("/search"):
|
|
|
|
var parts = line.split(" ")
|
|
|
|
if len(parts) == 2:
|
2021-12-16 10:05:20 +00:00
|
|
|
var peerId = PeerId.init(parts[1])
|
2019-03-05 12:09:26 +00:00
|
|
|
echo "= Searching for peer ", peerId.pretty()
|
2018-11-27 12:33:41 +00:00
|
|
|
var id = await udata.api.dhtFindPeer(peerId)
|
2018-11-27 20:58:16 +00:00
|
|
|
echo "= Peer " & parts[1] & " found at addresses:"
|
2018-11-27 12:33:41 +00:00
|
|
|
for item in id.addresses:
|
|
|
|
echo $item
|
|
|
|
elif line.startsWith("/consearch"):
|
|
|
|
var parts = line.split(" ")
|
|
|
|
if len(parts) == 2:
|
2021-12-16 10:05:20 +00:00
|
|
|
var peerId = PeerId.init(parts[1])
|
2018-11-27 12:33:41 +00:00
|
|
|
echo "= Searching for peers connected to peer ", parts[1]
|
|
|
|
var peers = await udata.api.dhtFindPeersConnectedToPeer(peerId)
|
2018-11-27 20:58:16 +00:00
|
|
|
echo "= Found ", len(peers), " connected to peer ", parts[1]
|
2018-11-27 12:33:41 +00:00
|
|
|
for item in peers:
|
2019-03-05 12:09:26 +00:00
|
|
|
var peer = item.peer
|
2018-11-27 20:58:16 +00:00
|
|
|
var addresses = newSeq[string]()
|
|
|
|
var relay = false
|
|
|
|
for a in item.addresses:
|
|
|
|
addresses.add($a)
|
|
|
|
if a.protoName() == "/p2p-circuit":
|
|
|
|
relay = true
|
|
|
|
break
|
|
|
|
if relay:
|
2019-03-05 12:09:26 +00:00
|
|
|
echo peer.pretty(), " * ", " [", addresses.join(", "), "]"
|
2018-11-27 20:58:16 +00:00
|
|
|
else:
|
2019-03-05 12:09:26 +00:00
|
|
|
echo peer.pretty(), " [", addresses.join(", "), "]"
|
2018-11-27 12:16:04 +00:00
|
|
|
elif line.startsWith("/exit"):
|
2019-08-14 05:31:06 +00:00
|
|
|
break
|
2018-11-22 14:12:46 +00:00
|
|
|
else:
|
|
|
|
var msg = line & "\r\n"
|
2018-11-22 15:35:44 +00:00
|
|
|
echo "<< ", line
|
2018-11-22 14:12:46 +00:00
|
|
|
var pending = newSeq[Future[int]]()
|
|
|
|
for item in udata.remotes:
|
|
|
|
pending.add(item.write(msg))
|
|
|
|
if len(pending) > 0:
|
|
|
|
var results = await all(pending)
|
|
|
|
except:
|
2019-03-05 20:28:46 +00:00
|
|
|
echo getCurrentException().msg
|
2018-11-22 14:12:46 +00:00
|
|
|
|
|
|
|
proc main() {.async.} =
|
|
|
|
var data = new CustomData
|
|
|
|
data.remotes = newSeq[StreamTransport]()
|
|
|
|
|
2019-08-14 05:31:06 +00:00
|
|
|
var (rfd, wfd) = createAsyncPipe()
|
|
|
|
if rfd == asyncInvalidPipe or wfd == asyncInvalidPipe:
|
|
|
|
raise newException(ValueError, "Could not initialize pipe!")
|
|
|
|
|
|
|
|
data.consoleFd = rfd
|
|
|
|
|
|
|
|
data.serveFut = serveThread(data)
|
|
|
|
var thread: Thread[AsyncFD]
|
|
|
|
thread.createThread(threadMain, wfd)
|
2018-11-22 14:12:46 +00:00
|
|
|
|
|
|
|
echo "= Starting P2P node"
|
2018-11-22 14:16:34 +00:00
|
|
|
data.api = await newDaemonApi({DHTFull, Bootstrap})
|
|
|
|
await sleepAsync(3000)
|
2018-11-22 14:12:46 +00:00
|
|
|
var id = await data.api.identity()
|
|
|
|
|
|
|
|
proc streamHandler(api: DaemonAPI, stream: P2PStream) {.async.} =
|
2019-03-05 12:09:26 +00:00
|
|
|
echo "= Peer ", stream.peer.pretty(), " joined chat"
|
2018-11-22 15:35:44 +00:00
|
|
|
data.remotes.add(stream.transp)
|
2018-11-22 14:12:46 +00:00
|
|
|
while true:
|
|
|
|
var line = await stream.transp.readLine()
|
2018-11-22 15:35:44 +00:00
|
|
|
if len(line) == 0:
|
|
|
|
break
|
2018-11-22 14:12:46 +00:00
|
|
|
echo ">> ", line
|
|
|
|
|
|
|
|
await data.api.addHandler(ServerProtocols, streamHandler)
|
2021-12-16 10:05:20 +00:00
|
|
|
echo "= Your PeerId is ", id.peer.pretty()
|
2019-08-14 05:31:06 +00:00
|
|
|
await data.serveFut
|
2019-03-05 12:09:26 +00:00
|
|
|
|
2018-11-22 14:12:46 +00:00
|
|
|
when isMainModule:
|
|
|
|
waitFor(main())
|