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: add shutdown logic (all


From: gnunet
Subject: [GNUnet-SVN] [gnunet-nim] branch master updated: add shutdown logic (allows GNUnet to shutdown on signals)
Date: Mon, 06 Aug 2018 20:45:02 +0200

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

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

The following commit(s) were added to refs/heads/master by this push:
     new 33102e7  add shutdown logic (allows GNUnet to shutdown on signals)
33102e7 is described below

commit 33102e747c285f58014d771914ac5ecd3cce9b73
Author: lurchi <address@hidden>
AuthorDate: Mon Aug 6 20:43:35 2018 +0200

    add shutdown logic (allows GNUnet to shutdown on signals)
---
 asynccadet.nim           | 52 +++++++++++++++++++++++++++++-------------------
 gnunet_nim.nim           | 10 ++++++----
 gnunet_scheduler_lib.nim |  4 ++++
 3 files changed, 42 insertions(+), 24 deletions(-)

diff --git a/asynccadet.nim b/asynccadet.nim
index 8a7c8b3..1e5716c 100644
--- a/asynccadet.nim
+++ b/asynccadet.nim
@@ -9,6 +9,7 @@ type
   CadetHandle* = object
     handle: ptr GNUNET_CADET_Handle
     openPorts: seq[ref CadetPort]
+    shutdownTask: ptr GNUNET_SCHEDULER_Task
 
   CadetPort* = object
     handle: ptr GNUNET_CADET_Port
@@ -52,14 +53,6 @@ proc channelMessageCheckCb(cls: pointer,
                            messageHeader: ptr GNUNET_MessageHeader): cint 
{.cdecl.} =
   result = GNUNET_OK
 
-proc cadetConnectCb(cls: pointer) {.cdecl.} =
-  let app = cast[ptr GnunetApplication](cls)
-  var future: FutureBase
-  if app.connectFutures.take("cadet", future):
-    let cadetHandle = CadetHandle(handle: 
GNUNET_CADET_connect(app.configHandle),
-                                  openPorts: newSeq[ref CadetPort]())
-    Future[CadetHandle](future).complete(cadetHandle)
-
 proc messageHandlers(): array[2, GNUNET_MQ_MessageHandler] =
   result = [
     GNUNET_MQ_MessageHandler(mv: channelMessageCheckCb,
@@ -87,7 +80,7 @@ proc sendMessage*(channel: ref CadetChannel, payload: string) 
=
   copyMem(messageHeader, cstring(payload), payload.len())
   GNUNET_MQ_send(GNUNET_CADET_get_mq(channel.handle), envelope)
 
-proc openPort*(handle: var CadetHandle, port: string): ref CadetPort =
+proc openPort*(handle: ref CadetHandle, port: string): ref CadetPort =
   var handlers = messageHandlers()
   var port = hashString(port)
   var openPort: ref CadetPort
@@ -104,12 +97,17 @@ proc openPort*(handle: var CadetHandle, port: string): ref 
CadetPort =
   handle.openPorts.add(openPort)
   return openPort
 
-proc closePort*(handle: var CadetHandle, port: ref CadetPort) =
+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 createChannel*(handle: CadetHandle, peer: string, port: string): ref 
CadetChannel =
+proc createChannel*(handle: ref CadetHandle,
+                    peer: string,
+                    port: string): ref CadetChannel =
   var peerIdentity: GNUNET_PeerIdentity
   discard GNUNET_CRYPTO_eddsa_public_key_from_string(peer, #FIXME: don't 
discard
                                                      peer.len(),
@@ -130,14 +128,28 @@ proc createChannel*(handle: CadetHandle, peer: string, 
port: string): ref CadetC
                                                addr handlers[0])
   return channel
 
-proc connectCadet*(app: ref GnunetApplication): Future[CadetHandle] =
-  result = newFuture[CadetHandle]("connectCadet")
+proc shutdownCb(cls: pointer) {.cdecl.} =
+  let cadetHandle = cast[ptr CadetHandle](cls)
+  echo "shutdownCb"
+  for port in cadetHandle.openPorts:
+    echo "closing port"
+    cadetHandle.internalClosePort(port)
+  cadetHandle.openPorts.setLen(0)
+  echo "disconnecting cadet"
+  GNUNET_CADET_disconnect(cadetHandle.handle)
+
+proc cadetConnectCb(cls: pointer) {.cdecl.} =
+  let app = cast[ptr GnunetApplication](cls)
+  var future: FutureBase
+  if app.connectFutures.take("cadet", future):
+    let cadetHandle = new(CadetHandle)
+    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)
+
+proc connectCadet*(app: ref GnunetApplication): Future[ref CadetHandle] =
+  result = newFuture[ref CadetHandle]("connectCadet")
   app.connectFutures.add("cadet", result)
   discard GNUNET_SCHEDULER_add_now(cadetConnectCb, addr app[])
-
-proc disconnect*(handle: var CadetHandle) =
-  for port in handle.openPorts:
-    handle.closePort(port)
-  GNUNET_CADET_disconnect(handle.handle)
-
-
diff --git a/gnunet_nim.nim b/gnunet_nim.nim
index e82b9bd..9088bdf 100644
--- a/gnunet_nim.nim
+++ b/gnunet_nim.nim
@@ -3,6 +3,7 @@ import asyncdispatch, asyncfutures, asyncfile
 import asynccadet
 import parseopt
 import strutils
+import posix
 
 proc firstTask(gnunetApp: ref GnunetApplication,
                peer: string,
@@ -14,16 +15,17 @@ proc firstTask(gnunetApp: ref GnunetApplication,
   if peer.isNil() and not port.isNil():
     let cadetPort = cadet.openPort(port)
     let (hasChannel, channel) = await cadetPort.channels.read()
-    if (hasChannel):
-      echo "incoming connection!"
-      cadetChannel = channel
+    if not hasChannel:
+      return
+    echo "incoming connection!"
+    cadetChannel = channel
   elif not peer.isNil() and not port.isNil():
     cadetChannel = cadet.createChannel(peer, port)
   if not inputFilename.isNil():
     let inputFile = openAsync(inputFilename, fmRead)
     while true:
       # 64k is close to the limit of GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE
-      let content = await inputFile.read(64000)
+      let content = await inputFile.read(64_000)
       if content.len() == 0:
         break
       cadetChannel.sendMessage(content)
diff --git a/gnunet_scheduler_lib.nim b/gnunet_scheduler_lib.nim
index f596b28..e430872 100644
--- a/gnunet_scheduler_lib.nim
+++ b/gnunet_scheduler_lib.nim
@@ -85,3 +85,7 @@ proc GNUNET_SCHEDULER_add_now*(task: 
GNUNET_SCHEDULER_TaskCallback;
                               task_cls: pointer): ptr GNUNET_SCHEDULER_Task 
{.cdecl,
     importc: "GNUNET_SCHEDULER_add_now", dynlib: libname.}
 
+proc GNUNET_SCHEDULER_add_shutdown*(task: GNUNET_SCHEDULER_TaskCallback;
+                                   task_cls: pointer): ptr 
GNUNET_SCHEDULER_Task {.
+    cdecl, importc: "GNUNET_SCHEDULER_add_shutdown", dynlib: libname.}
+

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



reply via email to

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