Skip to content

Commit

Permalink
Refactored Protocol library to support updated Serialization lib
Browse files Browse the repository at this point in the history
  • Loading branch information
HermanSchoenfeld committed May 27, 2024
1 parent 7c3d0aa commit 1f6433a
Show file tree
Hide file tree
Showing 35 changed files with 414 additions and 589 deletions.
47 changes: 17 additions & 30 deletions recipes/AbstractProtocol.AnonymousPipeComplex/AppProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,23 @@ namespace AbstractProtocol.AnonymousPipeComplex;
public class AppProtocol {
public static Protocol Build() {
return new ProtocolBuilder()
.Handshake
.ThreeWay
.InitiatedBy(CommunicationRole.Client)
.HandleWith<Sync, Ack, Verack>(InitiateHandshake, ReceiveHandshake, VerifyHandshake, AcknowledgeHandshake)
.Requests
.ForRequest<Ping>().RespondWith(Ping)
.ForRequest<RequestListFolder>().RespondWith(ListFolder)
.ForRequest<RequestFilePart>().RespondWith(GetFilePart)
.Responses
.ForResponse<Pong>().ToRequest<Ping>().HandleWith(HandlePong)
.ForResponse<FilePart>().ToRequest<RequestFilePart>().HandleWith(SaveFilePart)
.ForResponse<FolderContents>().ToRequest<RequestListFolder>().HandleWith(PrintFolderContents)
.Commands
.ForCommand<NotifyNewTransaction>().Execute(HandleNewTransaction)
.ForCommand<NotifyNewBlock>().Execute(HandleNewBlock)
.ForCommand<NotifyLayer2Message>().Execute(HandleNewLayer2Message)
.Messages
.For<Ping>(AppProtocolMessageType.Ping).SerializeWith(new BinaryFormattedSerializer<Ping>())
.For<Pong>(AppProtocolMessageType.Pong).SerializeWith(new BinaryFormattedSerializer<Pong>())
.For<RequestListFolder>(AppProtocolMessageType.RequestListFolder)
.SerializeWith(new BinaryFormattedSerializer<RequestListFolder>())
.For<RequestFilePart>(AppProtocolMessageType.RequestFilePart).SerializeWith(new BinaryFormattedSerializer<RequestFilePart>())
.For<FolderContents>(AppProtocolMessageType.FolderContents).SerializeWith(new BinaryFormattedSerializer<FolderContents>())
.For<FilePart>(AppProtocolMessageType.FilePart).SerializeWith(new BinaryFormattedSerializer<FilePart>())
.For<NotifyNewTransaction>().SerializeWith(new BinaryFormattedSerializer<NotifyNewTransaction>())
.For<NotifyNewBlock>().SerializeWith(new BinaryFormattedSerializer<NotifyNewBlock>())
.For<NotifyLayer2Message>().SerializeWith(new BinaryFormattedSerializer<NotifyLayer2Message>())
.For<Sync>().SerializeWith(new BinaryFormattedSerializer<Sync>())
.For<Ack>().SerializeWith(new BinaryFormattedSerializer<Ack>())
.For<Verack>().SerializeWith(new BinaryFormattedSerializer<Verack>())
.ConfigureHandshake(hb => hb
.UseThreeWay()
.InitiatedBy(CommunicationRole.Client)
.HandleWith<Sync, Ack, Verack>(InitiateHandshake, ReceiveHandshake, VerifyHandshake, AcknowledgeHandshake)
)
.AddRequestResponse<Ping, Pong>(Ping, HandlePong)
.AddRequestResponse<RequestListFolder, FolderContents>(ListFolder, PrintFolderContents)
.ConfigureRequest<RequestFilePart>(rb => rb.HandleRequestWith(GetFilePart).HandleResponseWith(SaveFilePart))
.AddCommand<NotifyNewTransaction>(HandleNewTransaction)
.AddCommand<NotifyNewBlock>(HandleNewBlock)
.ConfigureCommand<NotifyLayer2Message>(cb => cb.HandleWith(HandleNewLayer2Message))
.ConfigureSerialization( sf => sf
.SetMinTypeCode(1000)
.RegisterAutoBuild<Ping>()
.RegisterAutoBuild<Pong>()
)
.AutoBuildSerializers()
.Build();
}

Expand Down
4 changes: 2 additions & 2 deletions recipes/AbstractProtocol.AnonymousPipeSimple/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ static void Main(string[] args) {
// var protocol =
// new ProtocolBuilder()
// .Requests
// .ForRequest<Ping>().RespondWith((_, _) => new Pong())
// .ForRequest<Ping>().HandleRequestWith((_, _) => new Pong())
// .Responses
// .ForResponse<Pong>().ToRequest<Ping>().HandleWith((ch, pingMsg, pongMsg) => SystemLog.Info("Handled Pong OK!"))
// .ForResponse<Pong>().ToRequest<Ping>().HandleResponseWith((ch, pingMsg, pongMsg) => SystemLog.Info("Handled Pong OK!"))
// .Messages
// .For<Ping>().SerializeWith(new BinaryFormattedSerializer<Ping>())
// .For<Pong>().SerializeWith(new BinaryFormattedSerializer<Pong>())
Expand Down
21 changes: 7 additions & 14 deletions recipes/AbstractProtocol.UPDSimple/AppProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,15 @@ namespace AbstractProtocol.UDPSimple;
public class AppProtocol {
public static Protocol Build() {
return new ProtocolBuilder()
.Handshake
.ThreeWay
.InitiatedBy(CommunicationRole.Client)
.HandleWith<Sync, Ack, Verack>(InitiateHandshake, ReceiveHandshake, VerifyHandshake)
.Requests
.ForRequest<RequestBytes>().RespondWith(RequestBytes)
.Responses
.ForResponse<ReturnBytes>().ToRequest<RequestBytes>().HandleWith(HandleReturnBytes)
.ConfigureHandshake(hb => hb
.UseThreeWay()
.InitiatedBy(CommunicationRole.Client)
.HandleWith<Sync, Ack, Verack>(InitiateHandshake, ReceiveHandshake, VerifyHandshake)
)
.AddRequestResponse<RequestBytes, ReturnBytes>(RequestBytes, HandleReturnBytes)
//.Commands
// .ForCommand<NotifyNewTransaction>().Execute(HandleNewTransaction)
.Messages
.For<RequestBytes>().SerializeWith(new BinaryFormattedSerializer<RequestBytes>())
.For<ReturnBytes>().SerializeWith(new BinaryFormattedSerializer<ReturnBytes>())
.For<Sync>().SerializeWith(new BinaryFormattedSerializer<Sync>())
.For<Ack>().SerializeWith(new BinaryFormattedSerializer<Ack>())
.For<Verack>().SerializeWith(new BinaryFormattedSerializer<Verack>())
.AutoBuildSerializers()
.Build();
}

Expand Down
15 changes: 5 additions & 10 deletions src/Hydrogen.DApp.Core/Runtime/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,11 @@ public Host(ILogger logger, IApplicationPaths paths) {

private Protocol BuildProtocol()
=> new ProtocolBuilder()
.Requests
.ForRequest<PingMessage>().RespondWith(() => new PongMessage())
.Responses
.ForResponse<PongMessage>().ToRequest<PingMessage>().HandleWith(() => Logger.Info("Received Pong"))
.Commands
.ForCommand<UpgradeMessage>().Execute(async upgradeMessage => await UpgradeApplication(upgradeMessage.HydrogenApplicationPackagePath))
.ForCommand<ShutdownMessage>().Execute(async () => await RequestShutdown())
.Messages
.UseOnly(HostProtocolHelper.BuildMessageSerializer())
.Build();
.AddRequestResponse<PingMessage, PongMessage>(() => new PongMessage(), () => Logger.Info("Received Pong"))
.AddCommand<UpgradeMessage>( async upgradeMessage => await UpgradeApplication(upgradeMessage.HydrogenApplicationPackagePath))
.AddCommand<ShutdownMessage>(async () => await RequestShutdown())
.AutoBuildSerializers()
.Build();

public virtual async Task DeployHAP(string hapPath) {
CheckStatus(HostStatus.Stopped, HostStatus.Upgrading);
Expand Down
26 changes: 10 additions & 16 deletions src/Hydrogen.DApp.Core/Runtime/Protocol/HostProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,22 @@
//
// This notice must not be removed when duplicating this file or its contents, in whole or in part.

using System;
using Hydrogen.Communications;

namespace Hydrogen.DApp.Core.Runtime;

public static class HostProtocolHelper {

public static Protocol BuildForNode(ICommandHandler<UpgradeMessage> upgradeNodeHandler)
=> new ProtocolBuilder()
.Requests
.ForRequest<PingMessage>().RespondWith((_, _) => new PongMessage())
.Commands
.ForCommand<UpgradeMessage>().Execute(upgradeNodeHandler)
.Messages
.UseOnly(BuildMessageSerializer())
.Build();
=> throw new NotImplementedException();
//=> new ProtocolBuilder()
// .Requests
// .ForRequest<PingMessage>().RespondWith((_, _) => new PongMessage())
// .Commands
// .ForCommand<UpgradeMessage>().Execute(upgradeNodeHandler)
// .Messages
// .UseOnly(BuildMessageSerializer())
// .Build();

public static PolymorphicSerializer<object> BuildMessageSerializer()
=> new ProtocolSerializerBuilder<object>()
.For<PingMessage>(HostProtocolMessageType.Ping).SerializeWith(new BinaryFormattedSerializer<PingMessage>())
.For<PongMessage>(HostProtocolMessageType.Pong).SerializeWith(new BinaryFormattedSerializer<PongMessage>())
.For<RollbackMessage>(HostProtocolMessageType.Rollback).SerializeWith(new BinaryFormattedSerializer<RollbackMessage>())
.For<ShutdownMessage>(HostProtocolMessageType.Shutdown).SerializeWith(new BinaryFormattedSerializer<ShutdownMessage>())
.For<UpgradeMessage>(HostProtocolMessageType.Upgrade).SerializeWith(new BinaryFormattedSerializer<UpgradeMessage>())
.Build();
}
27 changes: 23 additions & 4 deletions src/Hydrogen/Collections/Dictionaries/MultiKeyDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Hydrogen;
Expand Down Expand Up @@ -47,10 +48,13 @@ public bool TryGetValue(K1 key1, K2 key2, out V value) {
return TryGetValue(key1, out var dict) && dict.TryGetValue(key2, out value);
}

public new IEnumerable<V> Values =>
from baseDict in base.Values
from baseKey in baseDict.Keys
select baseDict[baseKey];
public new IEnumerable<(K1, K2)> Keys =>
from k1 in base.Keys
from k2 in this[k1].Keys
select (k1, k2);

public new IEnumerable<V> Values
=> base.Values.SelectMany(x => x.Values);
}


Expand Down Expand Up @@ -87,6 +91,13 @@ public bool TryGetValue(K1 key1, K2 key2, K3 key3, out V value) {
return TryGetValue(key1, out var dict) && dict.TryGetValue(key2, key3, out value);
}

public new IEnumerable<(K1, K2, K3)> Keys =>
from k1 in base.Keys
from tail in this[k1].Keys
select (k1, tail.Item1, tail.Item2);

public new IEnumerable<V> Values
=> base.Values.SelectMany(x => x.Values);
}


Expand Down Expand Up @@ -124,6 +135,14 @@ public bool TryGetValue(K1 key1, K2 key2, K3 key3, K4 key4, out V value) {
value = default;
return TryGetValue(key1, out var dict) && dict.TryGetValue(key2, key3, key4, out value);
}

public new IEnumerable<(K1, K2, K3, K4)> Keys =>
from k1 in base.Keys
from tail in this[k1].Keys
select (k1, tail.Item1, tail.Item2, tail.Item3);

public new IEnumerable<V> Values
=> base.Values.SelectMany(x => x.Values);
}


Expand Down
1 change: 1 addition & 0 deletions src/Hydrogen/Protocol/ActionCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public ActionCommandHandler(Action<ProtocolOrchestrator, object> action) {
_action = action;
}

public override Type MessageType => typeof(object);
public override void Execute(ProtocolOrchestrator orchestrator, object command) {
Guard.ArgumentNotNull(orchestrator, nameof(orchestrator));
Guard.ArgumentNotNull(command, nameof(command));
Expand Down
1 change: 1 addition & 0 deletions src/Hydrogen/Protocol/ActionCommandHandlerT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public ActionCommandHandler(Action<ProtocolOrchestrator, TMessage> action) {
Guard.ArgumentNotNull(action, nameof(action));
_action = action;
}
public override Type MessageType => typeof(TMessage);

public override void Execute(ProtocolOrchestrator orchestrator, TMessage command) {
Guard.ArgumentNotNull(orchestrator, nameof(orchestrator));
Expand Down
4 changes: 4 additions & 0 deletions src/Hydrogen/Protocol/ActionRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ namespace Hydrogen.Communications;
public class ActionRequestHandler : RequestHandlerBase {
private readonly Func<ProtocolOrchestrator, object, object> _action;

public override Type RequestType => typeof(object);

public override Type ResponseType => typeof(object);

public ActionRequestHandler(Func<ProtocolOrchestrator, object, object> action) {
Guard.ArgumentNotNull(action, nameof(action));
_action = action;
Expand Down
4 changes: 4 additions & 0 deletions src/Hydrogen/Protocol/ActionRequestHandlerT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ namespace Hydrogen.Communications;
public class ActionRequestHandler<TRequest, TResponse> : RequestHandlerBase<TRequest, TResponse> {
private readonly Func<ProtocolOrchestrator, TRequest, TResponse> _action;

public override Type RequestType => typeof(TRequest);

public override Type ResponseType => typeof(TResponse);

public ActionRequestHandler(Func<ProtocolOrchestrator, TRequest, TResponse> action) {
Guard.ArgumentNotNull(action, nameof(action));
_action = action;
Expand Down
4 changes: 4 additions & 0 deletions src/Hydrogen/Protocol/ActionResponseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public ActionResponseHandler(Action<ProtocolOrchestrator, object, object> action
_action = action;
}

public override Type RequestType => typeof(object);

public override Type ResponseType => typeof(object);

public override void Execute(ProtocolOrchestrator orchestrator, object request, object response) {
Guard.ArgumentNotNull(orchestrator, nameof(orchestrator));
Guard.ArgumentNotNull(request, nameof(request));
Expand Down
4 changes: 4 additions & 0 deletions src/Hydrogen/Protocol/ActionResponseHandlerT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public ActionResponseHandler(Action<ProtocolOrchestrator, TRequest, TResponse> a
Guard.ArgumentNotNull(action, nameof(action));
_action = action;
}

public override Type RequestType => typeof(TRequest);

public override Type ResponseType => typeof(TResponse);

public override void Execute(ProtocolOrchestrator orchestrator, TRequest request, TResponse response) {
Guard.ArgumentNotNull(orchestrator, nameof(orchestrator));
Expand Down
26 changes: 0 additions & 26 deletions src/Hydrogen/Protocol/Builder/IProtocolBuilderMain.cs

This file was deleted.

Loading

0 comments on commit 1f6433a

Please sign in to comment.