Skip to content

Commit 7a9b209

Browse files
committed
Improve types of swarm, id and ping calls.
Make swarm test reliable.
1 parent c450dd2 commit 7a9b209

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

src/main/java/io/ipfs/api/IPFS.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,17 +475,24 @@ public List<Peer> peers() throws IOException {
475475
}).collect(Collectors.toList());
476476
}
477477

478-
public Map addrs() throws IOException {
478+
public Map<Multihash, List<MultiAddress>> addrs() throws IOException {
479479
Map m = retrieveMap("swarm/addrs?stream-channels=true");
480-
return (Map<String, Object>)m.get("Addrs");
480+
return ((Map<String, Object>)m.get("Addrs")).entrySet()
481+
.stream()
482+
.collect(Collectors.toMap(
483+
e -> Multihash.fromBase58(e.getKey()),
484+
e -> ((List<String>)e.getValue())
485+
.stream()
486+
.map(MultiAddress::new)
487+
.collect(Collectors.toList())));
481488
}
482489

483-
public Map connect(String multiAddr) throws IOException {
490+
public Map connect(MultiAddress multiAddr) throws IOException {
484491
Map m = retrieveMap("swarm/connect?arg="+multiAddr);
485492
return m;
486493
}
487494

488-
public Map disconnect(String multiAddr) throws IOException {
495+
public Map disconnect(MultiAddress multiAddr) throws IOException {
489496
Map m = retrieveMap("swarm/disconnect?arg="+multiAddr);
490497
return m;
491498
}
@@ -527,12 +534,12 @@ public String sys() throws IOException {
527534
}
528535
}
529536

530-
public Map ping(String target) throws IOException {
531-
return retrieveMap("ping/" + target);
537+
public Map ping(Multihash target) throws IOException {
538+
return retrieveMap("ping/" + target.toBase58());
532539
}
533540

534-
public Map id(String target) throws IOException {
535-
return retrieveMap("id/" + target);
541+
public Map id(Multihash target) throws IOException {
542+
return retrieveMap("id/" + target.toBase58());
536543
}
537544

538545
public Map id() throws IOException {

src/test/java/io/ipfs/api/APITest.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -657,18 +657,28 @@ public void resolveTest() throws IOException {
657657

658658
@Test
659659
public void swarmTest() throws IOException {
660-
String multiaddr = "/ip4/127.0.0.1/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ";
661-
Map connect = ipfs.swarm.connect(multiaddr);
662-
Map disconnect = ipfs.swarm.disconnect(multiaddr);
663-
Map<String, Object> addrs = ipfs.swarm.addrs();
660+
Map<Multihash, List<MultiAddress>> addrs = ipfs.swarm.addrs();
664661
if (addrs.size() > 0) {
665-
boolean contacted = addrs.keySet().stream()
666-
.anyMatch(target -> {
662+
boolean contacted = addrs.entrySet().stream()
663+
.anyMatch(e -> {
664+
Multihash target = e.getKey();
665+
List<MultiAddress> nodeAddrs = e.getValue();
666+
boolean contactable = nodeAddrs.stream()
667+
.anyMatch(addr -> {
668+
try {
669+
MultiAddress peer = new MultiAddress(addr.toString() + "/ipfs/" + target.toBase58());
670+
Map connect = ipfs.swarm.connect(peer);
671+
Map disconnect = ipfs.swarm.disconnect(peer);
672+
return true;
673+
} catch (Exception ex) {
674+
return false;
675+
}
676+
});
667677
try {
668678
Map id = ipfs.id(target);
669679
Map ping = ipfs.ping(target);
670-
return true;
671-
} catch (Exception e) {
680+
return contactable;
681+
} catch (Exception ex) {
672682
// not all nodes have to be contactable
673683
return false;
674684
}

0 commit comments

Comments
 (0)