internal static async Task <bool> GetServerConfiguration(IExtendedApiClient apiClient, ILog logger)
        {
            try
            {
                apiClient.ChangeServerLocation(App.Settings.ConnectionDetails.HostName, App.Settings.ConnectionDetails.PortNo);

                logger.Info("Getting server configuration. Hostname ({0}), Port ({1})", apiClient.ServerHostName, apiClient.ServerApiPort);

                var config = await apiClient.GetServerConfigurationAsync();

                App.Settings.ServerConfiguration = config;

                logger.Info("Getting System information");

                var sysInfo = await apiClient.GetSystemInfoAsync();

                App.Settings.SystemStatus = sysInfo;

                if (SimpleIoc.Default.IsRegistered <ApiWebSocket>())
                {
                    SimpleIoc.Default.Unregister <ApiWebSocket>();
                }

                App.WebSocketClient = await ApiWebSocket.Create(new MBLogger(), new NewtonsoftJsonSerializer(), (ApiClient)apiClient, () => new WebSocketClient(), new CancellationToken());

                return(true);
            }
            catch (HttpException ex)
            {
                logger.ErrorException("GetServerConfiguration()", ex);
                return(false);
            }
        }
Exemple #2
0
 private void sendUpdateToSocket(string serviceName, byte[] dtoSerialized, ApiWebSocket ws)
 {
     try
     {
         ws.SendMessage(dtoSerialized);
     }
     catch { }
 }
Exemple #3
0
        private HttpResponse handleApi(HttpRequest req)
        {
            var ws = new ApiWebSocket(this);

            _sockets.TryAdd(ws, true);
            Console.WriteLine($"API socket {ws.Id}: connected from {req.ClientIPAddress}");
            Console.WriteLine($"   live sockets: {_sockets.Keys.Select(s => s.Id).Order().JoinString(", ")}");
            return(HttpResponse.WebSocket(ws));
        }
Exemple #4
0
        /// <summary>
        /// Disposes the current ApiClient and creates a new one
        /// </summary>
        private void ReloadApiClient()
        {
            var logger = LogManager.GetLogger("ApiClient");

            var apiClient = new ApiClient(new HttpWebRequestClient(logger), logger, TheaterConfigurationManager.Configuration.ServerAddress, "Media Browser Theater", Environment.MachineName, Environment.MachineName, ApplicationVersion.ToString())
            {
                JsonSerializer = JsonSerializer,
                ImageQuality   = TheaterConfigurationManager.Configuration.DownloadCompressedImages
                                                       ? 90
                                                       : 100
            };

            ApiClient = apiClient;
            ApiClient.HttpResponseReceived += ApiClient_HttpResponseReceived;

            logger = LogManager.GetLogger("ApiWebSocket");

            ApiWebSocket = new ApiWebSocket(logger, JsonSerializer, apiClient.ServerAddress,
                                            ApiClient.DeviceId, apiClient.ApplicationVersion,
                                            apiClient.ClientName, apiClient.DeviceName, () => ClientWebSocketFactory.CreateWebSocket(logger));

            apiClient.WebSocketConnection = ApiWebSocket;
        }
