public void SetNewHomeServer(string backendServerSystemId)
    {
      ServiceRegistration.Get<ILogger>().Info("ServerConnectionManager: Attaching to MediaPortal backend server '{0}'", backendServerSystemId);

      ServiceRegistration.Get<ILogger>().Debug("ServerConnectionManager: Stopping to watch for MediaPortal servers");
      lock (_syncObj)
        if (_serverWatcher != null)
        {
          _serverWatcher.Stop();
          _serverWatcher = null;
        }

      ServiceRegistration.Get<ILogger>().Debug("ServerConnectionManager: Building UPnP control point for communication with the new home server");
      UPnPClientControlPoint cp;
      lock (_syncObj)
        cp = _controlPoint;
      if (cp != null)
        cp.Stop(); // Must be outside the lock - sends messages
      lock (_syncObj)
      {
        ISettingsManager settingsManager = ServiceRegistration.Get<ISettingsManager>();
        ServerConnectionSettings settings = settingsManager.Load<ServerConnectionSettings>();
        // Here, we only set the system ID of the new home server. The server's system ID will remain in the settings
        // until method SetNewHomeServer is called again.
        settings.HomeServerSystemId = backendServerSystemId;
        settingsManager.Save(settings);
        _controlPoint = BuildClientControlPoint(backendServerSystemId);
      }
      _controlPoint.Start(); // Outside the lock
      ServerConnectionMessaging.SendServerConnectionStateChangedMessage(ServerConnectionMessaging.MessageType.HomeServerAttached);
    }
    public void DetachFromHomeServer()
    {
      ISettingsManager settingsManager = ServiceRegistration.Get<ISettingsManager>();
      ServerConnectionSettings settings = settingsManager.Load<ServerConnectionSettings>();
      ServiceRegistration.Get<ILogger>().Info("ServerConnectionManager: Detaching from home server '{0}'", settings.HomeServerSystemId);

      ServiceRegistration.Get<ILogger>().Debug("ServerConnectionManager: Clearing pending import jobs and suspending importer worker");
      IImporterWorker importerWorker = ServiceRegistration.Get<IImporterWorker>();
      importerWorker.Suspend();
      importerWorker.CancelPendingJobs();

      ServiceRegistration.Get<ILogger>().Debug("ServerConnectionManager: Notifying the MediaPortal server about the detachment");
      UPnPServerControllerServiceProxy sc = ServerControllerServiceProxy;
      ISystemResolver systemResolver = ServiceRegistration.Get<ISystemResolver>();
      if (sc != null)
        try
        {
          sc.DetachClient(systemResolver.LocalSystemId);
          sc.AttachedClientsChanged -= OnAttachedClientsChanged;
          sc.ConnectedClientsChanged -= OnConnectedClientsChanged;
        }
        catch (Exception e)
        {
          ServiceRegistration.Get<ILogger>().Warn("ServerConnectionManager: Error detaching from home server '{0}'", e, HomeServerSystemId);
        }
      UPnPContentDirectoryServiceProxy cd = ContentDirectoryServiceProxy;
      if (cd != null)
        try
        {
          cd.PlaylistsChanged -= OnContentDirectoryPlaylistsChanged;
          cd.MIATypeRegistrationsChanged -= OnContentDirectoryMIATypeRegistrationsChanged;
          cd.RegisteredSharesChangeCounterChanged -= OnRegisteredSharesChangeCounterChanged;
          cd.CurrentlyImportingSharesChanged -= OnCurrentlyImportingSharesChanged;
        }
        catch (Exception e)
        {
          ServiceRegistration.Get<ILogger>().Warn("ServerConnectionManager: Error unregistering from state variable change events", e);
        }

      ServiceRegistration.Get<ILogger>().Debug("ServerConnectionManager: Closing server connection");
      UPnPClientControlPoint cp;
      lock (_syncObj)
        cp = _controlPoint;
      if (cp != null)
        cp.Stop(); // Must be outside the lock - sends messages
      lock (_syncObj)
      {
        settings.HomeServerSystemId = null;
        settings.LastHomeServerName = null;
        settings.LastHomeServerSystem = null;
        settingsManager.Save(settings);
        _controlPoint = null;
      }
      UpdateCurrentlyImportingShares(null); // Mark all shares as not being imported
      ServerConnectionMessaging.SendServerConnectionStateChangedMessage(ServerConnectionMessaging.MessageType.HomeServerDetached);

      ServiceRegistration.Get<ILogger>().Debug("ServerConnectionManager: Starting to watch for MediaPortal servers");
      if (_serverWatcher == null)
      {
        lock (_syncObj)
          _serverWatcher = BuildServerWatcher();
        _serverWatcher.Start(); // Outside the lock
      }
    }
 public ServerConnectionManager()
 {
   _messageQueue = new AsynchronousMessageQueue(this, new string[]
       {
         SharesMessaging.CHANNEL,
         ImporterWorkerMessaging.CHANNEL,
       });
   _messageQueue.MessageReceived += OnMessageReceived;
   _messageQueue.Start();
   string homeServerSystemId = HomeServerSystemId;
   if (string.IsNullOrEmpty(homeServerSystemId))
     // Watch for all MP 2 media servers, if we don't have a homeserver yet
     _serverWatcher = BuildServerWatcher();
   else
     // If we have a homeserver set, we'll try to connect to it
     _controlPoint = BuildClientControlPoint(homeServerSystemId);
 }
 protected UPnPClientControlPoint BuildClientControlPoint(string homeServerSystemId)
 {
   UPnPClientControlPoint result = new UPnPClientControlPoint(homeServerSystemId);
   result.BackendServerConnected += OnBackendServerConnected;
   result.BackendServerDisconnected += OnBackendServerDisconnected;
   return result;
 }
        public void DetachFromHomeServer()
        {
            ISettingsManager         settingsManager = ServiceRegistration.Get <ISettingsManager>();
            ServerConnectionSettings settings        = settingsManager.Load <ServerConnectionSettings>();

            ServiceRegistration.Get <ILogger>().Info("ServerConnectionManager: Detaching from home server '{0}'", settings.HomeServerSystemId);

            ServiceRegistration.Get <ILogger>().Debug("ServerConnectionManager: Clearing pending import jobs and suspending importer worker");
            IImporterWorker importerWorker = ServiceRegistration.Get <IImporterWorker>();

            importerWorker.Suspend();
            importerWorker.CancelPendingJobs();

            ServiceRegistration.Get <ILogger>().Debug("ServerConnectionManager: Notifying the MediaPortal server about the detachment");
            UPnPServerControllerServiceProxy sc = ServerControllerServiceProxy;
            ISystemResolver systemResolver      = ServiceRegistration.Get <ISystemResolver>();

            if (sc != null)
            {
                try
                {
                    sc.DetachClient(systemResolver.LocalSystemId);
                    sc.AttachedClientsChanged  -= OnAttachedClientsChanged;
                    sc.ConnectedClientsChanged -= OnConnectedClientsChanged;
                }
                catch (Exception e)
                {
                    ServiceRegistration.Get <ILogger>().Warn("ServerConnectionManager: Error detaching from home server '{0}'", e, HomeServerSystemId);
                }
            }
            UPnPContentDirectoryServiceProxy cd = ContentDirectoryServiceProxy;

            if (cd != null)
            {
                try
                {
                    cd.PlaylistsChanged                     -= OnContentDirectoryPlaylistsChanged;
                    cd.MIATypeRegistrationsChanged          -= OnContentDirectoryMIATypeRegistrationsChanged;
                    cd.RegisteredSharesChangeCounterChanged -= OnRegisteredSharesChangeCounterChanged;
                    cd.CurrentlyImportingSharesChanged      -= OnCurrentlyImportingSharesChanged;
                }
                catch (Exception e)
                {
                    ServiceRegistration.Get <ILogger>().Warn("ServerConnectionManager: Error unregistering from state variable change events", e);
                }
            }

            ServiceRegistration.Get <ILogger>().Debug("ServerConnectionManager: Closing server connection");
            UPnPClientControlPoint cp;

            lock (_syncObj)
                cp = _controlPoint;
            if (cp != null)
            {
                cp.Stop(); // Must be outside the lock - sends messages
            }
            lock (_syncObj)
            {
                settings.HomeServerSystemId   = null;
                settings.LastHomeServerName   = null;
                settings.LastHomeServerSystem = null;
                settingsManager.Save(settings);
                _controlPoint = null;
            }
            UpdateCurrentlyImportingShares(null); // Mark all shares as not being imported
            ServerConnectionMessaging.SendServerConnectionStateChangedMessage(ServerConnectionMessaging.MessageType.HomeServerDetached);

            ServiceRegistration.Get <ILogger>().Debug("ServerConnectionManager: Starting to watch for MediaPortal servers");
            if (_serverWatcher == null)
            {
                lock (_syncObj)
                    _serverWatcher = BuildServerWatcher();
                _serverWatcher.Start(); // Outside the lock
            }
        }