From edd7a8bcb1b055942c4ceecd1d9db9797a5071c1 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 30 May 2021 23:04:58 -0500 Subject: rough in PeerService --- src/slskd/Core/Peer/Services/IPeerService.cs | 27 ++++++++ src/slskd/Core/Peer/Services/PeerService.cs | 96 ++++++++++++++++++++++++++++ src/slskd/Startup.cs | 6 +- 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 src/slskd/Core/Peer/Services/IPeerService.cs create mode 100644 src/slskd/Core/Peer/Services/PeerService.cs (limited to 'src') diff --git a/src/slskd/Core/Peer/Services/IPeerService.cs b/src/slskd/Core/Peer/Services/IPeerService.cs new file mode 100644 index 00000000..0c0738a3 --- /dev/null +++ b/src/slskd/Core/Peer/Services/IPeerService.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.Peer +{ + using System.Threading; + using System.Threading.Tasks; + + public interface IPeerService + { + Task GetAsync(string username); + } +} diff --git a/src/slskd/Core/Peer/Services/PeerService.cs b/src/slskd/Core/Peer/Services/PeerService.cs new file mode 100644 index 00000000..782a4702 --- /dev/null +++ b/src/slskd/Core/Peer/Services/PeerService.cs @@ -0,0 +1,96 @@ +// +// 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.Peer +{ + using System; + using System.Threading.Tasks; + using Microsoft.EntityFrameworkCore; + using Microsoft.Extensions.Logging; + using ISoulseekClient = Soulseek.ISoulseekClient; + + public class PeerService : IPeerService + { + private const int CacheTTLSeconds = 300; + + public PeerService( + ISoulseekClient client, + IDbContextFactory contextFactory, + ILogger log) + { + Client = client; + ContextFactory = contextFactory; + Log = log; + } + + private ISoulseekClient Client { get; } + private IDbContextFactory ContextFactory { get; } + private ILogger Log { get; set; } + + public async Task GetAsync(string username) + { + using var context = ContextFactory.CreateDbContext(); + + var peer = await context.Peers.FirstOrDefaultAsync(peer => peer.Username == username); + var peerExists = peer != default; + + if (peerExists && peer.UpdatedAt < DateTime.UtcNow.AddSeconds(CacheTTLSeconds)) + { + return peer; + } + + var infoTask = Client.GetUserInfoAsync(username); + var statusTask = Client.GetUserStatusAsync(username); + var endpointTask = Client.GetUserEndPointAsync(username); + + await Task.WhenAll(infoTask, statusTask, endpointTask); + + var info = await infoTask; + var status = await statusTask; + var endpoint = await endpointTask; + + peer = new Peer() + { + Username = username, + Description = info.Description, + HasFreeUploadSlot = info.HasFreeUploadSlot, + HasPicture = info.HasPicture, + Picture = info.Picture, + QueueLength = info.QueueLength, + UploadSlots = info.UploadSlots, + IsPrivileged = status.IsPrivileged, + Presence = status.Presence, + IPAddress = endpoint.Address, + Port = endpoint.Port, + UpdatedAt = DateTime.UtcNow, + }; + + if (peerExists) + { + context.Update(peer); + } + else + { + context.Add(peer); + } + + await context.SaveChangesAsync(); + + return peer; + } + } +} diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index 4d11ad1a..0c8804f0 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -171,7 +171,10 @@ namespace slskd options.UseSqlite($"Data Source={Path.Combine(Options.Directories.App, "search.db")}"); }); - services.AddTransient(p => p.GetRequiredService>().CreateDbContext()); + services.AddDbContextFactory(options => + { + options.UseSqlite($"Data Source={Path.Combine(Options.Directories.App, "peer.db")}"); + }); services.AddSingleton(); services.AddSingleton(); @@ -181,6 +184,7 @@ namespace slskd new SharedFileCache(Options.Directories.Shared, 3600000)); services.AddSingleton(); + services.AddSingleton(); services.AddHostedService(); services.AddSingleton(_ => Service.SoulseekClient); -- cgit v1.2.3