diff options
Diffstat (limited to 'src/Network/Tox/Avahi.hs')
-rw-r--r-- | src/Network/Tox/Avahi.hs | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/src/Network/Tox/Avahi.hs b/src/Network/Tox/Avahi.hs index 7fe87db2..559e5fd7 100644 --- a/src/Network/Tox/Avahi.hs +++ b/src/Network/Tox/Avahi.hs | |||
@@ -1,16 +1,18 @@ | |||
1 | {-# OPTIONS_GHC -Wall #-} | ||
1 | {-# LANGUAGE RecordWildCards #-} | 2 | {-# LANGUAGE RecordWildCards #-} |
2 | {-# LANGUAGE ViewPatterns #-} | 3 | {-# LANGUAGE ViewPatterns #-} |
3 | module Network.Tox.Avahi | 4 | module Network.Tox.Avahi |
4 | ( module Network.Tox.Avahi | 5 | ( module Network.Tox.Avahi |
5 | , NodeInfo(..) | 6 | , NodeInfo(..) |
6 | , NodeId(..) | 7 | , NodeId |
7 | ) where | 8 | ) where |
8 | 9 | ||
9 | import Data.Foldable | 10 | import Control.Applicative |
10 | import Network.Address | 11 | import Data.Foldable |
11 | import Network.Avahi | 12 | import Network.Address |
12 | import Network.Tox.NodeId | 13 | import Network.Avahi |
13 | import Text.Read | 14 | import Network.Tox.NodeId |
15 | import Text.Read | ||
14 | 16 | ||
15 | toxServiceName :: String | 17 | toxServiceName :: String |
16 | toxServiceName = "_tox_dht._udp" | 18 | toxServiceName = "_tox_dht._udp" |
@@ -21,8 +23,8 @@ toxServiceDomain = "local" | |||
21 | (<.>) :: String -> String -> String | 23 | (<.>) :: String -> String -> String |
22 | a <.> b = a ++ "." ++ b | 24 | a <.> b = a ++ "." ++ b |
23 | 25 | ||
24 | toxService :: String -> PortNumber -> NodeId -> Service | 26 | toxService :: String -> PortNumber -> NodeId -> (Maybe NodeId) -> Service |
25 | toxService hostname (fromIntegral -> port) (show -> extra) = | 27 | toxService hostname (fromIntegral -> port) dhtkey toxid = |
26 | Service { | 28 | Service { |
27 | serviceProtocol = PROTO_UNSPEC, | 29 | serviceProtocol = PROTO_UNSPEC, |
28 | serviceName = "Tox DHT @ " ++ hostname, | 30 | serviceName = "Tox DHT @ " ++ hostname, |
@@ -31,16 +33,17 @@ toxService hostname (fromIntegral -> port) (show -> extra) = | |||
31 | serviceHost = if null hostname then "" else hostname <.> toxServiceDomain, | 33 | serviceHost = if null hostname then "" else hostname <.> toxServiceDomain, |
32 | serviceAddress = Nothing, | 34 | serviceAddress = Nothing, |
33 | servicePort = port, | 35 | servicePort = port, |
34 | serviceText = extra | 36 | serviceText = maybe (show dhtkey) (show . ((,) dhtkey)) toxid |
35 | } | 37 | } |
36 | 38 | ||
37 | announceToxServiceWithHostname :: String -> PortNumber -> NodeId -> IO () | 39 | announceToxServiceWithHostname :: String -> PortNumber -> NodeId -> (Maybe NodeId) -> IO () |
38 | announceToxServiceWithHostname = ((.).(.).(.)) announce toxService | 40 | announceToxServiceWithHostname = (boobs.boobs) announce toxService |
41 | where boobs = ((.).(.)) | ||
39 | 42 | ||
40 | announceToxService :: PortNumber -> NodeId -> IO () | 43 | announceToxService :: PortNumber -> NodeId -> (Maybe NodeId) -> IO () |
41 | announceToxService = announceToxServiceWithHostname "" | 44 | announceToxService = announceToxServiceWithHostname "" |
42 | 45 | ||
43 | queryToxService :: (NodeInfo -> IO ()) -> IO () | 46 | queryToxService :: (NodeInfo -> Maybe NodeId -> IO ()) -> IO () |
44 | queryToxService cb = | 47 | queryToxService cb = |
45 | browse $ | 48 | browse $ |
46 | BrowseQuery | 49 | BrowseQuery |
@@ -50,8 +53,10 @@ queryToxService cb = | |||
50 | , lookupCallback = runCallback | 53 | , lookupCallback = runCallback |
51 | } | 54 | } |
52 | where | 55 | where |
53 | runCallback x@Service {..} = do | 56 | runCallback Service {..} = do |
54 | let nid = readMaybe serviceText | 57 | let both :: Maybe (NodeId, NodeId) |
58 | both = readMaybe serviceText | ||
59 | nid = (fst <$> both) <|> readMaybe serviceText | ||
55 | addr = readMaybe =<< serviceAddress | 60 | addr = readMaybe =<< serviceAddress |
56 | p = fromIntegral servicePort | 61 | p = fromIntegral servicePort |
57 | forM_ nid $ \n -> forM_ addr $ \a -> cb (NodeInfo n a p) | 62 | forM_ nid $ \n -> forM_ addr $ \a -> cb (NodeInfo n a p) (snd <$> both) |