Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chertby committed Oct 17, 2023
1 parent b5ab584 commit 3947417
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
30 changes: 18 additions & 12 deletions src/libp2p/Libp2p.Protocols.Ping/LogMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,38 @@ internal static partial class LogMessages
[LoggerMessage(
EventId = EventId + 1,
EventName = nameof(ReadingPong),
Message = "Reading pong",
Message = "Reading pong {remotePeer}",
Level = LogLevel.Trace)]
internal static partial void ReadingPong(
this ILogger logger);
this ILogger logger,
Multiaddr remotePeer);

[LoggerMessage(
EventId = EventId + 2,
EventName = nameof(VerifyingPong),
Message = "Verifying pong",
Message = "Verifying pong {remotePeer}",
Level = LogLevel.Trace)]
internal static partial void VerifyingPong(
this ILogger logger);
this ILogger logger,
Multiaddr remotePeer);

[LoggerMessage(
EventId = EventId + 3,
EventName = nameof(ReadingPing),
Message = "Reading ping",
Message = "Reading ping {remotePeer}",
Level = LogLevel.Trace)]
internal static partial void ReadingPing(
this ILogger logger);
this ILogger logger,
Multiaddr remotePeer);

[LoggerMessage(
EventId = EventId + 4,
EventName = nameof(ReturningPong),
Message = "Returning pong",
Message = "Returning pong {remotePeer}",
Level = LogLevel.Trace)]
internal static partial void ReturningPong(
this ILogger logger);
this ILogger logger,
Multiaddr remotePeer);

[LoggerMessage(
EventId = EventId + 5,
Expand All @@ -54,10 +58,11 @@ internal static partial void LogPing(
[LoggerMessage(
EventId = EventId + 6,
EventName = nameof(LogPinged),
Message = "Pinged",
Message = "Pinged {remotePeer}",
Level = LogLevel.Debug)]
internal static partial void LogPinged(
this ILogger logger);
this ILogger logger,
Multiaddr remotePeer);

[LoggerMessage(
EventId = EventId + 7,
Expand All @@ -71,10 +76,11 @@ internal static partial void PingListenStarted(
[LoggerMessage(
EventId = EventId + 8,
EventName = nameof(PingFinished),
Message = "Ping finished",
Message = "Ping finished {remotePeer}",
Level = LogLevel.Debug)]
internal static partial void PingFinished(
this ILogger logger);
this ILogger logger,
Multiaddr remotePeer);

[LoggerMessage(
EventId = EventId + 9,
Expand Down
24 changes: 9 additions & 15 deletions src/libp2p/Libp2p.Protocols.Ping/PingProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PingProtocol : IProtocol
{
private const int PayloadLength = 32;

public string Id => "/ipfs/ping/1.0.0"; // TODO: order in class: fields, constructors, properties, methods?
public string Id => "/ipfs/ping/1.0.0";
private readonly Random _random = new();
private readonly ILogger<PingProtocol>? _logger;

Expand All @@ -34,41 +34,35 @@ public async Task DialAsync(IChannel channel, IChannelFactory? channelFactory,
_logger?.LogPing(context.RemotePeer.Address);
await channel.WriteAsync(bytes);

_logger?.ReadingPong();
_logger?.ReadingPong(context.RemotePeer.Address);
ReadOnlySequence<byte> response = await channel.ReadAsync(PayloadLength, ReadBlockingMode.WaitAll);

_logger?.VerifyingPong();
_logger?.VerifyingPong(context.RemotePeer.Address);
if (!byteArray[0..PayloadLength].SequenceEqual(response.ToArray()))
{
// TODO: do we need log context.RemotePeer or context.RemotePeer.Address?
// _logger?.LogWarning("Wrong response to ping from {from}", context.RemotePeer);

// TODO: if we throw exception, probably LogLevel bigger than warning?
_logger?.PingFailed(context.RemotePeer.Address);
throw new ApplicationException(); // TODO: add specific exception?
throw new ApplicationException();
}

_logger?.LogPinged(); // TODO: add context.RemotePeer.Address?
_logger?.LogPinged(context.RemotePeer.Address);
}

public async Task ListenAsync(IChannel channel, IChannelFactory? channelFactory,
IPeerContext context)
{
// TODO: do we need log context.RemotePeer or context.RemotePeer.Address?
// _logger?.LogDebug("Ping listen started from {remotePeer}", context.RemotePeer);
_logger?.PingListenStarted(context.RemotePeer.Address);

while (!channel.IsClosed)
{
_logger?.ReadingPing();
_logger?.ReadingPing(context.RemotePeer.Address);
ReadOnlySequence<byte> request = await channel.ReadAsync(PayloadLength, ReadBlockingMode.WaitAll);
byte[] byteArray = request.ToArray(); // TODO: do we need convert to array and then again to ReadOnlySequence?
byte[] byteArray = request.ToArray();
ReadOnlySequence<byte> bytes = new(byteArray);

_logger?.ReturningPong();
_logger?.ReturningPong(context.RemotePeer.Address);
await channel.WriteAsync(bytes);
}

_logger?.PingFinished();
_logger?.PingFinished(context.RemotePeer.Address);
}
}

0 comments on commit 3947417

Please sign in to comment.