diff options
author | Joe Crayne <joe@jerkface.net> | 2019-11-27 22:27:19 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-01 22:50:28 -0500 |
commit | 557b47bb3e9a39f74b35abcf4bb09cb85f211106 (patch) | |
tree | 3430cd53d4502a33eb5797b6eed19518467d93d4 /examples | |
parent | 978eecfe2854ce093a5e93e1cb7fbde3677173aa (diff) |
Added tcpclient example.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/tcpclient.hs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/examples/tcpclient.hs b/examples/tcpclient.hs new file mode 100644 index 00000000..d168b1fb --- /dev/null +++ b/examples/tcpclient.hs | |||
@@ -0,0 +1,49 @@ | |||
1 | import Control.Monad | ||
2 | import Control.Concurrent | ||
3 | import Control.Concurrent.STM | ||
4 | import System.Environment | ||
5 | |||
6 | import Crypto.Tox | ||
7 | import Network.Tox.TCP | ||
8 | import Network.QueryResponse | ||
9 | import DebugTag | ||
10 | import DPut | ||
11 | |||
12 | main :: IO () | ||
13 | main = do | ||
14 | setVerbose XMisc | ||
15 | setVerbose XTCP | ||
16 | setVerbose XUnexpected | ||
17 | crypto <- newCrypto | ||
18 | (_,client) <- newClient crypto id (\cb p -> cb (Just (False,p))) | ||
19 | quitTCP <- forkListener "TCP-recv" (addHandler print (handleMessage client) $ clientNet client) | ||
20 | args <- getArgs | ||
21 | let addr = read $ args !! 0 | ||
22 | |||
23 | putStrLn $ unlines | ||
24 | [ "-------------------------------" | ||
25 | , "PING TEST" | ||
26 | , "-------------------------------" | ||
27 | , "ping " ++ show addr | ||
28 | ] | ||
29 | r <- tcpPing client (addr :: NodeInfo) | ||
30 | print r | ||
31 | |||
32 | forM_ (drop 1 args) $ (. read) $ \gw -> do | ||
33 | putStrLn $ unlines | ||
34 | [ "-------------------------------" | ||
35 | , "GetNodes TEST" | ||
36 | , "-------------------------------" | ||
37 | ] | ||
38 | -- getUDPNodes :: TCPClient err () Nonce8 -> NodeId -> UDP.NodeInfo -> IO (Maybe ([UDP.NodeInfo], [UDP.NodeInfo], Maybe ())) | ||
39 | let nid = read "OrjBG.GyWuQhGc1pb0KssgmYAocohFh35Vx8mREC9Nu" | ||
40 | s <- getUDPNodes (TCPClient crypto client $ \_ -> return $ Just gw) nid (udpNodeInfo addr) | ||
41 | print s | ||
42 | |||
43 | putStrLn $ unlines | ||
44 | [ "-------------------------------" | ||
45 | , "quitTCP" | ||
46 | , "-------------------------------" | ||
47 | ] | ||
48 | quitTCP | ||
49 | return () | ||