Skip to content

Commit 6b38ef0

Browse files
feat(GenericApi): add PingAsync
1 parent 455eef6 commit 6b38ef0

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/CoreApi/GenericApi.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Newtonsoft.Json;
22
using System;
33
using System.Collections.Generic;
4+
using System.Globalization;
45
using System.Linq;
56
using System.Text;
67
using System.IO;
@@ -14,12 +15,53 @@ namespace Ipfs.Http
1415
{
1516
public partial class IpfsClient : IGenericApi
1617
{
18+
const double TicksPerNanosecond = (double)TimeSpan.TicksPerMillisecond * 0.000001;
19+
1720
/// <inheritdoc />
1821
public Task<Peer> IdAsync(MultiHash peer = null, CancellationToken cancel = default(CancellationToken))
1922
{
2023
return DoCommandAsync<Peer>("id", cancel, peer?.ToString());
2124
}
2225

26+
/// <inheritdoc />
27+
public async Task<IEnumerable<PingResult>> PingAsync(MultiHash peer, int count = 10, CancellationToken cancel = default(CancellationToken))
28+
{
29+
var stream = await PostDownloadAsync("ping", cancel,
30+
peer.ToString(),
31+
$"count={count.ToString(CultureInfo.InvariantCulture)}");
32+
return PingResultFromStream(stream);
33+
}
34+
35+
/// <inheritdoc />
36+
public async Task<IEnumerable<PingResult>> PingAsync(MultiAddress address, int count = 10, CancellationToken cancel = default(CancellationToken))
37+
{
38+
var stream = await PostDownloadAsync("ping", cancel,
39+
address.ToString(),
40+
$"count={count.ToString(CultureInfo.InvariantCulture)}");
41+
return PingResultFromStream(stream);
42+
}
43+
44+
IEnumerable<PingResult> PingResultFromStream(Stream stream)
45+
{
46+
using (var sr = new StreamReader(stream))
47+
{
48+
while (!sr.EndOfStream)
49+
{
50+
var json = sr.ReadLine();
51+
if (log.IsDebugEnabled)
52+
log.DebugFormat("RSP {0}", json);
53+
54+
var r = JObject.Parse(json);
55+
yield return new PingResult
56+
{
57+
Success = (bool)r["Success"],
58+
Text = (string)r["Text"],
59+
Time = TimeSpan.FromTicks((long)((long)r["Time"] * TicksPerNanosecond))
60+
};
61+
}
62+
}
63+
}
64+
2365
/// <inheritdoc />
2466
public async Task<string> ResolveAsync(string name, bool recursive = true, CancellationToken cancel = default(CancellationToken))
2567
{

test/CoreApi/GenericApiTest.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using Newtonsoft.Json.Linq;
2+
using System.Linq;
33
using System;
44
using System.Text;
55
using System.Threading.Tasks;
@@ -43,6 +43,25 @@ public void Resolve()
4343
Assert.AreEqual("/ipfs/QmYNQJoKGNHTpPxCBPh9KkDpaExgd2duMa3aF6ytMpHdao", path);
4444
}
4545

46+
[TestMethod]
47+
public async Task Ping_Peer()
48+
{
49+
var ipfs = TestFixture.Ipfs;
50+
MultiHash peer = "QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3";
51+
var actual = await ipfs.Generic.PingAsync(peer, count: 1);
52+
Assert.IsNotNull(actual);
53+
Assert.AreNotEqual(0, actual.Count());
54+
}
55+
56+
[TestMethod]
57+
public async Task Ping_Address()
58+
{
59+
var ipfs = TestFixture.Ipfs;
60+
MultiAddress addr = "/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM";
61+
var actual = await ipfs.Generic.PingAsync(addr, count: 1);
62+
Assert.IsNotNull(actual);
63+
Assert.AreNotEqual(0, actual.Count());
64+
}
4665
}
4766
}
4867

0 commit comments

Comments
 (0)