Skip to content

Commit

Permalink
Merge pull request #2 from HarryCordewener/Expand-MSDP
Browse files Browse the repository at this point in the history
Start with creating a WIP Handler for MSDP
  • Loading branch information
HarryCordewener authored Jan 8, 2024
2 parents 4cd273c + 2a9954d commit 7a5784b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Discord](https://img.shields.io/discord/1193672869104861195?style=for-the-badge)](https://discord.gg/SK2cWERJF7) [![Build Status](https://img.shields.io/github/actions/workflow/status/HarryCordewener/TelnetNegotiationCore/dotnet.yml?style=for-the-badge)](https://github.com/HarryCordewener/TelnetNegotiationCore/actions/workflows/dotnet.yml) [![Nuget](https://img.shields.io/nuget/dt/TelnetNegotiationCore?style=for-the-badge&color=blue)](https://www.nuget.org/packages/TelnetNegotiationCore)
[![Discord](https://img.shields.io/discord/1193672869104861195?style=for-the-badge)](https://discord.gg/SK2cWERJF7) [![Build Status](https://img.shields.io/github/actions/workflow/status/HarryCordewener/TelnetNegotiationCore/dotnet.yml?style=for-the-badge)](https://github.com/HarryCordewener/TelnetNegotiationCore/actions/workflows/dotnet.yml) [![NuGet](https://img.shields.io/nuget/dt/TelnetNegotiationCore?style=for-the-badge&color=blue)](https://www.nuget.org/packages/TelnetNegotiationCore)
![Larger Logo](LargerLogo.png)

# Telnet Negotiation Core
Expand Down
6 changes: 3 additions & 3 deletions TelnetNegotiationCore.UnitTests/CHARSETTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
namespace TelnetNegotiationCore.UnitTests
{
[TestFixture]
public class CHARSETTests: BaseTest
public class CHARSETTests : BaseTest
{
private byte[] _negotiationOutput;

private Task WriteBackToOutput(byte[] arg1, Encoding arg2, TelnetInterpreter t) => throw new NotImplementedException();

private Task WriteBackToGMCP((string module, string writeback) arg1) => throw new NotImplementedException();

private Task ClientWriteBackToNegotiate(byte[] arg1) => Task.Run(() => _negotiationOutput = arg1);
private Task ClientWriteBackToNegotiate(byte[] arg1) { _negotiationOutput = arg1; return Task.CompletedTask; }

private Task ServerWriteBackToNegotiate(byte[] arg1) => Task.Run(() => _negotiationOutput = arg1);
private Task ServerWriteBackToNegotiate(byte[] arg1) => ServerWriteBackToNegotiate(arg1);

[SetUp]
public void Setup()
Expand Down
20 changes: 20 additions & 0 deletions TelnetNegotiationCore/Handlers/MSDPClientHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TelnetNegotiationCore.Handlers
{
/// <summary>
/// A simple handler for MSDP that creates a workflow for requesting MSDP information, and storing returned information.
/// </summary>
public class MSDPClientHandler
{
public MSDPClientHandler() { }

public Task HandleAsync(string serverJson)
{
throw new NotImplementedException();
}
}
}
21 changes: 21 additions & 0 deletions TelnetNegotiationCore/Handlers/MSDPServerHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TelnetNegotiationCore.Handlers
{
/// <summary>
/// A simple handler for MSDP that creates a workflow for responding with MSDP information.
/// </summary>
public class MSDPServerHandler
{
public MSDPServerHandler() { }

public Task HandleAsync(string clientJson)
{
throw new NotImplementedException();
}
}
}
18 changes: 5 additions & 13 deletions TelnetNegotiationCore/Interpreters/TelnetMSDPInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ private StateMachine<State, Trigger> SetupMSDPNegotiation(StateMachine<State, Tr

tsm.Configure(State.SubNegotiation)
.Permit(Trigger.MSDP, State.AlmostNegotiatingMSDP)
.OnEntry(() =>
{
_currentMSDPInfo = [];
});
.OnEntry(() => _currentMSDPInfo = []);

tsm.Configure(State.AlmostNegotiatingMSDP)
.Permit(Trigger.MSDP_VAR, State.EvaluatingMSDP)
Expand All @@ -85,23 +82,18 @@ private StateMachine<State, Trigger> SetupMSDPNegotiation(StateMachine<State, Tr

tsm.Configure(State.CompletingMSDP)
.SubstateOf(State.Accepting)
.OnEntryAsync(ReadMSDPValues);
.OnEntry(ReadMSDPValues);

TriggerHelper.ForAllTriggersExcept([Trigger.IAC], t =>
TriggerHelper.ForAllTriggersButIAC(t =>
tsm.Configure(State.EvaluatingMSDP).OnEntryFrom(ParameterizedTrigger(t), CaptureMSDPValue).PermitReentry(t));
}

return tsm;
}

private void CaptureMSDPValue(OneOf<byte, Trigger> b) =>
_currentMSDPInfo.Add(b.AsT0);
private void CaptureMSDPValue(OneOf<byte, Trigger> b) => _currentMSDPInfo.Add(b.AsT0);

private Task ReadMSDPValues()
{
Functional.MSDPLibrary.MSDPScan(_currentMSDPInfo.Skip(1), CurrentEncoding);
return Task.CompletedTask;
}
private void ReadMSDPValues() => Functional.MSDPLibrary.MSDPScan(_currentMSDPInfo.Skip(1), CurrentEncoding);

/// <summary>
/// Announce we do MSDP negotiation to the client.
Expand Down
19 changes: 7 additions & 12 deletions TelnetNegotiationCore/Interpreters/TelnetSafeInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,12 @@ private StateMachine<State, Trigger> SetupSafeNegotiation(StateMachine<State, Tr
}
}

foreach (var trigger in triggers
.Except(info.States.First(x => (State)x.UnderlyingState == State.SubNegotiation).Transitions
.Select(x => (Trigger)x.Trigger.UnderlyingTrigger)))
{
tsm.Configure(State.SubNegotiation)
.Permit(trigger, State.BadSubNegotiation);
}
var underlyingTriggers = info.States.First(x => (State)x.UnderlyingState == State.SubNegotiation).Transitions
.Select(x => (Trigger)x.Trigger.UnderlyingTrigger);

triggers
.Except(underlyingTriggers)
.ForEach(trigger => tsm.Configure(State.SubNegotiation).Permit(trigger, State.BadSubNegotiation));

TriggerHelper.ForAllTriggersButIAC(t => tsm.Configure(State.BadSubNegotiation).Permit(t, State.BadSubNegotiationEvaluating));
TriggerHelper.ForAllTriggersButIAC(t => tsm.Configure(State.BadSubNegotiationEvaluating).PermitReentry(t));
Expand All @@ -131,11 +130,7 @@ private StateMachine<State, Trigger> SetupSafeNegotiation(StateMachine<State, Tr
var statesAllowingForErrorTransitions = states
.Except(acceptingStateInfo);

foreach (var state in statesAllowingForErrorTransitions)
{
tsm.Configure((State)state.UnderlyingState)
.Permit(Trigger.Error, State.Accepting);
}
statesAllowingForErrorTransitions.ForEach(state => tsm.Configure((State)state.UnderlyingState).Permit(Trigger.Error, State.Accepting));

tsm.OnUnhandledTrigger(async (state, trigger, unmetguards) =>
{
Expand Down

0 comments on commit 7a5784b

Please sign in to comment.