diff options
Diffstat (limited to 'dht/src/Network/Tox/Avahi.hs')
-rw-r--r-- | dht/src/Network/Tox/Avahi.hs | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/dht/src/Network/Tox/Avahi.hs b/dht/src/Network/Tox/Avahi.hs index 635ba656..2ca6515c 100644 --- a/dht/src/Network/Tox/Avahi.hs +++ b/dht/src/Network/Tox/Avahi.hs | |||
@@ -1,4 +1,5 @@ | |||
1 | {-# OPTIONS_GHC -Wall #-} | 1 | {-# OPTIONS_GHC -Wall #-} |
2 | {-# LANGUAGE CPP #-} | ||
2 | {-# LANGUAGE RecordWildCards #-} | 3 | {-# LANGUAGE RecordWildCards #-} |
3 | {-# LANGUAGE ViewPatterns #-} | 4 | {-# LANGUAGE ViewPatterns #-} |
4 | module Network.Tox.Avahi | 5 | module Network.Tox.Avahi |
@@ -10,11 +11,17 @@ module Network.Tox.Avahi | |||
10 | import Control.Applicative | 11 | import Control.Applicative |
11 | import Data.Foldable | 12 | import Data.Foldable |
12 | import Network.Address | 13 | import Network.Address |
13 | import Network.Avahi | ||
14 | import Network.BSD (getHostName) | 14 | import Network.BSD (getHostName) |
15 | import Network.Tox.NodeId | 15 | import Network.Tox.NodeId |
16 | import Text.Read | 16 | import Text.Read |
17 | 17 | ||
18 | #if defined(VERSION_avahi) | ||
19 | import Network.Avahi | ||
20 | #else | ||
21 | data Service = Service | ||
22 | #endif | ||
23 | |||
24 | |||
18 | toxServiceName :: String | 25 | toxServiceName :: String |
19 | toxServiceName = "_tox_dht._udp" | 26 | toxServiceName = "_tox_dht._udp" |
20 | 27 | ||
@@ -26,7 +33,9 @@ a <.> b = a ++ "." ++ b | |||
26 | 33 | ||
27 | toxService :: String -> PortNumber -> NodeId -> (Maybe NodeId) -> Service | 34 | toxService :: String -> PortNumber -> NodeId -> (Maybe NodeId) -> Service |
28 | toxService hostname (fromIntegral -> port) dhtkey toxid = | 35 | toxService hostname (fromIntegral -> port) dhtkey toxid = |
29 | Service { | 36 | Service |
37 | #if defined(VERSION_avahi) | ||
38 | { | ||
30 | serviceProtocol = PROTO_UNSPEC, | 39 | serviceProtocol = PROTO_UNSPEC, |
31 | serviceName = "Tox DHT @ " ++ hostname, | 40 | serviceName = "Tox DHT @ " ++ hostname, |
32 | serviceType = toxServiceName, | 41 | serviceType = toxServiceName, |
@@ -36,10 +45,15 @@ toxService hostname (fromIntegral -> port) dhtkey toxid = | |||
36 | servicePort = port, | 45 | servicePort = port, |
37 | serviceText = maybe (show dhtkey) (show . ((,) dhtkey)) toxid | 46 | serviceText = maybe (show dhtkey) (show . ((,) dhtkey)) toxid |
38 | } | 47 | } |
48 | #endif | ||
39 | 49 | ||
40 | announceToxServiceWithHostname :: String -> PortNumber -> NodeId -> (Maybe NodeId) -> IO () | 50 | announceToxServiceWithHostname :: String -> PortNumber -> NodeId -> (Maybe NodeId) -> IO () |
51 | #if defined(VERSION_avahi) | ||
41 | announceToxServiceWithHostname = (boobs.boobs) announce toxService | 52 | announceToxServiceWithHostname = (boobs.boobs) announce toxService |
42 | where boobs = ((.).(.)) | 53 | where boobs = ((.).(.)) |
54 | #else | ||
55 | announceToxServiceWithHostname _ _ _ _ = return () | ||
56 | #endif | ||
43 | 57 | ||
44 | announceToxService :: PortNumber -> NodeId -> (Maybe NodeId) -> IO () | 58 | announceToxService :: PortNumber -> NodeId -> (Maybe NodeId) -> IO () |
45 | announceToxService a b c = do | 59 | announceToxService a b c = do |
@@ -48,6 +62,7 @@ announceToxService a b c = do | |||
48 | 62 | ||
49 | queryToxService :: (NodeInfo -> Maybe NodeId -> IO ()) -> IO () | 63 | queryToxService :: (NodeInfo -> Maybe NodeId -> IO ()) -> IO () |
50 | queryToxService cb = | 64 | queryToxService cb = |
65 | #if defined(VERSION_avahi) | ||
51 | browse $ | 66 | browse $ |
52 | BrowseQuery | 67 | BrowseQuery |
53 | { lookupProtocol = PROTO_UNSPEC | 68 | { lookupProtocol = PROTO_UNSPEC |
@@ -63,3 +78,6 @@ queryToxService cb = | |||
63 | addr = readMaybe =<< serviceAddress | 78 | addr = readMaybe =<< serviceAddress |
64 | p = fromIntegral servicePort | 79 | p = fromIntegral servicePort |
65 | forM_ nid $ \n -> forM_ addr $ \a -> cb (NodeInfo n a p) (snd <$> both) | 80 | forM_ nid $ \n -> forM_ addr $ \a -> cb (NodeInfo n a p) (snd <$> both) |
81 | #else | ||
82 | return () | ||
83 | #endif | ||