2012-08-21 23:58:53 +00:00
|
|
|
{- git-annex assistant network connection watcher, using dbus
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Assistant.Threads.NetWatcher where
|
|
|
|
|
|
|
|
import Assistant.Common
|
2012-08-22 18:32:17 +00:00
|
|
|
import Assistant.Sync
|
2012-08-21 23:58:53 +00:00
|
|
|
import Utility.ThreadScheduler
|
|
|
|
import Remote.List
|
|
|
|
import qualified Types.Remote as Remote
|
|
|
|
|
|
|
|
#if WITH_DBUS
|
|
|
|
import Utility.DBus
|
|
|
|
import DBus.Client
|
|
|
|
import DBus
|
|
|
|
import Data.Word (Word32)
|
2012-11-07 18:53:09 +00:00
|
|
|
import Assistant.NetMessager
|
2012-08-21 23:58:53 +00:00
|
|
|
#else
|
|
|
|
#warning Building without dbus support; will poll for network connection changes
|
|
|
|
#endif
|
|
|
|
|
2012-10-29 06:21:04 +00:00
|
|
|
netWatcherThread :: NamedThread
|
2012-08-21 23:58:53 +00:00
|
|
|
#if WITH_DBUS
|
2012-10-29 06:21:04 +00:00
|
|
|
netWatcherThread = thread dbusThread
|
2012-09-06 18:56:04 +00:00
|
|
|
#else
|
2012-10-29 06:21:04 +00:00
|
|
|
netWatcherThread = thread noop
|
2012-08-21 23:58:53 +00:00
|
|
|
#endif
|
2012-10-29 06:21:04 +00:00
|
|
|
where
|
2013-01-26 06:09:33 +00:00
|
|
|
thread = namedThread "NetWatcher"
|
2012-09-06 18:56:04 +00:00
|
|
|
|
|
|
|
{- This is a fallback for when dbus cannot be used to detect
|
|
|
|
- network connection changes, but it also ensures that
|
|
|
|
- any networked remotes that may have not been routable for a
|
|
|
|
- while (despite the local network staying up), are synced with
|
|
|
|
- periodically. -}
|
2012-10-29 06:21:04 +00:00
|
|
|
netWatcherFallbackThread :: NamedThread
|
2013-01-26 06:09:33 +00:00
|
|
|
netWatcherFallbackThread = namedThread "NetWatcherFallback" $
|
2012-10-29 06:21:04 +00:00
|
|
|
runEvery (Seconds 3600) <~> handleConnection
|
2012-08-21 23:58:53 +00:00
|
|
|
|
|
|
|
#if WITH_DBUS
|
|
|
|
|
2012-10-29 06:21:04 +00:00
|
|
|
dbusThread :: Assistant ()
|
|
|
|
dbusThread = do
|
|
|
|
handleerr <- asIO2 onerr
|
2012-10-30 21:14:26 +00:00
|
|
|
runclient <- asIO1 go
|
2012-10-29 06:21:04 +00:00
|
|
|
liftIO $ persistentClient getSystemAddress () handleerr runclient
|
|
|
|
where
|
|
|
|
go client = ifM (checkNetMonitor client)
|
|
|
|
( do
|
|
|
|
listenNMConnections client <~> handleconn
|
|
|
|
listenWicdConnections client <~> handleconn
|
|
|
|
, do
|
|
|
|
liftAnnex $
|
|
|
|
warning "No known network monitor available through dbus; falling back to polling"
|
|
|
|
)
|
|
|
|
handleconn = do
|
|
|
|
debug ["detected network connection"]
|
2012-11-03 18:16:17 +00:00
|
|
|
notifyNetMessagerRestart
|
2012-10-29 06:21:04 +00:00
|
|
|
handleConnection
|
|
|
|
onerr e _ = do
|
|
|
|
liftAnnex $
|
|
|
|
warning $ "lost dbus connection; falling back to polling (" ++ show e ++ ")"
|
|
|
|
{- Wait, in hope that dbus will come back -}
|
|
|
|
liftIO $ threadDelaySeconds (Seconds 60)
|
2012-08-21 23:58:53 +00:00
|
|
|
|
|
|
|
{- Examine the list of services connected to dbus, to see if there
|
|
|
|
- are any we can use to monitor network connections. -}
|
2012-10-29 06:21:04 +00:00
|
|
|
checkNetMonitor :: Client -> Assistant Bool
|
2012-08-21 23:58:53 +00:00
|
|
|
checkNetMonitor client = do
|
2012-10-29 06:21:04 +00:00
|
|
|
running <- liftIO $ filter (`elem` [networkmanager, wicd])
|
2012-08-21 23:58:53 +00:00
|
|
|
<$> listServiceNames client
|
|
|
|
case running of
|
|
|
|
[] -> return False
|
|
|
|
(service:_) -> do
|
2012-10-29 06:21:04 +00:00
|
|
|
debug [ "Using running DBUS service"
|
2012-08-21 23:58:53 +00:00
|
|
|
, service
|
|
|
|
, "to monitor network connection events."
|
|
|
|
]
|
|
|
|
return True
|
2012-10-29 06:21:04 +00:00
|
|
|
where
|
|
|
|
networkmanager = "org.freedesktop.NetworkManager"
|
|
|
|
wicd = "org.wicd.daemon"
|
2012-08-21 23:58:53 +00:00
|
|
|
|
|
|
|
{- Listens for new NetworkManager connections. -}
|
|
|
|
listenNMConnections :: Client -> IO () -> IO ()
|
|
|
|
listenNMConnections client callback =
|
|
|
|
listen client matcher $ \event ->
|
|
|
|
when (Just True == anyM activeconnection (signalBody event)) $
|
|
|
|
callback
|
2012-10-29 06:21:04 +00:00
|
|
|
where
|
|
|
|
matcher = matchAny
|
|
|
|
{ matchInterface = Just "org.freedesktop.NetworkManager.Connection.Active"
|
|
|
|
, matchMember = Just "PropertiesChanged"
|
|
|
|
}
|
|
|
|
nm_connection_activated = toVariant (2 :: Word32)
|
|
|
|
nm_state_key = toVariant ("State" :: String)
|
|
|
|
activeconnection v = do
|
|
|
|
m <- fromVariant v
|
|
|
|
vstate <- lookup nm_state_key $ dictionaryItems m
|
|
|
|
state <- fromVariant vstate
|
|
|
|
return $ state == nm_connection_activated
|
2012-08-21 23:58:53 +00:00
|
|
|
|
|
|
|
{- Listens for new Wicd connections. -}
|
|
|
|
listenWicdConnections :: Client -> IO () -> IO ()
|
|
|
|
listenWicdConnections client callback =
|
|
|
|
listen client matcher $ \event ->
|
|
|
|
when (any (== wicd_success) (signalBody event)) $
|
|
|
|
callback
|
2012-10-29 06:21:04 +00:00
|
|
|
where
|
|
|
|
matcher = matchAny
|
|
|
|
{ matchInterface = Just "org.wicd.daemon"
|
|
|
|
, matchMember = Just "ConnectResultsSent"
|
|
|
|
}
|
|
|
|
wicd_success = toVariant ("success" :: String)
|
2012-08-21 23:58:53 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-10-29 06:21:04 +00:00
|
|
|
handleConnection :: Assistant ()
|
2012-10-29 20:22:14 +00:00
|
|
|
handleConnection = reconnectRemotes True =<< networkRemotes
|
2012-08-21 23:58:53 +00:00
|
|
|
|
|
|
|
{- Finds network remotes. -}
|
2012-10-29 06:21:04 +00:00
|
|
|
networkRemotes :: Assistant [Remote]
|
|
|
|
networkRemotes = liftAnnex $
|
2013-03-17 19:59:03 +00:00
|
|
|
filter (isNothing . Remote.localpath) <$> enabledRemoteList
|