diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-05-23 22:45:06 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-05-23 22:45:06 -0500 |
| commit | 752849c5ca06b31b951d4c3de00e5acd52946238 (patch) | |
| tree | 5894a4db88d0fbabf61e60b74637a2166b9ed9bf /src | |
| parent | ea78f7f0c5b0858f15a1af8a700100d7ae101ae1 (diff) | |
| download | slskd-752849c5ca06b31b951d4c3de00e5acd52946238.tar.gz | |
rough in SearchService
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Core/Search/Domain/Search.cs | 16 | ||||
| -rw-r--r-- | src/slskd/Core/Search/ISearchService.cs | 30 | ||||
| -rw-r--r-- | src/slskd/Core/Search/SearchService.cs | 110 |
3 files changed, 144 insertions, 12 deletions
diff --git a/src/slskd/Core/Search/Domain/Search.cs b/src/slskd/Core/Search/Domain/Search.cs index 10cbdc9d..9b54d367 100644 --- a/src/slskd/Core/Search/Domain/Search.cs +++ b/src/slskd/Core/Search/Domain/Search.cs @@ -22,7 +22,10 @@ namespace slskd.Search public class Search { - public Guid Id { get; init; } + public Guid Id { get; init; } = Guid.NewGuid(); + + public DateTime StartedAt { get; init; } = DateTime.UtcNow; + public DateTime? EndedAt { get; set; } /// <summary> /// Gets the text for which to search. @@ -38,16 +41,5 @@ namespace slskd.Search /// Gets the unique identifier for the search. /// </summary> public int Token { get; set; } - - public static Search FromSoulseekSearch(Soulseek.Search s) - { - return new Search - { - Id = Guid.NewGuid(), - SearchText = s.SearchText, - State = s.State, - Token = s.Token, - }; - } } } diff --git a/src/slskd/Core/Search/ISearchService.cs b/src/slskd/Core/Search/ISearchService.cs new file mode 100644 index 00000000..0182beab --- /dev/null +++ b/src/slskd/Core/Search/ISearchService.cs @@ -0,0 +1,30 @@ +// <copyright file="ISearchService.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd.Search +{ + using System; + using System.Threading.Tasks; + using Soulseek; + + public interface ISearchService + { + Task<Guid> BeginAsync(SearchQuery query, SearchScope scope = null, SearchOptions options = null); + bool TryCancel(Guid id); + Task<Search> FindAsync(Guid id); + } +} diff --git a/src/slskd/Core/Search/SearchService.cs b/src/slskd/Core/Search/SearchService.cs new file mode 100644 index 00000000..0f69f632 --- /dev/null +++ b/src/slskd/Core/Search/SearchService.cs @@ -0,0 +1,110 @@ +// <copyright file="SearchService.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd.Search +{ + using System; + using System.Collections.Concurrent; + using System.Threading; + using System.Threading.Tasks; + using Soulseek; + + public class SearchService : ISearchService + { + public SearchService(SearchDbContext context, ISoulseekClient client) + { + Context = context; + 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 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) + { + scope = scope ?? SearchScope.Network; + var token = Client.GetNextToken(); + options = options ?? new SearchOptions(); + var cts = new CancellationTokenSource(); + var id = Guid.NewGuid(); + + var record = new Search() + { + SearchText = query.SearchText, + Token = token, + Id = id, + State = SearchStates.Requested, + }; + + Context.Add(record); + await Context.SaveChangesAsync(); + + InProgress.TryAdd(token, cts); + Lookup.TryAdd(id, token); + + _ = Client.SearchAsync(query, scope, token, options, cancellationToken: cts.Token).ContinueWith(async task => + { + try + { + var result = await task; + + // todo: update db with status and end time + } + finally + { + InProgress.TryRemove(token, out _); + Lookup.TryRemove(id, out _); + } + }); + + return id; + } + + public bool TryCancel(Guid id) + { + if (Lookup.TryGetValue(id, out var token) && InProgress.TryGetValue(token, out var cts)) + { + cts.Cancel(); + return true; + } + + return false; + } + + public Task<Search> FindAsync(Guid id) + { + return Context.Searches.FindAsync(id).AsTask(); + } + } +} |
