|
|
|
@ -4,6 +4,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Linq; |
|
|
|
|
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using GBase.DataHandling.Factories; |
|
|
|
using GBase.DataHandling.Factories; |
|
|
|
using GBase.DataHandling.Factories.Pool; |
|
|
|
using GBase.DataHandling.Factories.Pool; |
|
|
|
@ -36,17 +37,17 @@ namespace GBase.DataHandling.Pool |
|
|
|
_waitingRequesters = new List<IPoolRequest>(); |
|
|
|
_waitingRequesters = new List<IPoolRequest>(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public async Task<IDataHandler> RequestDataHandler(object requester) //TODO: async? |
|
|
|
public async Task<IDataHandler> RequestDataHandler(object requester, bool overwrite, CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (_pool.All(i => i.InUse) && _pool.Count == _availableDataHandlers) //no free dataHandlers available |
|
|
|
if (_pool.All(i => i.InUse) && _pool.Count == _availableDataHandlers) //no free dataHandlers available |
|
|
|
return await WaitForDataHandler(requester); |
|
|
|
return await WaitForDataHandler(requester, cancellationToken); |
|
|
|
else if (_pool.All(i => i.InUse)) //every existing dataHandler is used -> create a new one |
|
|
|
else if (_pool.All(i => i.InUse)) //every existing dataHandler is used -> create a new one |
|
|
|
return CreateDataHandler(); |
|
|
|
return await CreateDataHandler(overwrite, cancellationToken); |
|
|
|
else //there are unused dataHandlers existing |
|
|
|
else //there are unused dataHandlers existing |
|
|
|
return GetDataHandler(); |
|
|
|
return GetDataHandler(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async Task<IDataHandler> WaitForDataHandler(object requester) //TODO: Add request to list/stack (FIFO) with all requests that have to be resolved |
|
|
|
private async Task<IDataHandler> WaitForDataHandler(object requester, CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
{ |
|
|
|
IDataHandler dataHandler = null; |
|
|
|
IDataHandler dataHandler = null; |
|
|
|
IPoolRequest request = _poolRequestFactory.Create(requester); |
|
|
|
IPoolRequest request = _poolRequestFactory.Create(requester); |
|
|
|
@ -58,10 +59,10 @@ namespace GBase.DataHandling.Pool |
|
|
|
if (_pool.Any(i => !i.InUse)) |
|
|
|
if (_pool.Any(i => !i.InUse)) |
|
|
|
dataHandler = GetDataHandler(); |
|
|
|
dataHandler = GetDataHandler(); |
|
|
|
else |
|
|
|
else |
|
|
|
await Task.Delay(1000); |
|
|
|
await Task.Delay(1000, cancellationToken); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (_waitingRequesters.Contains(request)) //request is in list but not first |
|
|
|
else if (_waitingRequesters.Contains(request)) //request is in list but not first |
|
|
|
await Task.Delay(1000); |
|
|
|
await Task.Delay(1000, cancellationToken); |
|
|
|
else |
|
|
|
else |
|
|
|
_waitingRequesters.Add(request); |
|
|
|
_waitingRequesters.Add(request); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -71,9 +72,10 @@ namespace GBase.DataHandling.Pool |
|
|
|
return dataHandler; |
|
|
|
return dataHandler; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private IDataHandler CreateDataHandler() |
|
|
|
private async Task<IDataHandler> CreateDataHandler(bool overwrite, CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
{ |
|
|
|
IPoolItem<IDataHandler> item = _poolItemFactory.Create(_dataHandlerFactory.Create()); |
|
|
|
IPoolItem<IDataHandler> item = _poolItemFactory.Create(_dataHandlerFactory.Create()); |
|
|
|
|
|
|
|
await item.Item.Init(overwrite, cancellationToken); |
|
|
|
_pool.Add(item); |
|
|
|
_pool.Add(item); |
|
|
|
|
|
|
|
|
|
|
|
item.InUse = true; |
|
|
|
item.InUse = true; |
|
|
|
@ -87,5 +89,18 @@ namespace GBase.DataHandling.Pool |
|
|
|
item.InUse = true; |
|
|
|
item.InUse = true; |
|
|
|
return item.Item; |
|
|
|
return item.Item; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async ValueTask DisposeAsync() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
while (_waitingRequesters.Any()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
await Task.Delay(1000); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var dataHandler in _pool.Select(i => i.Item)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
await dataHandler.DisposeAsync(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |