Skip to content

Commit

Permalink
Pair incoming responses to the original requests (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
lyonsil authored Mar 28, 2023
2 parents ee4ee2b + 5a96237 commit 84a69b5
Show file tree
Hide file tree
Showing 22 changed files with 541 additions and 374 deletions.
231 changes: 0 additions & 231 deletions c-sharp/Data/NetworkConnectorTypes.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Text.Json.Serialization;
using PtxUtils;

namespace Paranext.DataProvider.Data;
namespace Paranext.DataProvider.JsonUtils;

/// <summary>
/// Handles serialization and deserialization of string enums
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Paranext.DataProvider.Utils;
using Paranext.DataProvider.Messages;
using PtxUtils;

namespace Paranext.DataProvider.Data;
namespace Paranext.DataProvider.JsonUtils;

/// <summary>
/// Handles serialization and deserialization of Messages
/// </summary>
internal sealed class MessageConverter : JsonConverter<Message>
{
#region Member variables
private static readonly JsonSerializerOptions recursiveSafeOptions =
JsonUtils.CreateSerializationOptions();
SerializationOptions.CreateSerializationOptions();
private static readonly Dictionary<Enum<MessageType>, Type> messageTypeMap =
new()
{
{ MessageType.InitClient, typeof(MessageInitClient) },
{ MessageType.ClientConnect, typeof(MessageClientConnect) },
{ MessageType.Request, typeof(MessageRequest) },
{ MessageType.Response, typeof(MessageResponse) },
{ MessageType.Event, typeof(MessageEvent) },
};
#endregion

#region Implementation of JsonConverter
public override bool CanConvert(Type typeToConvert) =>
typeof(Message).IsAssignableFrom(typeToConvert);

Expand Down Expand Up @@ -54,9 +52,7 @@ JsonSerializerOptions options
{
JsonSerializer.Serialize(writer, message, message.GetType(), recursiveSafeOptions);
}
#endregion

#region Private helper methods
/// <summary>
/// Reads the type property from the message given the specified reader
/// </summary>
Expand Down Expand Up @@ -85,5 +81,4 @@ private static Enum<MessageType> ReadType(ref Utf8JsonReader reader)
return new Enum<MessageType>(reader.GetString());
} while (true);
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;

namespace Paranext.DataProvider.Data;
namespace Paranext.DataProvider.JsonUtils;

/// <summary>
/// Type resolver for creation objects that have a private default constructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using Paranext.DataProvider.Data;
using Paranext.DataProvider.Messages;

namespace Paranext.DataProvider.Utils;
namespace Paranext.DataProvider.JsonUtils;

internal static class JsonUtils
internal static class SerializationOptions
{
/// <summary>
/// Creates new serialization options used for communicating with the PAPI.
Expand All @@ -23,7 +23,7 @@ public static JsonSerializerOptions CreateSerializationOptions()
};

options.Converters.Add(new EnumConverter<MessageType>());
options.Converters.Add(new EnumConverter<RequestTypes>());
options.Converters.Add(new EnumConverter<RequestType>());
return options;
}
}
13 changes: 13 additions & 0 deletions c-sharp/MessageHandlers/IMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Paranext.DataProvider.Messages;

namespace Paranext.DataProvider.MessageHandlers;

internal interface IMessageHandler
{
/// <summary>
/// Handle an incoming message, and optionally respond with another message
/// </summary>
/// <param name="message">Incoming message to handle</param>
/// <returns>Optional response to the incoming message</returns>
public Message? HandleMessage(Message message);
}
22 changes: 22 additions & 0 deletions c-sharp/MessageHandlers/MessageHandlerEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Paranext.DataProvider.Messages;

namespace Paranext.DataProvider.MessageHandlers;

/// <summary>
/// Handler for "Event" messages
/// </summary>
internal class MessageHandlerEvent : IMessageHandler
{
public Message? HandleMessage(Message message)
{
if (message == null)
throw new ArgumentNullException(nameof(message));

if (message.Type != MessageType.Event)
throw new ArgumentException("Incorrect message type", nameof(message));

Console.WriteLine($"Event received: {message}");

return null;
}
}
Loading

0 comments on commit 84a69b5

Please sign in to comment.