2012-08-21 23:58:53 +00:00
|
|
|
{- git-annex assistant network connection watcher, using dbus
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-08-21 23:58:53 +00:00
|
|
|
-
|
|
|
|
- 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 qualified Types.Remote as Remote
|
2013-07-26 20:53:50 +00:00
|
|
|
import Assistant.DaemonStatus
|
2013-11-21 21:49:56 +00:00
|
|
|
import Utility.NotificationBroadcaster
|
2012-08-21 23:58:53 +00:00
|
|
|
|
|
|
|
#if WITH_DBUS
|
2014-12-29 21:25:59 +00:00
|
|
|
import Assistant.RemoteControl
|
2012-08-21 23:58:53 +00:00
|
|
|
import Utility.DBus
|
|
|
|
import DBus.Client
|
|
|
|
import DBus
|
2012-11-07 18:53:09 +00:00
|
|
|
import Assistant.NetMessager
|
2012-08-21 23:58:53 +00:00
|
|
|
#else
|
2014-02-25 18:49:27 +00:00
|
|
|
#ifdef linux_HOST_OS
|
2012-08-21 23:58:53 +00:00
|
|
|
#warning Building without dbus support; will poll for network connection changes
|
|
|
|
#endif
|
2014-02-25 18:49:27 +00:00
|
|
|
#endif
|
2012-08-21 23:58:53 +00:00
|
|
|
|
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
|
2013-08-24 17:49:04 +00:00
|
|
|
- periodically.
|
|
|
|
-
|
2014-04-08 19:23:50 +00:00
|
|
|
- Note that it does not call notifyNetMessagerRestart, or
|
|
|
|
- signal the RemoteControl, because it doesn't know that the
|
|
|
|
- network has changed.
|
2013-08-24 17:49:04 +00:00
|
|
|
-}
|
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
|
2014-04-12 21:58:19 +00:00
|
|
|
callback <- asIO1 connchange
|
|
|
|
liftIO $ do
|
|
|
|
listenNMConnections client callback
|
|
|
|
listenWicdConnections client callback
|
2012-10-29 06:21:04 +00:00
|
|
|
, do
|
|
|
|
liftAnnex $
|
|
|
|
warning "No known network monitor available through dbus; falling back to polling"
|
|
|
|
)
|
2014-04-12 21:58:19 +00:00
|
|
|
connchange False = do
|
|
|
|
debug ["detected network disconnection"]
|
2014-04-12 20:32:59 +00:00
|
|
|
sendRemoteControl LOSTNET
|
2014-04-12 21:58:19 +00:00
|
|
|
connchange True = do
|
|
|
|
debug ["detected network connection"]
|
2012-11-03 18:16:17 +00:00
|
|
|
notifyNetMessagerRestart
|
2012-10-29 06:21:04 +00:00
|
|
|
handleConnection
|
2014-04-08 19:23:50 +00:00
|
|
|
sendRemoteControl RESUME
|
2012-10-29 06:21:04 +00:00
|
|
|
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
|
|
|
|
2014-04-12 21:58:19 +00:00
|
|
|
{- Listens for NetworkManager connections and diconnections.
|
|
|
|
-
|
|
|
|
- Connection example (once fully connected):
|
|
|
|
- [Variant {"ActivatingConnection": Variant (ObjectPath "/"), "PrimaryConnection": Variant (ObjectPath "/org/freedesktop/NetworkManager/ActiveConnection/34"), "State": Variant 70}]
|
|
|
|
-
|
|
|
|
- Disconnection example:
|
|
|
|
- [Variant {"ActiveConnections": Variant []}]
|
|
|
|
-}
|
|
|
|
listenNMConnections :: Client -> (Bool -> IO ()) -> IO ()
|
|
|
|
listenNMConnections client setconnected =
|
2014-04-26 23:18:33 +00:00
|
|
|
#if MIN_VERSION_dbus(0,10,7)
|
|
|
|
void $ addMatch client matcher
|
|
|
|
#else
|
|
|
|
listen client matcher
|
|
|
|
#endif
|
2014-08-08 17:55:09 +00:00
|
|
|
$ \event -> mapM_ handleevent
|
2014-04-26 23:18:33 +00:00
|
|
|
(map dictionaryItems $ mapMaybe fromVariant $ signalBody event)
|
2012-10-29 06:21:04 +00:00
|
|
|
where
|
|
|
|
matcher = matchAny
|
2014-04-12 21:58:19 +00:00
|
|
|
{ matchInterface = Just "org.freedesktop.NetworkManager"
|
2012-10-29 06:21:04 +00:00
|
|
|
, matchMember = Just "PropertiesChanged"
|
|
|
|
}
|
2014-04-12 21:58:19 +00:00
|
|
|
nm_active_connections_key = toVariant ("ActiveConnections" :: String)
|
|
|
|
nm_activatingconnection_key = toVariant ("ActivatingConnection" :: String)
|
|
|
|
noconnections = Just $ toVariant $ toVariant ([] :: [ObjectPath])
|
|
|
|
rootconnection = Just $ toVariant $ toVariant $ objectPath_ "/"
|
2014-08-08 17:55:09 +00:00
|
|
|
handleevent m
|
2014-04-12 21:58:19 +00:00
|
|
|
| lookup nm_active_connections_key m == noconnections =
|
|
|
|
setconnected False
|
|
|
|
| lookup nm_activatingconnection_key m == rootconnection =
|
|
|
|
setconnected True
|
|
|
|
| otherwise = noop
|
2012-08-21 23:58:53 +00:00
|
|
|
|
2014-04-12 22:36:48 +00:00
|
|
|
{- Listens for Wicd connections and disconnections.
|
|
|
|
-
|
|
|
|
- Connection example:
|
|
|
|
- ConnectResultsSent:
|
|
|
|
- Variant "success"
|
|
|
|
-
|
|
|
|
- Diconnection example:
|
|
|
|
- StatusChanged
|
|
|
|
- [Variant 0, Variant [Varient ""]]
|
|
|
|
-}
|
2014-04-12 21:58:19 +00:00
|
|
|
listenWicdConnections :: Client -> (Bool -> IO ()) -> IO ()
|
2014-04-12 22:36:48 +00:00
|
|
|
listenWicdConnections client setconnected = do
|
2014-04-26 23:18:33 +00:00
|
|
|
match connmatcher $ \event ->
|
2012-08-21 23:58:53 +00:00
|
|
|
when (any (== wicd_success) (signalBody event)) $
|
2014-04-12 22:36:48 +00:00
|
|
|
setconnected True
|
2014-08-08 17:55:09 +00:00
|
|
|
match statusmatcher $ \event -> handleevent (signalBody event)
|
2012-10-29 06:21:04 +00:00
|
|
|
where
|
2014-04-12 22:36:48 +00:00
|
|
|
connmatcher = matchAny
|
2012-10-29 06:21:04 +00:00
|
|
|
{ matchInterface = Just "org.wicd.daemon"
|
|
|
|
, matchMember = Just "ConnectResultsSent"
|
|
|
|
}
|
2014-04-12 22:36:48 +00:00
|
|
|
statusmatcher = matchAny
|
|
|
|
{ matchInterface = Just "org.wicd.daemon"
|
|
|
|
, matchMember = Just "StatusChanged"
|
|
|
|
}
|
2012-10-29 06:21:04 +00:00
|
|
|
wicd_success = toVariant ("success" :: String)
|
2014-04-12 22:36:48 +00:00
|
|
|
wicd_disconnected = toVariant [toVariant ("" :: String)]
|
2014-08-08 17:55:09 +00:00
|
|
|
handleevent status
|
2014-04-12 22:36:48 +00:00
|
|
|
| any (== wicd_disconnected) status = setconnected False
|
|
|
|
| otherwise = noop
|
2014-04-26 23:18:33 +00:00
|
|
|
match matcher a =
|
|
|
|
#if MIN_VERSION_dbus(0,10,7)
|
|
|
|
void $ addMatch client matcher a
|
|
|
|
#else
|
|
|
|
listen client matcher a
|
|
|
|
#endif
|
2012-08-21 23:58:53 +00:00
|
|
|
#endif
|
|
|
|
|
2012-10-29 06:21:04 +00:00
|
|
|
handleConnection :: Assistant ()
|
2013-11-21 21:49:56 +00:00
|
|
|
handleConnection = do
|
|
|
|
liftIO . sendNotification . networkConnectedNotifier =<< getDaemonStatus
|
|
|
|
reconnectRemotes True =<< networkRemotes
|
2012-08-21 23:58:53 +00:00
|
|
|
|
2013-07-26 20:53:50 +00:00
|
|
|
{- Network remotes to sync with. -}
|
2012-10-29 06:21:04 +00:00
|
|
|
networkRemotes :: Assistant [Remote]
|
2013-07-26 20:53:50 +00:00
|
|
|
networkRemotes = filter (isNothing . Remote.localpath) . syncRemotes
|
|
|
|
<$> getDaemonStatus
|