diff options
author | joe <joe@jerkface.net> | 2018-06-26 07:56:02 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-06-26 07:56:02 -0400 |
commit | 8cca6e0577d127d8de1624e31a7a47dca74e2ada (patch) | |
tree | d446b7fd127fa4785879f90a129d7af2f781acdf /examples | |
parent | ec651ddc8ec890feebfbabe456d7515d7d83a012 (diff) |
testTox: a pair of simulated tox nodes.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/dhtd.hs | 2 | ||||
-rw-r--r-- | examples/testTox.hs | 80 |
2 files changed, 81 insertions, 1 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index 2bb4ca88..e4b10b8d 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -1750,7 +1750,7 @@ main = do | |||
1750 | addrTox | 1750 | addrTox |
1751 | (Just _netCryptoSessionsState) | 1751 | (Just _netCryptoSessionsState) |
1752 | (dhtkey opts) | 1752 | (dhtkey opts) |
1753 | (quitTox, toxStrap4, toxStrap6) <- Tox.forkTox tox | 1753 | (quitTox, toxStrap4, toxStrap6) <- Tox.forkTox tox True |
1754 | 1754 | ||
1755 | toxSearches <- atomically $ newTVar Map.empty | 1755 | toxSearches <- atomically $ newTVar Map.empty |
1756 | 1756 | ||
diff --git a/examples/testTox.hs b/examples/testTox.hs new file mode 100644 index 00000000..45bc661e --- /dev/null +++ b/examples/testTox.hs | |||
@@ -0,0 +1,80 @@ | |||
1 | {-# LANGUAGE NamedFieldPuns #-} | ||
2 | import Control.Concurrent.STM.TChan | ||
3 | import Control.Concurrent.STM.TMChan | ||
4 | import Control.Concurrent.STM.TVar | ||
5 | import Control.Concurrent.Supply | ||
6 | import Control.Monad.STM | ||
7 | import Crypto.Tox | ||
8 | import qualified Data.IntMap.Strict as IntMap | ||
9 | import Network.QueryResponse | ||
10 | import Network.Socket | ||
11 | import Network.Tox | ||
12 | import Network.Tox.ContactInfo | ||
13 | import qualified Network.Tox.Crypto.Handlers as CH | ||
14 | import Network.Tox.Crypto.Transport | ||
15 | import Network.Tox.DHT.Handlers as DHT | ||
16 | import Network.Tox.Onion.Transport (UDPTransport) | ||
17 | import DPut | ||
18 | |||
19 | |||
20 | makeToxNode :: UDPTransport -> Maybe SecretKey -> IO (Tox extra) | ||
21 | makeToxNode udp sec = do | ||
22 | keysdb <- newKeysDatabase | ||
23 | newToxOverTransport keysdb | ||
24 | (SockAddrInet 0 0) | ||
25 | Nothing | ||
26 | sec | ||
27 | udp | ||
28 | |||
29 | |||
30 | setToxID :: Tox () -> Maybe SecretKey -> IO () | ||
31 | setToxID tox (Just sec) = atomically $ addContactInfo (toxContactInfo tox) sec () | ||
32 | |||
33 | sessionChan :: Tox extra -> IO (TChan (TMChan CryptoMessage)) | ||
34 | sessionChan Tox{toxCryptoSessions} = do | ||
35 | tchan <- atomically newTChan | ||
36 | atomically $ CH.addNewSessionHook toxCryptoSessions $ \_ nc -> do | ||
37 | atomically $ do | ||
38 | session_chan <- newTMChan | ||
39 | writeTChan tchan session_chan | ||
40 | (n,supply) <- freshId <$> readTVar (CH.listenerIDSupply toxCryptoSessions) | ||
41 | writeTVar (CH.listenerIDSupply toxCryptoSessions) supply | ||
42 | modifyTVar' (CH.ncListeners nc) $ IntMap.insert n (0,session_chan) | ||
43 | return Nothing | ||
44 | return tchan | ||
45 | |||
46 | |||
47 | main :: IO () | ||
48 | main = do | ||
49 | mapM_ setVerbose [ minBound .. maxBound ] | ||
50 | setQuiet XRoutes | ||
51 | |||
52 | (udpA,udpB) <- testPairTransport | ||
53 | |||
54 | a_Ct7g5azVcJ8KnvxzbXs9GqeqcjrP7VdZXIC'uHeTlRf | ||
55 | <- makeToxNode udpA $ decodeSecret "YrJQFG7Xppg4WAyhooONNGSCRVQbrRF9L4VoQQsetF" | ||
56 | a_Ct7g5azVcJ8KnvxzbXs9GqeqcjrP7VdZXIC'uHeTlRf | ||
57 | `setToxID` -- BDDj3maMmK8hR3mTpju29wmtE2YJ5cf8NlNEWi1dXlI | ||
58 | decodeSecret "UdZUB9Mf1RD3pVGh02OsRJM6YpmGqJiVxYGVIVHkAG" | ||
59 | |||
60 | a_sessions <- sessionChan a_Ct7g5azVcJ8KnvxzbXs9GqeqcjrP7VdZXIC'uHeTlRf | ||
61 | |||
62 | let b = read "OM7znaPMYkTbm.9GcZJAdnDATXmZxZ9fnaSTP3qNCZk@2.0.0.0:2" | ||
63 | b_OM7znaPMYkTbm'9GcZJAdnDATXmZxZ9fnaSTP3qNCZk | ||
64 | <- makeToxNode udpB $ decodeSecret "Lm2pnsu1+80I8h9txMoZyGgcNwfaoqBlIfg5TwWUXL" | ||
65 | b_OM7znaPMYkTbm'9GcZJAdnDATXmZxZ9fnaSTP3qNCZk | ||
66 | `setToxID` -- AvETKhO-YqnNRopZrj8K3xUpGtGbX0sLhZZjh2VufJB | ||
67 | decodeSecret "L7WNtNIbm0ajNlPrkWvSRpn0nypTUZxlHBckZPlTje" | ||
68 | |||
69 | b_sessions <- sessionChan b_OM7znaPMYkTbm'9GcZJAdnDATXmZxZ9fnaSTP3qNCZk | ||
70 | |||
71 | (a_quit,_,_) <- forkTox a_Ct7g5azVcJ8KnvxzbXs9GqeqcjrP7VdZXIC'uHeTlRf False | ||
72 | (b_quit,_,_) <- forkTox b_OM7znaPMYkTbm'9GcZJAdnDATXmZxZ9fnaSTP3qNCZk False | ||
73 | |||
74 | |||
75 | DHT.ping (toxDHT a_Ct7g5azVcJ8KnvxzbXs9GqeqcjrP7VdZXIC'uHeTlRf) b | ||
76 | |||
77 | putStrLn "Type Enter to quit..." | ||
78 | getLine | ||
79 | |||
80 | return () | ||