-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from NiceOneFox/T19_Add_LIFO_BufferManager
Close T19 Add Lifo buffer manager
- Loading branch information
Showing
8 changed files
with
63 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
backend/ServiceSimulation/Bll.Domain/Entities/LIFOBufferManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Bll.Domain.Interfaces; | ||
using Bll.Domain.Models; | ||
|
||
namespace Bll.Domain.Entities | ||
{ | ||
public class LIFOBufferManager : IBufferManager | ||
{ | ||
private readonly IResults _results; | ||
|
||
private readonly ITimeProvider _time; | ||
|
||
private readonly LinkedList<Request> _requests = new(); | ||
|
||
public readonly int Capacity = 4; | ||
|
||
public LIFOBufferManager(IResults resultChannel, ITimeProvider time, int capacity) | ||
{ | ||
_results = resultChannel; | ||
_time = time; | ||
Capacity = capacity; | ||
} | ||
|
||
public void Add(Request request) | ||
{ | ||
if (_requests.Count >= Capacity) | ||
{ | ||
var removedRequest = _requests.Last(); | ||
_requests.RemoveLast(); | ||
|
||
removedRequest.EndTime = _time.Now; | ||
_results.Cancelled.Add(removedRequest); | ||
} | ||
|
||
_requests.AddFirst(request); | ||
} | ||
|
||
public Request Get() | ||
{ | ||
if (IsFree()) throw new ArgumentNullException("Buffer was empty"); // TODO template string for Ex | ||
|
||
var requestFromBuffer = _requests.First(); | ||
_requests.RemoveFirst(); | ||
|
||
return requestFromBuffer; | ||
} | ||
|
||
public bool IsFree() => _requests.Count == 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
namespace Api.enums; | ||
|
||
public enum SimulationType | ||
public enum BufferType | ||
{ | ||
Standard = 0 | ||
FIFO = 0, | ||
LIFO = 1 | ||
} |