summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-05-29 00:59:46 -0400
committerjoe <joe@jerkface.net>2018-05-29 01:01:38 -0400
commit1bdc1c4080e07a12ac625272347de7649fee8a04 (patch)
treebfab89095359ca50e850de233c0f884025ebf73e
parentf2e7d76a22681beabcfdbc727b3fe320d34af7e5 (diff)
Broadcast eventChan for each configured Tox identity.
-rw-r--r--dht-client.cabal2
-rw-r--r--examples/dhtd.hs4
-rw-r--r--src/Network/Tox/ContactInfo.hs35
-rw-r--r--todo.txt4
4 files changed, 31 insertions, 14 deletions
diff --git a/dht-client.cabal b/dht-client.cabal
index 9a121f23..60a42f00 100644
--- a/dht-client.cabal
+++ b/dht-client.cabal
@@ -143,7 +143,7 @@ library
143 , array 143 , array
144 , hashable 144 , hashable
145 , iproute 145 , iproute
146 , stm 146 , stm >= 2.4.0
147 , base16-bytestring 147 , base16-bytestring
148 , base32-bytestring 148 , base32-bytestring
149 , base64-bytestring 149 , base64-bytestring
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index 091b1565..47a4cd46 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -1230,9 +1230,7 @@ toxman announcer toxbkts tox = ToxManager
1230 accs <- readTVar accounts 1230 accs <- readTVar accounts
1231 case HashMap.lookup meid accs of 1231 case HashMap.lookup meid accs of
1232 Nothing -> return () -- Unknown account. 1232 Nothing -> return () -- Unknown account.
1233 Just acc -> modifyTVar' (contacts acc) 1233 Just acc -> setContactPolicy (Tox.id2key themid) TryingToConnect acc
1234 $ HashMap.alter (mergeContact nullContact { contactPolicy = Just TryingToConnect })
1235 themid
1236 -- If unscheduled and unconnected, schedule recurring search for this contact. 1234 -- If unscheduled and unconnected, schedule recurring search for this contact.
1237 _ -> return () -- Remove contact. 1235 _ -> return () -- Remove contact.
1238 } 1236 }
diff --git a/src/Network/Tox/ContactInfo.hs b/src/Network/Tox/ContactInfo.hs
index 153cd130..880740bf 100644
--- a/src/Network/Tox/ContactInfo.hs
+++ b/src/Network/Tox/ContactInfo.hs
@@ -27,15 +27,19 @@ data Account = Account
27 { userSecret :: SecretKey -- local secret key 27 { userSecret :: SecretKey -- local secret key
28 , contacts :: TVar (HashMap NodeId{-friend's userkey-} Contact) -- received contact info 28 , contacts :: TVar (HashMap NodeId{-friend's userkey-} Contact) -- received contact info
29 , clientRefs :: TVar (Set ConnectionKey) -- reference count so we know when to send offline etc 29 , clientRefs :: TVar (Set ConnectionKey) -- reference count so we know when to send offline etc
30 , eventChan :: TChan ContactEvent
30 } 31 }
31 32
33data ContactEvent = OnionRouted { contact :: PublicKey, onionRouted :: OnionData }
34 | PolicyChange { contact :: PublicKey, policyChange :: Policy }
35
32data Contact = Contact 36data Contact = Contact
33 { contactKeyPacket :: Maybe (DHT.DHTPublicKey) 37 { contactKeyPacket :: Maybe (DHT.DHTPublicKey)
34 , contactLastSeenAddr :: Maybe SockAddr 38 , contactLastSeenAddr :: Maybe SockAddr
35 , contactFriendRequest :: Maybe (DHT.FriendRequest) 39 , contactFriendRequest :: Maybe (DHT.FriendRequest)
36 , contactPolicy :: Maybe (Connection.Policy) 40 , contactPolicy :: Maybe (Connection.Policy)
37 -- Possible semantics 41 -- Possible semantics
38 -- RefusingToConnect : rejected friend-request or blocked. 42 -- RefusingToConnect : rejected friend-request or blocked or unknown.
39 -- OpenToConnect : pending friend-request. 43 -- OpenToConnect : pending friend-request.
40 -- TryingToConnect : roster entry. 44 -- TryingToConnect : roster entry.
41 } 45 }
@@ -43,6 +47,7 @@ data Contact = Contact
43nullContact :: Contact 47nullContact :: Contact
44nullContact = Contact 48nullContact = Contact
45 { contactKeyPacket = Nothing 49 { contactKeyPacket = Nothing
50 , contactLastSeenAddr = Nothing
46 , contactFriendRequest = Nothing 51 , contactFriendRequest = Nothing
47 , contactPolicy = Nothing 52 , contactPolicy = Nothing
48 } 53 }
@@ -69,6 +74,7 @@ newContactInfo = atomically $ ContactInfo <$> newTVar HashMap.empty
69newAccount :: SecretKey -> STM Account 74newAccount :: SecretKey -> STM Account
70newAccount sk = Account sk <$> newTVar HashMap.empty 75newAccount sk = Account sk <$> newTVar HashMap.empty
71 <*> newTVar Set.empty 76 <*> newTVar Set.empty
77 <*> newBroadcastTChan
72 78
73addContactInfo :: ContactInfo -> SecretKey -> STM () 79addContactInfo :: ContactInfo -> SecretKey -> STM ()
74addContactInfo (ContactInfo as) sk = do 80addContactInfo (ContactInfo as) sk = do
@@ -88,16 +94,25 @@ updateContactInfo roster Onion.AnnouncedRendezvous{remoteUserKey} (localUserKey,
88 $ HashMap.lookup (key2id localUserKey) as 94 $ HashMap.lookup (key2id localUserKey) as
89 95
90 96
97onionUpdate :: OnionData -> Maybe Contact -> Maybe Contact
98onionUpdate (Onion.OnionDHTPublicKey dhtpk)
99 = mergeContact nullContact { contactKeyPacket = Just dhtpk }
100onionUpdate (Onion.OnionFriendRequest fr)
101 = mergeContact nullContact { contactFriendRequest = Just fr }
102
103policyUpdate :: Policy -> Maybe Contact -> Maybe Contact
104policyUpdate policy = mergeContact nullContact { contactPolicy = Just policy }
105
91updateAccount :: PublicKey -> Onion.OnionData -> Account -> STM () 106updateAccount :: PublicKey -> Onion.OnionData -> Account -> STM ()
92updateAccount remoteUserKey (Onion.OnionDHTPublicKey dhtpk) acc = do 107updateAccount remoteUserKey omsg acc = do
93 modifyTVar' (contacts acc) 108 modifyTVar' (contacts acc) $ HashMap.alter (onionUpdate omsg) (key2id remoteUserKey)
94 $ HashMap.alter (mergeContact nullContact { contactKeyPacket = Just dhtpk }) 109 writeTChan (eventChan acc) $ OnionRouted remoteUserKey omsg
95 (key2id remoteUserKey) 110
96 111setContactPolicy :: PublicKey -> Policy -> Account -> STM ()
97updateAccount remoteUserKey (Onion.OnionFriendRequest fr) acc = do 112setContactPolicy remoteUserKey policy acc = do
98 modifyTVar' (contacts acc) 113 modifyTVar' (contacts acc) $ HashMap.alter (policyUpdate policy) (key2id remoteUserKey)
99 $ HashMap.alter (mergeContact nullContact { contactFriendRequest = Just fr }) 114 writeTChan (eventChan acc) $ PolicyChange remoteUserKey policy
100 (key2id remoteUserKey) 115
101 116
102dnsPresentation :: ContactInfo -> STM String 117dnsPresentation :: ContactInfo -> STM String
103dnsPresentation (ContactInfo accsvar) = do 118dnsPresentation (ContactInfo accsvar) = do
diff --git a/todo.txt b/todo.txt
index 9659e78a..f1f1ca9a 100644
--- a/todo.txt
+++ b/todo.txt
@@ -46,6 +46,10 @@ bt: Limit peers in get_peers response for UDP packet size limiting (around 1k).
46 46
47bt: Use LMDB backend for peer store (and nodes too?). 47bt: Use LMDB backend for peer store (and nodes too?).
48 48
49maint: Separate types for public keys instead of NodeId/NodeInfo: DHTKey/AliasKey/UserKey
50
51maint: Newtype for internet (ipv4,ipv6) sockaddr.
52
49maint: Rename files. 53maint: Rename files.
50 54
51 OnionRouter -> Network.Tox.Onion.Routes 55 OnionRouter -> Network.Tox.Onion.Routes