aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Core
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-05-26 23:48:13 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-05-26 23:48:13 -0500
commit8feb789ba5dd4729ea0a7fbafdccd86e9e06f74c (patch)
tree70e76bbdd477a651a4c05ffcc5fbd0b28f12f883 /src/slskd/Core
parent0354b842141d354a82072aee78000f87ce8b348f (diff)
downloadslskd-8feb789ba5dd4729ea0a7fbafdccd86e9e06f74c.tar.gz
rough in SearchService
Diffstat (limited to 'src/slskd/Core')
-rw-r--r--src/slskd/Core/Search/ISearchService.cs5
-rw-r--r--src/slskd/Core/Search/SearchService.cs94
2 files changed, 67 insertions, 32 deletions
diff --git a/src/slskd/Core/Search/ISearchService.cs b/src/slskd/Core/Search/ISearchService.cs
index 0182beab..1936b30f 100644
--- a/src/slskd/Core/Search/ISearchService.cs
+++ b/src/slskd/Core/Search/ISearchService.cs
@@ -18,13 +18,14 @@
namespace slskd.Search
{
using System;
+ using System.Linq.Expressions;
using System.Threading.Tasks;
using Soulseek;
public interface ISearchService
{
- Task<Guid> BeginAsync(SearchQuery query, SearchScope scope = null, SearchOptions options = null);
+ Task<(Guid Id, Task<Soulseek.Search> Completed)> BeginAsync(SearchQuery query, SearchScope scope = null, SearchOptions options = null, Guid? id = null);
bool TryCancel(Guid id);
- Task<Search> FindAsync(Guid id);
+ Task<Search> FindAsync(Expression<Func<Search, bool>> expression, bool includeResponses = false);
}
}
diff --git a/src/slskd/Core/Search/SearchService.cs b/src/slskd/Core/Search/SearchService.cs
index 0f69f632..f0632211 100644
--- a/src/slskd/Core/Search/SearchService.cs
+++ b/src/slskd/Core/Search/SearchService.cs
@@ -19,76 +19,86 @@ namespace slskd.Search
{
using System;
using System.Collections.Concurrent;
+ using System.Linq.Expressions;
+ using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
+ using Microsoft.EntityFrameworkCore;
using Soulseek;
public class SearchService : ISearchService
{
- public SearchService(SearchDbContext context, ISoulseekClient client)
+ public SearchService(IDbContextFactory<SearchDbContext> contextFactory, ISoulseekClient client)
{
- Context = context;
+ ContextFactory = contextFactory;
Client = client;
-
- Client.SearchStateChanged += Client_SearchStateChanged;
- Client.SearchRequestReceived += Client_SearchRequestReceived;
}
- private void Client_SearchRequestReceived(object sender, SearchRequestEventArgs e)
- {
- throw new NotImplementedException();
- }
-
- private void Client_SearchStateChanged(object sender, SearchStateChangedEventArgs e)
- {
- throw new NotImplementedException();
- }
-
- private SearchDbContext Context { get; }
+ private IDbContextFactory<SearchDbContext> ContextFactory { get; }
private ISoulseekClient Client { get; }
private ConcurrentDictionary<Guid, int> Lookup { get; }
= new ConcurrentDictionary<Guid, int>();
private ConcurrentDictionary<int, CancellationTokenSource> InProgress { get; }
= new ConcurrentDictionary<int, CancellationTokenSource>();
- public async Task<Guid> BeginAsync(SearchQuery query, SearchScope scope = null, SearchOptions options = null)
+ public async Task<(Guid Id, Task<Soulseek.Search> Completed)> BeginAsync(SearchQuery query, SearchScope scope = null, SearchOptions options = null, Guid? id = null)
{
scope = scope ?? SearchScope.Network;
var token = Client.GetNextToken();
- options = options ?? new SearchOptions();
+ id = id ?? Guid.NewGuid();
+
var cts = new CancellationTokenSource();
- var id = Guid.NewGuid();
var record = new Search()
{
SearchText = query.SearchText,
Token = token,
- Id = id,
+ Id = id.Value,
State = SearchStates.Requested,
+ StartedAt = DateTime.UtcNow,
};
- Context.Add(record);
- await Context.SaveChangesAsync();
+ options = options ?? new SearchOptions(stateChanged: (args) =>
+ {
+ record.State = args.Search.State;
+ ContextFactory.CreateDbContext().Update(record);
+ });
+
+ using var context = ContextFactory.CreateDbContext();
+ context.Add(record);
+ context.SaveChanges();
InProgress.TryAdd(token, cts);
- Lookup.TryAdd(id, token);
+ Lookup.TryAdd(id.Value, token);
+
+ var task = Client.SearchAsync(query, responseReceived: (response) => AddResponse(id.Value, response), scope, token, options, cancellationToken: cts.Token);
- _ = Client.SearchAsync(query, scope, token, options, cancellationToken: cts.Token).ContinueWith(async task =>
+ _ = task.ContinueWith(async task =>
{
+ Console.WriteLine($"Continued...");
+
try
{
- var result = await task;
+ var search = await task;
+ record.State = search.State;
+ record.EndedAt = DateTime.UtcNow;
+
+ Console.WriteLine($"Saving with state {record.State}");
- // todo: update db with status and end time
+ using var context = ContextFactory.CreateDbContext();
+ context.Update(record);
+ context.SaveChanges();
+
+ Console.WriteLine($"Saved");
}
finally
{
InProgress.TryRemove(token, out _);
- Lookup.TryRemove(id, out _);
+ Lookup.TryRemove(id.Value, out _);
}
});
- return id;
+ return (id.Value, task);
}
public bool TryCancel(Guid id)
@@ -102,9 +112,33 @@ namespace slskd.Search
return false;
}
- public Task<Search> FindAsync(Guid id)
+ public async Task<Search> FindAsync(Expression<Func<Search, bool>> expression, bool includeResponses = true)
+ {
+ using var context = ContextFactory.CreateDbContext();
+ var search = await context
+ .Searches
+ .Include(s => s.Responses)
+ .ThenInclude(r => r.Files)
+ .FirstOrDefaultAsync(expression);
+
+ //if (search == default)
+ //{
+ // return default;
+ //}
+
+ //if (includeResponses)
+ //{
+ // await context.Entry(search).Collection(s => s.Responses).LoadAsync();
+ //}
+
+ return search;
+ }
+
+ private void AddResponse(Guid searchId, Soulseek.SearchResponse soulseekResponse)
{
- return Context.Searches.FindAsync(id).AsTask();
+ using var context = ContextFactory.CreateDbContext();
+ context.Add(SearchResponse.FromSoulseekSearchResponse(soulseekResponse, searchId));
+ context.SaveChanges();
}
}
}