gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [gnunet-nim] 02/02: restructure groupchat application


From: gnunet
Subject: [GNUnet-SVN] [gnunet-nim] 02/02: restructure groupchat application
Date: Sat, 18 Aug 2018 01:46:59 +0200

This is an automated email from the git hooks/post-receive script.

lurchi pushed a commit to branch master
in repository gnunet-nim.

commit b95e5ca8c1512c1b94d537f28ddc60f90c17b6b0
Author: lurchi <address@hidden>
AuthorDate: Sat Aug 18 01:46:47 2018 +0200

    restructure groupchat application
---
 examples/groupchat.nim | 38 +++++++++++++++++---------------------
 1 file 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,
     chat.publish(message = message, sender = channel)
 
 proc processServerMessages(channel: ref CadetChannel) {.async.} =
-  let inputFile = openAsync("/dev/stdin", fmRead)
-  var inputFuture = inputFile.readline()
-  var messageFuture = channel.messages.read()
   while true:
-    await inputFuture or messageFuture
-    if inputFuture.finished():
-      let input = inputFuture.read()
-      channel.sendMessage(input)
-      inputFuture = inputFile.readline()
-    else:
-      let (hasData, message) = messageFuture.read()
-      if not hasData:
-        break
-      echo getDateStr()," ",getClockStr()," ",message
-      messageFuture = channel.messages.read()
+    let (hasData, message) = await channel.messages.read()
+    if not hasData:
+      shutdownGnunetApplication()
+      return
+    echo getDateStr()," ",getClockStr()," ",message
+
+proc processInput(inputFile: AsyncFile, channel: ref CadetChannel) {.async.} =
+  while true:
+    let input = await inputFile.readline()
+    channel.sendMessage(input)
 
 proc firstTask(gnunetApp: ref GnunetApplication,
                server: string,
@@ -44,8 +40,10 @@ proc firstTask(gnunetApp: ref GnunetApplication,
   var chat = new(Chat)
   chat.channels = newSeq[ref CadetChannel]()
   if not server.isNil():
+    let inputFile = openAsync("/dev/stdin", fmRead)
     let channel = cadet.createChannel(server, port)
-    processServerMessages(channel).addCallback(shutdownGnunetApplication)
+    await processServerMessages(channel) or processInput(inputFile, channel)
+    inputFile.close()
   else:
     let cadetPort = cadet.openPort(port)
     while true:
@@ -80,12 +78,10 @@ proc main() =
       assert(false)
   var gnunetApp = initGnunetApplication(configfile)
   asyncCheck firstTask(gnunetApp, server, port)
-  try:
-    while true:
-      poll(gnunetApp.millisecondsUntilTimeout())
-      gnunetApp.doWork()
-  except ValueError:
-    echo "quitting"
+  while hasPendingOperations():
+    poll(gnunetApp.millisecondsUntilTimeout())
+    gnunetApp.doWork()
+  echo "quitting"
 
 main()
 GC_fullCollect()

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]