From 7510f1d4165bdff9f3c656cd31b5ba883fb6cb18 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 25 Oct 2022 10:28:25 -0500 Subject: cleanup and docs of file info exchange --- src/slskd/Network/API/Hubs/NetworkHub.cs | 8 +--- src/slskd/Network/NetworkService.cs | 70 +++++++++++++++++++------------- 2 files changed, 42 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/slskd/Network/API/Hubs/NetworkHub.cs b/src/slskd/Network/API/Hubs/NetworkHub.cs index 32575e8d..e654f795 100644 --- a/src/slskd/Network/API/Hubs/NetworkHub.cs +++ b/src/slskd/Network/API/Hubs/NetworkHub.cs @@ -102,13 +102,7 @@ namespace slskd.Network public void ReturnFileInfo(Guid id, bool exists, long length) { - if (!Network.TryGetAgentRegistration(Context.ConnectionId, out var record)) - { - Log.Warning("Agent connection {Id} responded to a file info request with Id {Id}, but is not registered.", Context.ConnectionId, id); - return; - } - - Network.HandleFileInfoResponse(record.Agent.Name, id, (exists, length)); + Network.HandleFileInfoResponse(id, (exists, length)); } } } \ No newline at end of file diff --git a/src/slskd/Network/NetworkService.cs b/src/slskd/Network/NetworkService.cs index dffc305d..0a3079ae 100644 --- a/src/slskd/Network/NetworkService.cs +++ b/src/slskd/Network/NetworkService.cs @@ -65,6 +65,20 @@ namespace slskd.Network /// /// Retrieves information about the specified from the specified . /// + /// + /// This is the first step in a mult-step workflow. The entire sequence is: + /// + /// + /// Upload service calls and awaits , which requests the file info from the + /// remote agent, and waits for the response before returning it to the caller. + /// + /// + /// The remote agent sends the response via the SignalR hub, and the hub invokes + /// . The response is passed back to and + /// returned to the caller. + /// + /// + /// /// The agent from which to retrieve the file information. /// The file for which to retrieve information. /// An optional timeout value. @@ -118,10 +132,9 @@ namespace slskd.Network /// /// Handles the client response for a request. /// - /// The agent that provided the response. /// The ID of the request. /// The client response to the request. - void HandleFileInfoResponse(string agentName, Guid id, (bool Exists, long Length) response); + void HandleFileInfoResponse(Guid id, (bool Exists, long Length) response); /// /// Handles the client response for a request, returning when the corresponding file @@ -132,13 +145,6 @@ namespace slskd.Network /// The operation context. Task HandleFileStreamResponse(Guid id, Stream response); - /// - /// Returns a value indicating whether an agent with the specified is registered. - /// - /// The name of the agent. - /// A value indicating whether an agent with the specified name is registered. - bool IsAgentRegistered(string agentName); - /// /// Notifies the caller of of a failure to obtain a file stream from the requested agent. /// @@ -275,6 +281,20 @@ namespace slskd.Network /// /// Retrieves information about the specified from the specified . /// + /// + /// This is the first step in a mult-step workflow. The entire sequence is: + /// + /// + /// Upload service calls and awaits , which requests the file info from the + /// remote agent, and waits for the response before returning it to the caller. + /// + /// + /// The remote agent sends the response via the SignalR hub, and the hub invokes + /// . The response is passed back to and + /// returned to the caller. + /// + /// + /// /// The agent from which to retrieve the file information. /// The file for which to retrieve information. /// An optional timeout value. @@ -286,8 +306,10 @@ namespace slskd.Network throw new NotFoundException($"Agent {agentName} is not registered"); } + // prepare a wait for the response. we don't cache the token along with the agent here, as all data is exchanged over + // an authenticated SignalR connection var id = Guid.NewGuid(); - var key = new WaitKey(nameof(GetFileInfoAsync), agentName, id); + var key = new WaitKey(nameof(GetFileInfoAsync), id); var wait = Waiter.Wait<(bool Exists, long Length)>(key, timeout); try @@ -353,9 +375,9 @@ namespace slskd.Network throw new NotFoundException($"Agent {agentName} is not registered"); } - // cache the id to prevent replay attacks; the agent should respond within the timeout. this is somewhat redundant - // due to the wait using only the id, however caching the agent name along with the other elements of the request - // allows us to ensure that tokens are used only by the agent they were intended for. + // cache the id to prevent replay attacks; the agent should respond within the timeout. this is somewhat redundant due + // to the wait using only the id, however caching the agent name along with the other elements of the request allows + // us to ensure that tokens are used only by the agent they were intended for. MemoryCache.Set(GetFileTokenCacheKey(filename, id), agentName, TimeSpan.FromMilliseconds(timeout)); Log.Debug("Cached file upload token {Token} for agent {Agent}", id, agentName); @@ -385,21 +407,20 @@ namespace slskd.Network /// /// Handles the client response for a request. /// - /// The agent that provided the response. /// The ID of the request. /// The client response to the request. - public void HandleFileInfoResponse(string agentName, Guid id, (bool Exists, long Length) response) + public void HandleFileInfoResponse(Guid id, (bool Exists, long Length) response) { - var key = new WaitKey(nameof(GetFileInfoAsync), agentName, id); + var key = new WaitKey(nameof(GetFileInfoAsync), id); if (!Waiter.IsWaitingFor(key)) { - Log.Warning("Agent {Agent} responded to a file info request with Id {Id}, but a response was not expected", agentName, id); - return; + var msg = $"A file info response matching Id {id} was not expected"; + Log.Warning(msg); + throw new NotFoundException(msg); } Waiter.Complete(key, response); - Log.Information("Agent {Agent} responded to a file info request with Id {Id}: Exists: {Exists}, Length: {Length}", agentName, id, response.Exists, response.Length); } /// @@ -407,7 +428,7 @@ namespace slskd.Network /// upload is complete. /// /// - /// Assumes has previously been used to ensure the Id and agent match. + /// Assumes has previously been used to ensure the Id and agent match. /// /// The ID of the request. /// The client response to the request. @@ -438,15 +459,6 @@ namespace slskd.Network await completion; } - public void HandleGetFileStreamCompletion(string agentName, string filename) - { - // complete the indefinite wait for this upload. this sends execution back to the body of the file stream response - // handler, and ultimately back to the API controller. the agent's original HTTP request will complete. - var key = new WaitKey(nameof(GetFileStreamAsync), "completion", agentName, filename); - Waiter.Complete(key); - Log.Information("Upload of {File} from agent {Agent} reported as completed", filename, agentName); - } - /// /// Notifies the caller of of a failure to obtain a file stream from the requested agent. /// -- cgit v1.2.3