From 12f1437eb741b3eb1d2aaab7cf32a817130be709 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 19 Jun 2021 21:50:50 -0500 Subject: add IntentionalDisconnectException --- .../Exceptions/IntentionalDisconnectException.cs | 72 ++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/slskd/Application/Exceptions/IntentionalDisconnectException.cs (limited to 'src') diff --git a/src/slskd/Application/Exceptions/IntentionalDisconnectException.cs b/src/slskd/Application/Exceptions/IntentionalDisconnectException.cs new file mode 100644 index 00000000..393bf67e --- /dev/null +++ b/src/slskd/Application/Exceptions/IntentionalDisconnectException.cs @@ -0,0 +1,72 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd +{ + using System; + using System.Diagnostics.CodeAnalysis; + using System.Runtime.Serialization; + + /// + /// Represents a normal application shutdown. + /// + [ExcludeFromCodeCoverage] + [Serializable] + public class IntentionalDisconnectException : SlskdException + { + /// + /// Initializes a new instance of the class. + /// + public IntentionalDisconnectException() + : base() + { + } + + /// + /// Initializes a new instance of the class with a specified error message. + /// + /// The message that describes the error. + public IntentionalDisconnectException(string message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class with a specified error message and a + /// reference to the inner exception that is the cause of this exception. + /// + /// The message that describes the error. + /// + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no + /// inner exception is specified. + /// + public IntentionalDisconnectException(string message, Exception innerException) + : base(message, innerException) + { + } + + /// + /// Initializes a new instance of the class with serialized data. + /// + /// The SerializationInfo that holds the serialized object data about the exception being thrown. + /// The StreamingContext that contains contextual information about the source or destination. + protected IntentionalDisconnectException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} \ No newline at end of file -- cgit v1.2.3 From c7a3328be74412d324fe3d4a3e43727683c63079 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 19 Jun 2021 21:51:11 -0500 Subject: add ManagementService --- .../Application/Management/IManagementService.cs | 27 ++++++++++++++ .../Application/Management/ManagementService.cs | 42 ++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/slskd/Application/Management/IManagementService.cs create mode 100644 src/slskd/Application/Management/ManagementService.cs (limited to 'src') diff --git a/src/slskd/Application/Management/IManagementService.cs b/src/slskd/Application/Management/IManagementService.cs new file mode 100644 index 00000000..3457c3e9 --- /dev/null +++ b/src/slskd/Application/Management/IManagementService.cs @@ -0,0 +1,27 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Management +{ + using System.Threading.Tasks; + + public interface IManagementService + { + void DisconnectClient(string message = null); + Task ConnectClientAsync(); + } +} diff --git a/src/slskd/Application/Management/ManagementService.cs b/src/slskd/Application/Management/ManagementService.cs new file mode 100644 index 00000000..725cef1d --- /dev/null +++ b/src/slskd/Application/Management/ManagementService.cs @@ -0,0 +1,42 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Management +{ + using System.Threading.Tasks; + using Soulseek; + + public class ManagementService : IManagementService + { + public ManagementService( + Microsoft.Extensions.Options.IOptionsMonitor optionsMonitor, + ISoulseekClient soulseekClient) + { + Options = optionsMonitor.CurrentValue; + Client = soulseekClient; + } + + private ISoulseekClient Client { get; } + private Options Options { get; } + + public void DisconnectClient(string message = null) + => Client.Disconnect(message, new IntentionalDisconnectException(message)); + + public Task ConnectClientAsync() + => Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password); + } +} -- cgit v1.2.3 From c38f0ecc3c8e98d178af7113841485dcbd2506d9 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 19 Jun 2021 21:57:04 -0500 Subject: use ManagementService, remove args for connect method --- .../Management/API/Controllers/ServerController.cs | 32 ++++++---------------- .../Management/API/DTO/ConnectRequest.cs | 27 ------------------ 2 files changed, 8 insertions(+), 51 deletions(-) delete mode 100644 src/slskd/Application/Management/API/DTO/ConnectRequest.cs (limited to 'src') diff --git a/src/slskd/Application/Management/API/Controllers/ServerController.cs b/src/slskd/Application/Management/API/Controllers/ServerController.cs index ed911a13..da4a0182 100644 --- a/src/slskd/Application/Management/API/Controllers/ServerController.cs +++ b/src/slskd/Application/Management/API/Controllers/ServerController.cs @@ -20,7 +20,6 @@ namespace slskd.Management.API using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; - using Soulseek; /// /// Server. @@ -32,12 +31,13 @@ namespace slskd.Management.API [Consumes("application/json")] public class ServerController : ControllerBase { - public ServerController(ISoulseekClient client) + public ServerController( + IManagementService managementService) { - Client = client; + Management = managementService; } - private ISoulseekClient Client { get; } + private IManagementService Management { get; } /// /// Connects the client. @@ -46,26 +46,10 @@ namespace slskd.Management.API /// [HttpPost] [Authorize] - public async Task Connect([FromBody] ConnectRequest req) + public async Task Connect() { - var addr = !string.IsNullOrEmpty(req.Address); - var port = req.Port.HasValue; - var un = !string.IsNullOrEmpty(req.Username); - var pw = !string.IsNullOrEmpty(req.Password); - - if (addr && port && un && pw) - { - await Client.ConnectAsync(req.Address, req.Port.Value, req.Username, req.Password); - return Ok(); - } - - if (!addr && !port && un && pw) - { - await Client.ConnectAsync(req.Username, req.Password); - return Ok(); - } - - return BadRequest("Provide one of the following: address and port, username and password, or address, port, username and password"); + await Management.ConnectClientAsync(); + return Ok(); } /// @@ -77,7 +61,7 @@ namespace slskd.Management.API [Authorize] public IActionResult Disconnect([FromBody] string message) { - Client.Disconnect(message); + Management.DisconnectClient(message); return NoContent(); } } diff --git a/src/slskd/Application/Management/API/DTO/ConnectRequest.cs b/src/slskd/Application/Management/API/DTO/ConnectRequest.cs deleted file mode 100644 index e53dece7..00000000 --- a/src/slskd/Application/Management/API/DTO/ConnectRequest.cs +++ /dev/null @@ -1,27 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -namespace slskd.Management.API -{ - public class ConnectRequest - { - public string Address { get; set; } - public int? Port { get; set; } - public string Username { get; set; } - public string Password { get; set; } - } -} -- cgit v1.2.3 From 781fe3cc1f8ea8d35151fcd86f43889d066ecfb6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 19 Jun 2021 21:57:32 -0500 Subject: dont reconnect after IntentionalDisconnectException --- src/slskd/Application/Service.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Application/Service.cs b/src/slskd/Application/Service.cs index 246e7fc6..43688a0e 100644 --- a/src/slskd/Application/Service.cs +++ b/src/slskd/Application/Service.cs @@ -216,6 +216,10 @@ namespace slskd { Logger.Debug("Disconnected from the Soulseek server: the client is shutting down"); } + else if (args.Exception is IntentionalDisconnectException) + { + Logger.Debug("Disconnected from the Soulseek server: disconnected by the user"); + } else if (args.Exception is LoginRejectedException) { Logger.Error("Disconnected from the Soulseek server: invalid username or password"); @@ -226,7 +230,7 @@ namespace slskd } else { - Logger.Error("Disconnected from the Soulseek server: {Message}", args.Exception.Message); + Logger.Error("Disconnected from the Soulseek server: {Message}", args.Exception?.Message ?? args.Message); var attempts = 1; -- cgit v1.2.3 From 5ff4acd7b73a73371b9bfb7100943654f517fab2 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 19 Jun 2021 21:57:39 -0500 Subject: wire up ManagementService --- src/slskd/Startup.cs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index 2f35e6fc..dd44fd87 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -35,6 +35,7 @@ namespace slskd using Serilog; using slskd.Authentication; using slskd.Cryptography; + using slskd.Management; using slskd.Messaging; using slskd.Peer; using slskd.Search; @@ -184,6 +185,7 @@ namespace slskd services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); services.AddHostedService(); services.AddSingleton(_ => Service.SoulseekClient); -- cgit v1.2.3 From bf7baa04970161e80fc12f0628555fd2022c73df Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 20 Jun 2021 13:23:03 -0500 Subject: docs --- src/slskd/Application/Exceptions/IntentionalDisconnectException.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Application/Exceptions/IntentionalDisconnectException.cs b/src/slskd/Application/Exceptions/IntentionalDisconnectException.cs index 393bf67e..d5293e63 100644 --- a/src/slskd/Application/Exceptions/IntentionalDisconnectException.cs +++ b/src/slskd/Application/Exceptions/IntentionalDisconnectException.cs @@ -22,7 +22,7 @@ namespace slskd using System.Runtime.Serialization; /// - /// Represents a normal application shutdown. + /// Represents an intentional client disconnect. /// [ExcludeFromCodeCoverage] [Serializable] -- cgit v1.2.3 From 52fc763bfc14aa71a300cec9a706d6d46eea110f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 20 Jun 2021 13:52:23 -0500 Subject: add ServerState --- .../Application/Management/Domain/ServerState.cs | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/slskd/Application/Management/Domain/ServerState.cs (limited to 'src') diff --git a/src/slskd/Application/Management/Domain/ServerState.cs b/src/slskd/Application/Management/Domain/ServerState.cs new file mode 100644 index 00000000..80c1e856 --- /dev/null +++ b/src/slskd/Application/Management/Domain/ServerState.cs @@ -0,0 +1,33 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Management +{ + using System.Net; + using Soulseek; + + public class ServerState + { + public string Address { get; set; } + public IPEndPoint IPEndPoint { get; set; } + public SoulseekClientStates State { get; set; } + public string Username { get; set; } + public bool IsConnected => State.HasFlag(SoulseekClientStates.Connected); + public bool IsLoggedIn => State.HasFlag(SoulseekClientStates.LoggedIn); + public bool IsTransitioning => State.HasFlag(SoulseekClientStates.Connecting) || State.HasFlag(SoulseekClientStates.Disconnecting) || State.HasFlag(SoulseekClientStates.LoggingIn); + } +} -- cgit v1.2.3 From c9926bb6d2d78f2ba019e04bcff8d682001009d3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 20 Jun 2021 13:55:09 -0500 Subject: add GetServerState --- src/slskd/Application/Management/IManagementService.cs | 5 +++-- src/slskd/Application/Management/ManagementService.cs | 13 +++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/slskd/Application/Management/IManagementService.cs b/src/slskd/Application/Management/IManagementService.cs index 3457c3e9..8e890b1e 100644 --- a/src/slskd/Application/Management/IManagementService.cs +++ b/src/slskd/Application/Management/IManagementService.cs @@ -21,7 +21,8 @@ namespace slskd.Management public interface IManagementService { - void DisconnectClient(string message = null); - Task ConnectClientAsync(); + void DisconnectServer(string message = null); + Task ConnectServerAsync(); + ServerState GetServerState(); } } diff --git a/src/slskd/Application/Management/ManagementService.cs b/src/slskd/Application/Management/ManagementService.cs index 725cef1d..2c15b441 100644 --- a/src/slskd/Application/Management/ManagementService.cs +++ b/src/slskd/Application/Management/ManagementService.cs @@ -33,10 +33,19 @@ namespace slskd.Management private ISoulseekClient Client { get; } private Options Options { get; } - public void DisconnectClient(string message = null) + public void DisconnectServer(string message = null) => Client.Disconnect(message, new IntentionalDisconnectException(message)); - public Task ConnectClientAsync() + public Task ConnectServerAsync() => Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password); + + public ServerState GetServerState() => + new ServerState() + { + Address = Client.Address, + IPEndPoint = Client.IPEndPoint, + State = Client.State, + Username = Client.Username, + }; } } -- cgit v1.2.3 From f11272fb90650215eb0cc672dc1e79834e71ecf0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 20 Jun 2021 13:55:19 -0500 Subject: add GET for server state --- .../Management/API/Controllers/ServerController.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Application/Management/API/Controllers/ServerController.cs b/src/slskd/Application/Management/API/Controllers/ServerController.cs index da4a0182..5826d36a 100644 --- a/src/slskd/Application/Management/API/Controllers/ServerController.cs +++ b/src/slskd/Application/Management/API/Controllers/ServerController.cs @@ -48,7 +48,7 @@ namespace slskd.Management.API [Authorize] public async Task Connect() { - await Management.ConnectClientAsync(); + await Management.ConnectServerAsync(); return Ok(); } @@ -61,8 +61,15 @@ namespace slskd.Management.API [Authorize] public IActionResult Disconnect([FromBody] string message) { - Management.DisconnectClient(message); + Management.DisconnectServer(message); return NoContent(); } + + [HttpGet] + [Authorize] + public IActionResult Get() + { + return Ok(Management.GetServerState()); + } } } \ No newline at end of file -- cgit v1.2.3 From a6f5eecd8d5fa4158acaabf351c451dc6182a4dc Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 21 Jun 2021 21:36:36 -0500 Subject: rough in connection nav --- src/web/src/components/App.css | 11 +++++++++++ src/web/src/components/App.js | 12 +++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/web/src/components/App.css b/src/web/src/components/App.css index 99d3767a..4802ca5b 100644 --- a/src/web/src/components/App.css +++ b/src/web/src/components/App.css @@ -122,4 +122,15 @@ body { .placeholder-segment { opacity: .25; +} + +.menu-icon-group { + font-size: 1.71428571em !important; + margin: 0 auto .5rem !important; + text-shadow: unset !important; +} + +.menu-icon-no-shadow { + font-size: 1rem !important; + text-shadow: unset !important; } \ No newline at end of file diff --git a/src/web/src/components/App.js b/src/web/src/components/App.js index 26ff028d..3eceb12a 100644 --- a/src/web/src/components/App.js +++ b/src/web/src/components/App.js @@ -17,7 +17,8 @@ import { Menu, Icon, Modal, - Header + Header, + Dropdown } from 'semantic-ui-react'; import Rooms from './Rooms/Rooms'; @@ -143,6 +144,15 @@ class App extends Component { Browse + + Connected + + + + + + Disconnected + {token !== tokenPassthroughValue && -- cgit v1.2.3 From ceafe85157f2ca2e81c3a5864fcd7f843177b0cb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 22 Jun 2021 08:01:46 -0500 Subject: hide menu dividers --- src/web/src/components/App.css | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/web/src/components/App.css b/src/web/src/components/App.css index 4802ca5b..8fbb639d 100644 --- a/src/web/src/components/App.css +++ b/src/web/src/components/App.css @@ -7,6 +7,10 @@ body { padding-bottom: 5px !important; } +.ui.inverted.menu .item:before { + background: rgba(255,255,255, 0); +} + .app { margin: auto; border: 0px !important; -- cgit v1.2.3 From b0aac180d58cc10fe39869e89bbbe944cb326e3f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 22 Jun 2021 08:02:36 -0500 Subject: remove unused import, fix alignment of rightmost options --- src/web/src/components/App.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/web/src/components/App.js b/src/web/src/components/App.js index 3eceb12a..c28c9348 100644 --- a/src/web/src/components/App.js +++ b/src/web/src/components/App.js @@ -17,8 +17,7 @@ import { Menu, Icon, Modal, - Header, - Dropdown + Header } from 'semantic-ui-react'; import Rooms from './Rooms/Rooms'; @@ -155,7 +154,7 @@ class App extends Component { {token !== tokenPassthroughValue && + Log Out } -- cgit v1.2.3 From ad6e6cc996c1cf2d42d063443b4dc4efa6048e2f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 22 Jun 2021 09:43:37 -0500 Subject: don't connect or disconnect if the client is already in that state --- .../Management/API/Controllers/ServerController.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Application/Management/API/Controllers/ServerController.cs b/src/slskd/Application/Management/API/Controllers/ServerController.cs index 5826d36a..0fc18e9e 100644 --- a/src/slskd/Application/Management/API/Controllers/ServerController.cs +++ b/src/slskd/Application/Management/API/Controllers/ServerController.cs @@ -44,11 +44,15 @@ namespace slskd.Management.API /// /// /// - [HttpPost] + [HttpPut] [Authorize] public async Task Connect() { - await Management.ConnectServerAsync(); + if (!Management.GetServerState().IsConnected) + { + await Management.ConnectServerAsync(); + } + return Ok(); } @@ -61,10 +65,18 @@ namespace slskd.Management.API [Authorize] public IActionResult Disconnect([FromBody] string message) { - Management.DisconnectServer(message); + if (Management.GetServerState().IsConnected) + { + Management.DisconnectServer(message); + } + return NoContent(); } + /// + /// Retrieves the current state of the server. + /// + /// [HttpGet] [Authorize] public IActionResult Get() -- cgit v1.2.3 From d170ab0f1ba323b55001c0bfa5a0eef23c30551f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 22 Jun 2021 09:58:10 -0500 Subject: docs --- .../Management/API/Controllers/ServerController.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Application/Management/API/Controllers/ServerController.cs b/src/slskd/Application/Management/API/Controllers/ServerController.cs index 0fc18e9e..6468063c 100644 --- a/src/slskd/Application/Management/API/Controllers/ServerController.cs +++ b/src/slskd/Application/Management/API/Controllers/ServerController.cs @@ -42,10 +42,12 @@ namespace slskd.Management.API /// /// Connects the client. /// - /// /// [HttpPut] + [Route("")] [Authorize] + [ProducesResponseType(200)] + [ProducesResponseType(403)] public async Task Connect() { if (!Management.GetServerState().IsConnected) @@ -62,7 +64,10 @@ namespace slskd.Management.API /// /// [HttpDelete] + [Route("")] [Authorize] + [ProducesResponseType(204)] + [ProducesResponseType(403)] public IActionResult Disconnect([FromBody] string message) { if (Management.GetServerState().IsConnected) @@ -77,8 +82,12 @@ namespace slskd.Management.API /// Retrieves the current state of the server. /// /// + /// [HttpGet] + [Route("")] [Authorize] + [ProducesResponseType(typeof(ServerState), 200)] + [ProducesResponseType(403)] public IActionResult Get() { return Ok(Management.GetServerState()); -- cgit v1.2.3 From 1032aada7b104cfeafd59566a164ece2d137627b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 22 Jun 2021 09:58:19 -0500 Subject: add server lib --- src/web/src/lib/server.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/web/src/lib/server.js (limited to 'src') diff --git a/src/web/src/lib/server.js b/src/web/src/lib/server.js new file mode 100644 index 00000000..13881df7 --- /dev/null +++ b/src/web/src/lib/server.js @@ -0,0 +1,13 @@ +import api from './api'; + +export const getState = async () => { + return (await api.get('/server')).data; +}; + +export const connect = () => { + return api.put('/server'); +}; + +export const disconnect = ({ message = 'client disconnected from web UI' } = {}) => { + return api.delete('/server', { data: JSON.stringify(message) }); +}; \ No newline at end of file -- cgit v1.2.3 From 0e3bcb456b37e71895ae929190e0723438318685 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 22 Jun 2021 09:58:52 -0500 Subject: add server status and controls --- src/web/src/components/App.js | 59 +++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/web/src/components/App.js b/src/web/src/components/App.js index c28c9348..d3c0156c 100644 --- a/src/web/src/components/App.js +++ b/src/web/src/components/App.js @@ -2,6 +2,7 @@ import React, { Component } from 'react'; import { Route, Link, Switch } from "react-router-dom"; import { tokenKey, tokenPassthroughValue } from '../config'; import * as session from '../lib/session'; +import * as server from '../lib/server'; import './App.css'; import Search from './Search/Search'; @@ -27,7 +28,9 @@ const initialState = { initialized: false, pending: false, error: undefined - } + }, + serverStateInterval: undefined, + serverState: {} }; class App extends Component { @@ -35,6 +38,8 @@ class App extends Component { componentDidMount = async () => { const { login } = this.state; + + await this.fetchServerState(); const securityEnabled = await session.getSecurityEnabled(); if (!securityEnabled) { @@ -46,11 +51,17 @@ class App extends Component { login: { ...login, initialized: true - } + }, + serverStateInterval: window.setInterval(this.fetchServerState, 5000) }); await this.checkToken(); - } + }; + + componentWillUnmount = () => { + clearInterval(this.state.serverStateInterval); + this.setState({ serverStateInterval: undefined }); + }; checkToken = async () => { try { @@ -58,7 +69,7 @@ class App extends Component { } catch (error) { this.logout(); } - } + }; getToken = () => JSON.parse(sessionStorage.getItem(tokenKey) || localStorage.getItem(tokenKey)); setToken = (storage, token) => storage.setItem(tokenKey, JSON.stringify(token)); @@ -73,21 +84,37 @@ class App extends Component { this.setState({ login: { ...this.state.login, pending: false, error }}); } }); - } + }; logout = () => { localStorage.removeItem(tokenKey); sessionStorage.removeItem(tokenKey); this.setState({ ...initialState, login: { ...initialState.login, initialized: true }}); - } + }; withTokenCheck = (component) => { this.checkToken(); // async, runs in the background return { ...component }; + }; + + connect = async () => { + await server.connect(); + this.fetchServerState(); + }; + + disconnect = async () => { + await server.disconnect(); + this.fetchServerState(); } + fetchServerState = async () => { + this.setState({ + serverState: await server.getState() + }); + }; + render = () => { - const { token, login } = this.state; + const { token, login, serverState } = this.state; return ( <> @@ -143,15 +170,21 @@ class App extends Component { Browse - + {serverState.isConnected && this.disconnect()} + > Connected - - + } + {(!serverState.isConnected || serverState.isTransitioning) && this.connect()} + > Disconnected - + } {token !== tokenPassthroughValue && @@ -180,7 +213,7 @@ class App extends Component { } ) - } -} + }; +}; export default App; -- cgit v1.2.3 From 5cccb58849a2cb36cf41c26602df36a425e7c544 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 23 Jun 2021 08:34:06 -0500 Subject: docs for ManagementService --- .../Application/Management/IManagementService.cs | 21 ++++++++++++++++- .../Application/Management/ManagementService.cs | 26 ++++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Application/Management/IManagementService.cs b/src/slskd/Application/Management/IManagementService.cs index 8e890b1e..4ee2185e 100644 --- a/src/slskd/Application/Management/IManagementService.cs +++ b/src/slskd/Application/Management/IManagementService.cs @@ -17,12 +17,31 @@ namespace slskd.Management { + using System; using System.Threading.Tasks; + /// + /// Application and Soulseek client management. + /// public interface IManagementService { - void DisconnectServer(string message = null); + /// + /// Disconnects the Soulseek client from the server. + /// + /// An optional message containing the reason for the disconnect. + /// An optional Exception to associate with the disconnect. + void DisconnectServer(string message = null, Exception exception = null); + + /// + /// Connects the Soulseek client to the server using the configured username and password. + /// + /// The operation context. Task ConnectServerAsync(); + + /// + /// Gets the current state of the connection to the Soulseek server. + /// + /// The current server state. ServerState GetServerState(); } } diff --git a/src/slskd/Application/Management/ManagementService.cs b/src/slskd/Application/Management/ManagementService.cs index 2c15b441..84935879 100644 --- a/src/slskd/Application/Management/ManagementService.cs +++ b/src/slskd/Application/Management/ManagementService.cs @@ -17,11 +17,20 @@ namespace slskd.Management { + using System; using System.Threading.Tasks; using Soulseek; + /// + /// Application and Soulseek client management. + /// public class ManagementService : IManagementService { + /// + /// Initializes a new instance of the class. + /// + /// The options monitor used to derive application options. + /// The Soulseek client. public ManagementService( Microsoft.Extensions.Options.IOptionsMonitor optionsMonitor, ISoulseekClient soulseekClient) @@ -33,12 +42,25 @@ namespace slskd.Management private ISoulseekClient Client { get; } private Options Options { get; } - public void DisconnectServer(string message = null) - => Client.Disconnect(message, new IntentionalDisconnectException(message)); + /// + /// Disconnects the Soulseek client from the server. + /// + /// An optional message containing the reason for the disconnect. + /// An optional Exception to associate with the disconnect. + public void DisconnectServer(string message = null, Exception exception = null) + => Client.Disconnect(message, exception ?? new IntentionalDisconnectException(message)); + /// + /// Connects the Soulseek client to the server using the configured username and password. + /// + /// The operation context. public Task ConnectServerAsync() => Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password); + /// + /// Gets the current state of the connection to the Soulseek server. + /// + /// The current server state. public ServerState GetServerState() => new ServerState() { -- cgit v1.2.3 From 7383d448e7481ca5dc046b8118e5709ab571b69f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 24 Jun 2021 08:41:12 -0500 Subject: log connection events --- src/slskd/Application/Service.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Application/Service.cs b/src/slskd/Application/Service.cs index 43688a0e..60fae0cc 100644 --- a/src/slskd/Application/Service.cs +++ b/src/slskd/Application/Service.cs @@ -121,6 +121,7 @@ namespace slskd Client.RoomLeft += Client_RoomLeft; Client.Disconnected += Client_Disconnected; Client.Connected += Client_Connected; + Client.LoggedIn += Client_LoggedIn; SoulseekClient = Client; @@ -207,18 +208,23 @@ namespace slskd private void Client_Connected(object sender, EventArgs e) { - Logger.Information("Connected and logged in as {Username}", Options.Soulseek.Username); + Logger.Information("Connected to the Soulseek server"); + } + + private void Client_LoggedIn(object sender, EventArgs e) + { + Logger.Information("Logged in to the Soulseek server as {Username}", Options.Soulseek.Username); } private async void Client_Disconnected(object sender, SoulseekClientDisconnectedEventArgs args) { if (args.Exception is ObjectDisposedException || args.Exception is ApplicationShutdownException) { - Logger.Debug("Disconnected from the Soulseek server: the client is shutting down"); + Logger.Information("Disconnected from the Soulseek server: the client is shutting down"); } else if (args.Exception is IntentionalDisconnectException) { - Logger.Debug("Disconnected from the Soulseek server: disconnected by the user"); + Logger.Information("Disconnected from the Soulseek server: disconnected by the user"); } else if (args.Exception is LoginRejectedException) { -- cgit v1.2.3