feat: native chat clean up

This commit is contained in:
Dmitriy Ryajov 2019-09-12 18:05:20 -06:00
parent 1fe2a391ae
commit a1ee339680
1 changed files with 35 additions and 14 deletions

View File

@ -23,6 +23,8 @@ import ../libp2p/switch,
../libp2p/muxers/mplex/types ../libp2p/muxers/mplex/types
const ChatCodec = "/nim-libp2p/chat/1.0.0" const ChatCodec = "/nim-libp2p/chat/1.0.0"
const DefaultAddr = "/ip4/127.0.0.1/tcp/55505"
const Help = """ const Help = """
Commands: /[?|hep|connect|disconnect|exit] Commands: /[?|hep|connect|disconnect|exit]
@ -46,11 +48,17 @@ type
connected: bool connected: bool
started: bool started: bool
proc id (p: ChatProto): string =
result = "unknown"
if p.conn.peerInfo.peerId.isSome:
result = $p.conn.peerInfo.peerId.get()
# forward declaration # forward declaration
proc readWriteLoop(p: ChatProto) {.async, gcsafe.} proc readWriteLoop(p: ChatProto) {.async, gcsafe.}
proc readAndPrint(p: ChatProto) {.async, gcsafe.} = proc readAndPrint(p: ChatProto) {.async, gcsafe.} =
while true: while true:
while p.connected: while p.connected:
# echo &"{p.id} -> "
echo cast[string](await p.conn.readLp()) echo cast[string](await p.conn.readLp())
await sleepAsync(100.millis) await sleepAsync(100.millis)
@ -69,9 +77,9 @@ proc dialPeer(p: ChatProto, address: string) {.async, gcsafe.} =
proc writeAndPrint(p: ChatProto) {.async, gcsafe.} = proc writeAndPrint(p: ChatProto) {.async, gcsafe.} =
while true: while true:
if p.connected: if not p.connected:
echo "type your message:" # echo &"{p.id} ->"
else: # else:
echo "type an address or wait for a connection:" echo "type an address or wait for a connection:"
echo "type /[help|?] for help" echo "type /[help|?] for help"
@ -82,7 +90,8 @@ proc writeAndPrint(p: ChatProto) {.async, gcsafe.} =
if line.startsWith("/disconnect"): if line.startsWith("/disconnect"):
echo "Ending current session" echo "Ending current session"
await p.conn.close() if p.connected and p.conn.closed.not:
await p.conn.close()
p.connected = false p.connected = false
elif line.startsWith("/connect"): elif line.startsWith("/connect"):
if p.connected: if p.connected:
@ -103,9 +112,12 @@ proc writeAndPrint(p: ChatProto) {.async, gcsafe.} =
await p.dialPeer(address) await p.dialPeer(address)
elif line.startsWith("/exit"): elif line.startsWith("/exit"):
await p.conn.close() if p.connected and p.conn.closed.not:
p.connected = false await p.conn.close()
# await p.switch.stop() # TODO: remote p.connected = false
await p.switch.stop()
echo "quitting..."
quit(0) quit(0)
else: else:
if p.connected: if p.connected:
@ -155,13 +167,22 @@ proc serveThread(customData: CustomData) {.async.} =
let seckey = PrivateKey.random(RSA) let seckey = PrivateKey.random(RSA)
var peerInfo: PeerInfo var peerInfo: PeerInfo
peerInfo.peerId = some(PeerID.init(seckey)) peerInfo.peerId = some(PeerID.init(seckey))
var localAddress = "/ip4/127.0.0.1/tcp/55505" var localAddress = DefaultAddr
echo "Type an address to bind to or Enter use the default /ip4/127.0.0.1/tcp/55505" while true:
let a = await transp.readLine() echo &"Type an address to bind to or Enter to use the default {DefaultAddr}"
if a.len > 0: let a = await transp.readLine()
localAddress = a try:
if a.len > 0:
peerInfo.addrs.add(Multiaddress.init(a))
break
peerInfo.addrs.add(Multiaddress.init(localAddress))
break
except:
echo "invalid address"
localAddress = DefaultAddr
continue
peerInfo.addrs.add(Multiaddress.init(localAddress))
proc createMplex(conn: Connection): Muxer = proc createMplex(conn: Connection): Muxer =
result = newMplex(conn) result = newMplex(conn)