From 1c3a6246738a8fa1a2bce401ab29d8d0d2f23231 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 10 Nov 2022 18:00:21 -0600 Subject: split navigation into mode-specific versions --- src/web/src/components/App.js | 225 +++++++++++++++++++++++++----------------- 1 file changed, 137 insertions(+), 88 deletions(-) (limited to 'src') diff --git a/src/web/src/components/App.js b/src/web/src/components/App.js index ea24924a..c55ab830 100644 --- a/src/web/src/components/App.js +++ b/src/web/src/components/App.js @@ -116,10 +116,20 @@ class App extends Component { render = () => { const { login, applicationState = {}, applicationOptions = {}, error, initialized, retriesExhausted } = this.state; - const { version = {}, server, pendingReconnect, pendingRestart, shares = {}, user } = applicationState; + const { + version = {}, + network = {}, + server, + pendingReconnect, + pendingRestart, + shares = {}, + user, + } = applicationState; const { isUpdateAvailable, current, latest } = version; const { scanPending: pendingShareRescan } = shares; + const { mode } = network; + if (!initialized) { return ; } @@ -148,6 +158,129 @@ class App extends Component { ); } + const ModeSpecificNavigation = ({ mode }) => { + if (mode === 'Agent') { + return + + Agent + + ; + } else { + return <> + + + Search + + + + + Downloads + + + + + Uploads + + + + + Rooms + + + + + Chat + + + + + Users + + + + + Browse + + + ; + } + }; + + const ModeSpecificConnectButton = ({ mode, server }) => { + if (mode === 'Agent') { + return <>; + } else { + return <> + {server?.isConnected && disconnect()} + > + + + {user?.privileges?.isPrivileged && + } + Connected + } + {(!server?.isConnected) && connect()} + > + + + + Disconnected + } + {(pendingReconnect || pendingRestart || pendingShareRescan) && + + + + + Pending Action + }; + } + }; + + const ModeSpecificRoutes = ({ mode }) => { + if (mode === 'Agent') { + return <> + this.withTokenCheck( + + ) + }/> + + ; + } else { + return <> + + this.withTokenCheck(
+ +
)} + /> + this.withTokenCheck()}/> + this.withTokenCheck()}/> + this.withTokenCheck()}/> + this.withTokenCheck()}/> + + this.withTokenCheck(
) + }/> + + this.withTokenCheck(
+ +
) + }/> + this.withTokenCheck( + + ) + }/> + ; + } + }; + return ( <> @@ -164,66 +297,9 @@ class App extends Component { {version.isCanary && Canary } - - - Search - - - - - Downloads - - - - - Uploads - - - - - Rooms - - - - - Chat - - - - - Users - - - - - Browse - - + - {server?.isConnected && disconnect()} - > - - - {user?.privileges?.isPrivileged && - } - Connected - } - {(!server?.isConnected) && connect()} - > - - - - Disconnected - } - {(pendingReconnect || pendingRestart || pendingShareRescan) && - - - - - Pending Action - } + {isUpdateAvailable && @@ -271,34 +347,7 @@ class App extends Component { - - this.withTokenCheck(
- -
)} - /> - this.withTokenCheck()}/> - this.withTokenCheck()}/> - this.withTokenCheck()}/> - this.withTokenCheck()}/> - - this.withTokenCheck(
) - }/> - - this.withTokenCheck(
- -
) - }/> - this.withTokenCheck( - - ) - }/> - +
-- cgit v1.2.3 From cb268e8144796b2f94dd444525e0685c1ac00109 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 11 Nov 2022 09:42:28 -0600 Subject: allow the app to start if the controller can't be connected --- src/slskd/Application.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index f732594d..5d614d28 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -401,7 +401,7 @@ namespace slskd else if (OptionsAtStartup.Network.Mode.ToEnum() == OperationMode.Agent) { Log.Information("Running in Agent mode; not connecting to the Soulseek server."); - await Network.Client.StartAsync(cancellationToken); + _ = Network.Client.StartAsync(cancellationToken); } else { -- cgit v1.2.3 From 526d389da45dd86f01922615ce9145d7e9e0b152 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 11 Nov 2022 17:39:15 -0600 Subject: add endpoints for connect and disconnect --- src/slskd/Network/API/Controllers/NetworkController.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src') diff --git a/src/slskd/Network/API/Controllers/NetworkController.cs b/src/slskd/Network/API/Controllers/NetworkController.cs index eb0c01e3..b0e15663 100644 --- a/src/slskd/Network/API/Controllers/NetworkController.cs +++ b/src/slskd/Network/API/Controllers/NetworkController.cs @@ -47,6 +47,22 @@ namespace slskd.Network private ILogger Log { get; } = Serilog.Log.ForContext(); private INetworkService Network { get; } + [HttpPut("")] + [Authorize(Policy = AuthPolicy.Any)] + public async Task Connect() + { + await Network.Client.StartAsync(); + return Ok(); + } + + [HttpDelete("")] + [Authorize(Policy = AuthPolicy.Any)] + public async Task Disconnect() + { + await Network.Client.StopAsync(); + return NoContent(); + } + /// /// Uploads a file. /// -- cgit v1.2.3 From ee32613a71707dd49f14e60c0bf0a2f356bc15f4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 11 Nov 2022 17:39:22 -0600 Subject: add network lib --- src/web/src/lib/network.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/web/src/lib/network.js (limited to 'src') diff --git a/src/web/src/lib/network.js b/src/web/src/lib/network.js new file mode 100644 index 00000000..07ed89e3 --- /dev/null +++ b/src/web/src/lib/network.js @@ -0,0 +1,9 @@ +import api from './api'; + +export const connect = () => { + return api.put('/network'); +}; + +export const disconnect = () => { + return api.delete('/network'); +}; \ No newline at end of file -- cgit v1.2.3 From 7bc7e55208118ea622d72950648d09ccc6c816ee Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 11 Nov 2022 17:40:02 -0600 Subject: add agent navigation ui --- src/web/src/components/App.js | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/web/src/components/App.js b/src/web/src/components/App.js index c55ab830..0439943b 100644 --- a/src/web/src/components/App.js +++ b/src/web/src/components/App.js @@ -5,6 +5,7 @@ import { ToastContainer } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import * as session from '../lib/session'; +import * as networkAPI from '../lib/network'; import { connect, disconnect } from '../lib/server'; import { urlBase } from '../config'; @@ -128,7 +129,7 @@ class App extends Component { const { isUpdateAvailable, current, latest } = version; const { scanPending: pendingShareRescan } = shares; - const { mode } = network; + const { mode, controller } = network; if (!initialized) { return ; @@ -160,11 +161,9 @@ class App extends Component { const ModeSpecificNavigation = ({ mode }) => { if (mode === 'Agent') { - return - - Agent - - ; + return + Agent Mode + ; } else { return <> @@ -206,9 +205,21 @@ class App extends Component { } }; - const ModeSpecificConnectButton = ({ mode, server }) => { + const ModeSpecificConnectButton = ({ mode, server, controller = {} }) => { if (mode === 'Agent') { - return <>; + const isConnected = controller?.state === 'Connected'; + const isTransitioning = ['Connecting', 'Reconnecting'].includes(controller?.state); + + return <> + isConnected ? networkAPI.connect() : networkAPI.disconnect()} + > + + + {!isConnected && } + Controller {controller?.state} + + ; } else { return <> {server?.isConnected && } - + {isUpdateAvailable && -- cgit v1.2.3 From 74a1d79dd559891aae4d8a5a9d472dc9bfc49269 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 15 Nov 2022 10:16:06 -0600 Subject: fix Network references --- src/slskd/Relay/API/Controllers/RelayController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Relay/API/Controllers/RelayController.cs b/src/slskd/Relay/API/Controllers/RelayController.cs index 3440c8ea..952507ad 100644 --- a/src/slskd/Relay/API/Controllers/RelayController.cs +++ b/src/slskd/Relay/API/Controllers/RelayController.cs @@ -51,7 +51,7 @@ namespace slskd.Relay [Authorize(Policy = AuthPolicy.Any)] public async Task Connect() { - await Network.Client.StartAsync(); + await Relay.Client.StartAsync(); return Ok(); } @@ -59,7 +59,7 @@ namespace slskd.Relay [Authorize(Policy = AuthPolicy.Any)] public async Task Disconnect() { - await Network.Client.StopAsync(); + await Relay.Client.StopAsync(); return NoContent(); } -- cgit v1.2.3 From 46c76a768f515214dfaf3b58b79bc0ca48aa8a71 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 15 Nov 2022 10:18:47 -0600 Subject: update network references --- src/web/src/components/App.js | 8 ++++---- src/web/src/lib/network.js | 9 --------- src/web/src/lib/relay.js | 9 +++++++++ 3 files changed, 13 insertions(+), 13 deletions(-) delete mode 100644 src/web/src/lib/network.js create mode 100644 src/web/src/lib/relay.js (limited to 'src') diff --git a/src/web/src/components/App.js b/src/web/src/components/App.js index 0439943b..8e31741c 100644 --- a/src/web/src/components/App.js +++ b/src/web/src/components/App.js @@ -5,7 +5,7 @@ import { ToastContainer } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import * as session from '../lib/session'; -import * as networkAPI from '../lib/network'; +import * as relay from '../lib/relay'; import { connect, disconnect } from '../lib/server'; import { urlBase } from '../config'; @@ -119,7 +119,7 @@ class App extends Component { const { login, applicationState = {}, applicationOptions = {}, error, initialized, retriesExhausted } = this.state; const { version = {}, - network = {}, + relay = {}, server, pendingReconnect, pendingRestart, @@ -129,7 +129,7 @@ class App extends Component { const { isUpdateAvailable, current, latest } = version; const { scanPending: pendingShareRescan } = shares; - const { mode, controller } = network; + const { mode, controller } = relay; if (!initialized) { return ; @@ -212,7 +212,7 @@ class App extends Component { return <> isConnected ? networkAPI.connect() : networkAPI.disconnect()} + onClick={() => isConnected ? relay.connect() : relay.disconnect()} > diff --git a/src/web/src/lib/network.js b/src/web/src/lib/network.js deleted file mode 100644 index 07ed89e3..00000000 --- a/src/web/src/lib/network.js +++ /dev/null @@ -1,9 +0,0 @@ -import api from './api'; - -export const connect = () => { - return api.put('/network'); -}; - -export const disconnect = () => { - return api.delete('/network'); -}; \ No newline at end of file diff --git a/src/web/src/lib/relay.js b/src/web/src/lib/relay.js new file mode 100644 index 00000000..b20d0af2 --- /dev/null +++ b/src/web/src/lib/relay.js @@ -0,0 +1,9 @@ +import api from './api'; + +export const connect = () => { + return api.put('/relay'); +}; + +export const disconnect = () => { + return api.delete('/relay'); +}; \ No newline at end of file -- cgit v1.2.3 From 17d635f9471747de03f2cf98d2a3bd9474182dbc Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 15 Nov 2022 10:26:16 -0600 Subject: add default route back --- src/web/src/components/App.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/web/src/components/App.js b/src/web/src/components/App.js index 8e31741c..9380f19d 100644 --- a/src/web/src/components/App.js +++ b/src/web/src/components/App.js @@ -5,7 +5,7 @@ import { ToastContainer } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import * as session from '../lib/session'; -import * as relay from '../lib/relay'; +import * as relayAPI from '../lib/relay'; import { connect, disconnect } from '../lib/server'; import { urlBase } from '../config'; @@ -212,7 +212,7 @@ class App extends Component { return <> isConnected ? relay.connect() : relay.disconnect()} + onClick={() => isConnected ? relayAPI.connect() : relayAPI.disconnect()} > @@ -288,6 +288,7 @@ class App extends Component { ) }/> + ; } }; -- cgit v1.2.3 From c0e5c4b3646a2294c50adddaa11ba8379f0d7a1b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 15 Nov 2022 10:52:59 -0600 Subject: update state after disconnect --- src/slskd/Relay/RelayClient.cs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/slskd/Relay/RelayClient.cs b/src/slskd/Relay/RelayClient.cs index 854399dd..193f8cbb 100644 --- a/src/slskd/Relay/RelayClient.cs +++ b/src/slskd/Relay/RelayClient.cs @@ -181,6 +181,8 @@ namespace slskd.Relay LoggedIn = false; + State.SetValue(_ => TranslateState(HubConnectionState.Disconnected)); + Log.Information("Relay controller connection disconnected"); } } -- cgit v1.2.3 From b2b8042a5e722725ae077bf3312778977342bcdb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 16 Nov 2022 08:37:06 -0600 Subject: synchronize start --- src/slskd/Relay/RelayClient.cs | 92 ++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/slskd/Relay/RelayClient.cs b/src/slskd/Relay/RelayClient.cs index 193f8cbb..df4a3e89 100644 --- a/src/slskd/Relay/RelayClient.cs +++ b/src/slskd/Relay/RelayClient.cs @@ -95,7 +95,8 @@ namespace slskd.Relay private ManagedState State { get; } = new(); private IShareService Shares { get; } - private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); + private SemaphoreSlim ConfigurationSyncRoot { get; } = new SemaphoreSlim(1, 1); + private SemaphoreSlim StateSyncRoot { get; } = new SemaphoreSlim(1, 1); private string LastOptionsHash { get; set; } private IOptionsMonitor OptionsMonitor { get; } private IHttpClientFactory HttpClientFactory { get; } @@ -114,56 +115,69 @@ namespace slskd.Relay /// public async Task StartAsync(CancellationToken cancellationToken = default) { - var mode = OptionsMonitor.CurrentValue.Relay.Mode.ToEnum(); - - if (mode != OperationMode.Agent && mode != OperationMode.Debug) + if (!StateSyncRoot.Wait(0, cancellationToken)) { - throw new InvalidOperationException($"Relay client can only be started when operation mode is {OperationMode.Agent}"); + // we're already attempting to connect, let the existing attempt handle it + return; } - StartCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); - LoggedInTaskCompletionSource = new TaskCompletionSource(); - StartRequested = true; + try + { + var mode = OptionsMonitor.CurrentValue.Relay.Mode.ToEnum(); - Log.Information("Attempting to connect to the relay controller {Address}", OptionsMonitor.CurrentValue.Relay.Controller.Address); + if (mode != OperationMode.Agent && mode != OperationMode.Debug) + { + throw new InvalidOperationException($"Relay client can only be started when operation mode is {OperationMode.Agent}"); + } - State.SetValue(_ => TranslateState(HubConnectionState.Connecting)); + StartCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + LoggedInTaskCompletionSource = new TaskCompletionSource(); + StartRequested = true; - // retry indefinitely - await Retry.Do( - task: () => HubConnection.StartAsync(StartCancellationTokenSource.Token), - isRetryable: (attempts, ex) => true, - onFailure: (attempts, ex) => - { - Log.Debug(ex, "Relay hub connection failure"); - Log.Warning("Failed attempt #{Attempts} to connect to relay controller: {Message}", attempts, ex.Message); - }, - maxAttempts: int.MaxValue, - maxDelayInMilliseconds: 30000, - StartCancellationTokenSource.Token); + Log.Information("Attempting to connect to the relay controller {Address}", OptionsMonitor.CurrentValue.Relay.Controller.Address); - State.SetValue(_ => TranslateState(HubConnection.State)); - Log.Information("Relay controller connection established. Awaiting authentication..."); + State.SetValue(_ => TranslateState(HubConnectionState.Connecting)); - await LoggedInTaskCompletionSource.Task; + // retry indefinitely + await Retry.Do( + task: () => HubConnection.StartAsync(StartCancellationTokenSource.Token), + isRetryable: (attempts, ex) => true, + onFailure: (attempts, ex) => + { + Log.Debug(ex, "Relay hub connection failure"); + Log.Warning("Failed attempt #{Attempts} to connect to relay controller: {Message}", attempts, ex.Message); + }, + maxAttempts: int.MaxValue, + maxDelayInMilliseconds: 30000, + StartCancellationTokenSource.Token); - LoggedIn = true; + State.SetValue(_ => TranslateState(HubConnection.State)); + Log.Information("Relay controller connection established. Awaiting authentication..."); - Log.Information("Authenticated. Uploading shares..."); + await LoggedInTaskCompletionSource.Task; - try - { - await UploadSharesAsync(); + LoggedIn = true; + + Log.Information("Authenticated. Uploading shares..."); + + try + { + await UploadSharesAsync(); + } + catch (Exception ex) + { + Log.Error(ex, ex.Message); + Log.Error("Disconnecting from the relay controller"); + await StopAsync(); + throw; + } + + Log.Information("Shares uploaded. Ready to upload files."); } - catch (Exception ex) + finally { - Log.Error(ex, ex.Message); - Log.Error("Disconnecting from the relay controller"); - await StopAsync(); - throw; + StateSyncRoot.Release(); } - - Log.Information("Shares uploaded. Ready to upload files."); } /// @@ -425,7 +439,7 @@ namespace slskd.Relay return; } - SyncRoot.Wait(); + ConfigurationSyncRoot.Wait(); try { @@ -482,7 +496,7 @@ namespace slskd.Relay } finally { - SyncRoot.Release(); + ConfigurationSyncRoot.Release(); } } -- cgit v1.2.3 From 01b5eb5a02cc54d316cbdcba65f9ba5ae94526da Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 16 Nov 2022 09:18:27 -0600 Subject: fix rerender issue --- src/web/src/components/App.js | 200 ++++++++++++++++++++---------------------- 1 file changed, 97 insertions(+), 103 deletions(-) (limited to 'src') diff --git a/src/web/src/components/App.js b/src/web/src/components/App.js index 9380f19d..66fa3945 100644 --- a/src/web/src/components/App.js +++ b/src/web/src/components/App.js @@ -158,64 +158,23 @@ class App extends Component { /> ); } - - const ModeSpecificNavigation = ({ mode }) => { - if (mode === 'Agent') { - return - Agent Mode - ; - } else { - return <> - - - Search - - - - - Downloads - - - - - Uploads - - - - - Rooms - - - - - Chat - - - - - Users - - - - - Browse - - - ; - } - }; - + const ModeSpecificConnectButton = ({ mode, server, controller = {} }) => { - if (mode === 'Agent') { + if (mode !== 'Agent') { const isConnected = controller?.state === 'Connected'; const isTransitioning = ['Connecting', 'Reconnecting'].includes(controller?.state); return <> isConnected ? relayAPI.connect() : relayAPI.disconnect()} + onClick={() => isConnected ? relayAPI.disconnect() : relayAPI.connect()} > - + {!isConnected && } Controller {controller?.state} @@ -239,60 +198,12 @@ class App extends Component { Disconnected } - {(pendingReconnect || pendingRestart || pendingShareRescan) && - - - - - Pending Action - }; - } - }; - - const ModeSpecificRoutes = ({ mode }) => { - if (mode === 'Agent') { - return <> - this.withTokenCheck( - - ) - }/> - - ; - } else { - return <> - - this.withTokenCheck(
- -
)} - /> - this.withTokenCheck()}/> - this.withTokenCheck()}/> - this.withTokenCheck()}/> - this.withTokenCheck()}/> - - this.withTokenCheck(
) - }/> - - this.withTokenCheck(
- -
) - }/> - this.withTokenCheck( - - ) - }/> - ; } }; + const isAgent = mode !== 'Agent'; + return ( <> @@ -309,9 +220,55 @@ class App extends Component { {version.isCanary && Canary } - + {isAgent ? + Agent Mode + : + <> + + + Search + + + + + Downloads + + + + + Uploads + + + + + Rooms + + + + + Chat + + + + + Users + + + + + Browse + + + } - + + {(pendingReconnect || pendingRestart || pendingShareRescan) && + + + + + Pending Action + } {isUpdateAvailable && @@ -359,7 +316,44 @@ class App extends Component { - + {isAgent ? <> + this.withTokenCheck( + + ) + }/> + + : + <> + + this.withTokenCheck(
+ +
)} + /> + this.withTokenCheck()}/> + this.withTokenCheck()}/> + this.withTokenCheck()}/> + this.withTokenCheck()}/> + + this.withTokenCheck(
) + }/> + + this.withTokenCheck(
+ +
) + }/> + this.withTokenCheck( + + ) + }/> + + }
-- cgit v1.2.3 From a382bac1eb8bc1c8abeaef545b06c0d7ea48c358 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 16 Nov 2022 09:29:38 -0600 Subject: remove debugging --- src/web/src/components/App.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/web/src/components/App.js b/src/web/src/components/App.js index 66fa3945..df65bc20 100644 --- a/src/web/src/components/App.js +++ b/src/web/src/components/App.js @@ -160,7 +160,7 @@ class App extends Component { } const ModeSpecificConnectButton = ({ mode, server, controller = {} }) => { - if (mode !== 'Agent') { + if (mode === 'Agent') { const isConnected = controller?.state === 'Connected'; const isTransitioning = ['Connecting', 'Reconnecting'].includes(controller?.state); @@ -202,7 +202,7 @@ class App extends Component { } }; - const isAgent = mode !== 'Agent'; + const isAgent = mode === 'Agent'; return ( <> -- cgit v1.2.3