aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlurchi <lurchi@strangeplace.net>2018-08-18 01:46:47 +0200
committerlurchi <lurchi@strangeplace.net>2018-08-18 01:46:47 +0200
commitb95e5ca8c1512c1b94d537f28ddc60f90c17b6b0 (patch)
treea29212ec041dfb7d7b66da22b66f4db521dfcefb
parentf7e6bc754a832b7f2838bb49981e950417209061 (diff)
downloadgnunet-nim-b95e5ca8c1512c1b94d537f28ddc60f90c17b6b0.tar.gz
gnunet-nim-b95e5ca8c1512c1b94d537f28ddc60f90c17b6b0.zip
restructure groupchat application
-rw-r--r--examples/groupchat.nim38
1 files changed, 17 insertions, 21 deletions
diff --git a/examples/groupchat.nim b/examples/groupchat.nim
index 26f9848..93d4323 100644
--- a/examples/groupchat.nim
+++ b/examples/groupchat.nim
@@ -21,21 +21,17 @@ proc processClientMessages(channel: ref CadetChannel,
21 chat.publish(message = message, sender = channel) 21 chat.publish(message = message, sender = channel)
22 22
23proc processServerMessages(channel: ref CadetChannel) {.async.} = 23proc processServerMessages(channel: ref CadetChannel) {.async.} =
24 let inputFile = openAsync("/dev/stdin", fmRead)
25 var inputFuture = inputFile.readline()
26 var messageFuture = channel.messages.read()
27 while true: 24 while true:
28 await inputFuture or messageFuture 25 let (hasData, message) = await channel.messages.read()
29 if inputFuture.finished(): 26 if not hasData:
30 let input = inputFuture.read() 27 shutdownGnunetApplication()
31 channel.sendMessage(input) 28 return
32 inputFuture = inputFile.readline() 29 echo getDateStr()," ",getClockStr()," ",message
33 else: 30
34 let (hasData, message) = messageFuture.read() 31proc processInput(inputFile: AsyncFile, channel: ref CadetChannel) {.async.} =
35 if not hasData: 32 while true:
36 break 33 let input = await inputFile.readline()
37 echo getDateStr()," ",getClockStr()," ",message 34 channel.sendMessage(input)
38 messageFuture = channel.messages.read()
39 35
40proc firstTask(gnunetApp: ref GnunetApplication, 36proc firstTask(gnunetApp: ref GnunetApplication,
41 server: string, 37 server: string,
@@ -44,8 +40,10 @@ proc firstTask(gnunetApp: ref GnunetApplication,
44 var chat = new(Chat) 40 var chat = new(Chat)
45 chat.channels = newSeq[ref CadetChannel]() 41 chat.channels = newSeq[ref CadetChannel]()
46 if not server.isNil(): 42 if not server.isNil():
43 let inputFile = openAsync("/dev/stdin", fmRead)
47 let channel = cadet.createChannel(server, port) 44 let channel = cadet.createChannel(server, port)
48 processServerMessages(channel).addCallback(shutdownGnunetApplication) 45 await processServerMessages(channel) or processInput(inputFile, channel)
46 inputFile.close()
49 else: 47 else:
50 let cadetPort = cadet.openPort(port) 48 let cadetPort = cadet.openPort(port)
51 while true: 49 while true:
@@ -80,12 +78,10 @@ proc main() =
80 assert(false) 78 assert(false)
81 var gnunetApp = initGnunetApplication(configfile) 79 var gnunetApp = initGnunetApplication(configfile)
82 asyncCheck firstTask(gnunetApp, server, port) 80 asyncCheck firstTask(gnunetApp, server, port)
83 try: 81 while hasPendingOperations():
84 while true: 82 poll(gnunetApp.millisecondsUntilTimeout())
85 poll(gnunetApp.millisecondsUntilTimeout()) 83 gnunetApp.doWork()
86 gnunetApp.doWork() 84 echo "quitting"
87 except ValueError:
88 echo "quitting"
89 85
90main() 86main()
91GC_fullCollect() 87GC_fullCollect()