Skip to content

Commit b02a58a

Browse files
author
gjeanmart
committed
Merge branch 'master' of https://github.com/ipfs/java-ipfs-api
2 parents 73c7193 + 021d4ef commit b02a58a

File tree

7 files changed

+68
-43
lines changed

7 files changed

+68
-43
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ language: java
22
jdk:
33
- oraclejdk8
44
before_script:
5-
- wget https://dist.ipfs.io/go-ipfs/v0.4.11/go-ipfs_v0.4.11_linux-amd64.tar.gz -O /tmp/go-ipfs_v0.4.11_linux-amd64.tar.gz
6-
- tar -xvf /tmp/go-ipfs_v0.4.11_linux-amd64.tar.gz
5+
- wget https://dist.ipfs.io/go-ipfs/v0.4.13/go-ipfs_v0.4.13_linux-amd64.tar.gz -O /tmp/go-ipfs_linux-amd64.tar.gz
6+
- tar -xvf /tmp/go-ipfs_linux-amd64.tar.gz
77
- export PATH=$PATH:$PWD/go-ipfs/
88
- ipfs init
99
- ipfs daemon --enable-pubsub-experiment &

lib/multiaddr.jar

1.5 KB
Binary file not shown.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<dependency>
6060
<groupId>com.github.multiformats</groupId>
6161
<artifactId>java-multiaddr</artifactId>
62-
<version>v1.1.1</version>
62+
<version>v1.3.0</version>
6363
<scope>system</scope>
6464
<systemPath>${project.basedir}/lib/multiaddr.jar</systemPath>
6565
</dependency>

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

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,24 @@ public List<MerkleNode> add(List<NamedStreamable> files, boolean wrap, boolean h
114114
}
115115

116116
public List<MerkleNode> ls(Multihash hash) throws IOException {
117-
Map res = retrieveMap("ls/" + hash);
117+
Map res = retrieveMap("ls?arg=" + hash);
118118
return ((List<Object>) res.get("Objects")).stream().map(x -> MerkleNode.fromJSON((Map) x)).collect(Collectors.toList());
119119
}
120120

121121
public byte[] cat(Multihash hash) throws IOException {
122-
return retrieve("cat/" + hash);
122+
return retrieve("cat?arg=" + hash);
123123
}
124124

125125
public byte[] cat(Multihash hash, String subPath) throws IOException {
126126
return retrieve("cat?arg=" + hash + URLEncoder.encode(subPath, "UTF-8"));
127127
}
128128

129129
public byte[] get(Multihash hash) throws IOException {
130-
return retrieve("get/" + hash);
130+
return retrieve("get?arg=" + hash);
131131
}
132132

133133
public InputStream catStream(Multihash hash) throws IOException {
134-
return retrieveStream("cat/" + hash);
134+
return retrieveStream("cat?arg=" + hash);
135135
}
136136

