aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorlurchi <lurchi@strangeplace.net>2018-08-08 23:52:27 +0200
committerlurchi <lurchi@strangeplace.net>2018-08-08 23:52:27 +0200
commit4eb7568d2b14b1ba64bcc5d2a8056105a7bba03a (patch)
tree9e1310f55d1f67acc84dfb1998bba67041b4cabc /examples
parent5852e7b7bbc8ff5464d8dfd86d8d07f8fd49ceb0 (diff)
downloadgnunet-nim-4eb7568d2b14b1ba64bcc5d2a8056105a7bba03a.tar.gz
gnunet-nim-4eb7568d2b14b1ba64bcc5d2a8056105a7bba03a.zip
use peer IDs as chat identifiers
Diffstat (limited to 'examples')
-rw-r--r--examples/groupchat.nim16
1 files changed, 10 insertions, 6 deletions
diff --git a/examples/groupchat.nim b/examples/groupchat.nim
index c2786b0..0ee3b9e 100644
--- a/examples/groupchat.nim
+++ b/examples/groupchat.nim
@@ -1,5 +1,5 @@
1import ../gnunet_application, ../asynccadet 1import ../gnunet_application, ../asynccadet, ../gnunet_utils
2import asyncdispatch, asyncfile, parseopt, strutils 2import asyncdispatch, asyncfile, parseopt, strutils, sequtils
3 3
4type Chat = object 4type Chat = object
5 channels: seq[ref CadetChannel] 5 channels: seq[ref CadetChannel]
@@ -7,7 +7,7 @@ type Chat = object
7proc publish(chat: ref Chat, message: string, sender: ref CadetChannel = nil) = 7proc publish(chat: ref Chat, message: string, sender: ref CadetChannel = nil) =
8 let message = 8 let message =
9 if sender.isNil(): message.strip(leading = false) 9 if sender.isNil(): message.strip(leading = false)
10 else: "[Alice] " & message.strip(leading = false) 10 else: "[" & sender.peer.peerId() & "] " & message.strip(leading = false)
11 echo message 11 echo message
12 for c in chat.channels: 12 for c in chat.channels:
13 c.sendMessage(message) 13 c.sendMessage(message)
@@ -52,14 +52,18 @@ proc firstTask(gnunetApp: ref GnunetApplication,
52 let (hasChannel, channel) = await cadetPort.channels.read() 52 let (hasChannel, channel) = await cadetPort.channels.read()
53 if not hasChannel: 53 if not hasChannel:
54 break 54 break
55 chat.publish(message = "X joined\n") 55 let peerId = channel.peer.peerId()
56 chat.publish(message = peerId & " joined\n")
57 let listParticipants =
58 chat.channels.map(proc(c: ref CadetChannel): string = c.peer.peerId)
59 channel.sendMessage("Wlcome " & peerId & "! participants: " & $listParticipants)
56 chat.channels.add(channel) 60 chat.channels.add(channel)
57 channel.sendMessage("Welcome X! You are talking with: \n")
58 closureScope: 61 closureScope:
59 let channel = channel 62 let channel = channel
63 let peerId = peerId
60 proc channelDisconnected(future: Future[void]) = 64 proc channelDisconnected(future: Future[void]) =
61 chat.channels.delete(chat.channels.find(channel)) 65 chat.channels.delete(chat.channels.find(channel))
62 chat.publish(message = "X left\n") 66 chat.publish(message = peerId & " left\n")
63 processClientMessages(channel, chat).addCallback(channelDisconnected) 67 processClientMessages(channel, chat).addCallback(channelDisconnected)
64 68
65proc main() = 69proc main() =