Skip to content

Commit

Permalink
feat(GenericApi): add PingAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Aug 29, 2019
1 parent 455eef6 commit 6b38ef0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/CoreApi/GenericApi.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.IO;
Expand All @@ -14,12 +15,53 @@ namespace Ipfs.Http
{
public partial class IpfsClient : IGenericApi
{
const double TicksPerNanosecond = (double)TimeSpan.TicksPerMillisecond * 0.000001;

/// <inheritdoc />
public Task<Peer> IdAsync(MultiHash peer = null, CancellationToken cancel = default(CancellationToken))
{
return DoCommandAsync<Peer>("id", cancel, peer?.ToString());
}

/// <inheritdoc />
public async Task<IEnumerable<PingResult>> PingAsync(MultiHash peer, int count = 10, CancellationToken cancel = default(CancellationToken))
{
var stream = await PostDownloadAsync("ping", cancel,
peer.ToString(),
$"count={count.ToString(CultureInfo.InvariantCulture)}");
return PingResultFromStream(stream);
}

/// <inheritdoc />
public async Task<IEnumerable<PingResult>> PingAsync(MultiAddress address, int count = 10, CancellationToken cancel = default(CancellationToken))
{
var stream = await PostDownloadAsync("ping", cancel,
address.ToString(),
$"count={count.ToString(CultureInfo.InvariantCulture)}");
return PingResultFromStream(stream);
}

IEnumerable<PingResult> PingResultFromStream(Stream stream)
{
using (var sr = new StreamReader(stream))
{
while (!sr.EndOfStream)
{
var json = sr.ReadLine();
if (log.IsDebugEnabled)
log.DebugFormat("RSP {0}", json);

var r = JObject.Parse(json);
yield return new PingResult
{
Success = (bool)r["Success"],
Text = (string)r["Text"],
Time = TimeSpan.FromTicks((long)((long)r["Time"] * TicksPerNanosecond))
};
}
}
}

/// <inheritdoc />
public async Task<string> ResolveAsync(string name, bool recursive = true, CancellationToken cancel = default(CancellationToken))
{
Expand Down
21 changes: 20 additions & 1 deletion test/CoreApi/GenericApiTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using System.Linq;
using System;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -43,6 +43,25 @@ public void Resolve()
Assert.AreEqual("/ipfs/QmYNQJoKGNHTpPxCBPh9KkDpaExgd2duMa3aF6ytMpHdao", path);
}

[TestMethod]
public async Task Ping_Peer()
{
var ipfs = TestFixture.Ipfs;
MultiHash peer = "QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3";
var actual = await ipfs.Generic.PingAsync(peer, count: 1);
Assert.IsNotNull(actual);
Assert.AreNotEqual(0, actual.Count());
}

[TestMethod]
public async Task Ping_Address()
{
var ipfs = TestFixture.Ipfs;
MultiAddress addr = "/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM";
var actual = await ipfs.Generic.PingAsync(addr, count: 1);
Assert.IsNotNull(actual);
Assert.AreNotEqual(0, actual.Count());
}
}
}

0 comments on commit 6b38ef0

Please sign in to comment.