137137
public List<Multihash> refs(Multihash hash, boolean recursive) throws IOException {
@@ -263,17 +263,16 @@ public Object peers(String topic) throws IOException {
263263
* @return
264264
* @throws IOException
265265
*/
266-
public Object pub(String topic, String data) throws IOException {
266+
public Object pub(String topic, String data) throws Exception {
267267
return retrieveAndParse("pubsub/pub?arg="+topic + "&arg=" + data);
268268
}
269269

270-
public Supplier<Map<String, Object>> sub(String topic) throws IOException {
270+
public Stream<Map<String, Object>> sub(String topic) throws Exception {
271271
return sub(topic, ForkJoinPool.commonPool());
272272
}
273273

274-
public Supplier<Map<String, Object>> sub(String topic, ForkJoinPool threadSupplier) throws IOException {
275-
Supplier<Object> sup = retrieveAndParseStream("pubsub/sub?arg=" + topic, threadSupplier);
276-
return () -> (Map) sup.get();
274+
public Stream<Map<String, Object>> sub(String topic, ForkJoinPool threadSupplier) throws Exception {
275+
return retrieveAndParseStream("pubsub/sub?arg=" + topic, threadSupplier).map(obj -> (Map)obj);
277276
}
278277

279278
/**
@@ -282,8 +281,8 @@ public Supplier<Map<String, Object>> sub(String topic, ForkJoinPool threadSuppli
282281
* @param results
283282
* @throws IOException
284283
*/
285-
public void sub(String topic, Consumer<Map<String, Object>> results) throws IOException {
286-
retrieveAndParseStream("pubsub/sub?arg="+topic, res -> results.accept((Map)res));
284+
public void sub(String topic, Consumer<Map<String, Object>> results, Consumer<IOException> error) throws IOException {
285+
retrieveAndParseStream("pubsub/sub?arg="+topic, res -> results.accept((Map)res), error);
287286
}
288287

289288

@@ -417,8 +416,8 @@ public Map findprovs(Multihash hash) throws IOException {
417416
return retrieveMap("dht/findprovs?arg=" + hash);
418417
}
419418

420-
public Map query(MultiAddress addr) throws IOException {
421-
return retrieveMap("dht/query?arg=" + addr.toString());
419+
public Map query(Multihash peerId) throws IOException {
420+
return retrieveMap("dht/query?arg=" + peerId.toString());
422421
}
423422

424423
public Map findpeer(Multihash id) throws IOException {
@@ -443,7 +442,15 @@ public Map ls(Multihash path) throws IOException {
443442
// Network commands
444443

445444
public List<MultiAddress> bootstrap() throws IOException {
446-
return ((List<String>)retrieveMap("bootstrap/").get("Peers")).stream().map(x -> new MultiAddress(x)).collect(Collectors.toList());
445+
return ((List<String>)retrieveMap("bootstrap/").get("Peers"))
446+
.stream()
447+
.flatMap(x -> {
448+
try {
449+
return Stream.of(new MultiAddress(x));
450+
} catch (Exception e) {
451+
return Stream.empty();
452+
}
453+
}).collect(Collectors.toList());
447454
}
448455

449456
public class Bootstrap {
@@ -471,7 +478,14 @@ public List<MultiAddress> rm(MultiAddress addr, boolean all) throws IOException
471478
public class Swarm {
472479
public List<Peer> peers() throws IOException {
473480
Map m = retrieveMap("swarm/peers?stream-channels=true");
474-
return ((List<Object>)m.get("Peers")).stream().map(Peer::fromJSON).collect(Collectors.toList());
481+
return ((List<Object>)m.get("Peers")).stream()
482+
.flatMap(json -> {
483+
try {
484+
return Stream.of(Peer.fromJSON(json));
485+
} catch (Exception e) {
486+
return Stream.empty();
487+
}
488+
}).collect(Collectors.toList());
475489
}
476490

477491
public Map addrs() throws IOException {
@@ -603,16 +617,26 @@ private Object retrieveAndParse(String path) throws IOException {
603617
return JSONParser.parse(new String(res));
604618
}
605619

606-
private Supplier<Object> retrieveAndParseStream(String path, ForkJoinPool executor) throws IOException {
607-
BlockingQueue<byte[]> objects = new LinkedBlockingQueue<>();
608-
executor.submit(() -> getObjectStream(path, objects::add));
609-
return () -> {
620+
private Stream<Object> retrieveAndParseStream(String path, ForkJoinPool executor) throws IOException {
621+
BlockingQueue<CompletableFuture<byte[]>> results = new LinkedBlockingQueue<>();
622+
InputStream in = retrieveStream(path);
623+
executor.submit(() -> getObjectStream(in,
624+
res -> {
625+
results.add(CompletableFuture.completedFuture(res));
626+
},
627+
err -> {
628+
CompletableFuture<byte[]> fut = new CompletableFuture<>();
629+
fut.completeExceptionally(err);
630+
results.add(fut);
631+
})
632+
);
633+
return Stream.generate(() -> {
610634
try {
611-
return JSONParser.parse(new String(objects.take()));
612-
} catch (InterruptedException e) {
635+
return JSONParser.parse(new String(results.take().get()));
636+
} catch (Exception e) {
613637
throw new RuntimeException(e);
614638
}
615-
};
639+
});
616640
}
617641

618642
/**
@@ -621,8 +645,8 @@ private Supplier<Object> retrieveAndParseStream(String path, ForkJoinPool execut
621645
* @param results
622646
* @throws IOException
623647
*/
624-
private void retrieveAndParseStream(String path, Consumer<Object> results) throws IOException {
625-
getObjectStream(path, d -> results.accept(JSONParser.parse(new String(d))));
648+
private void retrieveAndParseStream(String path, Consumer<Object> results, Consumer<IOException> err) throws IOException {
649+
getObjectStream(retrieveStream(path), d -> results.accept(JSONParser.parse(new String(d))), err);
626650
}
627651

628652
private byte[] retrieve(String path) throws IOException {
@@ -652,11 +676,10 @@ private static byte[] get(URL target) throws IOException {
652676
}
653677
}
654678

655-
private void getObjectStream(String path, Consumer<byte[]> processor) {
679+
private void getObjectStream(InputStream in, Consumer<byte[]> processor, Consumer<IOException> error) {
656680
byte LINE_FEED = (byte)10;
657681

658682
try {
659-
InputStream in = retrieveStream(path);
660683
ByteArrayOutputStream resp = new ByteArrayOutputStream();
661684

662685
byte[] buf = new byte[4096];
@@ -669,7 +692,7 @@ private void getObjectStream(String path, Consumer<byte[]> processor) {
669692
}
670693
}
671694
} catch (IOException e) {
672-
e.printStackTrace();
695+
error.accept(e);
673696
}
674697
}
675698

@@ -682,7 +705,6 @@ private static InputStream getStream(URL target) throws IOException {
682705
HttpURLConnection conn = (HttpURLConnection) target.openConnection();
683706
conn.setRequestMethod("GET");
684707
conn.setRequestProperty("Content-Type", "application/json");
685-
686708
return conn.getInputStream();
687709
}
688710

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private static String encode(String in) {
8282
}
8383

8484
public void addFilePart(String fieldName, Path parent, NamedStreamable uploadFile) {
85-
Optional<String> fileName = uploadFile.getName().map(n -> encode(parent.resolve(n).toString()));
85+
Optional<String> fileName = uploadFile.getName().map(n -> encode(parent.resolve(n).toString().replace('\\','/')));
8686
writer.append("--").append(boundary).append(LINE_FEED);
8787
if (!fileName.isPresent())
8888
writer.append("Content-Disposition: file; name=\"").append(fieldName).append("\";").append(LINE_FEED);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ public static Peer fromJSON(Object json) {
3030
long latency = val.apply("Latency").length() > 0 ? Long.parseLong(val.apply("Latency")) : -1;
3131
return new Peer(new MultiAddress(val.apply("Addr")), Cid.decode(val.apply("Peer")), latency, val.apply("Muxer"), val.apply("Streams"));
3232
}
33+
34+
@Override
35+
public String toString() {
36+
return id + "@" + address;
37+
}
3338
}

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.*;
1010
import java.nio.file.*;
1111
import java.util.*;
12+
import java.util.concurrent.*;
1213
import java.util.function.*;
1314
import java.util.stream.*;
1415

@@ -381,12 +382,12 @@ public void bulkBlockTest() throws IOException {
381382
}
382383

383384
@Test
384-
public void pubsubSynchronous() throws IOException {
385+
public void pubsubSynchronous() throws Exception {
385386
String topic = "topic" + System.nanoTime();
386387
List<Map<String, Object>> res = Collections.synchronizedList(new ArrayList<>());
387388
new Thread(() -> {
388389
try {
389-
ipfs.pubsub.sub(topic, res::add);
390+
ipfs.pubsub.sub(topic, res::add, t -> t.printStackTrace());
390391
} catch (IOException e) {
391392
throw new RuntimeException(e);}
392393
}).start();
@@ -406,17 +407,14 @@ public void pubsubSynchronous() throws IOException {
406407
}
407408

408409
@Test
409-
public void pubsub() throws IOException {
410-
Object ls = ipfs.pubsub.ls();
411-
Object peers = ipfs.pubsub.peers();
410+
public void pubsub() throws Exception {
412411
String topic = "topic" + System.nanoTime();
413-
Supplier<Map<String, Object>> sub = ipfs.pubsub.sub(topic);
414-
Map<String, Object> first = sub.get();
415-
Assert.assertTrue(first.equals(Collections.emptyMap()));
412+
Stream<Map<String, Object>> sub = ipfs.pubsub.sub(topic);
416413
String data = "Hello!";
417414
Object pub = ipfs.pubsub.pub(topic, data);
418-
Map second = sub.get();
419-
Assert.assertTrue( ! second.equals(Collections.emptyMap()));
415+
Object pub2 = ipfs.pubsub.pub(topic, "G'day");
416+
List<Map> results = sub.limit(2).collect(Collectors.toList());
417+
Assert.assertTrue( ! results.get(0).equals(Collections.emptyMap()));
420418
}
421419

422420
private static String toEscapedHex(byte[] in) throws IOException {
@@ -613,7 +611,7 @@ public void dhtTest() throws IOException {
613611
Map put = ipfs.dht.put("somekey", "somevalue");
614612
Map findprovs = ipfs.dht.findprovs(pointer);
615613
List<Peer> peers = ipfs.swarm.peers();
616-
Map query = ipfs.dht.query(peers.get(0).address);
614+
Map query = ipfs.dht.query(peers.get(0).id);
617615
Map find = ipfs.dht.findpeer(peers.get(0).id);
618616
}
619617

0 commit comments

Comments
 (0)