Skip to content

Commit

Permalink
Add PutIntoTrailedPosition
Browse files Browse the repository at this point in the history
  • Loading branch information
rs22 committed Dec 16, 2023
1 parent 94e786c commit 44e3b13
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public class PointToInterlockingConnection : IPointToInterlockingConnection
public PointConfiguration Configuration { get; }
public CancellationToken TimeoutToken => _timeout.Token;

private IConnection? _currentConnection;
public IConnection? CurrentConnection { get => _currentConnection; }
public IConnection? CurrentConnection { get; private set; }
private CancellationTokenSource _timeout;
private readonly int _timeoutDuration;
private CancellationToken _stoppingToken;
Expand All @@ -29,7 +28,7 @@ public PointToInterlockingConnection(
_timeoutDuration = timeoutDuration;
_timeout = new CancellationTokenSource();
_logger = logger;
_currentConnection = null;
CurrentConnection = null;

var config = configuration.GetSection("PointSettings").Get<PointConfiguration>() ?? throw new Exception("No configuration provided");
_localId = config.LocalId;
Expand All @@ -40,7 +39,7 @@ public PointToInterlockingConnection(
public void Connect(IConnection connection)
{
ResetTimeout();
_currentConnection = connection;
CurrentConnection = connection;
}

public async Task<bool> InitializeConnection(GenericPointState state, CancellationToken stoppingToken)
Expand Down Expand Up @@ -101,24 +100,24 @@ async public Task SendTimeoutMessage()

private async Task SendMessage(Message message)
{
if (_currentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
if (CurrentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
await SendMessage(message.ToByteArray());
}

private async Task SendMessage(byte[] message)
{
if (_currentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
await _currentConnection.SendAsync(message);
if (CurrentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
await CurrentConnection.SendAsync(message);
}

private async Task<T?> ReceiveMessage<T>(CancellationToken cancellationToken) where T : Message
{
if (_currentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
if (CurrentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
ResetTimeout();

try
{
var message = Message.FromBytes(await _currentConnection.ReceiveAsync(CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _timeout.Token).Token));
var message = Message.FromBytes(await CurrentConnection.ReceiveAsync(CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _timeout.Token).Token));
if (message is not T)
{
_logger.LogError("Unexpected message: {}", message);
Expand All @@ -144,7 +143,7 @@ private void ResetTimeout()

public async Task SendGenericMessage(byte[] message)
{
if (_currentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
if (CurrentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
await SendMessage(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class PointToInterlockingConnection : IPointToInterlockingConnection
private readonly string _remoteId;
public PointConfiguration Configuration { get; }
public CancellationToken TimeoutToken => _timeout.Token;
private IConnection? _currentConnection;
public IConnection? CurrentConnection { get => _currentConnection; }

public IConnection? CurrentConnection { get; private set; }
private CancellationTokenSource _timeout;
private readonly int _timeoutDuration;
private readonly CancellationToken _stoppingToken;
Expand All @@ -30,7 +30,7 @@ public PointToInterlockingConnection(
_stoppingToken = stoppingToken;
_timeout = new CancellationTokenSource();
_logger = logger;
_currentConnection = null;
CurrentConnection = null;

var config = configuration.GetSection("PointSettings").Get<PointConfiguration>() ?? throw new Exception("No configuration provided");
_localId = config.LocalId;
Expand All @@ -41,7 +41,7 @@ public PointToInterlockingConnection(
public void Connect(IConnection connection)
{
ResetTimeout();
_currentConnection = connection;
CurrentConnection = connection;
}

public async Task<bool> InitializeConnection(GenericPointState state, CancellationToken cancellationToken)
Expand Down Expand Up @@ -102,23 +102,23 @@ async public Task SendTimeoutMessage()

private async Task SendMessage(Message message)
{
if (_currentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
await _currentConnection.SendAsync(message.ToByteArray());
if (CurrentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
await CurrentConnection.SendAsync(message.ToByteArray());
}

private async Task SendMessage(byte[] message)
{
if (_currentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
await _currentConnection.SendAsync(message);
if (CurrentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
await CurrentConnection.SendAsync(message);
}

private async Task<T?> ReceiveMessage<T>(CancellationToken cancellationToken) where T : Message
{
if (_currentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
if (CurrentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
ResetTimeout();
try
{
var message = Message.FromBytes(await _currentConnection.ReceiveAsync(_timeout.Token));
var message = Message.FromBytes(await CurrentConnection.ReceiveAsync(_timeout.Token));
if (message is not T)
{
_logger.LogError("Unexpected message: {}", message);
Expand All @@ -145,10 +145,10 @@ private void ResetTimeout()
_timeout.CancelAfter(_timeoutDuration);
}


public async Task SendGenericMessage(byte[] message)
{
if (_currentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
if (CurrentConnection == null) throw new InvalidOperationException("Connection is null. Did you call Connect()?");
await SendMessage(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ namespace EulynxLive.FieldElementSubsystems.Interfaces;
public interface IPointToInterlockingConnection {
PointConfiguration Configuration { get; }
CancellationToken TimeoutToken { get; }

void Connect(IConnection connection);

Task SendPointPosition(GenericPointState state);
Task SendGenericMessage(byte[] state);
Task SendTimeoutMessage();
void Connect(IConnection connection);
Task<bool> InitializeConnection(GenericPointState state, CancellationToken cancellationToken);
public Task<GenericPointPosition?> ReceivePointPosition(CancellationToken stoppingToken);
Task<GenericPointPosition?> ReceivePointPosition(CancellationToken stoppingToken);
}
17 changes: 8 additions & 9 deletions src/Point/Connections/ConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
namespace EulynxLive.Point.Connections;

public class ConnectionFactory{
private IConfiguration _configuration { get; }
private ILogger<ConnectionFactory> _logger { get; }
private readonly IConfiguration _configuration;
private readonly ILogger<ConnectionFactory> _logger;

public ConnectionFactory(ILogger<ConnectionFactory> logger, IConfiguration configuration){
_logger = logger;
Expand All @@ -19,15 +19,14 @@ public IPointToInterlockingConnection CreateConnection(IServiceProvider x) {
var connectionProtocol = _configuration.GetSection("ConnectionSettings").Get<PointConfiguration>()?.ConnectionProtocol;
switch (connectionProtocol){
case ConnectionProtocol.EulynxBaseline4R1:
return new EulynxBaseline4R1.PointToInterlockingConnection(x.GetRequiredService<ILogger<EulynxBaseline4R1.PointToInterlockingConnection>>(), _configuration);
return ActivatorUtilities.CreateInstance<EulynxBaseline4R1.PointToInterlockingConnection>(x, _configuration);
case ConnectionProtocol.EulynxBaseline4R2:
return new EulynxBaseline4R2.PointToInterlockingConnection(x.GetRequiredService<ILogger<EulynxBaseline4R2.PointToInterlockingConnection>>(), _configuration, CancellationToken.None);
return ActivatorUtilities.CreateInstance<EulynxBaseline4R2.PointToInterlockingConnection>(x, _configuration);
case null:
_logger.LogWarning($"No connection protocol specified. Using EulynxBaseline4R2.");
return ActivatorUtilities.CreateInstance<EulynxBaseline4R2.PointToInterlockingConnection>(x, _configuration);
default:
if (connectionProtocol != null)
_logger.LogWarning($"Unknown connection protocol {connectionProtocol}. Using EulynxBaseline4R2.");
else
_logger.LogWarning($"No connection protocol specified. Using EulynxBaseline4R2.");
return new EulynxBaseline4R2.PointToInterlockingConnection(x.GetRequiredService<ILogger<EulynxBaseline4R2.PointToInterlockingConnection>>(), _configuration, CancellationToken.None);
throw new NotImplementedException($"Unknown connection protocol {connectionProtocol}.");
}
}
}
Loading

0 comments on commit 44e3b13

Please sign in to comment.