Skip to content

Commit 94f2cdb

Browse files
Merge branch 'master' into netstandard
2 parents 578ff6c + 0fb315c commit 94f2cdb

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/CoreApi/SwarmApi.cs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,47 @@ public async Task<IEnumerable<ConnectedPeer>> PeersAsync()
5656
{
5757
var json = await ipfs.DoCommandAsync("swarm/peers", null, "verbose=true");
5858
var result = JObject.Parse(json);
59-
return ((JArray)JObject.Parse(json)["Strings"])
60-
.Select(s =>
59+
60+
// Older servers return an array of strings
61+
var strings = (JArray)result["Strings"];
62+
if (strings != null)
63+
{
64+
return strings
65+
.Select(s =>
66+
{
67+
var parts = ((string)s).Split(' ');
68+
var address = new MultiAddress(parts[0]);
69+
return new ConnectedPeer
70+
{
71+
Id = address.Protocols.First(p => p.Name == "ipfs").Value,
72+
ConnectedAddress = parts[0],
73+
Latency = ParseLatency(parts[1])
74+
};
75+
});
76+
}
77+
78+
// Current servers return JSON
79+
var peers = (JArray)result["Peers"];
80+
if (peers != null)
81+
{
82+
return peers.Select(p => new ConnectedPeer
6183
{
62-
var parts = ((string)s).Split(' ');
63-
var address = new MultiAddress(parts[0]);
64-
return new ConnectedPeer
65-
{
66-
Id = address.Protocols.First(p => p.Name == "ipfs").Value,
67-
ConnectedAddress = parts[0],
68-
Latency = ParseLatency(parts[1])
69-
};
84+
Id = (string)p["Peer"],
85+
ConnectedAddress = new MultiAddress((string)p["Addr"] + "/ipfs/" + (string)p["Peer"]),
86+
Latency = ParseLatency((string)p["Latency"])
7087
});
88+
}
89+
90+
// Hmmm. Another change we can handle
91+
throw new FormatException("Unknown response from 'swarm/peers");
7192
}
7293

7394
TimeSpan ParseLatency(string latency)
7495
{
96+
if (latency == "n/a")
97+
{
98+
return TimeSpan.Zero;
99+
}
75100
if (latency.EndsWith("ms"))
76101
{
77102
var ms = Double.Parse(latency.Substring(0, latency.Length - 2));

test/TrustedPeersTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void Trusted_Peers_Add_Missing_Peer_ID()
3636
{
3737
var missingPeerId = new MultiAddress("/ip4/25.196.147.100/tcp/4001");
3838
var ipfs = TestFixture.Ipfs;
39-
ExceptionAssert.Throws<Exception>(() => ipfs.TrustedPeers.Add(missingPeerId), "invalid ipfs address");
39+
ExceptionAssert.Throws<Exception>(() => ipfs.TrustedPeers.Add(missingPeerId), "invalid IPFS address");
4040
}
4141

4242
[TestMethod]

0 commit comments

Comments
 (0)