diff --git a/src/CoreApi/GenericApi.cs b/src/CoreApi/GenericApi.cs
index 039429c..0977aaa 100644
--- a/src/CoreApi/GenericApi.cs
+++ b/src/CoreApi/GenericApi.cs
@@ -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;
@@ -14,12 +15,53 @@ namespace Ipfs.Http
{
public partial class IpfsClient : IGenericApi
{
+ const double TicksPerNanosecond = (double)TimeSpan.TicksPerMillisecond * 0.000001;
+
///
public Task IdAsync(MultiHash peer = null, CancellationToken cancel = default(CancellationToken))
{
return DoCommandAsync("id", cancel, peer?.ToString());
}
+ ///
+ public async Task> 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);
+ }
+
+ ///
+ public async Task> 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 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))
+ };
+ }
+ }
+ }
+
///
public async Task ResolveAsync(string name, bool recursive = true, CancellationToken cancel = default(CancellationToken))
{
diff --git a/test/CoreApi/GenericApiTest.cs b/test/CoreApi/GenericApiTest.cs
index 38ec62a..c54c573 100644
--- a/test/CoreApi/GenericApiTest.cs
+++ b/test/CoreApi/GenericApiTest.cs
@@ -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;
@@ -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());
+ }
}
}