Runnable which can throw unchecked exception */
4 | public interface RunnableEx {
5 | void run() throws Exception;
6 | }
7 |
--------------------------------------------------------------------------------
/examples/android-chatter/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 | yamux protocol.
7 | */
8 | enum class YamuxType(val intValue: Int) {
9 | DATA(0),
10 | WINDOW_UPDATE(1),
11 | PING(2),
12 | GO_AWAY(3);
13 |
14 | companion object {
15 | private val intToTypeCache = values().associateBy { it.intValue }
16 |
17 | fun fromInt(intValue: Int): YamuxType =
18 | intToTypeCache[intValue] ?: throw InvalidFrameMuxerException("Invalid Yamux type value: $intValue")
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/libp2p/src/main/kotlin/io/libp2p/core/multistream/StrictProtocolBinding.kt:
--------------------------------------------------------------------------------
1 | package io.libp2p.core.multistream
2 |
3 | import io.libp2p.core.P2PChannel
4 | import io.libp2p.core.P2PChannelHandler
5 | import java.util.concurrent.CompletableFuture
6 |
7 | abstract class StrictProtocolBindingScheduledExecutorService which functions based on the current system time
7 | * supplied by {@link TimeController#getTime()} instead of System.currentTimeMillis()
8 | */
9 | public interface ControlledExecutorService extends ScheduledExecutorService {
10 |
11 | /**
12 | * Sets up the {@link TimeController} instance which manages ordered tasks execution and provides
13 | * current time
14 | */
15 | void setTimeController(TimeController timeController);
16 | }
17 |
--------------------------------------------------------------------------------
/tools/simulator/src/main/kotlin/io/libp2p/simulate/topology/AllToAllTopology.kt:
--------------------------------------------------------------------------------
1 | package io.libp2p.simulate.topology
2 |
3 | import io.libp2p.simulate.*
4 | import java.util.*
5 |
6 | class AllToAllTopology : Topology {
7 |
8 | override var random: Random
9 | get() = TODO("Not yet implemented")
10 | set(_) {}
11 |
12 | override fun generateGraph(verticesCount: Int): TopologyGraph =
13 | (0 until verticesCount)
14 | .flatMap { src ->
15 | (src + 1 until verticesCount)
16 | .map { dest ->
17 | TopologyGraph.Edge(src, dest)
18 | }
19 | }
20 | .let { CustomTopologyGraph(it) }
21 | }
22 |
--------------------------------------------------------------------------------
/tools/simulator/src/main/kotlin/io/libp2p/simulate/delay/TimeDelayer.kt:
--------------------------------------------------------------------------------
1 | package io.libp2p.simulate.delay
2 |
3 | import io.libp2p.simulate.MessageDelayer
4 | import io.libp2p.tools.schedule
5 | import java.util.concurrent.CompletableFuture
6 | import java.util.concurrent.ScheduledExecutorService
7 | import kotlin.time.Duration
8 |
9 | class TimeDelayer(
10 | val executor: ScheduledExecutorService,
11 | val delaySupplier: () -> Duration
12 | ) : MessageDelayer {
13 |
14 | override fun delay(size: Long): CompletableFutureyamux protocol.
7 | */
8 | enum class YamuxFlag(val intFlag: Int) {
9 | SYN(1),
10 | ACK(2),
11 | FIN(4),
12 | RST(8);
13 |
14 | val asSet: SetJmDNS is a Java implementation of multi-cast DNS and can be used for service registration and 7 | * discovery in local area networks. JmDNS is fully compatible with Apple's Bonjour. The project was 8 | * originally started in December 2002 by Arthur van Hoff at Strangeberry. In November 2003 the 9 | * project was moved to SourceForge, and the name was changed from JRendezvous to JmDNS for legal 10 | * reasons. Many thanks to Stuart Cheshire for help and moral support. In 2014, it was been moved 11 | * from Sourceforge to Github by Kai Kreuzer with the kind approval from Arthur and Rick. 12 | * 13 | *
https://github.com/jmdns/jmdns/ JmDNS was 14 | * originally licensed under the GNU Lesser General Public License as jRendevous. It was re-released 15 | * under the Apache License, Version 2.0 in 2005. It is under those terms it is reused here. 16 | * 17 | *
JmDNS License Notice Guarantees that the latest event would be processed, though other intermediate events could be
10 | * skipped.
11 | *
12 | * Skips subsequent events if any previous is still processing. Avoids creating scheduling a task
13 | * for each event thus allowing frequent events submitting.
14 | */
15 | public class LatestExecutor
18 | * JmDNS
20 | * Changelog
21 | */
22 |
--------------------------------------------------------------------------------
/tools/schedulers/src/main/java/io/libp2p/tools/schedulers/ControlledSchedulers.java:
--------------------------------------------------------------------------------
1 | package io.libp2p.tools.schedulers;
2 |
3 | import java.time.Duration;
4 |
5 | /**
6 | * Special Schedulers implementation which is mostly suitable for testing and simulation. The system
7 | * time is controlled manually and all the schedulers execute tasks according to this time. Initial
8 | * system time is equal to 0
9 | */
10 | public interface ControlledSchedulers extends Schedulers {
11 |
12 | /**
13 | * Sets current time.
14 | *
15 | * @throws IllegalStateException if this instance is dependent on a parent {@link TimeController}
16 | * @see TimeController#setTime(long)
17 | */
18 | default void setCurrentTime(long newTime) {
19 | getTimeController().setTime(newTime);
20 | }
21 |
22 | /** Just a handy helper method for {@link #setCurrentTime(long)} */
23 | default void addTime(Duration duration) {
24 | addTime(duration.toMillis());
25 | }
26 |
27 | /** Just a handy helper method for {@link #setCurrentTime(long)} */
28 | default void addTime(long millis) {
29 | setCurrentTime(getCurrentTime() + millis);
30 | }
31 |
32 | /**
33 | * Returns {@link TimeController} which manages tasks ordered execution and supplies current time
34 | * for this instance
35 | */
36 | TimeController getTimeController();
37 | }
38 |
--------------------------------------------------------------------------------
/examples/pinger/src/main/java/io/libp2p/example/ping/Pinger.java:
--------------------------------------------------------------------------------
1 | package io.libp2p.example.ping;
2 |
3 | import io.libp2p.core.Host;
4 | import io.libp2p.core.dsl.HostBuilder;
5 | import io.libp2p.core.multiformats.Multiaddr;
6 | import io.libp2p.protocol.Ping;
7 | import io.libp2p.protocol.PingController;
8 | import java.util.concurrent.ExecutionException;
9 |
10 | public class Pinger {
11 | public static void main(String[] args) throws ExecutionException, InterruptedException {
12 | // Create a libp2p node and configure it
13 | // to accept TCP connections on a random port
14 | Host node = new HostBuilder().protocol(new Ping()).listen("/ip4/127.0.0.1/tcp/0").build();
15 |
16 | // start listening
17 | node.start().get();
18 |
19 | System.out.print("Node started and listening on ");
20 | System.out.println(node.listenAddresses());
21 |
22 | if (args.length > 0) {
23 | Multiaddr address = Multiaddr.fromString(args[0]);
24 | PingController pinger = new Ping().dial(node, address).getController().get();
25 |
26 | System.out.println("Sending 5 ping messages to " + address.toString());
27 | for (int i = 1; i <= 5; ++i) {
28 | long latency = pinger.ping().get();
29 | System.out.println("Ping " + i + ", latency " + latency + "ms");
30 | }
31 |
32 | node.stop().get();
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/tools/simulator/src/main/kotlin/io/libp2p/simulate/delay/ChannelMessageDelayer.kt:
--------------------------------------------------------------------------------
1 | package io.libp2p.simulate.delay
2 |
3 | import io.libp2p.simulate.BandwidthDelayer
4 | import io.libp2p.simulate.MessageDelayer
5 | import io.libp2p.simulate.delay.SequentialDelayer.Companion.sequential
6 | import java.util.concurrent.CompletableFuture
7 | import java.util.concurrent.ScheduledExecutorService
8 |
9 | class ChannelMessageDelayer(
10 | executor: ScheduledExecutorService,
11 | localOutboundBandwidthDelayer: BandwidthDelayer,
12 | connectionLatencyDelayer: MessageDelayer,
13 | remoteInboundBandwidthDelayer: BandwidthDelayer,
14 | ) : MessageDelayer {
15 |
16 | private val sequentialOutboundBandwidthDelayer = localOutboundBandwidthDelayer.sequential(executor)
17 | private val sequentialInboundBandwidthDelayer = remoteInboundBandwidthDelayer.sequential(executor)
18 |
19 | private val delayer = MessageDelayer { size ->
20 | CompletableFuture.allOf(
21 | sequentialOutboundBandwidthDelayer.delay(size)
22 | .thenCompose { connectionLatencyDelayer.delay(size) },
23 | connectionLatencyDelayer.delay(size)
24 | .thenCompose { sequentialInboundBandwidthDelayer.delay(size) }
25 | ).thenApply { }
26 | }
27 |
28 | override fun delay(size: Long): CompletableFutureeventProcessor
7 | * on the specified scheduler.
8 | *
9 | *