Skip to content

Commit

Permalink
Make interface for routing table (#2046) (#2229)
Browse files Browse the repository at this point in the history
* Make interface for routing table (#2046)

remove redudant comments on RoutingTable.cs (#2046)

move changelog to 'To be released' section. (#2229)

Co-authored-by: Seo Myunggyun (Jonathan) <tkiapril@users.noreply.github.com>
Co-authored-by: Chanhyuck Ko <limeelbee@gmail.com>
  • Loading branch information
3 people authored Sep 21, 2022
1 parent 1ab3727 commit d05c843
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ To be released.

### Added APIs

- Added `IRoutingTable` interface. [[#2046], [#2229]]
- `RoutingTable` became to implement `IRoutingTable` interface.
[[#2046], [#2229]]

### Behavioral changes

- Many types became serialized and deserialized better with
Expand Down Expand Up @@ -48,6 +52,8 @@ To be released.

### CLI tools

[#2046]: https://github.com/planetarium/libplanet/issues/2046
[#2229]: https://github.com/planetarium/libplanet/pull/2229
[#2322]: https://github.com/planetarium/libplanet/pull/2322
[`System.Text.Json.JsonSerializer`]: https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializer
[custom converters]: https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to
Expand Down
44 changes: 44 additions & 0 deletions Libplanet.Net/Protocols/IRoutingTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;

namespace Libplanet.Net.Protocols
{
public interface IRoutingTable
{
/// <summary>
/// The number of peers in the table.
/// </summary>
int Count { get; }

/// <summary>
/// An <see cref="IReadOnlyList{T}"/> of peers in the table.
/// </summary>
IReadOnlyList<BoundPeer> Peers { get; }

/// <summary>
/// Adds the <paramref name="peer"/> to the table.
/// </summary>
/// <param name="peer">The <see cref="BoundPeer"/> to add.</param>
/// <exception cref="ArgumentException">Thrown when <paramref name="peer"/>'s
/// <see cref="Address"/> is equal to the <see cref="Address"/> of self.</exception>
void AddPeer(BoundPeer peer);

/// <summary>
/// Removes the <paramref name="peer"/> to the table.
/// </summary>
/// <param name="peer">The <see cref="BoundPeer"/> to remove.</param>
/// <returns><see langword="true"/> if the <paramref name="peer"/> is successfully
/// removed from <see cref="IRoutingTable"/>.</returns>
/// <exception cref="ArgumentException">Thrown when <paramref name="peer"/>'s
/// <see cref="Address"/> is equal to the <see cref="Address"/> of self.</exception>
bool RemovePeer(BoundPeer peer);

/// <summary>
/// Determines whether the <see cref="IRoutingTable"/> contains the specified key.
/// </summary>
/// <param name="peer">Key to locate in the <see cref="IRoutingTable"/>.</param>
/// <returns><see langword="true"/> if the <see cref="IRoutingTable" /> contains
/// an element with the specified key; otherwise, <see langword="false"/>.</returns>
bool Contains(BoundPeer peer);
}
}
25 changes: 6 additions & 19 deletions Libplanet.Net/Protocols/RoutingTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Libplanet.Net.Protocols
/// <summary>
/// Kademlia distributed hash table.
/// </summary>
public class RoutingTable
public class RoutingTable : IRoutingTable
{
private readonly Address _address;
private readonly KBucket[] _buckets;
Expand Down Expand Up @@ -66,14 +66,10 @@ public RoutingTable(
/// </summary>
public int BucketSize { get; }

/// <summary>
/// The number of peers in the table.
/// </summary>
/// <inheritdoc />
public int Count => _buckets.Sum(bucket => bucket.Count);

/// <summary>
/// An <see cref="IReadOnlyList{T}"/> of peers in the table.
/// </summary>
/// <inheritdoc />
public IReadOnlyList<BoundPeer> Peers =>
NonEmptyBuckets.SelectMany(bucket => bucket.Peers).ToImmutableArray();

Expand Down Expand Up @@ -112,14 +108,10 @@ internal IReadOnlyList<KBucket> NonEmptyBuckets
}
}

/// <summary>
/// Adds the <paramref name="peer"/> to the table.
/// </summary>
/// <param name="peer">The <see cref="BoundPeer"/> to add.</param>
/// <exception cref="ArgumentException">Thrown when <paramref name="peer"/>'s
/// <see cref="Address"/> is equal to the <see cref="Address"/> of self.</exception>
/// <inheritdoc />
public void AddPeer(BoundPeer peer) => AddPeer(peer, DateTimeOffset.UtcNow);

/// <inheritdoc />
public bool RemovePeer(BoundPeer peer)
{
if (peer.Address.Equals(_address))
Expand All @@ -134,12 +126,7 @@ public bool RemovePeer(BoundPeer peer)
return BucketOf(peer).RemovePeer(peer);
}

/// <summary>
/// Determines whether the <see cref="RoutingTable"/> contains the specified key.
/// </summary>
/// <param name="peer">Key to locate in the <see cref="RoutingTable"/>.</param>
/// <returns><see langword="true"/> if the <see cref="RoutingTable" /> contains
/// an element with the specified key; otherwise, <see langword="false"/>.</returns>
/// <inheritdoc />
public bool Contains(BoundPeer peer)
{
return BucketOf(peer).Contains(peer);
Expand Down

0 comments on commit d05c843

Please sign in to comment.