[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] [gnunet-nim] 34/61: add shutdown logic (allows GNUnet to sh
From: |
gnunet |
Subject: |
[GNUnet-SVN] [gnunet-nim] 34/61: add shutdown logic (allows GNUnet to shutdown on signals) |
Date: |
Sat, 13 Apr 2019 13:36:09 +0200 |
This is an automated email from the git hooks/post-receive script.
ng0 pushed a commit to branch master
in repository gnunet-nim.
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
- [GNUnet-SVN] [gnunet-nim] 08/61: Merge branch 'master' of ssh://gnunet.org/gnunet-nim, (continued)
- [GNUnet-SVN] [gnunet-nim] 08/61: Merge branch 'master' of ssh://gnunet.org/gnunet-nim, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 18/61: make the cleanup proc the finalizer (no explicit call necessary anymore), gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 35/61: use nicer new syntax, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 31/61: use ref object where we only use heap allocation, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 29/61: fix crashes, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 38/61: comment: explain why we're reading from stdin in a nasty way, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 28/61: newline behaviour like in gnunet-cadet, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 32/61: Revert "use ref object where we only use heap allocation", gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 26/61: first try sending messages from stdin, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 40/61: fix bug (application hangs when a peer disconnects); fixes & simplifications, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 34/61: add shutdown logic (allows GNUnet to shutdown on signals),
gnunet <=
- [GNUnet-SVN] [gnunet-nim] 46/61: groupchat.nim: fix typo in echo message, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 25/61: fix sending messages, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 43/61: Merge branch 'master' of ssh://gnunet.org/gnunet-nim, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 21/61: add GNUnet configs and scripts for testing, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 56/61: gnunet_nim.nim: fix import paths to point to parent dir, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 37/61: disconnect from the cadet service when CadetHandle is destroyed, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 53/61: restructure groupchat application, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 44/61: add peer ID string functions, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 45/61: use peer IDs as chat identifiers, gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 48/61: Makefile to clean the build., gnunet, 2019/04/13