diff options
author | joe <joe@jerkface.net> | 2018-05-29 00:59:46 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-05-29 01:01:38 -0400 |
commit | 1bdc1c4080e07a12ac625272347de7649fee8a04 (patch) | |
tree | bfab89095359ca50e850de233c0f884025ebf73e | |
parent | f2e7d76a22681beabcfdbc727b3fe320d34af7e5 (diff) |
Broadcast eventChan for each configured Tox identity.
-rw-r--r-- | dht-client.cabal | 2 | ||||
-rw-r--r-- | examples/dhtd.hs | 4 | ||||
-rw-r--r-- | src/Network/Tox/ContactInfo.hs | 35 | ||||
-rw-r--r-- | todo.txt | 4 |
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 | ||
33 | data ContactEvent = OnionRouted { contact :: PublicKey, onionRouted :: OnionData } | ||
34 | | PolicyChange { contact :: PublicKey, policyChange :: Policy } | ||
35 | |||
32 | data Contact = Contact | 36 | data 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 | |||
43 | nullContact :: Contact | 47 | nullContact :: Contact |
44 | nullContact = Contact | 48 | nullContact = 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 | |||
69 | newAccount :: SecretKey -> STM Account | 74 | newAccount :: SecretKey -> STM Account |
70 | newAccount sk = Account sk <$> newTVar HashMap.empty | 75 | newAccount sk = Account sk <$> newTVar HashMap.empty |
71 | <*> newTVar Set.empty | 76 | <*> newTVar Set.empty |
77 | <*> newBroadcastTChan | ||
72 | 78 | ||
73 | addContactInfo :: ContactInfo -> SecretKey -> STM () | 79 | addContactInfo :: ContactInfo -> SecretKey -> STM () |
74 | addContactInfo (ContactInfo as) sk = do | 80 | addContactInfo (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 | ||
97 | onionUpdate :: OnionData -> Maybe Contact -> Maybe Contact | ||
98 | onionUpdate (Onion.OnionDHTPublicKey dhtpk) | ||
99 | = mergeContact nullContact { contactKeyPacket = Just dhtpk } | ||
100 | onionUpdate (Onion.OnionFriendRequest fr) | ||
101 | = mergeContact nullContact { contactFriendRequest = Just fr } | ||
102 | |||
103 | policyUpdate :: Policy -> Maybe Contact -> Maybe Contact | ||
104 | policyUpdate policy = mergeContact nullContact { contactPolicy = Just policy } | ||
105 | |||
91 | updateAccount :: PublicKey -> Onion.OnionData -> Account -> STM () | 106 | updateAccount :: PublicKey -> Onion.OnionData -> Account -> STM () |
92 | updateAccount remoteUserKey (Onion.OnionDHTPublicKey dhtpk) acc = do | 107 | updateAccount 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 | 111 | setContactPolicy :: PublicKey -> Policy -> Account -> STM () | |
97 | updateAccount remoteUserKey (Onion.OnionFriendRequest fr) acc = do | 112 | setContactPolicy 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 | ||
102 | dnsPresentation :: ContactInfo -> STM String | 117 | dnsPresentation :: ContactInfo -> STM String |
103 | dnsPresentation (ContactInfo accsvar) = do | 118 | dnsPresentation (ContactInfo accsvar) = do |
@@ -46,6 +46,10 @@ bt: Limit peers in get_peers response for UDP packet size limiting (around 1k). | |||
46 | 46 | ||
47 | bt: Use LMDB backend for peer store (and nodes too?). | 47 | bt: Use LMDB backend for peer store (and nodes too?). |
48 | 48 | ||
49 | maint: Separate types for public keys instead of NodeId/NodeInfo: DHTKey/AliasKey/UserKey | ||
50 | |||
51 | maint: Newtype for internet (ipv4,ipv6) sockaddr. | ||
52 | |||
49 | maint: Rename files. | 53 | maint: Rename files. |
50 | 54 | ||
51 | OnionRouter -> Network.Tox.Onion.Routes | 55 | OnionRouter -> Network.Tox.Onion.Routes |