From 1b171265452f4552ffc7e1bdeee15152d3b3474e Mon Sep 17 00:00:00 2001 From: Benedikt Schenkel Date: Fri, 24 Nov 2023 11:44:23 +0100 Subject: [PATCH] refactor --- src/Point/Point.cs | 16 ++--- .../PointToInterlockingConnectionB4R1Impl.cs | 63 ++++++++++--------- 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/Point/Point.cs b/src/Point/Point.cs index d45f7d2..0ef1131 100644 --- a/src/Point/Point.cs +++ b/src/Point/Point.cs @@ -1,17 +1,13 @@ -using System.Net.WebSockets; -using System.Text.Json; -using Google.Protobuf; -using Grpc.Core; -using Sci; +using DegradedPointPosition = EulynxLive.Messages.IPointToInterlockingConnection.IPointToInterlockingConnection.DegradedPointPosition; using EulynxLive.Messages.IPointToInterlockingConnection; +using EulynxLive.Point.Proto; +using Grpc.Core; using PointPosition = EulynxLive.Messages.IPointToInterlockingConnection.IPointToInterlockingConnection.PointPosition; -using DegradedPointPosition = EulynxLive.Messages.IPointToInterlockingConnection.IPointToInterlockingConnection.DegradedPointPosition; using PointState = EulynxLive.Messages.IPointToInterlockingConnection.IPointToInterlockingConnection.PointState; -using static Sci.Rasta; +using Sci; +using System.Net.WebSockets; using System.Text; -using Grpc.Net.Client; -using EulynxLive.Point.Components; -using EulynxLive.Point.Proto; +using System.Text.Json; namespace EulynxLive.Point { diff --git a/src/Point/PointToInterlockingConnectionB4R1Impl.cs b/src/Point/PointToInterlockingConnectionB4R1Impl.cs index d90fb56..326916b 100644 --- a/src/Point/PointToInterlockingConnectionB4R1Impl.cs +++ b/src/Point/PointToInterlockingConnectionB4R1Impl.cs @@ -1,4 +1,3 @@ -using System.Net.WebSockets; using EulynxLive.Messages.Baseline4R1; using EulynxLive.Point; using Google.Protobuf; @@ -48,32 +47,30 @@ public void Connect() public async Task InitializeConnection(PointState state) { _logger.LogTrace("Connected. Waiting for request..."); - if (!await _currentConnection.ResponseStream.MoveNext(_timeout.Token) - || Message.FromBytes(_currentConnection.ResponseStream.Current.Message.ToByteArray()) is not PointPdiVersionCheckCommand) + if (await ReceiveMessage() == null) { _logger.LogError("Unexpected message."); return false; } var versionCheckResponse = new PointPdiVersionCheckMessage(_localId, _remoteId, PointPdiVersionCheckMessageResultPdiVersionCheck.PDIVersionsFromReceiverAndSenderDoMatch, /* TODO */ 0, 0, new byte[] { }); - await _currentConnection.RequestStream.WriteAsync(new SciPacket() { Message = ByteString.CopyFrom(versionCheckResponse.ToByteArray()) }); + await SendMessage(versionCheckResponse); - if (!await _currentConnection.ResponseStream.MoveNext(_timeout.Token) - || Message.FromBytes(_currentConnection.ResponseStream.Current.Message.ToByteArray()) is not PointInitialisationRequestCommand) + if (await ReceiveMessage() == null) { _logger.LogError("Unexpected message."); return false; } var startInitialization = new PointStartInitialisationMessage(_localId, _remoteId); - await _currentConnection.RequestStream.WriteAsync(new SciPacket() { Message = ByteString.CopyFrom(startInitialization.ToByteArray()) }); + await SendMessage(startInitialization); var pointState = new B4R1PointStateImpl(state); var initialPosition = new PointPointPositionMessage(_localId, _remoteId, pointState.PointPosition, pointState.DegradedPointPosition); - await _currentConnection.RequestStream.WriteAsync(new SciPacket() { Message = ByteString.CopyFrom(initialPosition.ToByteArray()) }); + await SendMessage(initialPosition); var completeInitialization = new PointInitialisationCompletedMessage(_localId, _remoteId); - await _currentConnection.RequestStream.WriteAsync(new SciPacket() { Message = ByteString.CopyFrom(completeInitialization.ToByteArray()) }); + await SendMessage(completeInitialization); return true; } @@ -81,41 +78,50 @@ public async Task SendPointPosition(PointState state) { var pointState = new B4R1PointStateImpl(state); var response = new PointPointPositionMessage(_localId, _remoteId, pointState.PointPosition, pointState.DegradedPointPosition); - await _currentConnection.RequestStream.WriteAsync(new SciPacket() { Message = ByteString.CopyFrom(response.ToByteArray()) }); + await SendMessage(response); } async public Task SendTimeoutMessage() { var response = new PointTimeoutMessage(_localId, _remoteId); - await _currentConnection.RequestStream.WriteAsync(new SciPacket() { Message = ByteString.CopyFrom(response.ToByteArray()) }); + await SendMessage(response); } public async Task ReceivePointPosition() { + var message = await ReceiveMessage(); - if (!await _currentConnection.ResponseStream.MoveNext()) + return (message != null)? message.CommandedPointPosition switch { - return null; - } - var message = Message.FromBytes(_currentConnection.ResponseStream.Current.Message.ToByteArray()); - - if (message is PointMovePointCommand movePointCommand) - { - return movePointCommand.CommandedPointPosition switch - { - PointMovePointCommandCommandedPointPosition.SubsystemElectronicInterlockingRequestsARightHandPointMoving => PointPosition.RIGHT, - PointMovePointCommandCommandedPointPosition.SubsystemElectronicInterlockingRequestsALeftHandPointMoving => PointPosition.LEFT, - }; - } - - _logger.LogInformation("Received unknown message {}", message.GetType()); - return null; + PointMovePointCommandCommandedPointPosition.SubsystemElectronicInterlockingRequestsARightHandPointMoving => PointPosition.RIGHT, + PointMovePointCommandCommandedPointPosition.SubsystemElectronicInterlockingRequestsALeftHandPointMoving => PointPosition.LEFT, + } : null; } public void Dispose() { _currentConnection?.Dispose(); } + + private async Task SendMessage(Message message) + { + if (_currentConnection == null) throw new NullReferenceException("Connection is null. Did you call Connect()?"); + await _currentConnection.RequestStream.WriteAsync(new SciPacket() { Message = ByteString.CopyFrom(message.ToByteArray()) }); + } + + private async Task ReceiveMessage() where T : Message + { + if (_currentConnection == null) throw new NullReferenceException("Connection is null. Did you call Connect()?"); + if (!await _currentConnection.ResponseStream.MoveNext(_timeout.Token)) return null; + + var message = Message.FromBytes(_currentConnection.ResponseStream.Current.Message.ToByteArray()); + if (message is not T) + { + _logger.LogError("Unexpected message: {}", message); + return null; + } + return message as T; + } } public class B4R1PointStateImpl @@ -124,7 +130,8 @@ public class B4R1PointStateImpl public PointPointPositionMessageReportedPointPosition PointPosition { get => map(_state.PointPosition); } public PointPointPositionMessageReportedDegradedPointPosition DegradedPointPosition { get => map(_state.DegradedPointPosition); } - public B4R1PointStateImpl(PointState state){ + public B4R1PointStateImpl(PointState state) + { _state = state; }