[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] [gnunet-nim] 26/61: first try sending messages from stdin
From: |
gnunet |
Subject: |
[GNUnet-SVN] [gnunet-nim] 26/61: first try sending messages from stdin |
Date: |
Sat, 13 Apr 2019 13:36:01 +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 839e924c2fb01d4629769621eed6c22f55d33993
Author: lurchi <address@hidden>
AuthorDate: Sat Aug 4 20:09:20 2018 +0200
first try sending messages from stdin
---
asynccadet.nim | 30 ++++++++++++++++--------------
gnunet_nim.nim | 54 ++++++++++++++++++++++++++----------------------------
2 files changed, 42 insertions(+), 42 deletions(-)
diff --git a/asynccadet.nim b/asynccadet.nim
index aaf11d0..8a7c8b3 100644
--- a/asynccadet.nim
+++ b/asynccadet.nim
@@ -77,7 +77,7 @@ proc messageHandlers(): array[2, GNUNET_MQ_MessageHandler] =
proc hashString(port: string): GNUNET_HashCode =
GNUNET_CRYPTO_hash(cstring(port), csize(port.len()), addr result)
-proc sendMessage*(channel: CadetChannel, payload: string) =
+proc sendMessage*(channel: ref CadetChannel, payload: string) =
let messageLen = uint16(payload.len() + sizeof(GNUNET_MessageHeader))
var messageHeader: ptr GNUNET_MessageHeader
var envelope = GNUNET_MQ_msg(addr messageHeader,
@@ -109,25 +109,27 @@ proc closePort*(handle: var CadetHandle, port: ref
CadetPort) =
port.channels.complete()
handle.openPorts.delete(handle.openPorts.find(port))
-proc createChannel*(handle: CadetHandle, peer: string, port: string):
CadetChannel =
+proc createChannel*(handle: 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(),
addr
peerIdentity.public_key)
- result = CadetChannel(handle: nil,
- peer: peerIdentity,
- messages: newFutureStream[string]("createChannel"))
var handlers = messageHandlers()
var port = hashString(port)
- result.handle = GNUNET_CADET_channel_create(handle.handle,
- addr result,
- addr result.peer,
- addr port,
- GNUNET_CADET_OPTION_DEFAULT,
- nil,
- channelDisconnectCb,
- addr handlers[0])
-
+ var channel: ref CadetChannel
+ new(channel)
+ channel.peer = peerIdentity
+ channel.messages = newFutureStream[string]("createChannel")
+ channel.handle = GNUNET_CADET_channel_create(handle.handle,
+ addr channel[],
+ addr channel.peer,
+ addr port,
+ GNUNET_CADET_OPTION_DEFAULT,
+ nil,
+ channelDisconnectCb,
+ addr handlers[0])
+ return channel
+
proc connectCadet*(app: ref GnunetApplication): Future[CadetHandle] =
result = newFuture[CadetHandle]("connectCadet")
app.connectFutures.add("cadet", result)
diff --git a/gnunet_nim.nim b/gnunet_nim.nim
index 32aea5f..a78bb42 100644
--- a/gnunet_nim.nim
+++ b/gnunet_nim.nim
@@ -1,34 +1,35 @@
import gnunet_application
-import asyncdispatch
+import asyncdispatch, asyncfutures, asyncfile
import asynccadet
import parseopt
-proc cadetListen(gnunetApp: ref GnunetApplication, port: string) {.async.} =
- echo "connecting Cadet"
+proc firstTask(gnunetApp: ref GnunetApplication,
+ peer: string,
+ port: string) {.async.} =
var cadet = await gnunetApp.connectCadet()
- echo "connected"
- let cadetPort = cadet.openPort(port)
- echo "port opened"
- let (hasChannel, channel) = await cadetPort.channels.read()
- if hasChannel:
- echo "incoming connection!"
- while true:
- let (hasData, message) = await channel.messages.read()
+ var cadetChannel: ref CadetChannel
+ 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
+ elif not peer.isNil() and not port.isNil():
+ cadetChannel = cadet.createChannel(peer, port)
+ let stdinFile = openAsync("/dev/stdin", fmRead)
+ while true:
+ let messagesFuture = cadetChannel.messages.read()
+ let stdinFuture = stdinFile.readLine()
+ await messagesFuture or stdinFuture
+ if messagesFuture.finished():
+ let (hasData, message) = messagesFuture.read()
if not hasData:
break;
- echo "got message: ", message
-
-proc cadetConnect(gnunetApp: ref GnunetApplication,
- peer: string,
- port: string) {.async.} =
- var cadet = await gnunetApp.connectCadet()
- let cadetChannel = cadet.createChannel(peer, port)
- while true:
- let (hasData, message) = await cadetChannel.messages.read()
- if not hasData:
- break;
- echo "got message: ", message
- cadetChannel.sendMessage("test")
+ echo message
+ if stdinFuture.finished():
+ let input = stdinFuture.read()
+ cadetChannel.sendMessage(input)
+ stdinFile.close()
proc main() =
var peer, port: string
@@ -43,10 +44,7 @@ proc main() =
of cmdEnd:
assert(false)
var gnunetApp = initGnunetApplication("gnunet.conf")
- if peer.isNil() and not port.isNil():
- asyncCheck cadetListen(gnunetApp, port)
- elif not peer.isNil() and not port.isNil():
- asyncCheck cadetConnect(gnunetApp, peer, port)
+ asyncCheck firstTask(gnunetApp, peer, port)
try:
while true:
#echo "polling, timeout = ", gnunetApp.millisecondsUntilTimeout()
--
To stop receiving notification emails like this one, please contact
address@hidden
- [GNUnet-SVN] [gnunet-nim] 16/61: disconnect after connecting (shuts down the application), (continued)
- [GNUnet-SVN] [gnunet-nim] 16/61: disconnect after connecting (shuts down the application), gnunet, 2019/04/13
- [GNUnet-SVN] [gnunet-nim] 12/61: fix segfaults using heap allocation, gnunet, 2019/04/13
- [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 <=
- [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, 2019/04/13
- [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