summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-06-26 07:56:02 -0400
committerjoe <joe@jerkface.net>2018-06-26 07:56:02 -0400
commit8cca6e0577d127d8de1624e31a7a47dca74e2ada (patch)
treed446b7fd127fa4785879f90a129d7af2f781acdf /examples
parentec651ddc8ec890feebfbabe456d7515d7d83a012 (diff)
testTox: a pair of simulated tox nodes.
Diffstat (limited to 'examples')
-rw-r--r--examples/dhtd.hs2
-rw-r--r--examples/testTox.hs80
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 #-}
2import Control.Concurrent.STM.TChan
3import Control.Concurrent.STM.TMChan
4import Control.Concurrent.STM.TVar
5import Control.Concurrent.Supply
6import Control.Monad.STM
7import Crypto.Tox
8import qualified Data.IntMap.Strict as IntMap
9import Network.QueryResponse
10import Network.Socket
11import Network.Tox
12import Network.Tox.ContactInfo
13import qualified Network.Tox.Crypto.Handlers as CH
14import Network.Tox.Crypto.Transport
15import Network.Tox.DHT.Handlers as DHT
16import Network.Tox.Onion.Transport (UDPTransport)
17import DPut
18
19
20makeToxNode :: UDPTransport -> Maybe SecretKey -> IO (Tox extra)
21makeToxNode udp sec = do
22 keysdb <- newKeysDatabase
23 newToxOverTransport keysdb
24 (SockAddrInet 0 0)
25 Nothing
26 sec
27 udp
28
29
30setToxID :: Tox () -> Maybe SecretKey -> IO ()
31setToxID tox (Just sec) = atomically $ addContactInfo (toxContactInfo tox) sec ()
32
33sessionChan :: Tox extra -> IO (TChan (TMChan CryptoMessage))
34sessionChan 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
47main :: IO ()
48main = 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 ()