diff options
author | Schanzenbach, Martin <mschanzenbach@posteo.de> | 2019-06-22 20:36:50 +0200 |
---|---|---|
committer | Schanzenbach, Martin <mschanzenbach@posteo.de> | 2019-06-22 20:36:50 +0200 |
commit | 8d66e33bfdeb2e717029427ffc746cf871560d6b (patch) | |
tree | 40c8dcf45b11df5c7103e1dc2a7c50561085ca81 | |
parent | a2fd8a874526c06663a4a3cdd00237c829ff0c9d (diff) | |
download | groupchat-schanzen/macos_scheduler.tar.gz groupchat-schanzen/macos_scheduler.zip |
attempt fix macos schedulerschanzen/macos_scheduler
-rw-r--r-- | groupchat.nim | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/groupchat.nim b/groupchat.nim index f035033..665f1c2 100644 --- a/groupchat.nim +++ b/groupchat.nim | |||
@@ -1,5 +1,6 @@ | |||
1 | import gnunet_nim | 1 | import gnunet_nim |
2 | import gnunet_nim/cadet | 2 | import gnunet_nim/cadet |
3 | import logging | ||
3 | 4 | ||
4 | import asyncdispatch, asyncfile, parseopt, strutils, sequtils, times, os | 5 | import asyncdispatch, asyncfile, parseopt, strutils, sequtils, times, os |
5 | 6 | ||
@@ -31,20 +32,43 @@ proc processServerMessages(channel: ref CadetChannel) {.async.} = | |||
31 | echo getDateStr()," ",getClockStr()," ",message | 32 | echo getDateStr()," ",getClockStr()," ",message |
32 | 33 | ||
33 | proc processInput(inputFile: AsyncFile, channel: ref CadetChannel) {.async.} = | 34 | proc processInput(inputFile: AsyncFile, channel: ref CadetChannel) {.async.} = |
34 | while true: | 35 | let input = await inputFile.readline() |
35 | let input = await inputFile.readline() | 36 | channel.sendMessage(input) |
36 | channel.sendMessage(input) | ||
37 | 37 | ||
38 | proc firstTask(gnunetApp: ref GnunetApplication, | 38 | proc firstTask(gnunetApp: ref GnunetApplication, |
39 | server: string, | 39 | server: string, |
40 | port: string) {.async.} = | 40 | port: string) {.async.} = |
41 | let cadet = await gnunetApp.initCadet() | 41 | let cadet = await gnunetApp.initCadet() |
42 | var inputFile = openAsync("/dev/stdin", fmRead) | ||
43 | debug("First Task!") | ||
42 | var chat = new(Chat) | 44 | var chat = new(Chat) |
43 | chat.channels = newSeq[ref CadetChannel]() | 45 | chat.channels = newSeq[ref CadetChannel]() |
44 | if server != "": | 46 | if server != "": |
45 | let inputFile = openAsync("/dev/stdin", fmRead) | 47 | debug("Opening stdin") |
48 | debug("Opened") | ||
46 | let channel = cadet.createChannel(server, port) | 49 | let channel = cadet.createChannel(server, port) |
47 | await processServerMessages(channel) or processInput(inputFile, channel) | 50 | debug("Awaiting IO") |
51 | var messagesFuture = channel.messages.read() | ||
52 | var inputFuture = inputFile.readline() | ||
53 | while true: | ||
54 | var hasData = false | ||
55 | var message = "" | ||
56 | debug("Awaiting events") | ||
57 | await messagesFuture or inputFuture | ||
58 | if inputFuture.finished(): | ||
59 | debug("Got input") | ||
60 | channel.sendMessage(inputFuture.read()) | ||
61 | inputFuture = inputFile.readline() | ||
62 | elif messagesFuture.finished(): | ||
63 | debug("Got message") | ||
64 | (hasData, message) = messagesFuture.read() | ||
65 | if hasData: | ||
66 | chat.publish(message = message, sender = channel) | ||
67 | messagesFuture = channel.messages.read() | ||
68 | else: | ||
69 | debug("Fin") | ||
70 | break | ||
71 | debug("While true") | ||
48 | inputFile.close() | 72 | inputFile.close() |
49 | else: | 73 | else: |
50 | let cadetPort = cadet.openPort(port) | 74 | let cadetPort = cadet.openPort(port) |
@@ -69,6 +93,8 @@ proc firstTask(gnunetApp: ref GnunetApplication, | |||
69 | proc main() = | 93 | proc main() = |
70 | var server, port, configfile: string | 94 | var server, port, configfile: string |
71 | var optParser = initOptParser() | 95 | var optParser = initOptParser() |
96 | var consoleLog = newConsoleLogger() | ||
97 | addHandler(consoleLog) | ||
72 | for kind, key, value in optParser.getopt(): | 98 | for kind, key, value in optParser.getopt(): |
73 | case kind | 99 | case kind |
74 | of cmdLongOption, cmdShortOption: | 100 | of cmdLongOption, cmdShortOption: |
@@ -91,8 +117,11 @@ proc main() = | |||
91 | var gnunetApp = initGnunetApplication(configfile) | 117 | var gnunetApp = initGnunetApplication(configfile) |
92 | asyncCheck firstTask(gnunetApp, server, port) | 118 | asyncCheck firstTask(gnunetApp, server, port) |
93 | while hasPendingOperations(): | 119 | while hasPendingOperations(): |
120 | debug("Processing OPs") | ||
94 | poll(gnunetApp.millisecondsUntilTimeout()) | 121 | poll(gnunetApp.millisecondsUntilTimeout()) |
122 | debug("polled, start working") | ||
95 | gnunetApp.doWork() | 123 | gnunetApp.doWork() |
124 | debug("done") | ||
96 | echo "Quitting." | 125 | echo "Quitting." |
97 | 126 | ||
98 | main() | 127 | main() |