11using Newtonsoft . Json ;
22using System ;
33using System . Collections . Generic ;
4+ using System . Globalization ;
45using System . Linq ;
56using System . Text ;
67using 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 {
0 commit comments