Exemple #5
0
        /// <summary>
        /// Disposes the current ApiClient and creates a new one
        /// </summary>
        private void ReloadApiClient()
        {
            var logger = LogManager.GetLogger("ApiClient");

            var apiClient = new ApiClient(logger, TheaterConfigurationManager.Configuration.ServerHostName, TheaterConfigurationManager.Configuration.ServerApiPort, "Media Browser Theater", Environment.MachineName, Environment.MachineName, ApplicationVersion.ToString())
            {
                JsonSerializer = JsonSerializer,
                ImageQuality   = TheaterConfigurationManager.Configuration.DownloadCompressedImages
                                                       ? 90
                                                       : 100
            };

            ApiClient = apiClient;

            logger = LogManager.GetLogger("ApiWebSocket");

            // WebSocketEntry point will handle figuring out the port and connecting
            ApiWebSocket = new ApiWebSocket(logger, JsonSerializer, apiClient.ServerHostName, 0,
                                            ApiClient.DeviceId, apiClient.ApplicationVersion,
                                            apiClient.ClientName, apiClient.DeviceName, () => ClientWebSocketFactory.CreateWebSocket(logger));

            apiClient.WebSocketConnection = ApiWebSocket;
        }
        public void LaunchEntryPoint(string entryPointPath)
        {
            Debug.Assert(Microsoft.MediaCenter.UI.Application.ApplicationThread == Thread.CurrentThread);
            
            this.entryPointPath = entryPointPath;

            if (IsInEntryPoint)
            {
                //add in a fake breadcrumb so they will show properly
                session.AddBreadcrumb("DIRECTENTRY");
            }

            if (this.EntryPointPath.ToLower() == ConfigEntryPointVal) //specialized case for config page
            {
                OpenConfiguration(true);
            }
            else
            {
                try
                {
                    this.RootFolderModel = (FolderModel)ItemFactory.Instance.Create(EntryPointResolver.EntryPoint(this.EntryPointPath));

                    WebSocket = new ApiWebSocket(new WebSocket4NetClientWebSocket());
                    WebSocket.Connect(Kernel.ApiClient.ServerHostName, Kernel.ApiClient.ServerApiPort, Kernel.ApiClient.ClientType, Kernel.ApiClient.DeviceId);
                    try
                    {
                        Kernel.ApiClient.ReportRemoteCapabilities();
                    }
                    catch (Exception e)
                    {
                        Logger.ReportException("Error reporting remote capabilities to server.",e);
                    }
                    WebSocket.LibraryChanged += LibraryChanged;
                    WebSocket.BrowseCommand += BrowseRequest;
                    WebSocket.PlayCommand += PlayRequest;
                    WebSocket.PlaystateCommand += PlayStateRequest;
                    WebSocket.SystemCommand += SystemCommand;
                    WebSocket.GeneralCommand += GeneralCommand;
                  
                    if (Config.EnableUpdates && Config.EnableSilentUpdates && !RunningOnExtender)
                    {
                        Async.Queue(Async.ThreadPoolName.StartupQueue, () =>
                                                             {
                                                                 while (!PackagesRetrieved) {Thread.Sleep(500);}
                                                                 RefreshPluginCollections();
                                                                 while (InstalledPluginsCollection == null) {Thread.Sleep(500);}
                                                                 UpdateAllPlugins(true);
                                                             });
                    }

                    // We check config here instead of in the Updater class because the Config class 
                    // CANNOT be instantiated outside of the application thread.
                    Async.Queue(Async.ThreadPoolName.StartupQueue, () => CheckForSystemUpdate(Config.EnableUpdates && !RunningOnExtender && Kernel.CurrentUser.Dto.Policy.IsAdministrator), 10000);

                    if (Kernel.CurrentUser.Dto.Policy.IsAdministrator) // don't show these prompts to non-admins
                    {
                        // Let the user know if the server needs to be restarted
                        // Put it on the same thread as the update checks so it will be behind them
                        Async.Queue(Async.ThreadPoolName.StartupQueue, () =>
                                                             {
                                                                 if (Kernel.ServerInfo.HasPendingRestart)
                                                                 {
                                                                     if (Kernel.ServerInfo.CanSelfRestart)
                                                                     {
                                                                         if (YesNoBox("The Emby Server needs to re-start to apply an update.  Restart now?") == "Y")
                                                                         {
                                                                             Kernel.ApiClient.PerformPendingRestart();
                                                                             MessageBox("Your server is being re-started.  Emby for WMC will now exit so you can re load it.");
                                                                             Close();
                                                                         }
                                                                     }
                                                                     else
                                                                     {
                                                                        MessageBox("Your server needs to be re-started to apply an update.  You must re-start it from the server machine.");
                                                                     }
                                                                 }
                                                             },35000);
                        
                    }

                    if (!Config.NewViewsIntroShown)
                    {
                        //Make this come up after the home screen
                        Async.Queue(Async.ThreadPoolName.NewViews, () => OpenMCMLPage("resx://MediaBrowser/MediaBrowser.Resources/NewViewsIntro", new Dictionary<string, object> {{"Application", CurrentInstance}}), 3000);
                    }

                    Navigate(this.RootFolderModel);
                }
                catch (Exception ex)
                {
                    Application.MediaCenterEnvironment.Dialog(CurrentInstance.StringData("EntryPointErrorDial") + this.EntryPointPath + ". " + ex.ToString() + " " + ex.StackTrace.ToString(), CurrentInstance.StringData("EntryPointErrorCapDial"), DialogButtons.Ok, 30, true);
                    Close();
                }
            }
        }
        public void LaunchEntryPoint(string entryPointPath)
        {
            this.entryPointPath = entryPointPath;

            if (IsInEntryPoint)
            {
                //add in a fake breadcrumb so they will show properly
                session.AddBreadcrumb("DIRECTENTRY");
            }

            if (this.EntryPointPath.ToLower() == ConfigEntryPointVal) //specialized case for config page
            {
                OpenConfiguration(true);
            }
            else
            {
                try
                {
                    this.RootFolderModel = (FolderModel)ItemFactory.Instance.Create(EntryPointResolver.EntryPoint(this.EntryPointPath));

                    WebSocket = new ApiWebSocket(new WebSocket4NetClientWebSocket());
                    WebSocket.Connect(Kernel.ApiClient.ServerHostName, Kernel.ServerInfo.WebSocketPortNumber, Kernel.ApiClient.ClientType, Kernel.ApiClient.DeviceId);
                    WebSocket.LibraryChanged += LibraryChanged;
                    WebSocket.BrowseCommand += BrowseRequest;
                    WebSocket.PlayCommand += PlayRequest;
                    WebSocket.PlaystateCommand += PlayStateRequest;
                    WebSocket.SystemCommand += SystemCommand;
                  
                    Updater = new Updater(this);
                    if (Kernel.CurrentUser.Dto.Configuration.IsAdministrator) // don't show these prompts to non-admins
                    {
                        // We check config here instead of in the Updater class because the Config class 
                        // CANNOT be instantiated outside of the application thread.
                        if (Config.EnableUpdates && !RunningOnExtender)
                        {
                            Async.Queue(Async.STARTUP_QUEUE, CheckForSystemUpdate, 10000);
                        }

                        // Let the user know if the server needs to be restarted
                        // Put it on the same thread as the update checks so it will be behind them
                        Async.Queue(Async.STARTUP_QUEUE, () =>
                                                             {
                                                                 if (Kernel.ServerInfo.HasPendingRestart)
                                                                 {
                                                                     if (Kernel.ServerInfo.CanSelfRestart)
                                                                     {
                                                                         if (YesNoBox("The MB Server needs to re-start to apply an update.  Restart now?") == "Y")
                                                                         {
                                                                             Kernel.ApiClient.PerformPendingRestart();
                                                                             MessageBox("Your server is being re-started.  MB Classic will now exit so you can re load it.");
                                                                             Close();
                                                                         }
                                                                     }
                                                                     else
                                                                     {
                                                                        MessageBox("Your server needs to be re-started to apply an update.  You must re-start it from the server machine.");
                                                                     }
                                                                 }
                                                             },35000);
                        
                    }

                    Navigate(this.RootFolderModel);
                }
                catch (Exception ex)
                {
                    Microsoft.MediaCenter.Hosting.AddInHost.Current.MediaCenterEnvironment.Dialog(CurrentInstance.StringData("EntryPointErrorDial") + this.EntryPointPath + ". " + ex.ToString() + " " + ex.StackTrace.ToString(), CurrentInstance.StringData("EntryPointErrorCapDial"), DialogButtons.Ok, 30, true);
                    Close();
                }
            }
        }
Exemple #8
0
 private void removeWebSocket(ApiWebSocket ws)
 {
     _sockets.TryRemove(ws, out _);
     Console.WriteLine($"API socket {ws.Id}: disconnected");
     Console.WriteLine($"   live sockets: {_sockets.Keys.Select(s => s.Id).Order().JoinString(", ")}");
 }