2018-11-22 14:12:46 +00:00
|
|
|
import asyncdispatch2, nimcrypto, strutils
|
2018-11-27 12:16:04 +00:00
|
|
|
import ../libp2p/daemon/daemonapi, ../libp2p/[base58, multiaddress]
|
2018-11-22 14:12:46 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
ConsoleAddress = "/tmp/console-chat.sock"
|
|
|
|
ServerAddress = "/tmp/remote-chat.sock"
|
|
|
|
ServerProtocols = @["/test-chat-stream"]
|
|
|
|
|
|
|
|
type
|
|
|
|
CustomData = ref object
|
|
|
|
api: DaemonAPI
|
|
|
|
remotes: seq[StreamTransport]
|
|
|
|
|
|
|
|
proc threadMain(a: int) {.thread.} =
|
|
|
|
## This procedure performs reading from `stdin` and sends data over
|
|
|
|
## unix domain socket to main thread.
|
|
|
|
var transp = waitFor connect(initTAddress(ConsoleAddress))
|
|
|
|
|
|
|
|
while true:
|
|
|
|
var line = stdin.readLine()
|
|
|
|
let res = waitFor transp.write(line & "\r\n")
|
|
|
|
|
|
|
|
proc serveThread(server: StreamServer,
|
|
|
|
transp: StreamTransport) {.async.} =
|
|
|
|
## This procedure perform readin on local unix domain socket and
|
|
|
|
## sends data to remote clients.
|
|
|
|
var udata = getUserData[CustomData](server)
|
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:
|
2018-11-27 12:16:04 +00:00
|
|
|
var peerId = Base58.decode(parts[1])
|
|
|
|
var address = MultiAddress.init(P_P2PCIRCUIT)
|
|
|
|
address &= MultiAddress.init(P_P2P, peerId)
|
|
|
|
echo "= Searching for peer ", parts[1]
|
|
|
|
var id = await udata.api.dhtFindPeer(peerId)
|
|
|
|
echo "Peer " & parts[1] & " found at addresses:"
|
|
|
|
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]
|
2018-11-22 15:35:44 +00:00
|
|
|
asyncCheck remoteReader(stream.transp)
|
2018-11-27 12:33:41 +00:00
|
|
|
elif line.startsWith("/search"):
|
|
|
|
var parts = line.split(" ")
|
|
|
|
if len(parts) == 2:
|
|
|
|
var peerId = Base58.decode(parts[1])
|
|
|
|
echo "= Searching for peer ", parts[1]
|
|
|
|
var id = await udata.api.dhtFindPeer(peerId)
|
|
|
|
echo "Peer " & parts[1] & " found at addresses:"
|
|
|
|
for item in id.addresses:
|
|
|
|
echo $item
|
|
|
|
elif line.startsWith("/consearch"):
|
|
|
|
var parts = line.split(" ")
|
|
|
|
if len(parts) == 2:
|
|
|
|
var peerId = Base58.decode(parts[1])
|
|
|
|
echo "= Searching for peers connected to peer ", parts[1]
|
|
|
|
var peers = await udata.api.dhtFindPeersConnectedToPeer(peerId)
|
|
|
|
echo "Found ", len(peers), " connected to peer ", parts[1]
|
|
|
|
for item in peers:
|
|
|
|
echo Base58.encode(item.peer)
|
2018-11-27 12:16:04 +00:00
|
|
|
elif line.startsWith("/exit"):
|
|
|
|
quit(0)
|
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:
|
|
|
|
break
|
|
|
|
|
|
|
|
proc main() {.async.} =
|
|
|
|
var data = new CustomData
|
|
|
|
data.remotes = newSeq[StreamTransport]()
|
|
|
|
|
|
|
|
var lserver = createStreamServer(initTAddress(ConsoleAddress),
|
|
|
|
serveThread, udata = data)
|
|
|
|
lserver.start()
|
|
|
|
var thread: Thread[int]
|
|
|
|
thread.createThread(threadMain, 0)
|
|
|
|
|
|
|
|
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.} =
|
2018-11-27 12:42:49 +00:00
|
|
|
echo "= Peer ", Base58.encode(stream.peer), " 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)
|
2018-11-27 12:16:04 +00:00
|
|
|
echo "= Your PeerID is ", Base58.encode(id.peer)
|
2018-11-22 14:12:46 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
waitFor(main())
|
|
|
|
while true:
|
|
|
|
poll()
|