gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] [gnunet-nim] branch master updated (691133f -> 172522a)


From: gnunet
Subject: [GNUnet-SVN] [gnunet-nim] branch master updated (691133f -> 172522a)
Date: Wed, 08 Aug 2018 20:19:59 +0200

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

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

    from 691133f  comment: explain why we're reading from stdin in a nasty way
     new 6781ce0  add shutdown procedure
     new 64d0ac0  fix bug (application hangs when a peer disconnects); fixes & 
simplifications
     new d3a8f59  move example applications / scripts / config files to example 
folder
     new cbddd7e  add first version of groupchat application
     new 172522a  Merge branch 'master' of ssh://gnunet.org/gnunet-nim

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 asynccadet.nim                            | 41 +++++++--------
 gnunet.conf => examples/gnunet1.conf      |  8 +--
 otherpeer.conf => examples/gnunet2.conf   |  8 +--
 gnunet.conf => examples/gnunet3.conf      |  8 +--
 gnunet_nim.nim => examples/gnunet_nim.nim |  0
 examples/groupchat.nim                    | 87 +++++++++++++++++++++++++++++++
 examples/start_peers.sh                   | 17 ++++++
 examples/stop_peers.sh                    |  8 +++
 gnunet_application.nim                    |  3 ++
 start_peers.sh                            | 10 ----
 stop_peers.sh                             |  6 ---
 11 files changed, 146 insertions(+), 50 deletions(-)
 copy gnunet.conf => examples/gnunet1.conf (75%)
 rename otherpeer.conf => examples/gnunet2.conf (74%)
 rename gnunet.conf => examples/gnunet3.conf (75%)
 rename gnunet_nim.nim => examples/gnunet_nim.nim (100%)
 create mode 100644 examples/groupchat.nim
 create mode 100755 examples/start_peers.sh
 create mode 100755 examples/stop_peers.sh
 delete mode 100755 start_peers.sh
 delete mode 100755 stop_peers.sh

diff --git a/asynccadet.nim b/asynccadet.nim
index 47575b7..409b636 100644
--- a/asynccadet.nim
+++ b/asynccadet.nim
@@ -8,13 +8,11 @@ import
 type
   CadetHandle* = object
     handle: ptr GNUNET_CADET_Handle
-    openPorts: seq[ref CadetPort]
     shutdownTask: ptr GNUNET_SCHEDULER_Task
 
   CadetPort* = object
     handle: ptr GNUNET_CADET_Port
     channels*: FutureStream[ref CadetChannel]
-    activeChannels: seq[ref CadetChannel]
 
   CadetChannel* = object
     handle: ptr GNUNET_CADET_Channel
@@ -24,6 +22,8 @@ type
 proc channelDisconnectCb(cls: pointer,
                          gnunetChannel: ptr GNUNET_CADET_Channel) {.cdecl.} =
   let channel = cast[ptr CadetChannel](cls)
