1
1
using Newtonsoft . Json ;
2
2
using System ;
3
3
using System . Collections . Generic ;
4
+ using System . Globalization ;
4
5
using System . Linq ;
5
6
using System . Text ;
6
7
using System . IO ;
@@ -14,12 +15,53 @@ namespace Ipfs.Http
14
15
{
15
16
public partial class IpfsClient : IGenericApi
16
17
{
18
+ const double TicksPerNanosecond = ( double ) TimeSpan . TicksPerMillisecond * 0.000001 ;
19
+
17
20
/// <inheritdoc />
18
21
public Task < Peer > IdAsync ( MultiHash peer = null , CancellationToken cancel = default ( CancellationToken ) )
19
22
{
20
23
return DoCommandAsync < Peer > ( "id" , cancel , peer ? . ToString ( ) ) ;
21
24
}
22
25
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
+
23
65
/// <inheritdoc />
24
66
public async Task < string > ResolveAsync ( string name , bool recursive = true , CancellationToken cancel = default ( CancellationToken ) )
25
67
{
0 commit comments