This commit is contained in:
cheatfate 2019-08-14 08:31:06 +03:00
parent dfd824bd03
commit 8c4c2159ed

View File

@ -5,29 +5,27 @@ when not(compileOption("threads")):
{.fatal: "Please, compile this program with the --threads:on option!".} {.fatal: "Please, compile this program with the --threads:on option!".}
const const
ConsoleAddress = "/tmp/console-chat.sock"
ServerAddress = "/tmp/remote-chat.sock"
ServerProtocols = @["/test-chat-stream"] ServerProtocols = @["/test-chat-stream"]
type type
CustomData = ref object CustomData = ref object
api: DaemonAPI api: DaemonAPI
remotes: seq[StreamTransport] remotes: seq[StreamTransport]
consoleFd: AsyncFD
serveFut: Future[void]
proc threadMain(a: int) {.thread.} = proc threadMain(wfd: AsyncFD) {.thread.} =
## This procedure performs reading from `stdin` and sends data over ## This procedure performs reading from `stdin` and sends data over
## unix domain socket to main thread. ## pipe to main thread.
var transp = waitFor connect(initTAddress(ConsoleAddress)) var transp = fromPipe(wfd)
while true: while true:
var line = stdin.readLine() var line = stdin.readLine()
let res = waitFor transp.write(line & "\r\n") let res = waitFor transp.write(line & "\r\n")
proc serveThread(server: StreamServer, proc serveThread(udata: CustomData) {.async.} =
transp: StreamTransport) {.async.} = ## This procedure perform reading on pipe and sends data to remote clients.
## This procedure perform readin on local unix domain socket and var transp = fromPipe(udata.consoleFd)
## sends data to remote clients.
var udata = getUserData[CustomData](server)
proc remoteReader(transp: StreamTransport) {.async.} = proc remoteReader(transp: StreamTransport) {.async.} =
while true: while true:
@ -87,7 +85,7 @@ proc serveThread(server: StreamServer,
else: else:
echo peer.pretty(), " [", addresses.join(", "), "]" echo peer.pretty(), " [", addresses.join(", "), "]"
elif line.startsWith("/exit"): elif line.startsWith("/exit"):
quit(0) break
else: else:
var msg = line & "\r\n" var msg = line & "\r\n"
echo "<< ", line echo "<< ", line
@ -103,11 +101,15 @@ proc main() {.async.} =
var data = new CustomData var data = new CustomData
data.remotes = newSeq[StreamTransport]() data.remotes = newSeq[StreamTransport]()
var lserver = createStreamServer(initTAddress(ConsoleAddress), var (rfd, wfd) = createAsyncPipe()
serveThread, udata = data) if rfd == asyncInvalidPipe or wfd == asyncInvalidPipe:
lserver.start() raise newException(ValueError, "Could not initialize pipe!")
var thread: Thread[int]
thread.createThread(threadMain, 0) data.consoleFd = rfd
data.serveFut = serveThread(data)
var thread: Thread[AsyncFD]
thread.createThread(threadMain, wfd)
echo "= Starting P2P node" echo "= Starting P2P node"
data.api = await newDaemonApi({DHTFull, Bootstrap}) data.api = await newDaemonApi({DHTFull, Bootstrap})
@ -125,8 +127,7 @@ proc main() {.async.} =
await data.api.addHandler(ServerProtocols, streamHandler) await data.api.addHandler(ServerProtocols, streamHandler)
echo "= Your PeerID is ", id.peer.pretty() echo "= Your PeerID is ", id.peer.pretty()
await data.serveFut
when isMainModule: when isMainModule:
waitFor(main()) waitFor(main())
while true:
poll()