diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/Tox/Avahi.hs | 60 |
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 @@ | |||
1 | module Network.Tox.Avahi where | 1 | {-# LANGUAGE RecordWildCards #-} |
2 | import Network.Avahi | 2 | {-# LANGUAGE ViewPatterns #-} |
3 | module Network.Tox.Avahi | ||
4 | ( module Network.Tox.Avahi | ||
5 | , NodeInfo(..) | ||
6 | , NodeId(..) | ||
7 | ) where | ||
8 | import Data.Foldable | ||
9 | import GHC.Conc.Sync | ||
10 | import GHC.Word (Word16) | ||
11 | import Network.Address | ||
12 | import Network.Avahi | ||
13 | import Network.Avahi | ||
14 | import Network.Socket | ||
15 | import Network.Tox.NodeId | ||
16 | import Text.Read | ||
17 | |||
18 | toxServiceName :: String | ||
19 | toxServiceName = "_tox_dht._udp" | ||
20 | |||
21 | toxServiceDomain :: String | ||
22 | toxServiceDomain = "local" | ||
23 | |||
24 | (<.>) :: String -> String -> String | ||
25 | a <.> b = a ++ "." ++ b | ||
26 | |||
27 | toxService :: String -> PortNumber -> NodeId -> Service | ||
28 | toxService 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 | |||
40 | announceToxService :: String -> PortNumber -> NodeId -> IO ThreadId | ||
41 | announceToxService = ((.).(.).(.)) (forkIO . announce) toxService | ||
42 | |||
43 | queryToxService :: (NodeInfo -> IO ()) -> IO ThreadId | ||
44 | queryToxService 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) | ||