+  GNUNET_CADET_receive_done(channel.handle)
+  channel.handle = nil
   channel.messages.complete()
 
 proc channelConnectCb(cls: pointer,
@@ -33,8 +33,7 @@ proc channelConnectCb(cls: pointer,
   let channel = new(CadetChannel)
   channel.handle = gnunetChannel
   channel.peer = GNUNET_PeerIdentity(public_key: source.public_key)
-  channel.messages = newFutureStream[string]()
-  port.activeChannels.add(channel)
+  channel.messages = newFutureStream[string]("channelConnectCb")
   waitFor port.channels.write(channel)
   return addr channel[]
 
@@ -70,6 +69,8 @@ proc hashString(port: string): GNUNET_HashCode =
   GNUNET_CRYPTO_hash(cstring(port), csize(port.len()), addr result)
 
 proc sendMessage*(channel: ref CadetChannel, payload: string) =
+  if channel.handle.isNil():
+    return
   let messageLen = uint16(payload.len() + sizeof(GNUNET_MessageHeader))
   var messageHeader: ptr GNUNET_MessageHeader
   let envelope = GNUNET_MQ_msg(addr messageHeader,
@@ -83,7 +84,6 @@ proc openPort*(handle: ref CadetHandle, port: string): ref 
CadetPort =
   let handlers = messageHandlers()
   let port = hashString(port)
   let openPort = new(CadetPort)
-  openPort.channels = newFutureStream[ref CadetChannel]()
   openPort.handle = GNUNET_CADET_open_port(handle.handle,
                                            unsafeAddr port,
                                            channelConnectCb,
@@ -91,17 +91,14 @@ proc openPort*(handle: ref CadetHandle, port: string): ref 
CadetPort =
                                            nil,
                                            channelDisconnectCb,
                                            unsafeAddr handlers[0])
-  openPort.activeChannels = newSeq[ref CadetChannel]()
-  handle.openPorts.add(openPort)
+  openPort.channels = newFutureStream[ref CadetChannel]("openPort")
   return openPort
 
-proc internalClosePort(handle: ptr CadetHandle, port: ref CadetPort) =
-  GNUNET_CADET_close_port(port.handle)
-  port.channels.complete()
-
-proc closePort*(handle: ref CadetHandle, port: ref CadetPort) =
-  internalClosePort(addr handle[], port)
-  handle.openPorts.delete(handle.openPorts.find(port))
+proc close*(port: ref CadetPort) =
+  if not port.handle.isNil():
+    GNUNET_CADET_close_port(port.handle)
+    port.handle = nil
+    port.channels.complete()
 
 proc createChannel*(handle: ref CadetHandle,
                     peer: string,
@@ -125,14 +122,15 @@ proc createChannel*(handle: ref CadetHandle,
                                                unsafeAddr handlers[0])
   return channel
 
+proc close*(channel: ref CadetChannel) =
+  if not channel.handle.isNil():
+    GNUNET_CADET_channel_destroy(channel.handle)
+    channel.handle = nil
+
 proc disconnect(cadetHandle: ptr CadetHandle) =
-  if cadetHandle.handle.isNil():
-    return
-  for port in cadetHandle.openPorts:
-    cadetHandle.internalClosePort(port)
-  cadetHandle.openPorts.setLen(0)
-  GNUNET_CADET_disconnect(cadetHandle.handle)
-  cadetHandle.handle = nil
+  if not cadetHandle.handle.isNil():
+    GNUNET_CADET_disconnect(cadetHandle.handle)
+    cadetHandle.handle = nil
 
 proc shutdownCb(cls: pointer) {.cdecl.} =
   disconnect(cast[ptr CadetHandle](cls))
@@ -144,7 +142,6 @@ proc cadetConnectCb(cls: pointer) {.cdecl.} =
     var cadetHandle: ref CadetHandle
     new(cadetHandle, proc(handle: ref CadetHandle) = disconnect(addr handle[]))
     cadetHandle.handle = GNUNET_CADET_connect(app.configHandle)
-    cadetHandle.openPorts = newSeq[ref CadetPort]()
     cadetHandle.shutdownTask = GNUNET_SCHEDULER_add_shutdown(shutdownCb,
                                                              addr 
cadetHandle[])
     Future[ref CadetHandle](future).complete(cadetHandle)
diff --git a/gnunet.conf b/examples/gnunet1.conf
similarity index 75%
copy from gnunet.conf
copy to examples/gnunet1.conf
index 6dd01ee..a5feacf 100644
--- a/gnunet.conf
+++ b/examples/gnunet1.conf
@@ -1,8 +1,8 @@
 [PATHS]
-GNUNET_HOME = ./gnunet_home
-GNUNET_DATA_HOME = ./gnunet_data_home
-GNUNET_RUNTIME_DIR = ./gnunet_runtime_dir
-GNUNET_USER_RUNTIME_DIR = ./gnunet_user_runtime_dir
+GNUNET_HOME = ./gnunet1_home
+GNUNET_DATA_HOME = ./gnunet1_data_home
+GNUNET_RUNTIME_DIR = ./gnunet1_runtime_dir
+GNUNET_USER_RUNTIME_DIR = ./gnunet1_user_runtime_dir
 
 [arm]
 GLOBAL_POSTFIX = -L WARNING -l $GNUNET_HOME/gnunet.log
diff --git a/otherpeer.conf b/examples/gnunet2.conf
similarity index 74%
rename from otherpeer.conf
rename to examples/gnunet2.conf
index e0bd90b..b4eaaac 100644
--- a/otherpeer.conf
+++ b/examples/gnunet2.conf
@@ -1,8 +1,8 @@
 [PATHS]
-GNUNET_HOME = ./otherpeer_home
-GNUNET_DATA_HOME = ./otherpeer_data_home
-GNUNET_RUNTIME_DIR = ./otherpeer_runtime_dir
-GNUNET_USER_RUNTIME_DIR = ./otherpeer_user_runtime_dir
+GNUNET_HOME = ./gnunet2_home
+GNUNET_DATA_HOME = ./gnunet2_data_home
+GNUNET_RUNTIME_DIR = ./gnunet2_runtime_dir
+GNUNET_USER_RUNTIME_DIR = ./gnunet2_user_runtime_dir
 
 [arm]
 GLOBAL_POSTFIX = -L WARNING -l $GNUNET_HOME/gnunet.log
diff --git a/gnunet.conf b/examples/gnunet3.conf
similarity index 75%
rename from gnunet.conf
rename to examples/gnunet3.conf
index 6dd01ee..54177f2 100644
--- a/gnunet.conf
+++ b/examples/gnunet3.conf
@@ -1,8 +1,8 @@
 [PATHS]
-GNUNET_HOME = ./gnunet_home
-GNUNET_DATA_HOME = ./gnunet_data_home
-GNUNET_RUNTIME_DIR = ./gnunet_runtime_dir
-GNUNET_USER_RUNTIME_DIR = ./gnunet_user_runtime_dir
+GNUNET_HOME = ./gnunet3_home
+GNUNET_DATA_HOME = ./gnunet3_data_home
+GNUNET_RUNTIME_DIR = ./gnunet3_runtime_dir
+GNUNET_USER_RUNTIME_DIR = ./gnunet3_user_runtime_dir
 
 [arm]
 GLOBAL_POSTFIX = -L WARNING -l $GNUNET_HOME/gnunet.log
diff --git a/gnunet_nim.nim b/examples/gnunet_nim.nim
similarity index 100%
rename from gnunet_nim.nim
rename to examples/gnunet_nim.nim
diff --git a/examples/groupchat.nim b/examples/groupchat.nim
new file mode 100644
index 0000000..c2786b0
--- /dev/null
+++ b/examples/groupchat.nim
@@ -0,0 +1,87 @@
+import ../gnunet_application, ../asynccadet
+import asyncdispatch, asyncfile, parseopt, strutils
+
+type Chat = object
+  channels: seq[ref CadetChannel]
+
+proc publish(chat: ref Chat, message: string, sender: ref CadetChannel = nil) =
+  let message =
+    if sender.isNil(): message.strip(leading = false)
+    else: "[Alice] " & message.strip(leading = false)
+  echo message
+  for c in chat.channels:
+    c.sendMessage(message)
+
+proc processClientMessages(channel: ref CadetChannel,
+                           chat: ref Chat) {.async.} =
+  while true:
+    let (hasData, message) = await channel.messages.read()
+    if not hasData:
+      break
+    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 message
+      messageFuture = channel.messages.read()
+
+proc firstTask(gnunetApp: ref GnunetApplication,
+               server: string,
+               port: string) {.async.} =
+  let cadet = await gnunetApp.initCadet()
+  var chat = new(Chat)
+  chat.channels = newSeq[ref CadetChannel]()
+  if not server.isNil():
+    let channel = cadet.createChannel(server, port)
+    processServerMessages(channel).addCallback(shutdownGnunetApplication)
+  else:
+    let cadetPort = cadet.openPort(port)
+    while true:
+      let (hasChannel, channel) = await cadetPort.channels.read()
+      if not hasChannel:
+        break
+      chat.publish(message = "X joined\n")
+      chat.channels.add(channel)
+      channel.sendMessage("Welcome X! You are talking with: \n")
+      closureScope:
+        let channel = channel
+        proc channelDisconnected(future: Future[void]) =
+          chat.channels.delete(chat.channels.find(channel))
+          chat.publish(message = "X left\n")
+        processClientMessages(channel, chat).addCallback(channelDisconnected)
+
+proc main() =
+  var server, port, configfile: string
+  var optParser = initOptParser()
+  for kind, key, value in optParser.getopt():
+    case kind
+    of cmdLongOption, cmdShortOption:
+      case key
+      of "config", "c": configfile = value
+      of "server", "s": server = value
+      of "port", "p": port = value
+    else:
+      assert(false)
+  var gnunetApp = initGnunetApplication(configfile)
+  asyncCheck firstTask(gnunetApp, server, port)
+  try:
+    while true:
+      poll(gnunetApp.millisecondsUntilTimeout())
+      gnunetApp.doWork()
+  except ValueError:
+    echo "quitting"
+
+main()
+GC_fullCollect()
diff --git a/examples/start_peers.sh b/examples/start_peers.sh
new file mode 100755
index 0000000..8d2c341
--- /dev/null
+++ b/examples/start_peers.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+CONFIG1=gnunet1.conf
+CONFIG2=gnunet2.conf
+CONFIG3=gnunet3.conf
+
+gnunet-arm -c $CONFIG1 -s
+echo "peer 1: $(gnunet-peerinfo -c ${CONFIG1} -sq)"
+gnunet-arm -c $CONFIG2 -s
+echo "peer 2: $(gnunet-peerinfo -c ${CONFIG2} -sq)"
+gnunet-arm -c $CONFIG3 -s
+echo "peer 3: $(gnunet-peerinfo -c ${CONFIG3} -sq)"
+gnunet-peerinfo -c $CONFIG1 -p $(gnunet-peerinfo -c $CONFIG2 -g)
+gnunet-peerinfo -c $CONFIG1 -p $(gnunet-peerinfo -c $CONFIG3 -g)
+gnunet-peerinfo -c $CONFIG2 -p $(gnunet-peerinfo -c $CONFIG1 -g)
+gnunet-peerinfo -c $CONFIG2 -p $(gnunet-peerinfo -c $CONFIG3 -g)
+gnunet-peerinfo -c $CONFIG3 -p $(gnunet-peerinfo -c $CONFIG1 -g)
+gnunet-peerinfo -c $CONFIG3 -p $(gnunet-peerinfo -c $CONFIG2 -g)
diff --git a/examples/stop_peers.sh b/examples/stop_peers.sh
new file mode 100755
index 0000000..eefc5f4
--- /dev/null
+++ b/examples/stop_peers.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+CONFIG1=gnunet1.conf
+CONFIG2=gnunet2.conf
+CONFIG3=gnunet3.conf
+
+gnunet-arm -c $CONFIG1 -e
+gnunet-arm -c $CONFIG2 -e
+gnunet-arm -c $CONFIG3 -e
diff --git a/gnunet_application.nim b/gnunet_application.nim
index 6c617fe..3695912 100644
--- a/gnunet_application.nim
+++ b/gnunet_application.nim
@@ -84,6 +84,9 @@ proc initGnunetApplication*(configFile: string): ref 
GnunetApplication =
   assert(GNUNET_SYSERR != GNUNET_CONFIGURATION_load(app.configHandle, 
configFile))
   return app
 
+proc shutdownGnunetApplication*() =
+  GNUNET_SCHEDULER_shutdown()
+
 proc doWork*(app: ref GnunetApplication) =
   discard GNUNET_SCHEDULER_do_work(app.schedulerHandle) #FIXME: don't discard
 
diff --git a/start_peers.sh b/start_peers.sh
deleted file mode 100755
index 756edac..0000000
--- a/start_peers.sh
+++ /dev/null
@@ -1,10 +0,0 @@
-#!/bin/bash
-CONFIG_OUR_PEER=gnunet.conf
-CONFIG_OTHER_PEER=otherpeer.conf
-
-gnunet-arm -c $CONFIG_OUR_PEER -s
-echo "our peer: $(gnunet-peerinfo -c ${CONFIG_OUR_PEER} -sq)"
-gnunet-arm -c $CONFIG_OTHER_PEER -s
-echo "other peer: $(gnunet-peerinfo -c ${CONFIG_OTHER_PEER} -sq)"
-gnunet-peerinfo -c $CONFIG_OUR_PEER -p $(gnunet-peerinfo -c $CONFIG_OTHER_PEER 
-g)
-gnunet-peerinfo -c $CONFIG_OTHER_PEER -p $(gnunet-peerinfo -c $CONFIG_OUR_PEER 
-g)
diff --git a/stop_peers.sh b/stop_peers.sh
deleted file mode 100755
index 5b6ec38..0000000
--- a/stop_peers.sh
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/bin/bash
-CONFIG_OUR_PEER=gnunet.conf
-CONFIG_OTHER_PEER=otherpeer.conf
-
-gnunet-arm -c $CONFIG_OUR_PEER -e
-gnunet-arm -c $CONFIG_OTHER_PEER -e

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



reply via email to

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