summaryrefslogtreecommitdiff
path: root/src/Network/Tox
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/Tox')
-rw-r--r--src/Network/Tox/Avahi.hs60
1 files changed, 58 insertions, 2 deletions
diff --git a/src/Network/Tox/Avahi.hs b/src/Network/Tox/Avahi.hs
index 58921744..ba5138bc 100644
--- a/src/Network/Tox/Avahi.hs
+++ b/src/Network/Tox/Avahi.hs
@@ -1,2 +1,58 @@
1module Network.Tox.Avahi where 1{-# LANGUAGE RecordWildCards #-}
2import Network.Avahi 2{-# LANGUAGE ViewPatterns #-}
3module Network.Tox.Avahi
4 ( module Network.Tox.Avahi
5 , NodeInfo(..)
6 , NodeId(..)
7 ) where
8import Data.Foldable
9import GHC.Conc.Sync
10import GHC.Word (Word16)
11import Network.Address
12import Network.Avahi
13import Network.Avahi
14import Network.Socket
15import Network.Tox.NodeId
16import Text.Read
17
18toxServiceName :: String
19toxServiceName = "_tox_dht._udp"
20
21toxServiceDomain :: String
22toxServiceDomain = "local"
23
24(<.>) :: String -> String -> String
25a <.> b = a ++ "." ++ b
26
27toxService :: String -> PortNumber -> NodeId -> Service
28toxService hostname (fromIntegral -> port) (show -> extra) =
29 Service {
30 serviceProtocol = PROTO_UNSPEC,
31 serviceName = "Tox DHT @ " ++ hostname,
32 serviceType = toxServiceName,
33 serviceDomain = toxServiceDomain,
34 serviceHost = if null hostname then "" else hostname <.> toxServiceDomain,
35 serviceAddress = Nothing,
36 servicePort = port,
37 serviceText = extra
38 }
39
40announceToxService :: String -> PortNumber -> NodeId -> IO ThreadId
41announceToxService = ((.).(.).(.)) (forkIO . announce) toxService
42
43queryToxService :: (NodeInfo -> IO ()) -> IO ThreadId
44queryToxService cb =
45 forkIO $
46 browse $
47 BrowseQuery
48 { lookupProtocol = PROTO_UNSPEC
49 , lookupServiceName = toxServiceName
50 , lookupDomain = toxServiceDomain
51 , lookupCallback = runCallback
52 }
53 where
54 runCallback x@Service {..} = do
55 let nid = readMaybe serviceText
56 addr = readMaybe =<< serviceAddress
57 p = fromIntegral servicePort
58 forM_ nid $ \n -> forM_ addr $ \a -> cb (NodeInfo n a p)