Пример #1
0
 void OnMessageReceived(AsynchronousMessageQueue queue, SystemMessage message)
 {
   if (message.ChannelName == SharesMessaging.CHANNEL)
   {
     IContentDirectory cd = ContentDirectory;
     SharesMessaging.MessageType messageType =
         (SharesMessaging.MessageType) message.MessageType;
     IImporterWorker importerWorker = ServiceRegistration.Get<IImporterWorker>();
     Share share;
     switch (messageType)
     {
       case SharesMessaging.MessageType.ShareAdded:
         share = (Share) message.MessageData[SharesMessaging.SHARE];
         if (cd != null)
           cd.RegisterShareAsync(share);
         importerWorker.ScheduleImport(share.BaseResourcePath, share.MediaCategories, true);
         break;
       case SharesMessaging.MessageType.ShareRemoved:
         share = (Share) message.MessageData[SharesMessaging.SHARE];
         importerWorker.CancelJobsForPath(share.BaseResourcePath);
         if (cd != null)
           cd.RemoveShareAsync(share.ShareId);
         break;
       case SharesMessaging.MessageType.ShareChanged:
         RelocationMode relocationMode = (RelocationMode) message.MessageData[SharesMessaging.RELOCATION_MODE];
         share = (Share) message.MessageData[SharesMessaging.SHARE];
         importerWorker.CancelJobsForPath(share.BaseResourcePath);
         if (cd == null)
         {
           ISettingsManager settingsManager = ServiceRegistration.Get<ISettingsManager>();
           ServerConnectionSettings settings = settingsManager.Load<ServerConnectionSettings>();
           RelocationMode oldMode;
           if (settings.CachedSharesUpdates.TryGetValue(share.ShareId, out oldMode) && oldMode == RelocationMode.ClearAndReImport)
             // ClearAndReimport is stronger than Relocate, use ClearAndReImport
             relocationMode = oldMode;
           settings.CachedSharesUpdates[share.ShareId] = relocationMode;
           settingsManager.Save(settings);
         }
         else
         {
           cd.UpdateShareAsync(share.ShareId, share.BaseResourcePath, share.Name, share.UseShareWatcher, share.MediaCategories, relocationMode);
           switch (relocationMode)
           {
             case RelocationMode.ClearAndReImport:
               importerWorker.ScheduleImport(share.BaseResourcePath, share.MediaCategories, true);
               break;
             case RelocationMode.Relocate:
               importerWorker.ScheduleRefresh(share.BaseResourcePath, share.MediaCategories, true);
               break;
           }
         }
         break;
       case SharesMessaging.MessageType.ReImportShare:
         share = (Share) message.MessageData[SharesMessaging.SHARE];
         importerWorker.ScheduleRefresh(share.BaseResourcePath, share.MediaCategories, true);
         break;
     }
   }
   else if (message.ChannelName == ImporterWorkerMessaging.CHANNEL)
   {
     IContentDirectory cd = ContentDirectory;
     ImporterWorkerMessaging.MessageType messageType = (ImporterWorkerMessaging.MessageType) message.MessageType;
     switch (messageType)
     {
       case ImporterWorkerMessaging.MessageType.ImportStarted:
       case ImporterWorkerMessaging.MessageType.ImportCompleted:
         if (cd == null)
           break;
         ResourcePath path = (ResourcePath) message.MessageData[ImporterWorkerMessaging.RESOURCE_PATH];
         ILocalSharesManagement lsm = ServiceRegistration.Get<ILocalSharesManagement>();
         ICollection<Share> shares = lsm.Shares.Values;
         Share share = shares.BestContainingPath(path);
         if (share == null)
           break;
         if (messageType == ImporterWorkerMessaging.MessageType.ImportStarted)
           cd.ClientStartedShareImportAsync(share.ShareId);
         else
           cd.ClientCompletedShareImportAsync(share.ShareId);
         break;
     }
   }
   else if (message.ChannelName == ServerStateMessaging.CHANNEL)
   {
     //Check if Tv Server state has changed and update if necessary
     ServerStateMessaging.MessageType messageType = (ServerStateMessaging.MessageType)message.MessageType;
     if (messageType == ServerStateMessaging.MessageType.StatesChanged)
     {
       var states = message.MessageData[ServerStateMessaging.STATES] as IDictionary<Guid, object>;
       if (states != null && states.ContainsKey(ShareImportServerState.STATE_ID))
       {
         ShareImportServerState importState = states[ShareImportServerState.STATE_ID] as ShareImportServerState;
         if (importState != null)
         {
           List<ShareImportState> shareStates = new List<ShareImportState>(importState.Shares);
           lock (_syncObj)
           {
             UpdateCurrentlyImportingShares(shareStates.Where(s => s.IsImporting).Select(s => s.ShareId).ToList());
             UpdateCurrentlyImportingSharesProgresses(shareStates.Where(s => s.IsImporting).ToDictionary(s => s.ShareId, s => s.Progress));
           }
         }
       }
       else if (states != null && states.ContainsKey(DatabaseUpgradeServerState.STATE_ID))
       {
         DatabaseUpgradeServerState upgradeState = states[DatabaseUpgradeServerState.STATE_ID] as DatabaseUpgradeServerState;
         if (upgradeState != null && !upgradeState.IsUpgrading && upgradeState.Progress == 100)
         {
           ServerConnectionMessaging.SendServerConnectionStateChangedMessage(ServerConnectionMessaging.MessageType.HomeServerDisconnected);
           ServerConnectionMessaging.SendServerConnectionStateChangedMessage(ServerConnectionMessaging.MessageType.HomeServerConnected);
         }
       }
     }
   }
 }